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 API: Add ignoredHookedBlocks meta when preparing REST API response #6330

Draft
wants to merge 7 commits into
base: trunk
Choose a base branch
from
29 changes: 28 additions & 1 deletion src/wp-includes/block-template-utils.php
Expand Up @@ -1449,7 +1449,7 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
* @param WP_REST_Request $request Request object.
* @return stdClass The updated object representing a template or template part.
*/
function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) {
function inject_ignored_hooked_blocks_metadata_attributes_for_database( $post, $request ) {
$filter_name = current_filter();
if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) {
return $post;
Expand Down Expand Up @@ -1477,3 +1477,30 @@ function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) {
$post->post_content = $content;
return $post;
}

/**
* Inject ignoredHookedBlocks metadata attributes into a template or template part before we return
* it in the REST API response.
*
* @since 6.6.0
* @access private
*
* @param WP_REST_Response $response The response object.
* @param WP_Block_Template $template An object representing a template or template part
*
* @return WP_Block_Template The updated object representing a template or template part.
*/
function inject_ignored_hooked_blocks_metadata_attributes_for_response( $response, $template ) {
$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return $template;
}

$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'set_ignored_hooked_blocks_metadata' );

$blocks = parse_blocks( $template->content );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );

return $template;
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 7 additions & 3 deletions src/wp-includes/default-filters.php
Expand Up @@ -752,8 +752,12 @@
add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );
add_action( 'init', '_wp_register_default_font_collections' );

// Add ignoredHookedBlocks metadata attribute to the template and template part post types.
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 2 );
// Add ignoredHookedBlocks metadata attribute to the template and template part post types before inserting it into the database.
add_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes_for_database' );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes_for_database' );

// Add ignoredHookedBlocks metadata attribute to the template and template part post types before returning them in the response.
add_filter( 'rest_prepare_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes_for_response', 10, 2 );
add_filter( 'rest_prepare_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes_for_response', 10, 2 );

unset( $filter, $action );
Expand Up @@ -754,7 +754,23 @@ public function prepare_item_for_response( $item, $request ) {
}
}

return $response;
/**
* Filters the 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 );
}

/**
Expand Down