update_ignored_hooked_blocks_postmeta()
云策文档标注
概述
update_ignored_hooked_blocks_postmeta() 函数用于更新 wp_postmeta 中存储的忽略钩子块列表,这些块的内块作为文章内容存储。它处理文章对象,在特定条件下跳过元数据生成,并整合现有忽略块数据。
关键要点
- 函数接受一个 $post 参数(stdClass 类型),返回更新后的文章对象。
- 在以下情况跳过处理:文章 ID 为空、未设置 post_content 或 post_type。
- 根据 post_type 确定包装块类型(如 wp_navigation 对应 core/navigation),生成块标记。
- 通过 apply_block_hooks_to_content() 应用块钩子算法,解析根块以获取 ignoredHookedBlocks。
- 合并现有忽略块数据,更新 _wp_ignored_hooked_blocks 元数据,并调整 post_content。
代码示例
function update_ignored_hooked_blocks_postmeta( $post ) {
if ( empty( $post->ID ) ) {
return $post;
}
if ( ! isset( $post->post_content ) ) {
return $post;
}
if ( ! isset( $post->post_type ) ) {
return $post;
}
// ... 后续处理逻辑
}注意事项
- 函数依赖于多个辅助函数,如 get_post_meta()、apply_block_hooks_to_content() 和 parse_blocks()。
- 从 WordPress 6.6.0 版本引入,6.8.0 版本扩展支持非 wp_navigation 文章类型。
原文内容
Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
Parameters
$poststdClassrequired-
Post object.
Source
function update_ignored_hooked_blocks_postmeta( $post ) {
/*
* In this scenario the user has likely tried to create a new post object via the REST API.
* In which case we won't have a post ID to work with and store meta against.
*/
if ( empty( $post->ID ) ) {
return $post;
}
/*
* Skip meta generation when consumers intentionally update specific fields
* and omit the content update.
*/
if ( ! isset( $post->post_content ) ) {
return $post;
}
/*
* Skip meta generation if post type is not set.
*/
if ( ! isset( $post->post_type ) ) {
return $post;
}
$attributes = array();
$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
if ( ! empty( $ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
$attributes['metadata'] = array(
'ignoredHookedBlocks' => $ignored_hooked_blocks,
);
}
if ( 'wp_navigation' === $post->post_type ) {
$wrapper_block_type = 'core/navigation';
} elseif ( 'wp_block' === $post->post_type ) {
$wrapper_block_type = 'core/block';
} else {
$wrapper_block_type = 'core/post-content';
}
$markup = get_comment_delimited_block_content(
$wrapper_block_type,
$attributes,
$post->post_content
);
$existing_post = get_post( $post->ID );
// Merge the existing post object with the updated post object to pass to the block hooks algorithm for context.
$context = (object) array_merge( (array) $existing_post, (array) $post );
$context = new WP_Post( $context ); // Convert to WP_Post object.
$serialized_block = apply_block_hooks_to_content( $markup, $context, 'set_ignored_hooked_blocks_metadata' );
$root_block = parse_blocks( $serialized_block )[0];
$ignored_hooked_blocks = isset( $root_block['attrs']['metadata']['ignoredHookedBlocks'] )
? $root_block['attrs']['metadata']['ignoredHookedBlocks']
: array();
if ( ! empty( $ignored_hooked_blocks ) ) {
$existing_ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
if ( ! empty( $existing_ignored_hooked_blocks ) ) {
$existing_ignored_hooked_blocks = json_decode( $existing_ignored_hooked_blocks, true );
$ignored_hooked_blocks = array_unique( array_merge( $ignored_hooked_blocks, $existing_ignored_hooked_blocks ) );
}
if ( ! isset( $post->meta_input ) ) {
$post->meta_input = array();
}
$post->meta_input['_wp_ignored_hooked_blocks'] = json_encode( $ignored_hooked_blocks );
}
$post->post_content = remove_serialized_parent_block( $serialized_block );
return $post;
}