From ab06c003670d33f007d516ab31d48f2f42a638a3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Wed, 21 Aug 2024 16:01:06 +0200 Subject: [PATCH] Navigation Block: Remove now-obsolete function_exists guards (#64673) - Remove a number of `function_exists()` guards that checked for existence of the `set_ignored_hooked_blocks_metadata` function. (That function was introduced in WP 6.5, and Gutenberg now requires WP 6.5 as a minimum.) - Delete code that checked if the `block_core_navigation_update_ignore_hooked_blocks_meta` function was attached to the `rest_insert_wp_navigation` _action_ hook, and removed it if it was. Co-authored-by: ockham Co-authored-by: gziolo --- .../block-library/src/navigation/index.php | 37 +++++-------------- .../block-navigation-block-hooks-test.php | 12 ------ 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index a73aa682f596c9..ccadd5c4a222d4 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -241,13 +241,11 @@ private static function get_inner_blocks_from_navigation_post( $attributes ) { // it encounters whitespace. This code strips it. $blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); - if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - // Run Block Hooks algorithm to inject hooked blocks. - $markup = block_core_navigation_insert_hooked_blocks( $blocks, $navigation_post ); - $root_nav_block = parse_blocks( $markup )[0]; + // Run Block Hooks algorithm to inject hooked blocks. + $markup = block_core_navigation_insert_hooked_blocks( $blocks, $navigation_post ); + $root_nav_block = parse_blocks( $markup )[0]; - $blocks = isset( $root_nav_block['innerBlocks'] ) ? $root_nav_block['innerBlocks'] : $blocks; - } + $blocks = isset( $root_nav_block['innerBlocks'] ) ? $root_nav_block['innerBlocks'] : $blocks; // TODO - this uses the full navigation block attributes for the // context which could be refined. @@ -1083,15 +1081,13 @@ function block_core_navigation_get_fallback_blocks() { // In this case default to the (Page List) fallback. $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks; - if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - // Run Block Hooks algorithm to inject hooked blocks. - // We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks. - $markup = block_core_navigation_insert_hooked_blocks( $fallback_blocks, $navigation_post ); - $blocks = parse_blocks( $markup ); + // Run Block Hooks algorithm to inject hooked blocks. + // We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks. + $markup = block_core_navigation_insert_hooked_blocks( $fallback_blocks, $navigation_post ); + $blocks = parse_blocks( $markup ); - if ( isset( $blocks[0]['innerBlocks'] ) ) { - $fallback_blocks = $blocks[0]['innerBlocks']; - } + if ( isset( $blocks[0]['innerBlocks'] ) ) { + $fallback_blocks = $blocks[0]['innerBlocks']; } } @@ -1621,26 +1617,15 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { /* * Do not add the `block_core_navigation_update_ignore_hooked_blocks_meta` filter in the following cases: * - If Core has added the `update_ignored_hooked_blocks_postmeta` filter already (WP >= 6.6); - * - or if the `set_ignored_hooked_blocks_metadata` function is unavailable (which is required for the filter to work. It was introduced by WP 6.5 but is not present in Gutenberg's WP 6.5 compatibility layer); * - or if the `$rest_insert_wp_navigation_core_callback` filter has already been added. */ if ( ! has_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' ) && - function_exists( 'set_ignored_hooked_blocks_metadata' ) && ! has_filter( 'rest_pre_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ) ) { add_filter( 'rest_pre_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta' ); } -/* - * Previous versions of Gutenberg were attaching the block_core_navigation_update_ignore_hooked_blocks_meta - * function to the `rest_insert_wp_navigation` _action_ (rather than the `rest_pre_insert_wp_navigation` _filter_). - * To avoid collisions, we need to remove the filter from that action if it's present. - */ -if ( has_filter( 'rest_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ) ) { - remove_filter( 'rest_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ); -} - /** * Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks. * @@ -1678,12 +1663,10 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons /* * Do not add the `block_core_navigation_insert_hooked_blocks_into_rest_response` filter in the following cases: * - If Core has added the `insert_hooked_blocks_into_rest_response` filter already (WP >= 6.6); - * - or if the `set_ignored_hooked_blocks_metadata` function is unavailable (which is required for the filter to work. It was introduced by WP 6.5 but is not present in Gutenberg's WP 6.5 compatibility layer); * - or if the `$rest_prepare_wp_navigation_core_callback` filter has already been added. */ if ( ! has_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response' ) && - function_exists( 'set_ignored_hooked_blocks_metadata' ) && ! has_filter( 'rest_prepare_wp_navigation', $rest_prepare_wp_navigation_core_callback ) ) { add_filter( 'rest_prepare_wp_navigation', 'block_core_navigation_insert_hooked_blocks_into_rest_response', 10, 3 ); diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index 3cb2e785a8e11a..1d3c86bf4bca4b 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -58,10 +58,6 @@ public function tear_down() { * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_preserves_entities() { - if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); - } - register_block_type( 'tests/my-block', array( @@ -97,10 +93,6 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ public function test_block_core_navigation_dont_modify_no_post_id() { - if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); - } - register_block_type( 'tests/my-block', array( @@ -127,10 +119,6 @@ public function test_block_core_navigation_dont_modify_no_post_id() { * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta */ public function test_block_core_navigation_retains_content_if_not_set() { - if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); - } - register_block_type( 'tests/my-block', array(