From e577c7b6c33d648640206b9a4cac23397369b096 Mon Sep 17 00:00:00 2001 From: mejta Date: Mon, 26 Jun 2023 15:05:43 +0200 Subject: [PATCH] Add custom fields --- README.md | 8 ++++++ composer-deps.json | 3 ++- composer-deps.lock | 39 ++++++++++++++++++++++++++- src/Blocks/BookLinkBlock.php | 39 +++++++++++++++++++++++++++ src/Managers/BlocksManager.php | 25 +++++++++++++++++ src/Plugin.php | 2 ++ src/PostTypes/BookPostType.php | 23 +++++++++++++++- src/Taxonomies/BookAuthorTaxonomy.php | 23 +++++++++++++--- 8 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/Blocks/BookLinkBlock.php create mode 100644 src/Managers/BlocksManager.php diff --git a/README.md b/README.md index 774c39c..c970369 100644 --- a/README.md +++ b/README.md @@ -36,3 +36,11 @@ links: [WPify Scoper](https://packagist.org/packages/wpify/scoper) ```bash composer require wpify/scoper ``` + +### 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) + 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/composer-deps.lock b/composer-deps.lock index 19d4f16..eae1d4a 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": "633d27b2781dd7bb1c61efd5b1619681", + "content-hash": "8d666c7053a321f1611c3044d5ded1cf", "packages": [ { "name": "laravel/serializable-closure", @@ -246,6 +246,43 @@ "source": "https://github.com/php-fig/container/tree/2.0.2" }, "time": "2021-11-05T16:47:00+00:00" + }, + { + "name": "wpify/custom-fields", + "version": "3.9.6", + "source": { + "type": "git", + "url": "git@gitlab.com:wpify/custom-fields.git", + "reference": "88f678e241e7fc856ea2a3299e9534c70239c232" + }, + "dist": { + "type": "zip", + "url": "https://gitlab.com/api/v4/projects/wpify%2Fcustom-fields/repository/archive.zip?sha=88f678e241e7fc856ea2a3299e9534c70239c232", + "reference": "88f678e241e7fc856ea2a3299e9534c70239c232", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=7.4||<=8.1" + }, + "require-dev": { + "symfony/var-dumper": "^v6.1.3" + }, + "type": "library", + "autoload": { + "files": [ + "functions.php" + ], + "psr-4": { + "Wpify\\CustomFields\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "GPL-2.0-or-later" + ], + "description": "WPify Custom Fields", + "time": "2023-06-15T19:37:40+00:00" } ], "packages-dev": [], 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,