Skip to content

Commit

Permalink
Navigation: Allow multiple navigations with the same ref (#47453)
Browse files Browse the repository at this point in the history
* skip rendering nested navigation blocks

* move the check

* Check for nested navigation blocks at multiple levels and add unit tests. Co-authored-by: Héctor <27339341+priethor@users.noreply.github.com>

* fix linting issues

* fix linting

Co-authored-by: Andrei Draganescu <andrei.draganescu@automattic.com>
Co-authored-by: Ben Dwyer <ben@escruffian.com>
  • Loading branch information
3 people committed Jan 26, 2023
1 parent 360e5a4 commit fd1247c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
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 ) );
}

}

1 comment on commit fd1247c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in fd1247c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4016329825
📝 Reported issues:

Please sign in to comment.