From d33e248866170f2d0372b72e83d8340b1eb13d11 Mon Sep 17 00:00:00 2001 From: Akshit Sethi Date: Thu, 12 May 2022 14:15:02 +0530 Subject: [PATCH] Apply suggested PR changes --- config.php | 1 + includes/ConvertToBlocks/Plugin.php | 24 +++++++++-- includes/ConvertToBlocks/Settings.php | 59 +++++++++++++-------------- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/config.php b/config.php index 7308516..8333bc9 100644 --- a/config.php +++ b/config.php @@ -18,6 +18,7 @@ convert_to_blocks_define( 'CONVERT_TO_BLOCKS_DIR', plugin_dir_path( __FILE__ ) ); convert_to_blocks_define( 'CONVERT_TO_BLOCKS_URL', plugin_dir_url( __FILE__ ) ); convert_to_blocks_define( 'CONVERT_TO_BLOCKS_SLUG', 'convert-to-blocks' ); +convert_to_blocks_define( 'CONVERT_TO_BLOCKS_DEFAULT_POST_TYPES', [ 'post', 'page' ] ); /* Labels */ diff --git a/includes/ConvertToBlocks/Plugin.php b/includes/ConvertToBlocks/Plugin.php index 06428ed..93f87ce 100644 --- a/includes/ConvertToBlocks/Plugin.php +++ b/includes/ConvertToBlocks/Plugin.php @@ -183,14 +183,30 @@ public function init_locale() { * @return bool */ public function post_type_supports_convert_to_blocks( $post_type ) { - $supports = post_type_supports( $post_type, 'convert-to-blocks' ); - $use_defaults = apply_filters( 'convert_to_blocks_defaults', true ); - $default_post_types = $this->get_default_post_types(); + $supports = post_type_supports( $post_type, 'convert-to-blocks' ); + $use_defaults = apply_filters( 'convert_to_blocks_defaults', true ); + $default_post_types = $this->get_default_post_types(); + $user_selected_post_types = get_option( sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ), $default_post_types ); if ( ! $supports && $use_defaults && in_array( $post_type, $default_post_types, true ) ) { $supports = true; } + // For user-selected option via the Settings UI. + if ( false !== $user_selected_post_types ) { + $supports = false; + + // If no post_type is selected. + if ( empty( $user_selected_post_types ) ) { + $supports = false; + } + + // Check if post_type is selected by the user. + if ( in_array( $post_type, $user_selected_post_types, true ) ) { + $supports = true; + } + } + $supports = apply_filters( 'post_type_supports_convert_to_blocks', $supports, $post_type ); return $supports; @@ -256,7 +272,7 @@ public function is_classic_editor_post( $post_id ) { * @return array */ public function get_default_post_types() { - return apply_filters( 'convert_to_blocks_default_post_types', [ 'post', 'page' ] ); + return apply_filters( 'convert_to_blocks_default_post_types', CONVERT_TO_BLOCKS_DEFAULT_POST_TYPES ); } /** diff --git a/includes/ConvertToBlocks/Settings.php b/includes/ConvertToBlocks/Settings.php index 66afee9..224ea2d 100644 --- a/includes/ConvertToBlocks/Settings.php +++ b/includes/ConvertToBlocks/Settings.php @@ -57,8 +57,7 @@ public function register() { add_action( 'admin_menu', [ $this, 'add_menu' ] ); add_action( 'admin_init', [ $this, 'register_section' ], 10 ); add_action( 'admin_init', [ $this, 'register_fields' ], 20 ); - - add_filter( 'post_type_supports_convert_to_blocks', [ $this, 'supported_post_types' ], PHP_INT_MAX, 2 ); + add_action( 'admin_notices', [ $this, 'filter_notice' ], 10 ); } /** @@ -190,7 +189,10 @@ public function register_fields() { * @return void */ public function field_post_types() { - $post_types = get_option( sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ), [] ); + $post_types = get_option( + sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ), + apply_filters( 'convert_to_blocks_default_post_types', CONVERT_TO_BLOCKS_DEFAULT_POST_TYPES ) + ); $output_html = ''; foreach ( $this->post_types as $post_type ) { @@ -226,36 +228,33 @@ public function sanitize_post_types( $input ) { } /** - * Adds support for supported post types to the plugin. - * - * @param bool $supports Whether the post_type is supported or not. - * @param string $post_type Post type to be be checked. - * - * @return bool + * Adds an admin notice if a filter is active for `post_type_supports_convert_to_blocks` as + * this might overwrite the outcome of the settings stored in DB. */ - public function supported_post_types( $supports, $post_type ) { - $supported_post_types = get_option( sprintf( '%s_post_types', CONVERT_TO_BLOCKS_SLUG ) ); - - // If the settings option does not exist in DB. - if ( false === $supported_post_types ) { - return $supports; - } - - // If no post_type is selected. - if ( empty( $supported_post_types ) ) { - return false; - } - - // Check if post_type is selected by the user. - if ( in_array( $post_type, $supported_post_types, true ) ) { - return true; + public function filter_notice() { + if ( ! has_filter( 'post_type_supports_convert_to_blocks' ) ) { + return; } - /** - * This also overrides default post_types since we have the option to de-select the - * default ones via the settings panel. - */ - return false; + // URL to the settings panel. + $settings_url = add_query_arg( + [ 'page' => CONVERT_TO_BLOCKS_SLUG ], + admin_url( 'options-general.php' ) + ); + ?> +
+

+ ', + '' + ); + ?> +

+
+