From 325c994a027fb829df6aeb7a57831480151a5086 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Fri, 31 May 2024 09:45:32 +0200 Subject: [PATCH] Navigation block: Check for insert_hooked_blocks_into_rest_response in Core (#62134) Adding another `has_filter` check before adding the Navigation block's `block_core_navigation_insert_hooked_blocks_into_rest_response` filter to insert hooked blocks into the REST API response for a `wp_navigation` post type. --- packages/block-library/src/navigation/index.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index ba36acf139a626..940277d47a2af9 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1674,9 +1674,15 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons $rest_prepare_wp_navigation_core_callback = 'block_core_navigation_' . 'insert_hooked_blocks_into_rest_response'; /* - * Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.5 - * that are not present in Gutenberg's WP 6.5 compatibility layer. + * 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 ( function_exists( 'set_ignored_hooked_blocks_metadata' ) && ! has_filter( 'rest_prepare_wp_navigation', $rest_prepare_wp_navigation_core_callback ) ) { +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 ); }