diff --git a/CHANGELOG.md b/CHANGELOG.md index 25a7f439..9def8fd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `WP Curate` will be documented in this file. +## 1.3.0 - 2023-10-26 + +- Only show the blocks and register the meta on supported post types. +- Supported post types defaults to all Block Editor post types, but can be filtered by filtering `wp_curate_supported_post_types`. + ## 1.2.0 - 2023-10-26 - Adds support for AND/OR operators in the Query Parameters, giving more control over what posts to show. diff --git a/blocks/post/index.php b/blocks/post/index.php index c4f7d0af..c46586e6 100644 --- a/blocks/post/index.php +++ b/blocks/post/index.php @@ -5,10 +5,17 @@ * @package wp-curate */ +use Alley\WP\WP_Curate\Supported_Post_Types; + /** * Registers the wp-curate/post block using the metadata loaded from the `block.json` file. */ function wp_curate_post_block_init(): void { + $supported_post_types = new Supported_Post_Types(); + if ( ! in_array( $supported_post_types->get_current_post_type(), $supported_post_types->get_supported_post_types(), true ) ) { + return; + } + // Register the block by passing the location of block.json. register_block_type( __DIR__ diff --git a/blocks/query/index.php b/blocks/query/index.php index 6624f18b..61b3f4c6 100644 --- a/blocks/query/index.php +++ b/blocks/query/index.php @@ -5,6 +5,8 @@ * @package wp-curate */ +use Alley\WP\WP_Curate\Supported_Post_Types; + /** * Registers the block using the metadata loaded from the `block.json` file. * Behind the scenes, it registers also all assets so they can be enqueued @@ -13,6 +15,11 @@ * @see https://developer.wordpress.org/reference/functions/register_block_type/ */ function wp_curate_query_block_init(): void { + $supported_post_types = new Supported_Post_Types(); + if ( ! in_array( $supported_post_types->get_current_post_type(), $supported_post_types->get_supported_post_types(), true ) ) { + return; + } + // Register the block by passing the location of block.json. register_block_type( __DIR__, diff --git a/config/post-meta.json b/config/post-meta.json index 1575f8d4..e69de29b 100644 --- a/config/post-meta.json +++ b/config/post-meta.json @@ -1,8 +0,0 @@ -{ - "wp_curate_deduplication": { - "post_types": [ - "all" - ], - "type": "boolean" - } -} diff --git a/src/class-supported-post-types.php b/src/class-supported-post-types.php new file mode 100644 index 00000000..f1aa5396 --- /dev/null +++ b/src/class-supported-post-types.php @@ -0,0 +1,87 @@ +initialize_supported_post_types(); + } + + /** + * Initialize the supported post types. + */ + public function initialize_supported_post_types(): void { + // Get all post types. + $post_types = get_post_types( [], 'objects' ); + $supported_post_types = array_filter( $post_types, fn( $type ) => $type->public && use_block_editor_for_post_type( $type->name ) ); + $this->supported_post_types = ( wp_list_pluck( $supported_post_types, 'name' ) ); + $this->register_post_meta(); + } + + /** + * Get the supported post types. + * + * @return string[] + */ + public function get_supported_post_types(): array { + return apply_filters( 'wp_curate_supported_post_types', $this->supported_post_types ); + } + + /** + * Get the current post type. + * + * @return string|false + */ + public function get_current_post_type(): string|false { + $post_type = ''; + + // Ensure we are in the admin before proceeding. + if ( is_admin() ) { + global $pagenow; + + // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.NonceVerification.Recommended + if ( 'post.php' === $pagenow && ! empty( $_GET['post'] ) ) { + // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.Recommended + $post_id = absint( $_GET['post'] ); + $post_type = get_post_type( $post_id ); + // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.NonceVerification.Recommended + } elseif ( 'post-new.php' === $pagenow && ! empty( $_GET['post_type'] ) ) { + // phpcs:ignore WordPress.VIP.SuperGlobalInputUsage.AccessDetected, WordPress.Security.NonceVerification.Recommended + $post_type = sanitize_text_field( $_GET['post_type'] ); + } + } + return $post_type; + } + + /** + * Register the post meta on the supported post types. + */ + public function register_post_meta(): void { + register_meta_helper( + 'post', + $this->get_supported_post_types(), + 'wp_curate_deduplication', + [ + 'type' => 'boolean', + ] + ); + } +} diff --git a/wp-curate.php b/wp-curate.php index 986e4ee6..f72c32ea 100644 --- a/wp-curate.php +++ b/wp-curate.php @@ -3,7 +3,7 @@ * Plugin Name: WP Curate * Plugin URI: https://github.com/alleyinteractive/wp-curate * Description: Plugin to curate homepages and other landing pages - * Version: 1.2.0 + * Version: 1.3.0 * Author: Alley Interactive * Author URI: https://github.com/alleyinteractive/wp-curate * Requires at least: 6.3