Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Hooks: Return early from saving meta data for the navigation without a $post->ID #59875

Merged
merged 11 commits into from
Mar 15, 2024
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 @@ -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.
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.'
);
}
}
Loading