_build_block_template_result_from_post

The timeline below displays how wordpress function _build_block_template_result_from_post has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

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;
}

WordPress Version: 6.5

/**
 * 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)
{
    $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);
    $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.'));
    }
    $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;
    }
    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;
        }
    }
    // 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;
}

WordPress Version: 6.4

/**
 * 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)
{
    $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);
    $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.'));
    }
    $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;
    }
    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;
        }
    }
    // 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;
            }
        }
    }
    return $template;
}

WordPress Version: 6.3

/**
 * Builds a unified template object based a post Object.
 *
 * @since 5.9.0
 * @since 6.3.0 Added `modified` property to template objects.
 * @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)
{
    $default_template_types = get_default_block_template_types();
    $terms = get_the_terms($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.'));
    }
    $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($post->ID, 'origin', true);
    $is_wp_suggestion = get_post_meta($post->ID, 'is_wp_suggestion', true);
    $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($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' === $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) {
        $type_terms = get_the_terms($post, 'wp_template_part_area');
        if (!is_wp_error($type_terms) && false !== $type_terms) {
            $template->area = $type_terms[0]->name;
        }
    }
    // Check for a block template without a description and title or with a title equal to the slug.
    if ('wp_template' === $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;
            }
        }
    }
    return $template;
}

WordPress Version: 1.1

/**
 * Builds a unified template object based a post Object.
 *
 * @since 5.9.0
 * @access private
 *
 * @param WP_Post $post Template post.
 * @return WP_Block_Template|WP_Error Template.
 */
function _build_block_template_result_from_post($post)
{
    $default_template_types = get_default_block_template_types();
    $terms = get_the_terms($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.'));
    }
    $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($post->ID, 'origin', true);
    $is_wp_suggestion = get_post_meta($post->ID, 'is_wp_suggestion', true);
    $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($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;
    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) {
        $type_terms = get_the_terms($post, 'wp_template_part_area');
        if (!is_wp_error($type_terms) && false !== $type_terms) {
            $template->area = $type_terms[0]->name;
        }
    }
    // Check for a block template without a description and title or with a title equal to the slug.
    if ('wp_template' === $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;
            }
        }
    }
    return $template;
}

WordPress Version: 6.1

/**
 * Builds a unified template object based a post Object.
 *
 * @since 5.9.0
 * @access private
 *
 * @param WP_Post $post Template post.
 * @return WP_Block_Template|WP_Error Template.
 */
function _build_block_template_result_from_post($post)
{
    $default_template_types = get_default_block_template_types();
    $terms = get_the_terms($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.'));
    }
    $theme = $terms[0]->name;
    $template_file = _get_block_template_file($post->post_type, $post->post_name);
    $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== $template_file;
    $origin = get_post_meta($post->ID, 'origin', true);
    $is_wp_suggestion = get_post_meta($post->ID, 'is_wp_suggestion', true);
    $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($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;
    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) {
        $type_terms = get_the_terms($post, 'wp_template_part_area');
        if (!is_wp_error($type_terms) && false !== $type_terms) {
            $template->area = $type_terms[0]->name;
        }
    }
    // Check for a block template without a description and title or with a title equal to the slug.
    if ('wp_template' === $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;
            }
        }
    }
    return $template;
}

WordPress Version: 5.9

/**
 * Build a unified template object based a post Object.
 *
 * @since 5.9.0
 * @access private
 *
 * @param WP_Post $post Template post.
 *
 * @return WP_Block_Template|WP_Error Template.
 */
function _build_block_template_result_from_post($post)
{
    $default_template_types = get_default_block_template_types();
    $terms = get_the_terms($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.'));
    }
    $theme = $terms[0]->name;
    $has_theme_file = wp_get_theme()->get_stylesheet() === $theme && null !== _get_block_template_file($post->post_type, $post->post_name);
    $origin = get_post_meta($post->ID, 'origin', true);
    $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($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 = true;
    $template->author = $post->post_author;
    if ('wp_template' === $post->post_type && isset($default_template_types[$template->slug])) {
        $template->is_custom = false;
    }
    if ('wp_template_part' === $post->post_type) {
        $type_terms = get_the_terms($post, 'wp_template_part_area');
        if (!is_wp_error($type_terms) && false !== $type_terms) {
            $template->area = $type_terms[0]->name;
        }
    }
    return $template;
}