From 8b3c463af87934c6029959dd507b15abb20b81e9 Mon Sep 17 00:00:00 2001 From: mejta Date: Mon, 26 Jun 2023 15:36:55 +0200 Subject: [PATCH 1/3] Add models --- README.md | 5 +++ bootstrap.php | 11 +++++-- composer-deps.json | 3 +- composer-deps.lock | 38 ++++++++++++++++++++++- src/Blocks/BookLinkBlock.php | 18 +++++++++-- src/Managers/RepositoriesManager.php | 27 ++++++++++++++++ src/Models/BookAuthorModel.php | 14 +++++++++ src/Models/BookModel.php | 14 +++++++++ src/Plugin.php | 2 ++ src/Repositories/BookAuthorRepository.php | 17 ++++++++++ src/Repositories/BookRepository.php | 17 ++++++++++ 11 files changed, 159 insertions(+), 7 deletions(-) create mode 100644 src/Managers/RepositoriesManager.php create mode 100644 src/Models/BookAuthorModel.php create mode 100644 src/Models/BookModel.php create mode 100644 src/Repositories/BookAuthorRepository.php create mode 100644 src/Repositories/BookRepository.php diff --git a/README.md b/README.md index c970369..37fa9b1 100644 --- a/README.md +++ b/README.md @@ -44,3 +44,8 @@ plugin. Custom fields are required in composer-deps.json file. links: [WPify Custom Fields](https://packagist.org/packages/wpify/custom-fields) +### 04 - WPify Model + +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. + diff --git a/bootstrap.php b/bootstrap.php index 26ec16e..5075f08 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\autowire; 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 => autowire()->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/composer-deps.lock b/composer-deps.lock index eae1d4a..783823f 100644 --- a/composer-deps.lock +++ b/composer-deps.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8d666c7053a321f1611c3044d5ded1cf", + "content-hash": "6d884eae86a1f3ed0623884da32887a9", "packages": [ { "name": "laravel/serializable-closure", @@ -283,6 +283,42 @@ ], "description": "WPify Custom Fields", "time": "2023-06-15T19:37:40+00:00" + }, + { + "name": "wpify/model", + "version": "4.0.6", + "source": { + "type": "git", + "url": "git@gitlab.com:wpify/model.git", + "reference": "8109864e25436d36fa4008c64dca71b13ff832f2" + }, + "dist": { + "type": "zip", + "url": "https://gitlab.com/api/v4/projects/wpify%2Fmodel/repository/archive.zip?sha=8109864e25436d36fa4008c64dca71b13ff832f2", + "reference": "8109864e25436d36fa4008c64dca71b13ff832f2", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Wpify\\Model\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "authors": [ + { + "name": "Daniel Mejta", + "email": "daniel@mejta.net" + } + ], + "description": "WPify Model", + "time": "2023-06-16T12:06:42+00:00" } ], "packages-dev": [], 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 '

Book: ' . $block_attributes['book_id'] . '

'; + // Get the book from the repository. + $book = $this->book_repository->get( $block_attributes['book_id'] ); + + // Set the attributes needed for the render. + $block_attributes['title'] = $book->title; + $block_attributes['isbn'] = $book->isbn; + + // return the string with the rendered block. + return '

Book: ' . $block_attributes['title'] . ' (' . $block_attributes['isbn'] . ')

'; } } diff --git a/src/Managers/RepositoriesManager.php b/src/Managers/RepositoriesManager.php new file mode 100644 index 0000000..ca0a138 --- /dev/null +++ b/src/Managers/RepositoriesManager.php @@ -0,0 +1,27 @@ +get_repositories() as $repository ) { + $container->set( $repository::class, $repository ); + } + + // Register all custom repositorues in WPify Models, so it knows how to use them. + $manager->register_repository( $book_repository ); + $manager->register_repository( $book_author_repository ); + } +} diff --git a/src/Models/BookAuthorModel.php b/src/Models/BookAuthorModel.php new file mode 100644 index 0000000..5dae642 --- /dev/null +++ b/src/Models/BookAuthorModel.php @@ -0,0 +1,14 @@ + Date: Mon, 26 Jun 2023 16:23:45 +0200 Subject: [PATCH 2/3] Update --- bootstrap.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bootstrap.php b/bootstrap.php index 5075f08..54c9bbc 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -20,7 +20,7 @@ use WpifyPluginTutorial\Plugin; use WpifyPluginTutorialDeps\Wpify\Model\Manager; -use function WpifyPluginTutorialDeps\DI\autowire; +use function WpifyPluginTutorialDeps\DI\create; require 'deps/scoper-autoload.php'; require 'deps/autoload.php'; @@ -39,7 +39,7 @@ function wpifypt_container(): Container { if ( empty( $container ) ) { $containerBuilder = new ContainerBuilder(); $containerBuilder->addDefinitions( array( - Manager::class => autowire()->constructor( array() ) + Manager::class => create()->constructor( array() ) ) ); $container = $containerBuilder->build(); } From 7f876ac51c426639bd56495fd2ca50f2c739bf03 Mon Sep 17 00:00:00 2001 From: mejta Date: Mon, 26 Jun 2023 17:30:16 +0200 Subject: [PATCH 3/3] Update --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d92f71..0df76b5 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,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 @@ -42,10 +42,11 @@ 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 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)