From 12284ee65244883eca0ce9e245fc38fc73dd7f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Tue, 4 May 2021 09:08:01 +0200 Subject: [PATCH] Plugin: Call deprecated hooks only when new filters not present (#31027) * Plugin: Call deprecated hooks only when new filters not present * Update PHP filters for block editor to be a type agnostic --- lib/block-editor.php | 14 +++++++------- lib/blocks.php | 6 ++++-- lib/client-assets.php | 18 ++++++++++++++++-- lib/editor-settings.php | 7 ++++++- lib/global-styles.php | 10 +++++++++- lib/widgets.php | 9 ++++++++- phpunit/block-editor-test.php | 25 +++++++------------------ 7 files changed, 57 insertions(+), 32 deletions(-) diff --git a/lib/block-editor.php b/lib/block-editor.php index 4497a20d559b4..922dd87ce032d 100644 --- a/lib/block-editor.php +++ b/lib/block-editor.php @@ -86,7 +86,7 @@ function gutenberg_get_block_categories( $editor_name_or_post ) { * * @param array[] $default_categories Array of categories for block types. */ - $block_categories = apply_filters( "block_categories_{$editor_name}", $default_categories ); + $block_categories = apply_filters( 'block_categories_all', $default_categories ); if ( 'post-editor' === $editor_name ) { $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post(); @@ -99,7 +99,7 @@ function gutenberg_get_block_categories( $editor_name_or_post ) { * @param array[] $block_categories Array of categories for block types. * @param WP_Post $post Post being loaded. */ - $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', "block_categories_{$editor_name}" ); + $block_categories = apply_filters_deprecated( 'block_categories', array( $block_categories, $post ), '5.8.0', 'block_categories_all' ); } return $block_categories; @@ -131,7 +131,7 @@ function gutenberg_get_allowed_block_types( $editor_name ) { * @param bool|array $allowed_block_types Array of block type slugs, or * boolean to enable/disable all. */ - $allowed_block_types = apply_filters( "allowed_block_types_{$editor_name}", $allowed_block_types ); + $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types ); if ( 'post-editor' === $editor_name ) { $post = get_post(); @@ -146,7 +146,7 @@ function gutenberg_get_allowed_block_types( $editor_name ) { * boolean to enable/disable all. * @param WP_Post $post The post resource data. */ - $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', "allowed_block_types_{$editor_name}" ); + $allowed_block_types = apply_filters_deprecated( 'allowed_block_types', array( $allowed_block_types, $post ), '5.8.0', 'allowed_block_types_all' ); } return $allowed_block_types; @@ -267,13 +267,13 @@ function gutenberg_get_block_editor_settings( $editor_name, $custom_settings = a ); /** - * Filters the settings to pass to the block editor for a given editor type. + * Filters the settings to pass to the block editor for all editor types. * * @since 5.8.0 * * @param array $editor_settings Default editor settings. */ - $editor_settings = apply_filters( "block_editor_settings_{$editor_name}", $editor_settings ); + $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings ); if ( 'post-editor' === $editor_name ) { $post = get_post(); @@ -286,7 +286,7 @@ function gutenberg_get_block_editor_settings( $editor_name, $custom_settings = a * @param array $editor_settings Default editor settings. * @param WP_Post $post Post being edited. */ - $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', "block_editor_settings_{$editor_name}" ); + $editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' ); } return $editor_settings; diff --git a/lib/blocks.php b/lib/blocks.php index 2800e4df6b238..6919116b04af9 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -380,8 +380,10 @@ function gutenberg_register_theme_block_category( $categories ) { ); return $categories; } - -add_filter( 'block_categories', 'gutenberg_register_theme_block_category' ); +// This can be removed when plugin support requires WordPress 5.8.0+. +if ( ! function_exists( 'get_default_block_categories' ) ) { + add_filter( 'block_categories', 'gutenberg_register_theme_block_category' ); +} /** * Checks whether the current block type supports the feature requested. diff --git a/lib/client-assets.php b/lib/client-assets.php index bc5928ef025c3..c1443eef31fa4 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -620,6 +620,8 @@ function gutenberg_register_vendor_script( $scripts, $handle, $src, $deps = arra /** * Extends block editor settings to remove the Gutenberg's `editor-styles.css`; * + * This can be removed when plugin support requires WordPress 5.8.0+. + * * @param array $settings Default editor settings. * * @return array Filtered editor settings. @@ -671,11 +673,18 @@ function gutenberg_extend_block_editor_styles( $settings ) { return $settings; } -add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' ); +// This can be removed when plugin support requires WordPress 5.8.0+. +if ( function_exists( 'get_block_editor_settings' ) ) { + add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_styles' ); +} else { + add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_styles' ); +} /** * Adds a flag to the editor settings to know whether we're in FSE theme or not. * + * This can be removed when plugin support requires WordPress 5.8.0+. + * * @param array $settings Default editor settings. * * @return array Filtered editor settings. @@ -688,7 +697,12 @@ function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) return $settings; } -add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); +// This can be removed when plugin support requires WordPress 5.8.0+. +if ( function_exists( 'get_block_editor_settings' ) ) { + add_filter( 'block_editor_settings_all', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); +} else { + add_filter( 'block_editor_settings', 'gutenberg_extend_block_editor_settings_with_fse_theme_flag' ); +} /** * Sets the editor styles to be consumed by JS. diff --git a/lib/editor-settings.php b/lib/editor-settings.php index e5d0ce8e53cbd..7e09cd52e6e22 100644 --- a/lib/editor-settings.php +++ b/lib/editor-settings.php @@ -30,7 +30,12 @@ function gutenberg_extend_post_editor_settings( $settings ) { return $settings; } -add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' ); +// This can be removed when plugin support requires WordPress 5.8.0+. +if ( function_exists( 'get_block_editor_settings' ) ) { + add_filter( 'block_editor_settings_all', 'gutenberg_extend_post_editor_settings' ); +} else { + add_filter( 'block_editor_settings', 'gutenberg_extend_post_editor_settings' ); +} /** * Initialize a block-based editor. diff --git a/lib/global-styles.php b/lib/global-styles.php index de843af00fee5..b90175f4015a5 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -81,6 +81,8 @@ function gutenberg_experimental_global_styles_enqueue_assets() { /** * Adds the necessary data for the Global Styles client UI to the block settings. * + * This can be removed when plugin support requires WordPress 5.8.0+. + * * @param array $settings Existing block editor settings. * @return array New block editor settings */ @@ -168,7 +170,13 @@ function gutenberg_experimental_global_styles_register_user_cpt() { } add_action( 'init', 'gutenberg_experimental_global_styles_register_user_cpt' ); -add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); +// This can be removed when plugin support requires WordPress 5.8.0+. +if ( function_exists( 'get_block_editor_settings' ) ) { + add_filter( 'block_editor_settings_all', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); +} else { + add_filter( 'block_editor_settings', 'gutenberg_experimental_global_styles_settings', PHP_INT_MAX ); + +} add_action( 'wp_enqueue_scripts', 'gutenberg_experimental_global_styles_enqueue_assets' ); diff --git a/lib/widgets.php b/lib/widgets.php index d748a2fa6323f..c718542363b70 100644 --- a/lib/widgets.php +++ b/lib/widgets.php @@ -210,6 +210,8 @@ function gutenberg_get_legacy_widget_settings() { /** * Extends default editor settings with values supporting legacy widgets. * + * This can be removed when plugin support requires WordPress 5.8.0+. + * * @param array $settings Default editor settings. * * @return array Filtered editor settings. @@ -217,7 +219,12 @@ function gutenberg_get_legacy_widget_settings() { function gutenberg_legacy_widget_settings( $settings ) { return array_merge( $settings, gutenberg_get_legacy_widget_settings() ); } -add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' ); +// This can be removed when plugin support requires WordPress 5.8.0+. +if ( function_exists( 'get_block_editor_settings' ) ) { + add_filter( 'block_editor_settings_all', 'gutenberg_legacy_widget_settings' ); +} else { + add_filter( 'block_editor_settings', 'gutenberg_legacy_widget_settings' ); +} /** * Function to enqueue admin-widgets as part of the block editor assets. diff --git a/phpunit/block-editor-test.php b/phpunit/block-editor-test.php index f450c9fde2939..633f7987ddbf2 100644 --- a/phpunit/block-editor-test.php +++ b/phpunit/block-editor-test.php @@ -242,17 +242,7 @@ function test_get_default_block_editor_settings() { /** * @ticket 52920 */ - function test_get_block_editor_settings_returns_default_settings() { - $this->assertSameSets( - gutenberg_get_block_editor_settings( 'my-editor' ), - gutenberg_get_default_block_editor_settings() - ); - } - - /** - * @ticket 52920 - */ - function test_get_block_editor_settings_overrides_default_settings_my_editor() { + function test_get_block_editor_settings_overrides_default_settings_all_editors() { function filter_allowed_block_types_my_editor() { return array( 'test/filtered-my-block' ); } @@ -271,15 +261,15 @@ function filter_block_editor_settings_my_editor( $editor_settings ) { return $editor_settings; } - add_filter( 'allowed_block_types_my-editor', 'filter_allowed_block_types_my_editor', 10, 1 ); - add_filter( 'block_categories_my-editor', 'filter_block_categories_my_editor', 10, 1 ); - add_filter( 'block_editor_settings_my-editor', 'filter_block_editor_settings_my_editor', 10, 1 ); + add_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor', 10, 1 ); + add_filter( 'block_categories_all', 'filter_block_categories_my_editor', 10, 1 ); + add_filter( 'block_editor_settings_all', 'filter_block_editor_settings_my_editor', 10, 1 ); $settings = gutenberg_get_block_editor_settings( 'my-editor' ); - remove_filter( 'allowed_block_types_my-editor', 'filter_allowed_block_types_my_editor' ); - remove_filter( 'block_categories_my-editor', 'filter_block_categories_my_editor' ); - remove_filter( 'block_editor_settings_my-editor', 'filter_block_editor_settings_my_editor' ); + remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor' ); + remove_filter( 'block_categories_all', 'filter_block_categories_my_editor' ); + remove_filter( 'block_editor_settings_all', 'filter_block_editor_settings_my_editor' ); $this->assertSameSets( array( 'test/filtered-my-block' ), $settings['allowedBlockTypes'] ); $this->assertSameSets( @@ -297,7 +287,6 @@ function filter_block_editor_settings_my_editor( $editor_settings ) { /** * @ticket 52920 - * @expectedDeprecated block_categories * @expectedDeprecated block_editor_settings */ function test_get_block_editor_settings_deprecated_filter_post_editor() {