Skip to content

Commit

Permalink
Navigation Block: Fix warning error when no ref (#47479)
Browse files Browse the repository at this point in the history
* Navigation Block: Fix warning error when no ref

* Nest checks using inner_blocks, independent of the wp_navigation post data

* FIx unit tests

* Add extra safety to block_core_navigation_block_contains_core_navigation
  • Loading branch information
t-hamano authored Jan 31, 2023
1 parent c73da69 commit e6ccc25
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
13 changes: 6 additions & 7 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ function( $block ) {
/**
* Returns true if the navigation block contains a nested navigation block.
*
* @param array $parsed_blocks the parsed blocks to be normalized.
* @param WP_Block_List $inner_blocks Inner block instance 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'] ) {
function block_core_navigation_block_contains_core_navigation( $inner_blocks ) {
foreach ( $inner_blocks as $block ) {
if ( 'core/navigation' === $block->name ) {
return true;
}
if ( block_core_navigation_block_contains_core_navigation( $block['innerBlocks'] ) ) {
if ( $block->inner_blocks && block_core_navigation_block_contains_core_navigation( $block->inner_blocks ) ) {
return true;
}
}
Expand Down Expand Up @@ -643,8 +643,7 @@ 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 ) ) {
if ( block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) {
return '';
}

Expand Down
9 changes: 6 additions & 3 deletions phpunit/blocks/render-block-navigation-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@ public function test_block_core_navigation_get_post_ids_from_block_with_submenu(
*/
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 ) );
$inner_blocks = new WP_Block_List( $parsed_blocks );
$this->assertTrue( gutenberg_block_core_navigation_block_contains_core_navigation( $inner_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 ) );
$inner_blocks = new WP_Block_List( $parsed_blocks );
$this->assertTrue( gutenberg_block_core_navigation_block_contains_core_navigation( $inner_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 ) );
$inner_blocks = new WP_Block_List( $parsed_blocks );
$this->assertFalse( gutenberg_block_core_navigation_block_contains_core_navigation( $inner_blocks ) );
}

}

1 comment on commit e6ccc25

@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 e6ccc25.
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/4054049283
📝 Reported issues:

Please sign in to comment.