Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Allow multiple navigations with the same ref #47453

Merged
merged 5 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,25 @@ function( $block ) {
return array_values( $filtered );
}

/**
* Returns true if the navigation block contains a nested navigation block.
*
* @param array $parsed_blocks the parsed blocks to be normalized.
* @return bool true if the navigation block contains a nested navigation block.
*/
function block_core_navigation_block_contains_core_navigation( $parsed_blocks ) {
foreach ( $parsed_blocks as $block ) {
if ( 'core/navigation' === $block['blockName'] ) {
return true;
}
if ( block_core_navigation_block_contains_core_navigation( $block['innerBlocks'] ) ) {
return true;
}
}

return false;
}

/**
* Retrieves the appropriate fallback to be used on the front of the
* site when there is no menu assigned to the Nav block.
Expand Down Expand Up @@ -443,7 +462,8 @@ function block_core_navigation_get_fallback_blocks() {

// Use the first non-empty Navigation as fallback if available.
if ( $navigation_post ) {
$maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) );
$parsed_blocks = parse_blocks( $navigation_post->post_content );
$maybe_fallback = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );

// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
// In this case default to the (Page List) fallback.
Expand Down Expand Up @@ -511,7 +531,6 @@ function block_core_navigation_from_block_get_post_ids( $block ) {
function render_block_core_navigation( $attributes, $content, $block ) {

static $seen_menu_names = array();
static $seen_ref = array();

// Flag used to indicate whether the rendered output is considered to be
// a fallback (i.e. the block has no menu associated with it).
Expand Down Expand Up @@ -582,11 +601,6 @@ function render_block_core_navigation( $attributes, $content, $block ) {

// Load inner blocks from the navigation post.
if ( array_key_exists( 'ref', $attributes ) ) {
if ( in_array( $attributes['ref'], $seen_ref, true ) ) {
return '';
}
$seen_ref[] = $attributes['ref'];

$navigation_post = get_post( $attributes['ref'] );
if ( ! isset( $navigation_post ) ) {
return '';
Expand Down Expand Up @@ -629,6 +643,11 @@ function render_block_core_navigation( $attributes, $content, $block ) {
$inner_blocks = new WP_Block_List( $fallback_blocks, $attributes );
}

$parsed_blocks = parse_blocks( $navigation_post->post_content );
if ( block_core_navigation_block_contains_core_navigation( $parsed_blocks ) ) {
return '';
}

/**
* Filter navigation block $inner_blocks.
* Allows modification of a navigation block menu items.
Expand Down
17 changes: 17 additions & 0 deletions phpunit/blocks/render-block-navigation-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,22 @@ public function test_block_core_navigation_get_post_ids_from_block_with_submenu(
$this->assertSameSetsWithIndex( array( 755, 789 ), $post_ids );
}

/**
* @covers :: gutengberg_block_core_navigation_block_contains_core_navigation
*/
public function test_gutenberg_block_core_navigation_block_contains_core_navigation() {
$parsed_blocks = parse_blocks( '<!-- wp:navigation /-->' );
$this->assertTrue( gutenberg_block_core_navigation_block_contains_core_navigation( $parsed_blocks ) );
}

public function test_gutenberg_block_core_navigation_block_contains_core_navigation_deep() {
$parsed_blocks = parse_blocks( '<!-- wp:group --><!-- /wp:group --><!-- wp:group --><!-- wp:group --><!-- wp:navigation /--><!-- /wp:group --><!-- /wp:group -->' );
$this->assertTrue( gutenberg_block_core_navigation_block_contains_core_navigation( $parsed_blocks ) );
}

public function test_gutenberg_block_core_navigation_block_contains_core_navigation_no_navigation() {
$parsed_blocks = parse_blocks( '<!-- wp:group --><!-- wp:group --><!-- /wp:group --><!-- /wp:group -->' );
$this->assertFalse( gutenberg_block_core_navigation_block_contains_core_navigation( $parsed_blocks ) );
}

}