Skip to content

Commit

Permalink
skip rendering nested navigation blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Jan 20, 2023
1 parent 77c6c5d commit 936b88d
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 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,24 @@ function( $block ) {
return array_values( $filtered );
}

/**
* Bail on rendering nested navigation blocks
*
* @param array $parsed_blocks the parsed blocks to be normalized.
* @return array the normalized parsed blocks.
*/
function block_core_navigation_has_nested_core_navigation( $parsed_blocks ) {
$filtered = array_filter(
$parsed_blocks,
function( $block ) {
return $block['blockName'] === 'core/navigation';
}
);

// Reset keys.
return count( $filtered ) > 0;
}

/**
* 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,13 +461,18 @@ 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.
$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
}

if ( block_core_navigation_has_nested_core_navigation( $parsed_blocks ) ) {
return [];
}

/**
* Filters the fallback experience for the Navigation block.
*
Expand Down Expand Up @@ -511,7 +534,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 +604,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

0 comments on commit 936b88d

Please sign in to comment.