From 4e53565e2dc9d8366286aafec032826ad122ebee Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Fri, 15 Mar 2024 10:43:35 +0000 Subject: [PATCH] Block Hooks: Return early from saving meta data for the navigation without a $post->ID (#59875) Co-authored-by: Bernie Reiter <96308+ockham@users.noreply.github.com> Co-authored-by: Hugo Drelon <69580439+Hug0-Drelon@users.noreply.github.com> --- .../block-library/src/navigation/index.php | 8 +++++ .../block-navigation-block-hooks-test.php | 32 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 1a8b523e9328b..1f623f4a158c5 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.' + ); + } }