inject_ignored_hooked_blocks_metadata_attributes()
云策文档标注
概述
inject_ignored_hooked_blocks_metadata_attributes() 函数用于向模板或模板部件注入 ignoredHookedBlocks 元数据属性。它处理 wp_template 或 wp_template_part 对象,定位所有具有 hooked blocks 的块,并在锚块中注入 metadata.ignoredHookedBlocks 属性。
关键要点
- 函数接受一个表示模板或模板部件的对象作为参数,用于数据库插入或更新。
- 通过 get_hooked_blocks() 获取 hooked blocks,并应用 apply_block_hooks_to_content() 处理内容。
- 对于 wp_template_part 类型,会处理序列化块和元数据存储,包括 _wp_ignored_hooked_blocks 元字段。
- 函数返回更新后的对象或 WP_Error,并处理了废弃参数和错误检查。
代码示例
function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated = null ) {
if ( null !== $deprecated ) {
_deprecated_argument( __FUNCTION__, '6.5.3' );
}
if ( ! isset( $changes->post_content ) ) {
return $changes;
}
$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return $changes;
}
// ... 更多代码处理逻辑
}注意事项
- 参数 $deprecated 已废弃,从版本 6.5.3 起不建议使用。
- 函数内部调用了多个 WordPress 核心函数,如 get_hooked_blocks()、apply_block_hooks_to_content() 等,需确保这些函数可用。
- 对于 wp_template_part,会处理序列化块和元数据,开发者需注意相关数据结构和存储方式。
原文内容
Inject ignoredHookedBlocks metadata attributes into a template or template part.
Description
Given an object that represents a wp_template or wp_template_part post object prepared for inserting or updating the database, locate all blocks that have hooked blocks, and inject a metadata.ignoredHookedBlocks attribute into the anchor blocks to reflect the latter.
Parameters
$changesstdClassrequired-
An object representing a template or template part prepared for inserting or updating the database.
$deprecatedWP_REST_Requestoptional-
Deprecated. Not used.
Default:
null
Source
function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated = null ) {
if ( null !== $deprecated ) {
_deprecated_argument( __FUNCTION__, '6.5.3' );
}
if ( ! isset( $changes->post_content ) ) {
return $changes;
}
$hooked_blocks = get_hooked_blocks();
if ( empty( $hooked_blocks ) && ! has_filter( 'hooked_block_types' ) ) {
return $changes;
}
$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;
}
if ( 'wp_template_part' === $post->post_type ) {
$attributes = array();
$existing_ignored_hooked_blocks = isset( $post->ID ) ? get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ) : '';
if ( ! empty( $existing_ignored_hooked_blocks ) ) {
$attributes['metadata'] = array(
'ignoredHookedBlocks' => json_decode( $existing_ignored_hooked_blocks, true ),
);
}
$content = get_comment_delimited_block_content(
'core/template-part',
$attributes,
$changes->post_content
);
$content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
$changes->post_content = remove_serialized_parent_block( $content );
$wrapper_block_markup = extract_serialized_parent_block( $content );
$wrapper_block = parse_blocks( $wrapper_block_markup )[0];
$ignored_hooked_blocks = $wrapper_block['attrs']['metadata']['ignoredHookedBlocks'] ?? array();
if ( ! empty( $ignored_hooked_blocks ) ) {
if ( ! isset( $changes->meta_input ) ) {
$changes->meta_input = array();
}
$changes->meta_input['_wp_ignored_hooked_blocks'] = wp_json_encode( $ignored_hooked_blocks );
}
} else {
$changes->post_content = apply_block_hooks_to_content( $changes->post_content, $template, 'set_ignored_hooked_blocks_metadata' );
}
return $changes;
}
Changelog
| Version | Description |
|---|---|
| 6.5.0 | Introduced. |