Skip to content

Commit

Permalink
Navigation Block: Remove now-obsolete function_exists guards (#64673)
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
3 people authored Aug 21, 2024
1 parent 5e77997 commit ab06c00
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 39 deletions.
37 changes: 10 additions & 27 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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'];
}
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 );
Expand Down
12 changes: 0 additions & 12 deletions phpunit/blocks/block-navigation-block-hooks-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit ab06c00

Please sign in to comment.