From b2e6c452f47f4ea940581b45867f0a9c0dfe7dd8 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Mon, 22 Nov 2021 16:22:38 +0000 Subject: [PATCH] Use non-empty Nav post as fallback --- .../block-library/src/navigation/index.php | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 6db226fe960fef..f096dd9763fde8 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -221,12 +221,38 @@ function( $block ) { $is_fallback = true; // indicate we are rendering the fallback. $attributes['__unstableMaxPages'] = 4; // set value to be passed as context to Page List block. - $page_list_block = array( - 'blockName' => 'core/page-list', - 'attrs' => array(), + $navigation_posts = get_posts( + array( + 'post_type' => 'wp_navigation', + ) ); - $inner_blocks = new WP_Block_List( array( $page_list_block ), $attributes ); + $navigation_post = null; + + // Pick first non-empty Navigation. + foreach ( $navigation_posts as $navigation_maybe ) { + if ( ! empty( $navigation_maybe->post_content ) ) { + $navigation_post = $navigation_maybe; + break; + } + } + + // Use non-empty Navigation if available. + if ( $navigation_post ) { + $fallback_blocks = parse_blocks( $navigation_post->post_content ); + } else { + $attributes['__unstableMaxPages'] = 4; // set value to be passed as context to Page List block. + + // Requires wrapping array. + $fallback_blocks = array( + array( + 'blockName' => 'core/page-list', + 'attrs' => array(), + ), + ); + } + + $inner_blocks = new WP_Block_List( $fallback_blocks, $attributes ); } // Restore legacy classnames for submenu positioning.