From 6f8a7ddc5b168f68625335c732c49c1bd55de657 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 8 Feb 2024 17:30:46 +0000 Subject: [PATCH] Navigation block: Check Block Hooks API callback hasn't already been added. (#58772) Check for the presence of a filter callback hooked in Core ([here](https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/blocks/navigation.php#L1423-L1425) and [here](https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/blocks/navigation.php#L1455-L1457)) before adding ours in Gutenberg. We have to concatenate Core's callback name to prevent the Gutenberg build step from prefixing it with `gutenberg_` which is not what it's called in Core. Co-authored-by: tjcafferkey Co-authored-by: ockham Co-authored-by: gziolo --- packages/block-library/src/navigation/index.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 7ef03cefefc169..db24143ac74044 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1440,9 +1440,14 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { } } +// Before adding our filter, we verify if it's already added in Core. +// However, during the build process, Gutenberg automatically prefixes our functions with "gutenberg_". +// Therefore, we concatenate the Core's function name to circumvent this prefix for our check. +$rest_insert_wp_navigation_core_callback = 'block_core_navigation_' . 'update_ignore_hooked_blocks_meta'; + // 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. -if ( function_exists( 'get_hooked_block_markup' ) ) { +if ( function_exists( 'get_hooked_block_markup' ) && ! has_filter( 'rest_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ) ) { add_action( 'rest_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta', 10, 3 ); } @@ -1472,8 +1477,13 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons return $response; } +// Before adding our filter, we verify if it's already added in Core. +// However, during the build process, Gutenberg automatically prefixes our functions with "gutenberg_". +// Therefore, we concatenate the Core's function name to circumvent this prefix for our check. +$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. -if ( function_exists( 'get_hooked_block_markup' ) ) { +if ( function_exists( 'get_hooked_block_markup' ) && ! 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 ); }