diff --git a/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php b/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php index 52ec4f508246ac..b2b40229b3afcb 100644 --- a/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php +++ b/lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php @@ -185,7 +185,7 @@ private static function get_inner_blocks_html( $attributes, $inner_blocks ) { private static function get_inner_blocks_from_navigation_post( $attributes ) { $navigation_post = get_post( $attributes['ref'] ); if ( ! isset( $navigation_post ) ) { - return ''; + return new WP_Block_List( array(), $attributes ); } // Only published posts are valid. If this is changed then a corresponding change @@ -214,7 +214,7 @@ private static function get_inner_blocks_from_fallback( $attributes ) { // Fallback my have been filtered so do basic test for validity. if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) { - return ''; + return new WP_Block_List( array(), $attributes ); } return new WP_Block_List( $fallback_blocks, $attributes ); diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 6550d896656b1b..6fb55efe9f8b93 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -75,7 +75,7 @@ function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) { function block_core_navigation_get_inner_blocks_from_unstable_location( $attributes ) { $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ); if ( empty( $menu_items ) ) { - return ''; + return new WP_Block_List( array(), $attributes ); } $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items ); diff --git a/phpunit/class-wp-navigation-block-renderer-test.php b/phpunit/class-wp-navigation-block-renderer-test.php index 73f3c89d798838..124e0fe91bd1e6 100644 --- a/phpunit/class-wp-navigation-block-renderer-test.php +++ b/phpunit/class-wp-navigation-block-renderer-test.php @@ -62,4 +62,23 @@ public function test_gutenberg_get_markup_for_inner_block_site_title() { $expected = '
  • Test Blog

  • '; $this->assertEquals( $expected, $result ); } + + /** + * Test that the `get_inner_blocks_from_navigation_post` method returns an empty block list for a non-existent post. + * + * @group navigation-renderer + * + * @covers WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_post + */ + public function test_gutenberg_get_inner_blocks_from_navigation_post_returns_empty_block_list() { + $reflection = new ReflectionClass( 'WP_Navigation_Block_Renderer' ); + $method = $reflection->getMethod( 'get_inner_blocks_from_navigation_post' ); + $method->setAccessible( true ); + $attributes = array( 'ref' => 0 ); + + $actual = $method->invoke( $reflection, $attributes ); + $expected = new WP_Block_List( array(), $attributes ); + $this->assertEquals( $actual, $expected ); + $this->assertCount( 0, $actual ); + } }