Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code review updates for get_template_hierarchy()
  • Loading branch information
hellofromtonya committed Sep 20, 2022
commit 4ef9146da49de94b12f316c33b77b309dcc9f5ec
12 changes: 9 additions & 3 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ function wp_generate_block_templates_export_file() {
* @param string $slug The template slug to be created.
* @param boolean $is_custom Optional. Indicates if a template is custom or
* part of the template hierarchy. Default false.
* @param string $template_prefix The template prefix for the created template.
* @param string $template_prefix Optional. The template prefix for the created template.
* Used to extract the main template type, e.g.
* in `taxonomy-books` the `taxonomy` is extracted.
* Default empty string.
Expand All @@ -1046,18 +1046,21 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
if ( 'front-page' === $slug ) {
return array( 'front-page', 'home', 'index' );
}

$template_hierarchy = array( $slug );

// Most default templates don't have `$template_prefix` assigned.
if ( $template_prefix ) {
list($type) = explode( '-', $template_prefix );
// We need these checks because we always add the `$slug` above.
list( $type ) = explode( '-', $template_prefix );
// These checks are needed because the `$slug` above is always added.
if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
$template_hierarchy[] = $template_prefix;
}
if ( $slug !== $type ) {
$template_hierarchy[] = $type;
}
}

// Handle `archive` template.
if (
str_starts_with( $slug, 'author' ) ||
Expand All @@ -1072,6 +1075,7 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
if ( 'attachment' === $slug ) {
$template_hierarchy[] = 'single';
}

// Handle `singular` template.
if (
str_starts_with( $slug, 'single' ) ||
Expand All @@ -1080,6 +1084,8 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
) {
$template_hierarchy[] = 'singular';
}

$template_hierarchy[] = 'index';

return $template_hierarchy;
};