From 936b88d102705f3718fe91d4c75b6c63d0ca76ba Mon Sep 17 00:00:00 2001 From: Andrei Draganescu Date: Fri, 20 Jan 2023 15:53:56 +0800 Subject: [PATCH] skip rendering nested navigation blocks --- .../block-library/src/navigation/index.php | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 971a5f500e774..1d430b1af84c3 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -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. @@ -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. * @@ -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). @@ -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 '';