From c8598d03ea0a239559017fbd18a4d46c9d006fe6 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Thu, 14 Mar 2024 00:36:30 +0000 Subject: [PATCH 01/14] Add abstract taxonomy and post classes --- .../classes/PostTypes/AbstractPostType.php | 187 ++++++++++++++++++ .../classes/Taxonomies/AbstractTaxonomy.php | 142 +++++++++++++ 2 files changed, 329 insertions(+) create mode 100644 mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php create mode 100644 mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php new file mode 100644 index 0000000..9b3efcc --- /dev/null +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php @@ -0,0 +1,187 @@ + $this->get_labels(), + 'public' => true, + 'has_archive' => true, + 'show_ui' => true, + 'show_in_menu' => true, + 'show_in_nav_menus' => false, + 'show_in_rest' => true, + 'supports' => $this->get_editor_supports(), + ]; + + return $options; + } + + /** + * Get the labels for the post type. + * + * @return array + */ + public function get_labels() { + $plural_label = $this->get_plural_label(); + $singular_label = $this->get_singular_label(); + + // phpcs:disable -- ignoring template strings without translators placeholder since this is dynamic + $labels = array( + 'name' => $plural_label, + // Already translated via get_plural_label(). + 'singular_name' => $singular_label, + // Already translated via get_singular_label(). + 'add_new_item' => sprintf( __( 'Add New %s', 'tenup-plugin' ), $singular_label ), + 'edit_item' => sprintf( __( 'Edit %s', 'tenup-plugin' ), $singular_label ), + 'new_item' => sprintf( __( 'New %s', 'tenup-plugin' ), $singular_label ), + 'view_item' => sprintf( __( 'View %s', 'tenup-plugin' ), $singular_label ), + 'view_items' => sprintf( __( 'View %s', 'tenup-plugin' ), $plural_label ), + 'search_items' => sprintf( __( 'Search %s', 'tenup-plugin' ), $plural_label ), + 'not_found' => sprintf( __( 'No %s found.', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'not_found_in_trash' => sprintf( __( 'No %s found in Trash.', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'parent_item_colon' => sprintf( __( 'Parent %s:', 'tenup-plugin' ), $plural_label ), + 'all_items' => sprintf( __( 'All %s', 'tenup-plugin' ), $plural_label ), + 'archives' => sprintf( __( '%s Archives', 'tenup-plugin' ), $singular_label ), + 'attributes' => sprintf( __( '%s Attributes', 'tenup-plugin' ), $singular_label ), + 'insert_into_item' => sprintf( __( 'Insert into %s', 'tenup-plugin' ), strtolower( $singular_label ) ), + 'uploaded_to_this_item' => sprintf( __( 'Uploaded to this %s', 'tenup-plugin' ), strtolower( $singular_label ) ), + 'filter_items_list' => sprintf( __( 'Filter %s list', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'items_list_navigation' => sprintf( __( '%s list navigation', 'tenup-plugin' ), $plural_label ), + 'items_list' => sprintf( __( '%s list', 'tenup-plugin' ), $plural_label ), + 'item_published' => sprintf( __( '%s published.', 'tenup-plugin' ), $singular_label ), + 'item_published_privately' => sprintf( __( '%s published privately.', 'tenup-plugin' ), $singular_label ), + 'item_reverted_to_draft' => sprintf( __( '%s reverted to draft.', 'tenup-plugin' ), $singular_label ), + 'item_scheduled' => sprintf( __( '%s scheduled.', 'tenup-plugin' ), $singular_label ), + 'item_updated' => sprintf( __( '%s updated.', 'tenup-plugin' ), $singular_label ), + 'menu_name' => $plural_label, + 'name_admin_bar' => $singular_label, + ); + // phpcs:enable + + return $labels; + } + + /** + * Registers a post type and associates its taxonomies. + * + * @uses $this->get_name() to get the post's type name. + * @return Bool Whether this theme has supports for this post type. + */ + public function register() { + $this->register_post_type(); + $this->register_taxonomies(); + + $this->after_register(); + + return true; + } + + /** + * Registers the current post type with WordPress. + * + * @return void + */ + public function register_post_type() { + register_post_type( + $this->get_name(), + $this->get_options() + ); + } + + /** + * Registers the taxonomies declared with the current post type. + * + * @return void + */ + public function register_taxonomies() { + $taxonomies = $this->get_supported_taxonomies(); + + $object_type = $this->get_name(); + + if ( ! empty( $taxonomies ) ) { + foreach ( $taxonomies as $taxonomy ) { + register_taxonomy_for_object_type( + $taxonomy, + $object_type + ); + } + } + } + + /** + * Returns the default supported taxonomies. The subclass should declare the + * Taxonomies that it supports here if required. + * + * @return array + */ + public function get_supported_taxonomies() { + return []; + } + + /** + * Run any code after the post type has been registered. + * + * @return void + */ + public function after_register() { + // Do nothing. + } + +} diff --git a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php new file mode 100644 index 0000000..6570531 --- /dev/null +++ b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php @@ -0,0 +1,142 @@ +get_name() to get the taxonomy's slug. + * @return bool + */ + public function register() { + \register_taxonomy( + $this->get_name(), + $this->get_post_types(), + $this->get_options() + ); + + return true; + } + + /** + * Get the options for the taxonomy. + * + * @return array + */ + public function get_options() { + return array( + 'labels' => $this->get_labels(), + 'hierarchical' => false, + 'show_ui' => true, + 'show_admin_column' => true, + 'query_var' => true, + 'show_in_rest' => true, + 'public' => true, + ); + } + + /** + * Get the labels for the taxonomy. + * + * @return array + */ + public function get_labels() { + $plural_label = $this->get_plural_label(); + $singular_label = $this->get_singular_label(); + + // phpcs:disable + $labels = array( + 'name' => $plural_label, // Already translated via get_plural_label(). + 'singular_name' => $singular_label, // Already translated via get_singular_label(). + 'search_items' => sprintf( __( 'Search %s', 'tenup-plugin' ), $plural_label ), + 'popular_items' => sprintf( __( 'Popular %s', 'tenup-plugin' ), $plural_label ), + 'all_items' => sprintf( __( 'All %s', 'tenup-plugin' ), $plural_label ), + 'edit_item' => sprintf( __( 'Edit %s', 'tenup-plugin' ), $singular_label ), + 'update_item' => sprintf( __( 'Update %s', 'tenup-plugin' ), $singular_label ), + 'add_new_item' => sprintf( __( 'Add %s', 'tenup-plugin' ), $singular_label ), + 'new_item_name' => sprintf( __( 'New %s Name', 'tenup-plugin' ), $singular_label ), + 'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'choose_from_most_used' => sprintf( __( 'Choose from the most used %s', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'not_found' => sprintf( __( 'No %s found.', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'not_found_in_trash' => sprintf( __( 'No %s found in Trash.', 'tenup-plugin' ), strtolower( $plural_label ) ), + 'view_item' => sprintf( __( 'View %s', 'tenup-plugin' ), $singular_label ), + ); + // phpcs:enable + + return $labels; + } + + /** + * Setting the post types to null to ensure no post type is registered with + * this taxonomy. Post Type classes declare their supported taxonomies. + * + * @return array|null + */ + public function get_post_types() { + return null; + } +} From a253d521adbb77066f386896657d0d31c2623d69 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Thu, 14 Mar 2024 00:37:17 +0000 Subject: [PATCH 02/14] Base Post and Page post type classes to make any needed changes --- .../includes/classes/PostTypes/Page.php | 32 +++++++++++++++++++ .../includes/classes/PostTypes/Post.php | 32 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php create mode 100644 mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php new file mode 100644 index 0000000..316f8c5 --- /dev/null +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php @@ -0,0 +1,32 @@ + Date: Thu, 14 Mar 2024 00:37:36 +0000 Subject: [PATCH 03/14] Demo post type and taxonomy that can be cloned --- .../includes/classes/PostTypes/Demo.php | 71 +++++++++++++++++++ .../includes/classes/Taxonomies/Demo.php | 50 +++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php create mode 100644 mu-plugins/10up-plugin/includes/classes/Taxonomies/Demo.php diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php new file mode 100644 index 0000000..49db86f --- /dev/null +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php @@ -0,0 +1,71 @@ + Date: Thu, 14 Mar 2024 00:37:44 +0000 Subject: [PATCH 04/14] Composer fix --- composer.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 409ffe6..6b516f7 100755 --- a/composer.json +++ b/composer.json @@ -37,5 +37,11 @@ "type:wordpress-plugin" ] } + }, + "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true, + "composer/installers": true + } } -} \ No newline at end of file +} From 9f3f634e65cdcd1a8649034075104505c836c1a4 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 19 Mar 2024 12:44:48 +0000 Subject: [PATCH 05/14] Add additional options --- .../classes/PostTypes/AbstractPostType.php | 32 +++++++++++++++++++ .../includes/classes/PostTypes/Demo.php | 15 +++++++++ .../classes/Taxonomies/AbstractTaxonomy.php | 11 ++++++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php index 9b3efcc..c675c37 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php @@ -35,6 +35,35 @@ abstract public function get_singular_label(); */ abstract public function get_plural_label(); + /** + * Get the menu icon for the post type. + * + * This can be a base64 encoded SVG, a dashicons class or 'none' to leave it empty so it can be filled with CSS. + * + * @see https://developer.wordpress.org/resource/dashicons/ + * + * @return string + */ + abstract public function get_menu_icon(); + + /** + * Get the menu position for the post type. + * + * @return int|null + */ + public function get_menu_position() { + return null; + } + + /** + * Is the post type hierarchical? + * + * @return bool + */ + public function is_hierarchical() { + return false; + } + /** * Default post type supported feature names. * @@ -68,6 +97,9 @@ public function get_options() { 'show_in_nav_menus' => false, 'show_in_rest' => true, 'supports' => $this->get_editor_supports(), + 'menu_icon' => $this->get_menu_icon(), + 'menu_position' => $this->get_menu_position(), + 'hierarchical' => $this->is_hierarchical(), ]; return $options; diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php index 49db86f..f7896dd 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php @@ -39,6 +39,19 @@ public function get_plural_label() { return esc_html__( 'Demos', 'tenup-plugin' ); } + /** + * Get the menu icon for the post type. + * + * This can be a base64 encoded SVG, a dashicons class or 'none' to leave it empty so it can be filled with CSS. + * + * @see https://developer.wordpress.org/resource/dashicons/ + * + * @return string + */ + public function get_menu_icon() { + return 'dashicons-chart-pie'; + } + /** * Can the class be registered? * @@ -68,4 +81,6 @@ public function get_supported_taxonomies() { public function after_register() { // Register any hooks/filters you need. } + + } diff --git a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php index 6570531..580cee4 100644 --- a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php +++ b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php @@ -65,6 +65,15 @@ abstract public function get_singular_label(); */ abstract public function get_plural_label(); + /** + * Is the taxonomy hierarchical? + * + * @return bool + */ + public function is_hierarchical() { + return false; + } + /** * Register hooks and actions. * @@ -89,7 +98,7 @@ public function register() { public function get_options() { return array( 'labels' => $this->get_labels(), - 'hierarchical' => false, + 'hierarchical' => $this->is_hierarchical(), 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, From 5492fbcb6030c57525c1f3c2a62f1f314458be1d Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 19 Mar 2024 16:22:39 +0000 Subject: [PATCH 06/14] Fix PHPCS issues --- .../10up-plugin/includes/classes/PostTypes/AbstractPostType.php | 1 - mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php | 2 -- 2 files changed, 3 deletions(-) diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php index c675c37..f912cc7 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php @@ -215,5 +215,4 @@ public function get_supported_taxonomies() { public function after_register() { // Do nothing. } - } diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php index f7896dd..2435bd7 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Demo.php @@ -81,6 +81,4 @@ public function get_supported_taxonomies() { public function after_register() { // Register any hooks/filters you need. } - - } From 64c13091a50444d3074af3cfede085c21739880b Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 19 Mar 2024 17:48:40 +0000 Subject: [PATCH 07/14] Add an after_register for taxonomies --- .../includes/classes/Taxonomies/AbstractTaxonomy.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php index 580cee4..59ca905 100644 --- a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php +++ b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php @@ -87,6 +87,8 @@ public function register() { $this->get_options() ); + $this->after_register(); + return true; } @@ -148,4 +150,13 @@ public function get_labels() { public function get_post_types() { return null; } + + /** + * Run any code after the taxonomy has been registered. + * + * @return void + */ + public function after_register() { + // Do nothing. + } } From 81343b152d5e60d28e2885050903b1bd6c304cd9 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Wed, 20 Mar 2024 09:24:42 +0000 Subject: [PATCH 08/14] Add first-draft of docs --- docs/creating-post-types-and-taxonomies.md | 271 +++++++++++++++++++++ 1 file changed, 271 insertions(+) create mode 100644 docs/creating-post-types-and-taxonomies.md diff --git a/docs/creating-post-types-and-taxonomies.md b/docs/creating-post-types-and-taxonomies.md new file mode 100644 index 0000000..85bd94d --- /dev/null +++ b/docs/creating-post-types-and-taxonomies.md @@ -0,0 +1,271 @@ +# Creating Post Types and Taxonomies in the MU-Plugin + +The MU plugin contains abstract classses that can be extended to easily register new post types and taxonomies. This document will explain how to use these classes to create new post types and taxonomies. + +If you want to jump right in, take a look at the `TenUpPlugin\PostTypes\Demo::class` and `TenUpPlugin\Taxonomies\Demo::class` classes. + + +## Post Types + +To create a new post type, you will need to create a new class that extends the `TenUpPlugin\PostTypes\AbstractPostType` class. This class will contain the configuration for the new post type. + +Once you've extended the class (or copied the `Demo` class), you will need to define the following methods: + +- `get_name()` - This should return the name of the post type as it will be used within the WordPress database. +- `get_singular_label()` - This should return the singular label for the post type. +- `get_plural_label()` - This should return the plural label for the post type. +- `get_menu_icon()` - This should return the dashicon to use for the post type in the WordPress admin menu, it can also return a base64 encoded SVG or the string `'none'`. +- `can_register()` - Whether the post type should be actively registered or not. See [Registering Classes](./registering-classes.md). + +Once those are defined, ensure that `can_register()` is returning `true` and you should be able to see your post type within the admin. + +### Heirarchical Post Types + +Should you need to create a heirarchical post type, you can override the `is_hierarchical()` method and return `true`. + +```php +/** + * Is the post type hierarchical? + * + * @return bool + */ +public function is_hierarchical() { + return true; +} +``` + +### Changing the Post Supports + +There are a few options that can exist within [post supports](https://developer.wordpress.org/reference/functions/register_post_type/#parameters:~:text=handling.%0ADefault%20false.-,supports,-array), the scaffold aims to set sensible defaults but sometimes they will need changing. There are two ways to do this: + +a. Override the `get_editor_supports()` method and return an array of the supports you want to enable. + +```php +/** + * Default post type supported feature names. + * + * @return array + */ +public function get_editor_supports() { + $supports = [ + 'title', + 'editor', + 'thumbnail', + 'custom-fields', + ]; + + return $supports; +} +``` +b. Merge your additional supports into the base version + +```php +/** + * Default post type supported feature names. + * + * @return array + */ +public function get_editor_supports() { + $supports = parent::get_editor_supports(); + $supports[] = 'custom-fields'; + + return $supports; +} +``` + +### Changing Post Type Options + +Much like with the Supports, the scaffold aims to set sensible defaults for the post type options. Should you want to change these, you can handle it in much the same way: + +a. Override the `get_options()` method and return an array of the options you want to enable. + +```php +/** + * Default post type supported feature names. + * + * @return array + */ +public function get_options() { + $options = [ + 'labels' => $this->get_labels(), + 'public' => false, + 'has_archive' => false, + 'show_ui' => true, + 'show_in_menu' => true, + 'show_in_nav_menus' => false, + 'show_in_rest' => true, + 'supports' => $this->get_editor_supports(), + 'menu_icon' => $this->get_menu_icon(), + 'menu_position' => $this->get_menu_position(), + 'hierarchical' => $this->is_hierarchical(), + ]; + + return $options; +} +``` +b. Merge your additional options into the base version + +```php +/** + * Default post type supported feature names. + * + * @return array + */ +public function get_options() { + $options = parent::get_options(); + $options['public'] = false; + $options['has_archive'] = false; + + return $options; +} +``` + +### Adding Taxonomies to a Post Type + +To add a taxonomy to a post type, you'll need to override the `get_supported_taxonomies()` method on the post type class and return an array of taxonomies you'd like to support. + +E.G. + +```php +/** + * Returns the default supported taxonomies. The subclass should declare the + * Taxonomies that it supports here if required. + * + * @return array + */ +public function get_supported_taxonomies() { + return [ + 'category', + 'post_tag', + ]; +} +``` + +### Registering Post Meta + +Whilst there is no official way to register post meta, the scaffold provides an `after_register()` method that's called after the post type is registered. + +A useful pattern is: + +```php +/** + * Run any code after the post type has been registered. + * + * @return void + */ +public function after_register() { + register_post_meta( + $this->get_name(), + 'my_test_meta_key', + [ + 'show_in_rest' => true, + 'single' => true, + 'type' => 'string', + ] + ); +} +``` + +The `after_register()` method can also be useful to hook into anything else you may need to do, directly related to the registration of the post type. + +## Taxonomies + +To create a new taxonomy, you will need to create a new class that extends the `TenUpPlugin\Taxonomies\AbstractTaxonomy` class. This class will contain the configuration for the new taxonomy. + +Once you've extended the class (or copied the `Demo` class), you will need to define the following methods: + +- `get_name()` - This should return the name of the taxonomy as it will be used within the WordPress database. +- `get_singular_label()` - This should return the singular label for the taxonomy. +- `get_plural_label()` - This should return the plural label for the taxonomy. +- `can_register()` - Whether the taxonomy should be actively registered or not. See [Registering Classes](./registering-classes.md). + +Once those are defined, ensure that `can_register()` is returning `true` and that you've [registered the taxonomy with a post type](#adding-taxonomies-to-a-post-type), then you should be able to see your taxonomy within the admin. + +### Heirarchical Taxonomies + +Should you need to create a heirarchical taxonomy, you can override the `is_hierarchical()` method and return `true`. + +```php +/** + * Is the taxonomy hierarchical? + * + * @return bool + */ +public function is_hierarchical() { + return true; +} +``` + +### Changing the Taxonomy Options + +The scaffold aims to set sensible defaults for the taxonomy options. Should you want to change these, you can handle it in much the same way as post types: + +a. Override the `get_options()` method and return an array of the options you want to enable. + +```php +/** + * Get the options for the taxonomy. + * + * @return array + */ +public function get_options() { + $options = [ + 'labels' => $this->get_labels(), + 'hierarchical' => $this->is_hierarchical(), + 'show_ui' => true, + 'show_admin_column' => true, + 'query_var' => true, + 'show_in_rest' => true, + 'public' => true, + ]; + + return $options; +} +``` +b. Merge your additional options into the base version + +```php +/** + * Get the options for the taxonomy. + * + * @return array + */ +public function get_options() { + $options = parent::get_options(); + $options['public'] = false; + + return $options; +} +``` + +### Registering Taxonomy Meta + +Whilst there is no official way to register taxonomy meta, the scaffold provides an `after_register()` method that's called after the taxonomy is registered. + +A useful pattern is: + +```php +/** + * Run any code after the taxonomy has been registered. + * + * @return void + */ +public function after_register() { + register_term_meta( + $this->get_name(), + 'my_test_meta_key', + [ + 'show_in_rest' => true, + 'single' => true, + 'type' => 'string', + ] + ); +} +``` + +## Further Information + +I encourage you to look at the abstract post classes, whilst they aim to provide a sensible set of defaults, everything within them can be overidden, should it need to be. + +If you find yourself making the same changes across multiple classes or projects, please consider [opening a ticket](https://github.com/10up/wp-scaffold/issues/new?assignees=&labels=type%3Aenhancement&projects=&template=2-enhancement.yml) which describes those changes. +That way we can look at updating the scaffold to help others in the future. From 44c4c2f6bd801d54dc22b1dca01acde966814e70 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Wed, 20 Mar 2024 09:26:25 +0000 Subject: [PATCH 09/14] Spelling fix --- docs/creating-post-types-and-taxonomies.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/creating-post-types-and-taxonomies.md b/docs/creating-post-types-and-taxonomies.md index 85bd94d..5302f1f 100644 --- a/docs/creating-post-types-and-taxonomies.md +++ b/docs/creating-post-types-and-taxonomies.md @@ -19,9 +19,9 @@ Once you've extended the class (or copied the `Demo` class), you will need to de Once those are defined, ensure that `can_register()` is returning `true` and you should be able to see your post type within the admin. -### Heirarchical Post Types +### Hierarchical Post Types -Should you need to create a heirarchical post type, you can override the `is_hierarchical()` method and return `true`. +Should you need to create a hierarchical post type, you can override the `is_hierarchical()` method and return `true`. ```php /** @@ -181,9 +181,9 @@ Once you've extended the class (or copied the `Demo` class), you will need to de Once those are defined, ensure that `can_register()` is returning `true` and that you've [registered the taxonomy with a post type](#adding-taxonomies-to-a-post-type), then you should be able to see your taxonomy within the admin. -### Heirarchical Taxonomies +### Hierarchical Taxonomies -Should you need to create a heirarchical taxonomy, you can override the `is_hierarchical()` method and return `true`. +Should you need to create a hierarchical taxonomy, you can override the `is_hierarchical()` method and return `true`. ```php /** From 8e9c890bfc692bd67a009e220473e5cb8d79d64f Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 2 Apr 2024 11:32:15 +0100 Subject: [PATCH 10/14] Be consistent with imports --- mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php | 4 +++- mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php index 316f8c5..b1327a7 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php @@ -7,10 +7,12 @@ namespace TenUpPlugin\PostTypes; +use TenUpPlugin\Module; + /** * Page Post Type */ -class Page extends \TenUpPlugin\Module { +class Page extends Module { /** * Can the class be registered? diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php index 7fd1ea6..4ead182 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php @@ -7,10 +7,12 @@ namespace TenUpPlugin\PostTypes; +use TenUpPlugin\Module; + /** * Post Post type. */ -class Post extends \TenUpPlugin\Module { +class Post extends Module { /** * Can the class be registered? From f9ae10af76b189852671e9bf8d96e23e0ef142af Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 2 Apr 2024 11:34:56 +0100 Subject: [PATCH 11/14] Add Usage to comment --- .../classes/PostTypes/AbstractPostType.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php index f912cc7..3a067f6 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php @@ -11,6 +11,31 @@ /** * Abstract class for post types. + * + * Usage: + * + * class FooPostType extends AbstractPostType { + * + * public function get_name() { + * return 'custom-post-type'; + * } + * + * public function get_singular_label() { + * return 'Custom Post' + * } + * + * public function get_plural_label() { + * return 'Custom Posts'; + * } + * + * public function get_menu_icon() { + * return 'embed-post'; + * } + * + * public function can_register() { + * return true; + * } + * } */ abstract class AbstractPostType extends Module { From d388f533d05b2ad608dd0a9675324450f204e5f7 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 2 Apr 2024 11:37:27 +0100 Subject: [PATCH 12/14] Use shorthand arrays --- .../includes/classes/PostTypes/AbstractPostType.php | 8 +++----- .../includes/classes/Taxonomies/AbstractTaxonomy.php | 8 ++++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php index 3a067f6..04b20b8 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractPostType.php @@ -113,7 +113,7 @@ public function get_editor_supports() { * @return array */ public function get_options() { - $options = [ + return [ 'labels' => $this->get_labels(), 'public' => true, 'has_archive' => true, @@ -126,8 +126,6 @@ public function get_options() { 'menu_position' => $this->get_menu_position(), 'hierarchical' => $this->is_hierarchical(), ]; - - return $options; } /** @@ -140,7 +138,7 @@ public function get_labels() { $singular_label = $this->get_singular_label(); // phpcs:disable -- ignoring template strings without translators placeholder since this is dynamic - $labels = array( + $labels = [ 'name' => $plural_label, // Already translated via get_plural_label(). 'singular_name' => $singular_label, @@ -169,7 +167,7 @@ public function get_labels() { 'item_updated' => sprintf( __( '%s updated.', 'tenup-plugin' ), $singular_label ), 'menu_name' => $plural_label, 'name_admin_bar' => $singular_label, - ); + ]; // phpcs:enable return $labels; diff --git a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php index 59ca905..d7d0b13 100644 --- a/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php +++ b/mu-plugins/10up-plugin/includes/classes/Taxonomies/AbstractTaxonomy.php @@ -98,7 +98,7 @@ public function register() { * @return array */ public function get_options() { - return array( + return [ 'labels' => $this->get_labels(), 'hierarchical' => $this->is_hierarchical(), 'show_ui' => true, @@ -106,7 +106,7 @@ public function get_options() { 'query_var' => true, 'show_in_rest' => true, 'public' => true, - ); + ]; } /** @@ -119,7 +119,7 @@ public function get_labels() { $singular_label = $this->get_singular_label(); // phpcs:disable - $labels = array( + $labels = [ 'name' => $plural_label, // Already translated via get_plural_label(). 'singular_name' => $singular_label, // Already translated via get_singular_label(). 'search_items' => sprintf( __( 'Search %s', 'tenup-plugin' ), $plural_label ), @@ -135,7 +135,7 @@ public function get_labels() { 'not_found' => sprintf( __( 'No %s found.', 'tenup-plugin' ), strtolower( $plural_label ) ), 'not_found_in_trash' => sprintf( __( 'No %s found in Trash.', 'tenup-plugin' ), strtolower( $plural_label ) ), 'view_item' => sprintf( __( 'View %s', 'tenup-plugin' ), $singular_label ), - ); + ]; // phpcs:enable return $labels; From 1fd2092cb1337f5ddbbd75a14d3f67120df530b4 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Wed, 3 Apr 2024 17:44:56 +0100 Subject: [PATCH 13/14] Update Page and Post to use a similar approach to CPTs --- .../PostTypes/AbstractCorePostType.php | 75 +++++++++++++++++++ .../includes/classes/PostTypes/Page.php | 29 +++++-- .../includes/classes/PostTypes/Post.php | 33 +++++--- 3 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractCorePostType.php diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractCorePostType.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractCorePostType.php new file mode 100644 index 0000000..0cf8e42 --- /dev/null +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/AbstractCorePostType.php @@ -0,0 +1,75 @@ +get_name() to get the post's type name. + * @return Bool Whether this theme has supports for this post type. + */ + public function register() { + $this->register_taxonomies(); + $this->after_register(); + + return true; + } +} diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php index b1327a7..b48d3b9 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php @@ -11,24 +11,37 @@ /** * Page Post Type + * + * This class is a placeholder for the core Page post type. + * It's here to allow engineers to extend the core Page post type in the same way as custom post types. */ -class Page extends Module { +class Page extends AbstractCorePostType { + + /** + * Get the post type name. + * + * @return string + */ + public function get_name() { + return 'page'; + } /** - * Can the class be registered? + * Returns the default supported taxonomies. The subclass should declare the + * Taxonomies that it supports here if required. * - * @return bool + * @return array */ - public function can_register() { - return true; + public function get_supported_taxonomies() { + return []; } /** - * Register hooks and filters. + * Run any code after the post type has been registered. * * @return void */ - public function register() { - // Register any hooks/filters you need. + public function after_register() { + // Do nothing. } } diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php index 4ead182..343aa0a 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Post.php @@ -7,28 +7,41 @@ namespace TenUpPlugin\PostTypes; -use TenUpPlugin\Module; - /** * Post Post type. + * + * This class is a placeholder for the core Post post type. + * It's here to allow engineers to extend the core Post post type in the same way as custom post types. */ -class Post extends Module { +class Post extends AbstractCorePostType { + + /** + * Get the post type name. + * + * @return string + */ + public function get_name() { + return 'post'; + } /** - * Can the class be registered? + * Returns the default supported taxonomies. The subclass should declare the + * Taxonomies that it supports here if required. + * + * Note: This will not remove the default taxonomies that are registered by core. * - * @return bool + * @return array */ - public function can_register() { - return true; + public function get_supported_taxonomies() { + return []; } /** - * Register hooks and filters. + * Run any code after the post type has been registered. * * @return void */ - public function register() { - // Register any hooks/filters you need. + public function after_register() { + // Do nothing. } } From 55f701d61b4f8646e0e599296a299789ef1eb539 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Wed, 3 Apr 2024 17:55:53 +0100 Subject: [PATCH 14/14] Remove unused Module --- mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php index b48d3b9..0eab002 100644 --- a/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php +++ b/mu-plugins/10up-plugin/includes/classes/PostTypes/Page.php @@ -7,8 +7,6 @@ namespace TenUpPlugin\PostTypes; -use TenUpPlugin\Module; - /** * Page Post Type *