Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Remove need for template prefix in gutenberg_get_template_hierarchy. #46257

Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function register_routes() {
* @return WP_REST_Response|WP_Error
*/
public function get_template_fallback( $request ) {
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
$hierarchy = gutenberg_get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
$response = $this->prepare_item_for_response( $fallback_template, $request );
return rest_ensure_response( $response );
Expand Down
112 changes: 112 additions & 0 deletions lib/compat/wordpress-6.2/block-template-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Temporary compatibility shims for features present in Gutenberg.
* This file should be removed when WordPress 6.1.0 becomes the lowest
* supported version by this plugin.
*
* @package gutenberg
*/

/**
* Helper function to get the Template Hierarchy for a given slug.
* We need to Handle special cases here like `front-page`, `singular` and `archive` templates.
*
* Noting that we always add `index` as the last fallback template.
*
* @param string $slug The template slug to be created.
* @param boolean $is_custom Indicates if a template is custom or part of the template hierarchy.
* @param string $template_prefix The template prefix for the created template. This is used to extract the main template type ex. in `taxonomy-books` we extract the `taxonomy`.
*
* @return array<string> The template hierarchy.
*/
function gutenberg_get_template_hierarchy( $slug, $is_custom = false, $template_prefix = '' ) {
if ( 'index' === $slug ) {
return array( 'index' );
}
if ( $is_custom ) {
return array( 'page', 'singular', 'index' );
}
if ( 'front-page' === $slug ) {
return array( 'front-page', 'home', 'index' );
}

$template_hierarchy = array( $slug );
// Most default templates don't have `$template_prefix` assigned.
if ( ! empty( $template_prefix ) ) {
list($type) = explode( '-', $template_prefix );
// We need these checks because we always add the `$slug` above.
if ( ! in_array( $template_prefix, array( $slug, $type ), true ) ) {
$template_hierarchy[] = $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;
}
}
}
}
}
}
// Handle `archive` template.
if (
str_starts_with( $slug, 'author' ) ||
str_starts_with( $slug, 'taxonomy' ) ||
str_starts_with( $slug, 'category' ) ||
str_starts_with( $slug, 'tag' ) ||
'date' === $slug
) {
$template_hierarchy[] = 'archive';
}
// Handle `single` template.
if ( 'attachment' === $slug ) {
$template_hierarchy[] = 'single';
}
// Handle `singular` template.
if (
str_starts_with( $slug, 'single' ) ||
str_starts_with( $slug, 'page' ) ||
'attachment' === $slug
) {
$template_hierarchy[] = 'singular';
}
$template_hierarchy[] = 'index';
return $template_hierarchy;
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// WordPress 6.2 compat.
require __DIR__ . '/compat/wordpress-6.2/script-loader.php';
require __DIR__ . '/compat/wordpress-6.2/block-template-utils.php';
require __DIR__ . '/compat/wordpress-6.2/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.2/default-filters.php';
require __DIR__ . '/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php';
Expand Down
135 changes: 112 additions & 23 deletions phpunit/block-template-utils-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,40 @@ public function set_up() {
switch_theme( 'emptytheme' );
}

public static function wpSetUpBeforeClass() {
register_post_type(
'custom_book',
array(
'public' => true,
'show_in_rest' => true,
)
);
register_taxonomy( 'book_type', 'custom_book' );
}

public static function wpTearDownAfterClass() {
unregister_post_type( 'custom_book' );
unregister_taxonomy( 'book_type' );
}

public function test_get_template_hierarchy() {
$hierarchy = get_template_hierarchy( 'front-page' );
$hierarchy = gutenberg_get_template_hierarchy( 'front-page' );
$this->assertEquals( array( 'front-page', 'home', 'index' ), $hierarchy );
// Custom templates.
$hierarchy = get_template_hierarchy( 'whatever-slug', true );
$hierarchy = gutenberg_get_template_hierarchy( 'whatever-slug', true );
$this->assertEquals( array( 'page', 'singular', 'index' ), $hierarchy );
// Single slug templates(ex. page, tag, author, etc..
$hierarchy = get_template_hierarchy( 'page' );
$hierarchy = gutenberg_get_template_hierarchy( 'page' );
$this->assertEquals( array( 'page', 'singular', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'tag' );
$hierarchy = gutenberg_get_template_hierarchy( 'tag' );
$this->assertEquals( array( 'tag', 'archive', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'author' );
$hierarchy = gutenberg_get_template_hierarchy( 'author' );
$this->assertEquals( array( 'author', 'archive', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'date' );
$hierarchy = gutenberg_get_template_hierarchy( 'date' );
$this->assertEquals( array( 'date', 'archive', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'taxonomy' );
$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy' );
$this->assertEquals( array( 'taxonomy', 'archive', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'attachment' );
$hierarchy = gutenberg_get_template_hierarchy( 'attachment' );
$this->assertEquals(
array(
'attachment',
Expand All @@ -38,20 +54,34 @@ public function test_get_template_hierarchy() {
),
$hierarchy
);
$hierarchy = get_template_hierarchy( 'singular' );
$hierarchy = gutenberg_get_template_hierarchy( 'singular' );
$this->assertEquals( array( 'singular', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'single' );
$hierarchy = gutenberg_get_template_hierarchy( 'single' );
$this->assertEquals( array( 'single', 'singular', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'archive' );
$hierarchy = gutenberg_get_template_hierarchy( 'archive' );
$this->assertEquals( array( 'archive', 'index' ), $hierarchy );
$hierarchy = get_template_hierarchy( 'index' );
$hierarchy = gutenberg_get_template_hierarchy( 'index' );
$this->assertEquals( array( 'index' ), $hierarchy );

// Taxonomies.
$hierarchy = get_template_hierarchy( 'taxonomy-books', false, 'taxonomy-books' );
$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy-book_type', false );
$this->assertEquals( array( 'taxonomy-book_type', 'taxonomy', 'archive', 'index' ), $hierarchy );

$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy-books', false, 'taxonomy-books' );
$this->assertEquals( array( 'taxonomy-books', 'taxonomy', 'archive', 'index' ), $hierarchy );
// Single word category.
$hierarchy = get_template_hierarchy( 'category-fruits', false, 'category' );
$hierarchy = gutenberg_get_template_hierarchy( 'category-fruits', false );
$this->assertEquals(
array(
'category-fruits',
'category',
'archive',
'index',
),
$hierarchy
);

$hierarchy = gutenberg_get_template_hierarchy( 'category-fruits', false, 'category' );
$this->assertEquals(
array(
'category-fruits',
Expand All @@ -62,7 +92,18 @@ public function test_get_template_hierarchy() {
$hierarchy
);
// Multi word category.
$hierarchy = get_template_hierarchy( 'category-fruits-yellow', false, 'category' );
$hierarchy = gutenberg_get_template_hierarchy( 'category-fruits-yellow', false );
$this->assertEquals(
array(
'category-fruits-yellow',
'category',
'archive',
'index',
),
$hierarchy
);

$hierarchy = gutenberg_get_template_hierarchy( 'category-fruits-yellow', false, 'category' );
$this->assertEquals(
array(
'category-fruits-yellow',
Expand All @@ -73,7 +114,7 @@ public function test_get_template_hierarchy() {
$hierarchy
);
// Single word taxonomy.
$hierarchy = get_template_hierarchy( 'taxonomy-books-action', false, 'taxonomy-books' );
$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy-books-action', false, 'taxonomy-books' );
$this->assertEquals(
array(
'taxonomy-books-action',
Expand All @@ -84,7 +125,11 @@ public function test_get_template_hierarchy() {
),
$hierarchy
);
$hierarchy = get_template_hierarchy( 'taxonomy-books-action-adventure', false, 'taxonomy-books' );

$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy-book_type-adventure', false );
$this->assertEquals( array( 'taxonomy-book_type-adventure', 'taxonomy-book_type', 'taxonomy', 'archive', 'index' ), $hierarchy );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why in this case it returned both 'taxonomy-book_type' and taxonomy as parents? What's the logic for extracting the "prefix" from the slug, are there any risk of ambiguity?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a template for taxonomy-book_type-adventure. It is a template for the term adventure of the taxonomy book type. The hierarchy of the templates is to try to use the term specific term template taxonomy-book_type-adventure. If it does not exist, WordPress tries to use the general template for the specific taxonomy in this case, taxonomy-book_type and if it does not exists WordPress tries to use taxonomy. WordPress tries to use both taxonomy-book_type and taxonomy so both should be included.
The logic is available at https://github.com/WordPress/gutenberg/pull/46257/files#diff-02603047171e2b2aa5174d1b541451ed8245519310dde37fda28fd8fe8b7480aR68-R85.
We check all the available taxonomies and verify if the prefixes match.

are there any risk of ambiguity

Yes, we have this risk, but so has the WordPress template resolution engine. Imagine we have the following template taxonomy-a-b-c.
And we have taxonomy a-b with term c.
And we have taxonomy a with term b-c.
The WordPress template resolution engine is going to apply the template to both term c of taxonomy a-b and term b-c of taxonomy a. In practice is not big deal normally developers would not create an a-b taxonomy for post types and taxonomies normally "-" is not used and "_" is used instead. I don't think that's a case we should optimize for.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming, there's no perf impact on these lookups (available taxonomies...), let's ship this.


$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy-books-action-adventure', false, 'taxonomy-books' );
$this->assertEquals(
array(
'taxonomy-books-action-adventure',
Expand All @@ -96,7 +141,7 @@ public function test_get_template_hierarchy() {
$hierarchy
);
// Multi word taxonomy/terms.
$hierarchy = get_template_hierarchy( 'taxonomy-greek-books-action-adventure', false, 'taxonomy-greek-books' );
$hierarchy = gutenberg_get_template_hierarchy( 'taxonomy-greek-books-action-adventure', false, 'taxonomy-greek-books' );
$this->assertEquals(
array(
'taxonomy-greek-books-action-adventure',
Expand All @@ -108,7 +153,7 @@ public function test_get_template_hierarchy() {
$hierarchy
);
// Post types.
$hierarchy = get_template_hierarchy( 'single-book', false, 'single-book' );
$hierarchy = gutenberg_get_template_hierarchy( 'single-book', false, 'single-book' );
$this->assertEquals(
array(
'single-book',
Expand All @@ -118,7 +163,7 @@ public function test_get_template_hierarchy() {
),
$hierarchy
);
$hierarchy = get_template_hierarchy( 'single-art-project', false, 'single-art-project' );
$hierarchy = gutenberg_get_template_hierarchy( 'single-art-project', false, 'single-art-project' );
$this->assertEquals(
array(
'single-art-project',
Expand All @@ -128,7 +173,7 @@ public function test_get_template_hierarchy() {
),
$hierarchy
);
$hierarchy = get_template_hierarchy( 'single-art-project-imagine', false, 'single-art-project' );
$hierarchy = gutenberg_get_template_hierarchy( 'single-art-project-imagine', false, 'single-art-project' );
$this->assertEquals(
array(
'single-art-project-imagine',
Expand All @@ -139,7 +184,41 @@ public function test_get_template_hierarchy() {
),
$hierarchy
);
$hierarchy = get_template_hierarchy( 'page-hi', false, 'page' );

$hierarchy = gutenberg_get_template_hierarchy( 'single-custom_book', false );
$this->assertEquals(
array(
'single-custom_book',
'single',
'singular',
'index',
),
$hierarchy
);

$hierarchy = gutenberg_get_template_hierarchy( 'single-custom_book-book-1', false );
$this->assertEquals(
array(
'single-custom_book-book-1',
'single-custom_book',
'single',
'singular',
'index',
),
$hierarchy
);

$hierarchy = gutenberg_get_template_hierarchy( 'page-hi', false, 'page' );
$this->assertEquals(
array(
'page-hi',
'page',
'singular',
'index',
),
$hierarchy
);
$hierarchy = gutenberg_get_template_hierarchy( 'page-hi', false );
$this->assertEquals(
array(
'page-hi',
Expand All @@ -150,7 +229,7 @@ public function test_get_template_hierarchy() {
$hierarchy
);
// Authors.
$hierarchy = get_template_hierarchy( 'author-rigas', false, 'author' );
$hierarchy = gutenberg_get_template_hierarchy( 'author-rigas', false, 'author' );
$this->assertEquals(
array(
'author-rigas',
Expand All @@ -160,5 +239,15 @@ public function test_get_template_hierarchy() {
),
$hierarchy
);
// Archive post types.
$hierarchy = gutenberg_get_template_hierarchy( 'archive-book', false );
$this->assertEquals(
array(
'archive-book',
'archive',
'index',
),
$hierarchy
);
}
}