Skip to content

Commit

Permalink
Update src/wp-includes/block-template-utils.php
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Feb 1, 2023
1 parent 470d5a0 commit 56ce69c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
67 changes: 26 additions & 41 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,8 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
return array( 'front-page', 'home', 'index' );
}

$matches = array();

$template_hierarchy = array( $slug );
// Most default templates don't have `$template_prefix` assigned.
if ( ! empty( $template_prefix ) ) {
Expand All @@ -1336,47 +1338,30 @@ function get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '
if ( $slug !== $type ) {
$template_hierarchy[] = $type;
}
} else {
$matches = array();
if ( preg_match( '/^(author|category|archive|tag|page)-(.+)$/', $slug, $matches ) ) {
$template_hierarchy[] = $matches[1];
} elseif ( preg_match( '/^(single|taxonomy)-(.+)$/', $slug, $matches ) ) {
$type = $matches[1];
$slug_remaining = $matches[2];
if ( 'single' === $type ) {
$post_types = get_post_types();
foreach ( $post_types as $post_type ) {
if ( str_starts_with( $slug_remaining, $post_type ) ) {
// If $slug_remaining is equal to $post_type we have the single-$post_type template.
if ( $slug_remaining === $post_type ) {
$template_hierarchy[] = 'single';
break;
}
// If $slug_remaining is single-$post_type-$slug template.
if ( str_starts_with( $slug_remaining, $post_type . '-' ) && strlen( $slug_remaining ) > strlen( $post_type ) + 1 ) {
$template_hierarchy[] = "single-$post_type";
$template_hierarchy[] = 'single';
break;
}
}
}
} elseif ( 'taxonomy' === $type ) {
$taxonomies = get_taxonomies();
foreach ( $taxonomies as $taxonomy ) {
if ( str_starts_with( $slug_remaining, $taxonomy ) ) {
// If $slug_remaining is equal to $taxonomy we have the taxonomy-$taxonomy template.
if ( $slug_remaining === $taxonomy ) {
$template_hierarchy[] = 'taxonomy';
break;
}
// If $slug_remaining is taxonomy-$taxonomy-$term template.
if ( str_starts_with( $slug_remaining, $taxonomy . '-' ) && strlen( $slug_remaining ) > strlen( $taxonomy ) + 1 ) {
$template_hierarchy[] = "taxonomy-$taxonomy";
$template_hierarchy[] = 'taxonomy';
break;
}
}
}
} else if ( preg_match( '/^(author|category|archive|tag|page)-.+$/', $slug, $matches ) ) {
$template_hierarchy[] = $matches[1];
} else if ( preg_match( '/^(taxonomy|single)-(.+)$/', $slug, $matches ) ) {
$type = $matches[1];
$slug_remaining = $matches[2];

$items = 'single' === $type ? get_post_types() : get_taxonomies();
foreach ( $items as $item ) {
if ( ! str_starts_with( $slug_remaining, $item ) ) {
continue;
}

// If $slug_remaining is equal to $post_type or $taxonomy we have
// the single-$post_type template or the taxonomy-$taxonomy template.
if ( $slug_remaining === $item ) {
$template_hierarchy[] = $type;
break;
}

// If $slug_remaining is single-$post_type-$slug template.
if ( strlen( $slug_remaining ) > strlen( $item ) + 1 ) {
$template_hierarchy[] = "$type-$item";
$template_hierarchy[] = $type;
break;
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions tests/phpunit/tests/block-templates/getTemplateHierarchy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
class Tests_Block_Templates_GetTemplate_Hierarchy extends WP_Block_Templates_UnitTestCase {

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
public function set_up() {
parent::set_up();
register_post_type(
'custom_book',
array(
Expand All @@ -17,11 +18,14 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
)
);
register_taxonomy( 'book_type', 'custom_book' );
register_taxonomy( 'books', 'custom_book' );
}

public static function wpTearDownAfterClass() {
public function tear_down() {
unregister_post_type( 'custom_book' );
unregister_taxonomy( 'book_type' );
unregister_taxonomy( 'books' );
parent::tear_down();
}

/**
Expand Down

0 comments on commit 56ce69c

Please sign in to comment.