diff --git a/README.md b/README.md index 79a79c0..d1b889f 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ composer require php-di/php-di In this step, we are adding the WPify Scoper to the plugin, so that the dependencies are not conflicting with other plugins. -links: [WPify Scoper](https://packagist.org/packages/wpify/scoper) +Links: [WPify Scoper](https://packagist.org/packages/wpify/scoper) ```bash composer require wpify/scoper @@ -47,7 +47,14 @@ composer require wpify/scoper In this step, we are adding the WPify Custom Fields to the plugin, so that we can easily create custom fields for our plugin. Custom fields are required in composer-deps.json file. -links: [WPify Custom Fields](https://packagist.org/packages/wpify/custom-fields) +Links: [WPify Custom Fields](https://packagist.org/packages/wpify/custom-fields) +### 04 - WPify Model -[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/04-models) +We have a custom post type, taxonomy, but we have no easy way how to access data. In this step, we are adding +WPify Models, so we can access data easily and use them wherever we need. + +Links: [WPify Model](https://packagist.org/packages/wpify/model) + + +[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/05-templates) diff --git a/bootstrap.php b/bootstrap.php index 26ec16e..54c9bbc 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -18,6 +18,9 @@ use WpifyPluginTutorialDeps\DI\DependencyException; use WpifyPluginTutorialDeps\DI\NotFoundException; use WpifyPluginTutorial\Plugin; +use WpifyPluginTutorialDeps\Wpify\Model\Manager; + +use function WpifyPluginTutorialDeps\DI\create; require 'deps/scoper-autoload.php'; require 'deps/autoload.php'; @@ -35,7 +38,9 @@ function wpifypt_container(): Container { if ( empty( $container ) ) { $containerBuilder = new ContainerBuilder(); - $containerBuilder->addDefinitions( array() ); + $containerBuilder->addDefinitions( array( + Manager::class => create()->constructor( array() ) + ) ); $container = $containerBuilder->build(); } @@ -90,8 +95,10 @@ function wpifypt_uninstall(): void { wpifypt()->uninstall(); } +/** + * Hook basic stuff, so the plugin can work. + */ add_action( 'plugins_loaded', 'wpifypt' ); - register_activation_hook( __FILE__, 'wpifypt_activate' ); register_deactivation_hook( __FILE__, 'wpifypt_deactivate' ); register_uninstall_hook( __FILE__, 'wpifypt_uninstall' ); diff --git a/composer-deps.json b/composer-deps.json index baa6210..e1bcfd7 100644 --- a/composer-deps.json +++ b/composer-deps.json @@ -2,6 +2,7 @@ "require": { "php": "^8.0", "php-di/php-di": "^7.0", - "wpify/custom-fields": "^3.9" + "wpify/custom-fields": "^3.9", + "wpify/model": "^4.0" } } diff --git a/src/Blocks/BookLinkBlock.php b/src/Blocks/BookLinkBlock.php index 5fcb29f..3f6af9c 100644 --- a/src/Blocks/BookLinkBlock.php +++ b/src/Blocks/BookLinkBlock.php @@ -3,19 +3,23 @@ namespace WpifyPluginTutorial\Blocks; use WpifyPluginTutorial\Managers\BlocksManager; +use WpifyPluginTutorial\Repositories\BookRepository; use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields; class BookLinkBlock { const KEY = 'wpify/book-link'; - public function __construct( CustomFields $custom_fields ) { + public function __construct( + CustomFields $custom_fields, + private BookRepository $book_repository, + ) { $custom_fields->create_gutenberg_block( array( 'name' => self::KEY, 'title' => __( 'Book link', 'wpifypt' ), 'category' => BlocksManager::BOOKS_KEY, 'icon' => 'book', 'supports' => array( 'anchor' => true ), - 'example' => array( 'book_id' => 1 ), + 'example' => array( 'book_id' => 1, 'title' => 'Example book' ), 'render_callback' => array( $this, 'render' ), 'items' => array( array( @@ -34,6 +38,14 @@ public function __construct( CustomFields $custom_fields ) { } public function render( $block_attributes, $content ) { - return '