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 ( ! isset( $post->ID ) ) {
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
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
21 changes: 21 additions & 0 deletions phpunit/blocks/block-navigation-block-hooks-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,25 @@ 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_rest_creation() {
wp_set_current_user( 1 );

$post_type_object = get_post_type_object( 'wp_navigation' );
$request = new WP_REST_Request( 'POST', '/wp/v2/' . $post_type_object->rest_base );
$request->set_body_params(
array(
'title' => 'Title ' . $post_type_object->label,
'content' => $post_type_object->label,
'_locale' => 'user',
)
);
$response = rest_get_server()->dispatch( $request );

$this->assertNotEmpty( $response->get_status() );
$this->assertSame( 201, $response->get_status() );
}
}
Loading