Skip to content

Commit

Permalink
Fix Code Standards for RC 2 release (WordPress#59774)
Browse files Browse the repository at this point in the history
Co-authored-by: getdave <get_dave@git.wordpress.org>
Co-authored-by: swissspidy <swissspidy@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: ockham <bernhard-reiter@git.wordpress.org>
  • Loading branch information
5 people authored and carstingaxion committed Mar 27, 2024
1 parent d5d0aa6 commit cc942f6
Showing 1 changed file with 41 additions and 24 deletions.
65 changes: 41 additions & 24 deletions packages/block-library/src/navigation/index.php
Expand Up @@ -543,7 +543,7 @@ private static function get_nav_wrapper_attributes( $attributes, $inner_blocks )
/**
* Gets the nav element directives.
*
* @param bool $is_interactive Whether the block is interactive.
* @param bool $is_interactive Whether the block is interactive.
* @return string the directives for the navigation element.
*/
private static function get_nav_element_directives( $is_interactive ) {
Expand Down Expand Up @@ -1457,17 +1457,25 @@ function block_core_navigation_set_ignored_hooked_blocks_metadata( $inner_blocks
/**
* Updates the post meta with the list of ignored hooked blocks when the navigation is created or updated via the REST API.
*
* @access private
* @since 6.5.0
*
* @param stdClass $post Post object.
* @return stdClass The updated post object.
*/
function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) {
// We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into
// all anchor blocks. For the root level, we create a mock Navigation and extract them from there.
/*
* We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into
* all anchor blocks. For the root level, we create a mock Navigation and extract them from there.
*/
$blocks = parse_blocks( $post->post_content );

// Block Hooks logic requires a `WP_Post` object (rather than the `stdClass` with the updates that
// we're getting from the `rest_pre_insert_wp_navigation` filter) as its second argument (to be
// used as context for hooked blocks insertion).
// We thus have to look it up from the DB,based on `$post->ID`.
/*
* Block Hooks logic requires a `WP_Post` object (rather than the `stdClass` with the updates that
* we're getting from the `rest_pre_insert_wp_navigation` filter) as its second argument (to be
* used as context for hooked blocks insertion).
* We thus have to look it up from the DB,based on `$post->ID`.
*/
$markup = block_core_navigation_set_ignored_hooked_blocks_metadata( $blocks, get_post( $post->ID ) );

$root_nav_block = parse_blocks( $markup )[0];
Expand All @@ -1488,30 +1496,35 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) {
return $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';
/*
* 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'; // phpcs:ignore Generic.Strings.UnnecessaryStringConcat.Found

// 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.
/*
* 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( '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', 10 );
add_filter( 'rest_pre_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta' );
}

// Previous versions of Gutenberg and WordPress 6.5 Betas 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.
/*
* 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, 10 );
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.
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response The response object.
*/
function block_core_navigation_insert_hooked_blocks_into_rest_response( $response, $post ) {
Expand All @@ -1530,13 +1543,17 @@ 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.
/*
* 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.
/*
* 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( '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 );
}

0 comments on commit cc942f6

Please sign in to comment.