WordPress Version: 5.3
/**
* Builds a unified template object based a post Object.
*
* @since 5.9.0
* @since 6.3.0 Added `modified` property to template objects.
* @since 6.4.0 Added support for a revision post to be passed to this function.
* @access private
*
* @param WP_Post $post Template post.
* @return WP_Block_Template|WP_Error Template or error object.
*/
function _build_block_template_result_from_post($post)
{
$post_id = wp_is_post_revision($post);
if (!$post_id) {
$post_id = $post;
}
$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');
if (is_wp_error($terms)) {
return $terms;
}
if (!$terms) {
return new WP_Error('template_missing_theme', __('No theme is defined for this template.'));
}
$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) {
$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();
// Check for a block template for a single author, page, post, tag, category, custom post type, or custom taxonomy.
if (preg_match('/(author|page|single|tag|category|taxonomy)-(.+)/', $template->slug, $matches)) {
$type = $matches[1];
$slug_remaining = $matches[2];
switch ($type) {
case 'author':
$nice_name = $slug_remaining;
$users = get_users(array('capability' => 'edit_posts', 'search' => $nice_name, 'search_columns' => array('user_nicename'), 'fields' => 'display_name'));
if (empty($users)) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor, referencing a deleted author. %s: Author nicename. */
__('Deleted author: %s'),
$nice_name
);
} else {
$author_name = $users[0];
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. %s: Author name. */
__('Author: %s'),
$author_name
);
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Author name. */
__('Template for %s'),
$author_name
);
$users_with_same_name = get_users(array('capability' => 'edit_posts', 'search' => $author_name, 'search_columns' => array('display_name'), 'fields' => 'display_name'));
if (count($users_with_same_name) > 1) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Template title of an author template, 2: Author nicename. */
__('%1$s (%2$s)'),
$template->title,
$nice_name
);
}
}
break;
case 'page':
_wp_build_title_and_description_for_single_post_type_block_template('page', $slug_remaining, $template);
break;
case 'single':
$post_types = get_post_types();
foreach ($post_types as $post_type) {
$post_type_length = strlen($post_type) + 1;
// If $slug_remaining starts with $post_type followed by a hyphen.
if (0 === strncmp($slug_remaining, $post_type . '-', $post_type_length)) {
$slug = substr($slug_remaining, $post_type_length, strlen($slug_remaining));
$found = _wp_build_title_and_description_for_single_post_type_block_template($post_type, $slug, $template);
if ($found) {
break;
}
}
}
break;
case 'tag':
_wp_build_title_and_description_for_taxonomy_block_template('post_tag', $slug_remaining, $template);
break;
case 'category':
_wp_build_title_and_description_for_taxonomy_block_template('category', $slug_remaining, $template);
break;
case 'taxonomy':
$taxonomies = get_taxonomies();
foreach ($taxonomies as $taxonomy) {
$taxonomy_length = strlen($taxonomy) + 1;
// If $slug_remaining starts with $taxonomy followed by a hyphen.
if (0 === strncmp($slug_remaining, $taxonomy . '-', $taxonomy_length)) {
$slug = substr($slug_remaining, $taxonomy_length, strlen($slug_remaining));
$found = _wp_build_title_and_description_for_taxonomy_block_template($taxonomy, $slug, $template);
if ($found) {
break;
}
}
}
break;
}
}
}
$hooked_blocks = get_hooked_blocks();
if (!empty($hooked_blocks) || has_filter('hooked_block_types')) {
$before_block_visitor = make_before_block_visitor($hooked_blocks, $template);
$after_block_visitor = make_after_block_visitor($hooked_blocks, $template);
$blocks = parse_blocks($template->content);
$template->content = traverse_and_serialize_blocks($blocks, $before_block_visitor, $after_block_visitor);
}
return $template;
}