Block Hooks: Pass correct context to filters.
The `$context` argument passed to filters such as `hooked_block_types`, `hooked_block`, and `hooked_block_{$hooked_block_type}` allows them to conditionally insert a hooked block. If the anchor block is contained in a template or template part, `$context` will be set to a `WP_Block_Template` object reflecting that template or part. The aforementioned filters are applied when hooked block insertion is run upon reading a template (or part) from the DB (and before sending the template/part content with hooked blocks inserted over the REST API to the client), but also upon writing to the DB, as that's when the `ignoredHookedBlocks` metadata attribute is set. Prior to this changeset, the `$context` passed to Block Hooks related filters in the latter case reflected the template/part that was already stored in the database (if any), which is a bug; instead, it needs to reflect the template/part that will result from the incoming `POST` network request that will trigger a database update. Those incoming changes are encapsulated in the `$changes` argument passed to the `reset_pre_insert_template` and `reset_pre_insert_template_part` filters, respectively, and thus to the `inject_ignored_hooked_blocks_metadata_attributes` function that is hooked to them. `$changes` is of type `stdClass` and only contains the fields that need to be updated. That means that in order to create a `WP_Block_Template` object, a two-step process is needed: - Emulate what the updated `wp_template` or `wp_template_part` post object in the database will look like by merging `$changes` on top of the existing `$post` object fetched from the DB, or from the theme's block template (part) file, if any. - Create a `WP_Block_Template` from the resulting object. To achieve the latter, a new helper method (`_build_block_template_object_from_post_object`) is extracted from the existing `_build_block_template_result_from_post` function. (The latter cannot be used directly as it includes a few database calls that will fail if no post object for the template has existed yet in the database.) While somewhat complicated to implement, the overall change allows for better separation of concerns and isolation of entities. This is visible e.g. in the fact that `inject_ignored_hooked_blocks_metadata_attributes` no longer requires a `$request` argument, which is reflected by unit tests no longer needing to create a `$request` object to pass to it, thus decoupling the function from the templates endpoint controller. Unit tests for `inject_ignored_hooked_blocks_metadata_attributes` have been moved to a new, separate file. Test coverage has been added such that now, all three relevant scenarios are covered: - The template doesn't exist in the DB, nor is there a block theme template file for it. - The template doesn't exist in the DB, but there is a block theme template file for it. - The template already exists in the DB. Those scenarios also correspond to the logical branching inside `WP_REST_Templates_Controller::prepare_item_for_database`, which is where `inject_ignored_hooked_blocks_metadata_attributes` gets its data from. Reviewed by gziolo. Merges [57919] to the to the 6.5 branch. Props tomjcafferkey, bernhard-reiter, gziolo, swissspidy. Fixes #60754. Built from https://develop.svn.wordpress.org/branches/6.5@58041 git-svn-id: http://core.svn.wordpress.org/branches/6.5@57507 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
b37dea1ca7
commit
5b64778886
|
@ -722,6 +722,64 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy,
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a block template object from a post object.
|
||||
*
|
||||
* This is a helper function that creates a block template object from a given post object.
|
||||
* It is self-sufficient in that it only uses information passed as arguments; it does not
|
||||
* query the database for additional information.
|
||||
*
|
||||
* @since 6.5.1
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Post $post Template post.
|
||||
* @param array $terms Additional terms to inform the template object.
|
||||
* @param array $meta Additional meta fields to inform the template object.
|
||||
* @return WP_Block_Template|WP_Error Template or error object.
|
||||
*/
|
||||
function _build_block_template_object_from_post_object( $post, $terms = array(), $meta = array() ) {
|
||||
if ( empty( $terms['wp_theme'] ) ) {
|
||||
return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
|
||||
}
|
||||
$theme = $terms['wp_theme'];
|
||||
|
||||
$default_template_types = get_default_block_template_types();
|
||||
|
||||
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
|
||||
$has_theme_file = get_stylesheet() === $theme && null !== $template_file;
|
||||
|
||||
$template = new WP_Block_Template();
|
||||
$template->wp_id = $post->ID;
|
||||
$template->id = $theme . '//' . $post->post_name;
|
||||
$template->theme = $theme;
|
||||
$template->content = $post->post_content;
|
||||
$template->slug = $post->post_name;
|
||||
$template->source = 'custom';
|
||||
$template->origin = ! empty( $meta['origin'] ) ? $meta['origin'] : null;
|
||||
$template->type = $post->post_type;
|
||||
$template->description = $post->post_excerpt;
|
||||
$template->title = $post->post_title;
|
||||
$template->status = $post->post_status;
|
||||
$template->has_theme_file = $has_theme_file;
|
||||
$template->is_custom = empty( $meta['is_wp_suggestion'] );
|
||||
$template->author = $post->post_author;
|
||||
$template->modified = $post->post_modified;
|
||||
|
||||
if ( 'wp_template' === $post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
|
||||
$template->post_types = $template_file['postTypes'];
|
||||
}
|
||||
|
||||
if ( 'wp_template' === $post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
|
||||
$template->is_custom = false;
|
||||
}
|
||||
|
||||
if ( 'wp_template_part' === $post->post_type && isset( $terms['wp_template_part_area'] ) ) {
|
||||
$template->area = $terms['wp_template_part_area'];
|
||||
}
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a unified template object based a post Object.
|
||||
*
|
||||
|
@ -734,13 +792,13 @@ function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy,
|
|||
* @return WP_Block_Template|WP_Error Template or error object.
|
||||
*/
|
||||
function _build_block_template_result_from_post( $post ) {
|
||||
$default_template_types = get_default_block_template_types();
|
||||
|
||||
$post_id = wp_is_post_revision( $post );
|
||||
if ( ! $post_id ) {
|
||||
$post_id = $post;
|
||||
}
|
||||
$parent_post = get_post( $post_id );
|
||||
$parent_post = get_post( $post_id );
|
||||
$post->post_name = $parent_post->post_name;
|
||||
$post->post_type = $parent_post->post_type;
|
||||
|
||||
$terms = get_the_terms( $parent_post, 'wp_theme' );
|
||||
|
||||
|
@ -752,45 +810,28 @@ function _build_block_template_result_from_post( $post ) {
|
|||
return new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
|
||||
}
|
||||
|
||||
$theme = $terms[0]->name;
|
||||
$template_file = _get_block_template_file( $post->post_type, $post->post_name );
|
||||
$has_theme_file = get_stylesheet() === $theme && null !== $template_file;
|
||||
|
||||
$origin = get_post_meta( $parent_post->ID, 'origin', true );
|
||||
$is_wp_suggestion = get_post_meta( $parent_post->ID, 'is_wp_suggestion', true );
|
||||
|
||||
$template = new WP_Block_Template();
|
||||
$template->wp_id = $post->ID;
|
||||
$template->id = $theme . '//' . $parent_post->post_name;
|
||||
$template->theme = $theme;
|
||||
$template->content = $post->post_content;
|
||||
$template->slug = $post->post_name;
|
||||
$template->source = 'custom';
|
||||
$template->origin = ! empty( $origin ) ? $origin : null;
|
||||
$template->type = $post->post_type;
|
||||
$template->description = $post->post_excerpt;
|
||||
$template->title = $post->post_title;
|
||||
$template->status = $post->post_status;
|
||||
$template->has_theme_file = $has_theme_file;
|
||||
$template->is_custom = empty( $is_wp_suggestion );
|
||||
$template->author = $post->post_author;
|
||||
$template->modified = $post->post_modified;
|
||||
|
||||
if ( 'wp_template' === $parent_post->post_type && $has_theme_file && isset( $template_file['postTypes'] ) ) {
|
||||
$template->post_types = $template_file['postTypes'];
|
||||
}
|
||||
|
||||
if ( 'wp_template' === $parent_post->post_type && isset( $default_template_types[ $template->slug ] ) ) {
|
||||
$template->is_custom = false;
|
||||
}
|
||||
$terms = array(
|
||||
'wp_theme' => $terms[0]->name,
|
||||
);
|
||||
|
||||
if ( 'wp_template_part' === $parent_post->post_type ) {
|
||||
$type_terms = get_the_terms( $parent_post, 'wp_template_part_area' );
|
||||
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
|
||||
$template->area = $type_terms[0]->name;
|
||||
$terms['wp_template_part_area'] = $type_terms[0]->name;
|
||||
}
|
||||
}
|
||||
|
||||
$meta = array(
|
||||
'origin' => get_post_meta( $parent_post->ID, 'origin', true ),
|
||||
'is_wp_suggestion' => get_post_meta( $parent_post->ID, 'is_wp_suggestion', true ),
|
||||
);
|
||||
|
||||
$template = _build_block_template_object_from_post_object( $post, $terms, $meta );
|
||||
|
||||
if ( is_wp_error( $template ) ) {
|
||||
return $template;
|
||||
}
|
||||
|
||||
// Check for a block template without a description and title or with a title equal to the slug.
|
||||
if ( 'wp_template' === $parent_post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
|
||||
$matches = array();
|
||||
|
@ -1442,36 +1483,70 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
|
|||
* @since 6.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param stdClass $post An object representing a template or template part
|
||||
* prepared for inserting or updating the database.
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return stdClass The updated object representing a template or template part.
|
||||
* @param stdClass $changes An object representing a template or template part
|
||||
* prepared for inserting or updating the database.
|
||||
* @param WP_REST_Request $deprecated Deprecated. Not used.
|
||||
* @return stdClass|WP_Error The updated object representing a template or template part.
|
||||
*/
|
||||
function inject_ignored_hooked_blocks_metadata_attributes( $post, $request ) {
|
||||
$filter_name = current_filter();
|
||||
if ( ! str_starts_with( $filter_name, 'rest_pre_insert_' ) ) {
|
||||
return $post;
|
||||
function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated = null ) {
|
||||
if ( null !== $deprecated ) {
|
||||
_deprecated_argument( __FUNCTION__, '6.5.1' );
|
||||
}
|
||||
$post_type = str_replace( 'rest_pre_insert_', '', $filter_name );
|
||||
|
||||
$hooked_blocks = get_hooked_blocks();
|
||||
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
|
||||
return $post;
|
||||
return $changes;
|
||||
}
|
||||
|
||||
// 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 = $request['id'] ? get_block_template( $request['id'], $post_type ) : null;
|
||||
remove_filter( 'hooked_block_types', '__return_empty_array', 99999 );
|
||||
$meta = isset( $changes->meta_input ) ? $changes->meta_input : array();
|
||||
$terms = isset( $changes->tax_input ) ? $changes->tax_input : array();
|
||||
|
||||
if ( empty( $changes->ID ) ) {
|
||||
// There's no post object for this template in the database for this template yet.
|
||||
$post = $changes;
|
||||
} else {
|
||||
// Find the existing post object.
|
||||
$post = get_post( $changes->ID );
|
||||
|
||||
// If the post is a revision, use the parent post's post_name and post_type.
|
||||
$post_id = wp_is_post_revision( $post );
|
||||
if ( $post_id ) {
|
||||
$parent_post = get_post( $post_id );
|
||||
$post->post_name = $parent_post->post_name;
|
||||
$post->post_type = $parent_post->post_type;
|
||||
}
|
||||
|
||||
// Apply the changes to the existing post object.
|
||||
$post = (object) array_merge( (array) $post, (array) $changes );
|
||||
|
||||
$type_terms = get_the_terms( $changes->ID, 'wp_theme' );
|
||||
$terms['wp_theme'] = ! is_wp_error( $type_terms ) && ! empty( $type_terms ) ? $type_terms[0]->name : null;
|
||||
}
|
||||
|
||||
// Required for the WP_Block_Template. Update the post object with the current time.
|
||||
$post->post_modified = current_time( 'mysql' );
|
||||
|
||||
// If the post_author is empty, set it to the current user.
|
||||
if ( empty( $post->post_author ) ) {
|
||||
$post->post_author = get_current_user_id();
|
||||
}
|
||||
|
||||
if ( 'wp_template_part' === $post->post_type && ! isset( $terms['wp_template_part_area'] ) ) {
|
||||
$area_terms = get_the_terms( $changes->ID, 'wp_template_part_area' );
|
||||
$terms['wp_template_part_area'] = ! is_wp_error( $area_terms ) && ! empty( $area_terms ) ? $area_terms[0]->name : null;
|
||||
}
|
||||
|
||||
$template = _build_block_template_object_from_post_object( new WP_Post( $post ), $terms, $meta );
|
||||
|
||||
if ( is_wp_error( $template ) ) {
|
||||
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( $post->post_content );
|
||||
$content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
|
||||
$blocks = parse_blocks( $changes->post_content );
|
||||
$changes->post_content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
|
||||
|
||||
$post->post_content = $content;
|
||||
return $post;
|
||||
return $changes;
|
||||
}
|
||||
|
|
|
@ -753,7 +753,7 @@ 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_filter( 'rest_pre_insert_wp_template', 'inject_ignored_hooked_blocks_metadata_attributes' );
|
||||
add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' );
|
||||
|
||||
unset( $filter, $action );
|
||||
|
|
|
@ -532,7 +532,7 @@ class WP_REST_Templates_Controller extends WP_REST_Controller {
|
|||
* @since 5.8.0
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return stdClass Changes to pass to wp_update_post.
|
||||
* @return stdClass|WP_Error Changes to pass to wp_update_post.
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
$template = $request['id'] ? get_block_template( $request['id'], $this->post_type ) : null;
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.5.3-alpha-58038';
|
||||
$wp_version = '6.5.3-alpha-58041';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue