-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 API: Add ignoredHookedBlocks meta when preparing REST API response #6330
base: trunk
Are you sure you want to change the base?
Block Hooks API: Add ignoredHookedBlocks meta when preparing REST API response #6330
Conversation
…ribute for hooked blocks
/** | ||
* Filters the post data for a REST API response. | ||
* | ||
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug. | ||
* | ||
* Possible hook names include: | ||
* | ||
* - `rest_prepare_wp_template` | ||
* - `rest_prepare_wp_template_part` | ||
* | ||
* @since 6.6.0 | ||
* | ||
* @param WP_REST_Response $response The response object. | ||
* @param WP_Block_Template $template Template object. | ||
* @param WP_REST_Request $request Request object. | ||
*/ | ||
return apply_filters( "rest_prepare_{$this->post_type}", $response, $template, $request ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In our other filter here instead of documenting it we referenced the WP Posts controller since it is an identical filter.
In this case we can't do that since the second param is different and returns WP_Block_Template
instead of WP_Post
.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for taking this on! This is shaping up nicely 😄
I was originally thinking to tackle this at a different level, by basically composing a new callback from set_ignored_hooked_blocks_metadata
and insert_hooked_blocks
that we'd then pass as third argument to instances of make_{before|after}_block_visitor
where we'd previously passed insert_hooked_blocks
(or no third argument at all, since it's the default).
function set_ignored_hooked_blocks_metadata_and_insert_hooked_blocks ( &$parsed_anchor_block, $relative_position, $hooked_blocks, $context ) {
$markup = insert_hooked_blocks( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
set_ignored_hooked_blocks_metadata( $parsed_anchor_block, $relative_position, $hooked_blocks, $context );
return $markup;
}
/ * ... */
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata_and_insert_hooked_blocks' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata_and_insert_hooked_blocks' );
}
$blocks = parse_blocks( $template->content );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
The downside of my approach is that it'll inject the ignoredHookedBlocks
attribute not only into the REST API response, but also into the frontend-rendered markup, where it's not needed (although it's also not harmful). In this regard, your approach arguably better preserves separation of concerns. Its downside is that it's going to parse and re-serialize block markup twice for REST API responses: Once at the low level for hooked block insertion, and once at the newly introduced REST API response hook for ignoredHookedBlocks
attribute injection. Repeated parsing and re-serialization of block markup is something we've tried to avoid on the frontend, as it did impact performance measurably; it's probably a bit less critical for the REST API.
So I think we basically need to pick which trade-off we're willing to make 😄
The block hooks toggle relies on the
ignoredHookedBlocks
when determining whether to show the toggle in the Site Editor UI. The current problem is that for uncustomized templates we don't add this meta data so to fix this problem we need to run the block hooks logic with theset_ignored_hooked_blocks_metadata
callback when preparing the template for the response.TODO:
prepare_item_for_response()
methodTrac ticket: https://core.trac.wordpress.org/ticket/59574
Testing instructions:
functions.php
file to add a hooked block.This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.