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 Templates Controller filter to avoid triggering wp_update_post #6225

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1432,39 +1432,68 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
$template_hierarchy[] = 'index';
return $template_hierarchy;
}

/**
* Inject ignoredHookedBlocks metadata attributes into a template or template part.
*
* Given a `wp_template` or `wp_template_part` post object, locate all blocks that have
* hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor
* blocks to reflect the latter.
*
* @param WP_Post $post A post object with post type set to `wp_template` or `wp_template_part`.
* @return WP_Post The updated post object.
* @param stdClass $post A post object with post type set to `wp_template` or `wp_template_part`.
* @param WP_REST_Request $request Request object.
* @param string $post_type The post type of the post object.
* @return stdClass The updated post object.
*/
function inject_ignored_hooked_blocks_metadata_attributes( $post ) {
function inject_ignored_hooked_blocks_metadata_attributes( $post, $request, $post_type ) {
$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return;
return $post;
}

// At this point, the post has already been created.
// We need to build the corresponding `WP_Block_Template` object as context argument for the visitor.
// To that end, we need to suppress hooked blocks from getting inserted into the template.
add_filter( 'hooked_block_types', '__return_empty_array', 99999, 0 );
$template = _build_block_template_result_from_post( $post );
$template = $request['id'] ? get_block_template( $request['id'], $post_type ) : null;
gziolo marked this conversation as resolved.
Show resolved Hide resolved
remove_filter( 'hooked_block_types', '__return_empty_array', 99999 );

$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 );
$blocks = parse_blocks( $post->post_content );
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );

wp_update_post(
array(
'ID' => $post->ID,
'post_content' => $content,
)
);
$post->post_content = $content;
return $post;
}

/**
* Inject ignoredHookedBlocks metadata attributes into a wp_template_part.
*
* Given a `wp_template` or `wp_template_part` post object, locate all blocks that have
* hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor
* blocks to reflect the latter.
*
* @param stdClass $post A post object with post type set to `wp_template` or `wp_template_part`.
* @param WP_REST_Request $request Request object.
* @return stdClass The updated post object.
*/
function inject_ignored_hooked_blocks_metadata_attributes_into_template_part( $post, $request ) {
return inject_ignored_hooked_blocks_metadata_attributes( $post, $request, 'wp_template_part' );
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Inject ignoredHookedBlocks metadata attributes into a wp_template.
*
* Given a `wp_template` or `wp_template_part` post object, locate all blocks that have
* hooked blocks, and inject a `metadata.ignoredHookedBlocks` attribute into the anchor
* blocks to reflect the latter.
*
* @param stdClass $post A post object with post type set to `wp_template` or `wp_template_part`.
* @param WP_REST_Request $request Request object.
* @return stdClass The updated post object.
*/
function inject_ignored_hooked_blocks_metadata_attributes_into_template( $post, $request ) {
return inject_ignored_hooked_blocks_metadata_attributes( $post, $request, 'wp_template' );
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 3 additions & 4 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,8 @@
add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );
add_action( 'init', '_wp_register_default_font_collections' );

// It might be nice to use a filter instead of an action, but the `WP_REST_Templates_Controller` doesn't
// provide one (unlike e.g. `WP_REST_Posts_Controller`, which has `rest_pre_insert_{$this->post_type}`).
add_action( 'rest_after_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 );
add_action( 'rest_after_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes', 10, 3 );
// 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_into_template', 10, 2 );
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes_into_template_part', 10, 2 );

unset( $filter, $action );
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,23 @@ protected function prepare_item_for_database( $request ) {
$changes->post_author = $post_author;
}

return $changes;
/**
* Filters a post before it is inserted via the REST API.
*
* The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug.
*
* Possible hook names include:
*
* - `rest_pre_insert_wp_template`
* - `rest_pre_insert_wp_template_part`
*
* @since 6.5.0
*
* @param stdClass $changes An object representing a single post prepared
* for inserting or updating the database.
* @param WP_REST_Request $request Request object.
*/
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
return apply_filters( "rest_pre_insert_{$this->post_type}", $changes, $request );
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Loading