wpseek.com
A WordPress-centric search engine for devs and theme authors
resolve_block_template is private and should not be used in themes or plugins directly.
resolve_block_template › WordPress Function
Since5.8.0
Deprecatedn/a
› resolve_block_template ( $template_type, $template_hierarchy, $fallback_template )
| Access: |
|
| Parameters: (3) |
|
| Returns: |
|
| Defined at: |
|
| Codex: | |
| Change Log: |
|
Returns the correct 'wp_template' to render for the request template type.
Source
function resolve_block_template( $template_type, $template_hierarchy, $fallback_template ) {
if ( ! $template_type ) {
return null;
}
if ( empty( $template_hierarchy ) ) {
$template_hierarchy = array( $template_type );
}
$slugs = array_map(
'_strip_template_file_suffix',
$template_hierarchy
);
$object = get_queried_object();
$specific_template = $object ? get_page_template_slug( $object ) : null;
$active_templates = (array) get_option( 'active_templates', array() );
// Remove templates slugs that are deactivated, except if it's the specific
// template or index.
$slugs = array_filter(
$slugs,
function ( $slug ) use ( $specific_template, $active_templates ) {
$should_ignore = $slug === $specific_template || 'index' === $slug;
return $should_ignore || ( ! isset( $active_templates[ $slug ] ) || false !== $active_templates[ $slug ] );
}
);
// We expect one template for each slug. Use the active template if it is
// set and exists. Otherwise use the static template.
$templates = array();
$remaining_slugs = array();
foreach ( $slugs as $slug ) {
if ( $slug === $specific_template || empty( $active_templates[ $slug ] ) ) {
$remaining_slugs[] = $slug;
continue;
}
// TODO: it need to be possible to set a static template as active.
$post = get_post( $active_templates[ $slug ] );
if ( ! $post || 'publish' !== $post->post_status ) {
$remaining_slugs[] = $slug;
continue;
}
$template = _build_block_template_result_from_post( $post );
// Ensure the active templates are associated with the active theme.
// See _build_block_template_object_from_post_object.
if ( get_stylesheet() !== $template->theme ) {
$remaining_slugs[] = $slug;
continue;
}
$templates[] = $template;
}
// Apply the filter to the active templates for backward compatibility.
/** This filter is documented in wp-includes/block-template-utils.php */
if ( ! empty( $templates ) ) {
$templates = apply_filters(
'get_block_templates',
$templates,
array(
'slug__in' => array_map(
function ( $template ) {
return $template->slug;
},
$templates
),
),
'wp_template'
);
}
// For any remaining slugs, use the static template.
$query = array(
'slug__in' => $remaining_slugs,
);
$templates = array_merge( $templates, get_registered_block_templates( $query ) );
if ( $specific_template ) {
$templates = array_merge( $templates, get_block_templates( array( 'slug__in' => array( $specific_template ) ) ) );
}
// Order these templates per slug priority.
// Build map of template slugs to their priority in the current hierarchy.
$slug_priorities = array_flip( $slugs );
usort(
$templates,
static function ( $template_a, $template_b ) use ( $slug_priorities ) {
return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
}
);
$theme_base_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR;
$parent_theme_base_path = get_template_directory() . DIRECTORY_SEPARATOR;
// Is the active theme a child theme, and is the PHP fallback template part of it?
if (
str_starts_with( $fallback_template, $theme_base_path ) &&
! str_contains( $fallback_template, $parent_theme_base_path )
) {
$fallback_template_slug = substr(
$fallback_template,
// Starting position of slug.
strpos( $fallback_template, $theme_base_path ) + strlen( $theme_base_path ),
// Remove '.php' suffix.
-4
);
// Is our candidate block template's slug identical to our PHP fallback template's?
if (
count( $templates ) &&
$fallback_template_slug === $templates[0]->slug &&
'theme' === $templates[0]->source
) {
// Unfortunately, we cannot trust $templates[0]->theme, since it will always
// be set to the active theme's slug by _build_block_template_result_from_file(),
// even if the block template is really coming from the active theme's parent.
// (The reason for this is that we want it to be associated with the active theme
// -- not its parent -- once we edit it and store it to the DB as a wp_template CPT.)
// Instead, we use _get_block_template_file() to locate the block template file.
$template_file = _get_block_template_file( 'wp_template', $fallback_template_slug );
if ( $template_file && get_template() === $template_file['theme'] ) {
// The block template is part of the parent theme, so we
// have to give precedence to the child theme's PHP template.
array_shift( $templates );
}
}
}
return count( $templates ) ? $templates[0] : null;
}