diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index ca146868f4bbd..568de8fb0edf4 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1512,6 +1512,14 @@ function block_core_navigation_set_ignored_hooked_blocks_metadata( $inner_blocks * @return stdClass The updated post object. */ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { + /* + * In this scenario the user has likely tried to create a navigation via the REST API. + * In which case we won't have a post ID to work with and store meta against. + */ + if ( empty( $post->ID ) ) { + return $post; + } + /* * We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into * all anchor blocks. For the root level, we create a mock Navigation and extract them from there. diff --git a/phpunit/blocks/block-navigation-block-hooks-test.php b/phpunit/blocks/block-navigation-block-hooks-test.php index e0e7bd45cfc17..ba30c2e26d25c 100644 --- a/phpunit/blocks/block-navigation-block-hooks-test.php +++ b/phpunit/blocks/block-navigation-block-hooks-test.php @@ -59,7 +59,7 @@ public function tear_down() { */ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_preserves_entities() { if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { - $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionalit.' ); + $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); } register_block_type( @@ -92,4 +92,34 @@ public function test_block_core_navigation_update_ignore_hooked_blocks_meta_pres 'Block was not added to ignored hooked blocks metadata.' ); } + + /** + * @covers ::gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta + */ + public function test_block_core_navigation_dont_modify_no_post_id() { + if ( ! function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { + $this->markTestSkipped( 'Test skipped on WordPress versions that do not included required Block Hooks functionality.' ); + } + + register_block_type( + 'tests/my-block', + array( + 'block_hooks' => array( + 'core/navigation' => 'last_child', + ), + ) + ); + + $original_markup = ''; + $post = new stdClass(); + $post->post_content = $original_markup; + + $post = gutenberg_block_core_navigation_update_ignore_hooked_blocks_meta( $post ); + + $this->assertSame( + $original_markup, + $post->post_content, + 'Post content did not match the original markup.' + ); + } }