diff --git a/README.md b/README.md index 14efdb4..79a79c0 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,12 @@ links: [WPify Scoper](https://packagist.org/packages/wpify/scoper) composer require wpify/scoper ``` -[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/03-custom-fields) +### 03 - WPify Custom Fields + +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) + + +[Next step >>>](https://github.com/wpify/plugin-tutorial/tree/04-models) diff --git a/composer-deps.json b/composer-deps.json index aabaa7b..baa6210 100644 --- a/composer-deps.json +++ b/composer-deps.json @@ -1,6 +1,7 @@ { "require": { "php": "^8.0", - "php-di/php-di": "^7.0" + "php-di/php-di": "^7.0", + "wpify/custom-fields": "^3.9" } } diff --git a/src/Blocks/BookLinkBlock.php b/src/Blocks/BookLinkBlock.php new file mode 100644 index 0000000..5fcb29f --- /dev/null +++ b/src/Blocks/BookLinkBlock.php @@ -0,0 +1,39 @@ +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 ), + 'render_callback' => array( $this, 'render' ), + 'items' => array( + array( + 'type' => 'hidden', + 'id' => 'anchor', + ), + array( + 'type' => 'post', + 'id' => 'book_id', + 'title' => __( 'WPify Book', 'wpifypt' ), + 'post_type' => 'book', + 'async' => true, + ) + ), + ) ); + } + + public function render( $block_attributes, $content ) { + return '

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

'; + } +} diff --git a/src/Managers/BlocksManager.php b/src/Managers/BlocksManager.php new file mode 100644 index 0000000..b0c532c --- /dev/null +++ b/src/Managers/BlocksManager.php @@ -0,0 +1,25 @@ + self::BOOKS_KEY, + 'title' => __( 'WPify Books', 'wpifypt' ), + 'icon' => 'books', + ); + + return $categories; + } +} diff --git a/src/Plugin.php b/src/Plugin.php index 2f01b94..a386cdd 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -2,6 +2,7 @@ namespace WpifyPluginTutorial; +use WpifyPluginTutorial\Managers\BlocksManager; use WpifyPluginTutorial\Managers\PostTypesManager; use WpifyPluginTutorial\Managers\TaxonomiesManager; @@ -16,6 +17,7 @@ class Plugin { public function __construct( PostTypesManager $post_types_manager, TaxonomiesManager $taxonomies_manager, + BlocksManager $blocks_manager, ) { // stuff to do on plugin load } diff --git a/src/PostTypes/BookPostType.php b/src/PostTypes/BookPostType.php index 53a67ae..83cc774 100644 --- a/src/PostTypes/BookPostType.php +++ b/src/PostTypes/BookPostType.php @@ -3,12 +3,33 @@ namespace WpifyPluginTutorial\PostTypes; use WpifyPluginTutorial\Taxonomies\BookAuthorTaxonomy; +use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields; class BookPostType { const KEY = 'book'; - public function __construct() { + public function __construct( CustomFields $custom_fields ) { add_action( 'init', array( $this, 'register' ), 0 ); + + $custom_fields->create_metabox( array( + 'id' => 'book-info', + 'title' => __( 'Book info', 'wpifypt' ), + 'context' => 'advanced', + 'priority' => 'high', + 'items' => array( + array( + 'id' => 'isbn', + 'type' => 'text', + 'label' => __( 'ISBN', 'wpifypt' ), + ), + array( + 'id' => 'first_published', + 'type' => 'date', + 'label' => __( 'Published first', 'wpifypt' ), + ), + ), + 'post_types' => array( self::KEY ), + ) ); } public function register() { diff --git a/src/Taxonomies/BookAuthorTaxonomy.php b/src/Taxonomies/BookAuthorTaxonomy.php index 321646a..8e37aaa 100644 --- a/src/Taxonomies/BookAuthorTaxonomy.php +++ b/src/Taxonomies/BookAuthorTaxonomy.php @@ -3,16 +3,33 @@ namespace WpifyPluginTutorial\Taxonomies; use WpifyPluginTutorial\PostTypes\BookPostType; +use WpifyPluginTutorialDeps\Wpify\CustomFields\CustomFields; class BookAuthorTaxonomy { const KEY = 'book_author'; - public function __construct() { + public function __construct( CustomFields $custom_fields ) { add_action( 'init', array( $this, 'register' ) ); + + $custom_fields->create_taxonomy_options( array( + 'taxonomy' => self::KEY, + 'items' => array( + array( + 'id' => 'date_of_birth', + 'type' => 'date', + 'label' => __( 'Date of Birth', 'wpifypt' ), + ), + array( + 'id' => 'date_of_death', + 'type' => 'date', + 'label' => __( 'Date of Death', 'wpifypt' ), + ), + ), + ) ); } public function register() { - $labels = array( + $labels = array( 'name' => _x( 'Book Authors', 'Taxonomy General Name', 'wpifypt' ), 'singular_name' => _x( 'Book Author', 'Taxonomy Singular Name', 'wpifypt' ), 'menu_name' => __( 'Book Author', 'wpifypt' ), @@ -41,7 +58,7 @@ public function register() { 'hierarchical' => false, ); - $args = array( + $args = array( 'labels' => $labels, 'hierarchical' => false, 'public' => true,