From 4ac39957a270b5e7f10ade44fa77dc77625479f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Wed, 21 Apr 2021 09:30:40 +0000 Subject: [PATCH] Editor: Shape block editor filters to work better with the Gutenberg plugin This should allow to use new filters in the Gutenberg plugin and therefore it prevents deprecation warnings when in the debug mode. See #52920. git-svn-id: https://develop.svn.wordpress.org/trunk@50777 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-editor.php | 58 ++++++++++++++++----- tests/phpunit/tests/blocks/block-editor.php | 53 ++++++++++++++----- 2 files changed, 84 insertions(+), 27 deletions(-) diff --git a/src/wp-includes/block-editor.php b/src/wp-includes/block-editor.php index b5aa648fbb..d96569e331 100644 --- a/src/wp-includes/block-editor.php +++ b/src/wp-includes/block-editor.php @@ -66,17 +66,18 @@ function get_default_block_categories() { */ function get_block_categories( $editor_name_or_post ) { // Assume the post editor when the WP_Post object passed. - $editor_name = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post; - $default_categories = get_default_block_categories(); + $editor_name = is_object( $editor_name_or_post ) ? 'post-editor' : $editor_name_or_post; + $block_categories = get_default_block_categories(); /** * Filters the default array of categories for block types. * * @since 5.8.0 * - * @param array[] $default_categories Array of categories for block types. + * @param array[] $block_categories Array of categories for block types. + * @param string $editor_name The name of the editor, e.g. 'post-editor'. */ - $block_categories = apply_filters( "block_categories_{$editor_name}", $default_categories ); + $block_categories = apply_filters( 'block_categories_all', $block_categories, $editor_name ); if ( 'post-editor' === $editor_name ) { $post = is_object( $editor_name_or_post ) ? $editor_name_or_post : get_post(); @@ -89,7 +90,7 @@ function 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; @@ -108,15 +109,32 @@ function get_allowed_block_types( $editor_name ) { $allowed_block_types = true; /** - * Filters the allowed block types for the given editor, defaulting to true (all - * registered block types supported). + * Filters the allowed block types for all editor types, defaulting to `true` + * (all registered block types supported). + * * * @since 5.8.0 * * @param bool|array $allowed_block_types Array of block type slugs, or * boolean to enable/disable all. + * @param string $editor_name The name of the editor, e.g. 'post-editor'. */ - $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, $editor_name ); + + /** + * Filters the allowed block types for the given editor, defaulting to `true` + * (all registered block types supported). + * + * The dynamic portion of the hook name, `$editor_name`, refers to the name + * of the editor type, e.g. 'post-editor', 'site-editor', etc. + * + * @since 5.8.0 + * + * @param bool|array $allowed_block_types Array of block type slugs, or + * boolean to enable/disable all. + * @param string $editor_name The name of the editor, e.g. 'post-editor'. + */ + $allowed_block_types = apply_filters( "allowed_block_types_{$editor_name}", $allowed_block_types, $editor_name ); if ( 'post-editor' === $editor_name ) { $post = get_post(); @@ -131,7 +149,7 @@ function 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; @@ -241,13 +259,27 @@ function get_block_editor_settings( $editor_name, $custom_settings = array() ) { ); /** - * 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 type. * * @since 5.8.0 * - * @param array $editor_settings Default editor settings. + * @param array $editor_settings Default editor settings. + * @param string $editor_name The name of the editor, e.g. 'post-editor'. */ - $editor_settings = apply_filters( "block_editor_settings_{$editor_name}", $editor_settings ); + $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $editor_name ); + + /** + * Filters the settings to pass to the block editor for a given editor type. + * + * The dynamic portion of the hook name, `$editor_name`, refers to the name + * of the editor type, e.g. 'post-editor', 'site-editor', etc. + * + * @since 5.8.0 + * + * @param array $editor_settings Default editor settings. + * @param string $editor_name The name of the editor, e.g. 'post-editor'. + */ + $editor_settings = apply_filters( "block_editor_settings_{$editor_name}", $editor_settings, $editor_name ); if ( 'post-editor' === $editor_name ) { $post = get_post(); @@ -260,7 +292,7 @@ function get_block_editor_settings( $editor_name, $custom_settings = array() ) { * @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/tests/phpunit/tests/blocks/block-editor.php b/tests/phpunit/tests/blocks/block-editor.php index d638298f05..cfb8ccca14 100644 --- a/tests/phpunit/tests/blocks/block-editor.php +++ b/tests/phpunit/tests/blocks/block-editor.php @@ -252,15 +252,6 @@ class WP_Test_Block_Editor extends WP_UnitTestCase { function filter_allowed_block_types_my_editor() { return array( 'test/filtered-my-block' ); } - function filter_block_categories_my_editor() { - return array( - array( - 'slug' => 'filtered-my-category', - 'title' => 'Filtered My Category', - 'icon' => null, - ), - ); - } function filter_block_editor_settings_my_editor( $editor_settings ) { $editor_settings['maxUploadFileSize'] = 12345; @@ -268,27 +259,61 @@ class WP_Test_Block_Editor extends WP_UnitTestCase { } 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 ); $settings = 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' ); $this->assertSameSets( array( 'test/filtered-my-block' ), $settings['allowedBlockTypes'] ); + $this->assertSame( 12345, $settings['maxUploadFileSize'] ); + } + + /** + * @ticket 52920 + */ + function test_get_block_editor_settings_overrides_default_settings_any_editor() { + function filter_allowed_block_types_any_editor() { + return array( 'test/filtered-any-block' ); + } + function filter_block_categories_any_editor() { + return array( + array( + 'slug' => 'filtered-any-category', + 'title' => 'Filtered Any Category', + 'icon' => null, + ), + ); + } + function filter_block_editor_settings_any_editor( $editor_settings ) { + $editor_settings['maxUploadFileSize'] = 54321; + + return $editor_settings; + } + + add_filter( 'allowed_block_types_all', 'filter_allowed_block_types_any_editor', 10, 1 ); + add_filter( 'block_categories_all', 'filter_block_categories_any_editor', 10, 1 ); + add_filter( 'block_editor_settings_all', 'filter_block_editor_settings_any_editor', 10, 1 ); + + $settings = get_block_editor_settings( 'any-editor' ); + + remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_any_editor' ); + remove_filter( 'block_categories_all', 'filter_block_categories_any_editor' ); + remove_filter( 'block_editor_settings_all', 'filter_block_editor_settings_any_editor' ); + + $this->assertSameSets( array( 'test/filtered-any-block' ), $settings['allowedBlockTypes'] ); $this->assertSameSets( array( array( - 'slug' => 'filtered-my-category', - 'title' => 'Filtered My Category', + 'slug' => 'filtered-any-category', + 'title' => 'Filtered Any Category', 'icon' => null, ), ), $settings['blockCategories'] ); - $this->assertSame( 12345, $settings['maxUploadFileSize'] ); + $this->assertSame( 54321, $settings['maxUploadFileSize'] ); } /**