Skip to content

Commit

Permalink
Check the filter callback hasn't already been added before in Core be…
Browse files Browse the repository at this point in the history
…fore hooking into it in Gutenberg
  • Loading branch information
tjcafferkey committed Feb 7, 2024
1 parent 30b4349 commit 0c095e8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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 );
}

0 comments on commit 0c095e8

Please sign in to comment.