: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Returns an array containing the references of
* the passed blocks and their inner blocks.
* @param array $blocks array of blocks.
* @return array block references to the passed blocks and their inner blocks.
function _flatten_blocks( &$blocks ) {
foreach ( $blocks as &$block ) {
while ( count( $queue ) > 0 ) {
if ( ! empty( $block['innerBlocks'] ) ) {
foreach ( $block['innerBlocks'] as &$inner_block ) {
$queue[] = &$inner_block;
* Injects the active theme's stylesheet as a `theme` attribute
* into a given template part block.
* @param array $block a parsed block.
function _inject_theme_attribute_in_template_part_block( &$block ) {
'core/template-part' === $block['blockName'] &&
! isset( $block['attrs']['theme'] )
$block['attrs']['theme'] = get_stylesheet();
* Removes the `theme` attribute from a given template part block.
* @param array $block a parsed block.
function _remove_theme_attribute_from_template_part_block( &$block ) {
'core/template-part' === $block['blockName'] &&
isset( $block['attrs']['theme'] )
unset( $block['attrs']['theme'] );
* Builds a unified template object based on a theme file.
* @since 6.3.0 Added `modified` property to template objects.
* @param array $template_file Theme file.
* @param string $template_type Template type. Either 'wp_template' or 'wp_template_part'.
* @return WP_Block_Template Template.
function _build_block_template_result_from_file( $template_file, $template_type ) {
$default_template_types = get_default_block_template_types();
$theme = get_stylesheet();
$template = new WP_Block_Template();
$template->id = $theme . '//' . $template_file['slug'];
$template->theme = $theme;
$template->content = file_get_contents( $template_file['path'] );
$template->slug = $template_file['slug'];
$template->source = 'theme';
$template->type = $template_type;
$template->title = ! empty( $template_file['title'] ) ? $template_file['title'] : $template_file['slug'];
$template->status = 'publish';
$template->has_theme_file = true;
$template->is_custom = true;
$template->modified = null;
if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
$template->is_custom = false;
if ( 'wp_template' === $template_type && isset( $template_file['postTypes'] ) ) {
$template->post_types = $template_file['postTypes'];
if ( 'wp_template_part' === $template_type && isset( $template_file['area'] ) ) {
$template->area = $template_file['area'];
$before_block_visitor = '_inject_theme_attribute_in_template_part_block';
$after_block_visitor = null;
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
$blocks = parse_blocks( $template->content );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
* Builds the title and description of a post-specific template based on the underlying referenced post.
* Mutates the underlying template object.
* @param string $post_type Post type, e.g. page, post, product.
* @param string $slug Slug of the post, e.g. a-story-about-shoes.
* @param WP_Block_Template $template Template to mutate adding the description and title computed.
* @return bool Returns true if the referenced post was found and false otherwise.
function _wp_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, WP_Block_Template $template ) {
$post_type_object = get_post_type_object( $post_type );
'post_type' => $post_type,
'post_status' => 'publish',
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'ignore_sticky_posts' => true,
$args = wp_parse_args( $args, $default_args );
$posts_query = new WP_Query( $args );
if ( empty( $posts_query->posts ) ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor referencing a post that was not found. 1: Post type singular name, 2: Post type slug. */
__( 'Not found: %1$s (%2$s)' ),
$post_type_object->labels->singular_name,
$post_title = $posts_query->posts[0]->post_title;
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Post type singular name, 2: Post title. */
$post_type_object->labels->singular_name,
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Post title. */
$args = wp_parse_args( $args, $default_args );
$posts_with_same_title_query = new WP_Query( $args );
if ( count( $posts_with_same_title_query->posts ) > 1 ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Template title, 2: Post type slug. */
* Builds the title and description of a taxonomy-specific template based on the underlying entity referenced.
* Mutates the underlying template object.
* @param string $taxonomy Identifier of the taxonomy, e.g. category.
* @param string $slug Slug of the term, e.g. shoes.
* @param WP_Block_Template $template Template to mutate adding the description and title computed.
* @return bool True if the term referenced was found and false otherwise.
function _wp_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, WP_Block_Template $template ) {
$taxonomy_object = get_taxonomy( $taxonomy );
'update_term_meta_cache' => false,
$term_query = new WP_Term_Query();
$args = wp_parse_args( $args, $default_args );
$terms_query = $term_query->query( $args );
if ( empty( $terms_query ) ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor, referencing a taxonomy term that was not found. 1: Taxonomy singular name, 2: Term slug. */
__( 'Not found: %1$s (%2$s)' ),
$taxonomy_object->labels->singular_name,
$term_title = $terms_query[0]->name;
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Taxonomy singular name, 2: Term title. */
$taxonomy_object->labels->singular_name,
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Term title. */
$term_query = new WP_Term_Query();
$args = wp_parse_args( $args, $default_args );
$terms_with_same_title_query = $term_query->query( $args );
if ( count( $terms_with_same_title_query ) > 1 ) {
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. 1: Template title, 2: Term slug. */
* Builds a block template object from a post object.
* 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.
* @param WP_Post $post Template post.
* @param array $terms Additional terms to inform the template object.
* @param array $meta Additional meta fields to inform the template object.
* @return WP_Block_Template|WP_Error Template or error object.
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'];
* Builds a unified template object based a post Object.
* @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.
* @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 );
$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 new WP_Error( 'template_missing_theme', __( 'No theme is defined for this template.' ) );
'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;
'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 ) ) {
// 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 ) ) {
// 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 ) ) {
$slug_remaining = $matches[2];
$nice_name = $slug_remaining;
'capability' => 'edit_posts',
'search_columns' => array( 'user_nicename' ),
'fields' => 'display_name',
$template->title = sprintf(
/* translators: Custom template title in the Site Editor, referencing a deleted author. %s: Author nicename. */
__( 'Deleted author: %s' ),
$author_name = $users[0];
$template->title = sprintf(
/* translators: Custom template title in the Site Editor. %s: Author name. */
$template->description = sprintf(
/* translators: Custom template description in the Site Editor. %s: Author name. */
$users_with_same_name = get_users(
'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. */
_wp_build_title_and_description_for_single_post_type_block_template( 'page', $slug_remaining, $template );
$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 );
_wp_build_title_and_description_for_taxonomy_block_template( 'post_tag', $slug_remaining, $template );
_wp_build_title_and_description_for_taxonomy_block_template( 'category', $slug_remaining, $template );
$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 );
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );