diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php index 0e367eb96bfaf..c2fa7d00196c6 100644 --- a/src/wp-admin/edit-form-blocks.php +++ b/src/wp-admin/edit-form-blocks.php @@ -23,7 +23,6 @@ */ global $post_type, $post_type_object, $post, $title, $editor_styles, $wp_meta_boxes; -$editor_name = 'post-editor'; $block_editor_context = new WP_Block_Editor_Context( array( 'post' => $post ) ); // Flag that we're loading the block editor. @@ -280,7 +279,7 @@ unset( $editor_settings['enableCustomFields'] ); } -$editor_settings = get_block_editor_settings( $editor_name, $editor_settings ); +$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context ); $init_script = << $post_or_block_editor_context, + ) + ) : $post_or_block_editor_context; /** * Filters the default array of categories for block types. * * @since 5.8.0 * - * @param array[] $block_categories Array of categories for block types. - * @param string $editor_name The name of the editor, e.g. 'post-editor'. + * @param array[] $block_categories Array of categories for block types. + * @param WP_Block_Editor_Context $block_editor_context The current block editor context. */ - $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(); + $block_categories = apply_filters( 'block_categories_all', $block_categories, $block_editor_context ); + if ( ! empty( $block_editor_context->post ) ) { + $post = $block_editor_context->post; /** * Filters the default array of categories for block types. @@ -101,11 +106,11 @@ function get_block_categories( $editor_name_or_post ) { * * @since 5.8.0 * - * @param string $editor_name The name of the editor (e.g. 'post-editor'). + * @param WP_Block_Editor_Context $block_editor_context The current block editor context. * * @return bool|array Array of block type slugs, or boolean to enable/disable all. */ -function get_allowed_block_types( $editor_name ) { +function get_allowed_block_types( $block_editor_context ) { $allowed_block_types = true; /** @@ -115,13 +120,13 @@ function get_allowed_block_types( $editor_name ) { * * @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'. + * @param bool|array $allowed_block_types Array of block type slugs, or + * boolean to enable/disable all. + * @param WP_Block_Editor_Context $block_editor_context The current block editor context. */ - $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $editor_name ); - if ( 'post-editor' === $editor_name ) { - $post = get_post(); + $allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context ); + if ( ! empty( $block_editor_context->post ) ) { + $post = $block_editor_context->post; /** * Filters the allowed block types for the editor, defaulting to true (all @@ -224,21 +229,21 @@ function get_default_block_editor_settings() { } /** - * Returns the contextualized block editor settings settings for a selected editor type. + * Returns the contextualized block editor settings settings for a selected editor context. * * @since 5.8.0 * - * @param string $editor_name The name of the editor (e.g. 'post-editor'). - * @param array $custom_settings Optional custom settings to use with the editor type. + * @param array $custom_settings Optional custom settings to use with the editor type. + * @param WP_Block_Editor_Context $block_editor_context The current block editor context. * * @return array The contextualized block editor settings. */ -function get_block_editor_settings( $editor_name, $custom_settings = array() ) { +function get_block_editor_settings( array $custom_settings, $block_editor_context ) { $editor_settings = array_merge( get_default_block_editor_settings(), array( - 'allowedBlockTypes' => get_allowed_block_types( $editor_name ), - 'blockCategories' => get_block_categories( $editor_name ), + 'allowedBlockTypes' => get_allowed_block_types( $block_editor_context ), + 'blockCategories' => get_block_categories( $block_editor_context ), ), $custom_settings ); @@ -292,12 +297,12 @@ function get_block_editor_settings( $editor_name, $custom_settings = array() ) { * * @since 5.8.0 * - * @param array $editor_settings Default editor settings. - * @param string $editor_name The name of the editor, e.g. 'post-editor'. + * @param array $editor_settings Default editor settings. + * @param WP_Block_Editor_Context $block_editor_context The current block editor context. */ - $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $editor_name ); - if ( 'post-editor' === $editor_name ) { - $post = get_post(); + $editor_settings = apply_filters( 'block_editor_settings_all', $editor_settings, $block_editor_context ); + if ( ! empty( $block_editor_context->post ) ) { + $post = $block_editor_context->post; /** * Filters the settings to pass to the block editor. diff --git a/tests/phpunit/tests/blocks/block-editor.php b/tests/phpunit/tests/blocks/block-editor.php index 0ffad06fa1cb9..b11ac707c8933 100644 --- a/tests/phpunit/tests/blocks/block-editor.php +++ b/tests/phpunit/tests/blocks/block-editor.php @@ -76,9 +76,9 @@ function test_block_editor_context_no_settings() { * @ticket 52920 */ function test_block_editor_context_post() { - $context = new WP_Block_Editor_Context( array( 'post' => $this->post ) ); + $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); - $this->assertSame( $this->post, $context->post ); + $this->assertSame( get_post(), $context->post ); } /** @@ -111,7 +111,8 @@ function test_get_block_categories_deprecated_filter_post_object() { function test_get_block_categories_deprecated_filter_post_editor() { add_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ), 10, 2 ); - $block_categories = get_block_categories( 'post-editor' ); + $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); + $block_categories = get_block_categories( $post_editor_context ); remove_filter( 'block_categories', array( $this, 'filter_set_block_categories_post' ) ); @@ -131,7 +132,8 @@ function test_get_block_categories_deprecated_filter_post_editor() { * @ticket 52920 */ function test_get_allowed_block_types_default() { - $allowed_block_types = get_allowed_block_types( 'post-editor' ); + $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); + $allowed_block_types = get_allowed_block_types( $post_editor_context ); $this->assertTrue( $allowed_block_types ); } @@ -143,7 +145,8 @@ function test_get_allowed_block_types_default() { function test_get_allowed_block_types_deprecated_filter_post_editor() { add_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ), 10, 2 ); - $allowed_block_types = get_allowed_block_types( 'post-editor' ); + $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); + $allowed_block_types = get_allowed_block_types( $post_editor_context ); remove_filter( 'allowed_block_types', array( $this, 'filter_set_allowed_block_types_post' ) ); @@ -279,7 +282,8 @@ function filter_block_editor_settings_my_editor( $editor_settings ) { 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 = get_block_editor_settings( 'my-editor' ); + $my_editor_context = new WP_Block_Editor_Context(); + $settings = get_block_editor_settings( array(), $my_editor_context ); remove_filter( 'allowed_block_types_all', 'filter_allowed_block_types_my_editor' ); remove_filter( 'block_categories_all', 'filter_block_categories_my_editor' ); @@ -306,7 +310,8 @@ function filter_block_editor_settings_my_editor( $editor_settings ) { function test_get_block_editor_settings_deprecated_filter_post_editor() { add_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ), 10, 2 ); - $settings = get_block_editor_settings( 'post-editor' ); + $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); + $settings = get_block_editor_settings( array(), $post_editor_context ); remove_filter( 'block_editor_settings', array( $this, 'filter_set_block_editor_settings_post' ) ); @@ -322,8 +327,8 @@ function test_get_block_editor_settings_deprecated_filter_post_editor() { * @ticket 52920 */ function test_block_editor_rest_api_preload_no_paths() { - $context = new WP_Block_Editor_Context(); - block_editor_rest_api_preload( array(), $context ); + $editor_context = new WP_Block_Editor_Context(); + block_editor_rest_api_preload( array(), $editor_context ); $after = implode( '', wp_scripts()->registered['wp-api-fetch']->extra['after'] ); $this->assertNotContains( 'wp.apiFetch.createPreloadingMiddleware', $after ); @@ -342,12 +347,12 @@ function filter_remove_preload_paths( $preload_paths, $post ) { } add_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths', 10, 2 ); - $context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); + $post_editor_context = new WP_Block_Editor_Context( array( 'post' => get_post() ) ); block_editor_rest_api_preload( array( array( '/wp/v2/blocks', 'OPTIONS' ), ), - $context + $post_editor_context ); remove_filter( 'block_editor_preload_paths', 'filter_remove_preload_paths' ); @@ -369,12 +374,12 @@ function filter_add_preload_paths( $preload_paths, WP_Block_Editor_Context $cont } add_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths', 10, 2 ); - $context = new WP_Block_Editor_Context(); + $editor_context = new WP_Block_Editor_Context(); block_editor_rest_api_preload( array( array( '/wp/v2/blocks', 'OPTIONS' ), ), - $context + $editor_context ); remove_filter( 'block_editor_rest_api_preload_paths', 'filter_add_preload_paths' );