_build_block_template_object_from_post_object()
云策文档标注
概述
_build_block_template_object_from_post_object() 是一个辅助函数,用于从给定的 WP_Post 对象构建一个 WP_Block_Template 对象。该函数仅依赖于传入的参数,不查询数据库获取额外信息。
关键要点
- 函数接受三个参数:必需的 $post(WP_Post 对象)、可选的 $terms 数组和 $meta 数组,用于提供模板的额外信息。
- 返回值为 WP_Block_Template 对象或 WP_Error 错误对象,具体取决于参数的有效性。
- 函数内部会检查 $terms 数组中是否包含 'wp_theme' 键,若无则返回错误。
- 通过 _get_block_template_file() 和 get_stylesheet() 判断模板文件是否存在。
- 根据 post_type(如 'wp_template' 或 'wp_template_part')设置模板对象的属性,如 id、theme、content、slug 等。
- 处理特定条件,如为 'wp_template' 类型时设置 post_types 和 is_custom 属性,为 'wp_template_part' 时设置 area 属性。
代码示例
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;
}注意事项
- 确保传入的 $terms 数组包含 'wp_theme' 键,否则函数会返回 WP_Error。
- 函数依赖于其他辅助函数如 get_default_block_template_types() 和 _get_block_template_file(),需确保这些函数可用。
- 模板对象的属性设置基于 post 对象和传入参数,开发者应理解 WP_Block_Template 的结构以正确使用返回值。
原文内容
Builds a block template object from a post object.
Description
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.
Parameters
$postWP_Postrequired-
Template post.
$termsarrayoptional-
Additional terms to inform the template object.
Default:
array() $metaarrayoptional-
Additional meta fields to inform the template object.
Default:
array()
Source
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;
}
Changelog
| Version | Description |
|---|---|
| 6.5.3 | Introduced. |