Skip to content

Commit

Permalink
Block Hooks: Return early from saving meta data for the navigation wi…
Browse files Browse the repository at this point in the history
…thout 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>
  • Loading branch information
3 people authored and getdave committed Mar 18, 2024
1 parent 62e24b6 commit 37a8390
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,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.
Expand Down
32 changes: 31 additions & 1 deletion phpunit/blocks/block-navigation-block-hooks-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 = '<!-- wp:navigation-link {"label":"News","type":"page","id":2,"url":"http://localhost:8888/?page_id=2","kind":"post-type"} /-->';
$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.'
);
}
}

0 comments on commit 37a8390

Please sign in to comment.