-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
jorgefilipecosta
merged 1 commit into
trunk
from
update/remove-need-for-template-prefix-in-gutenberg_get_template_hierarchy
Dec 20, 2022
+226
−24
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'
andtaxonomy
as parents? What's the logic for extracting the "prefix" from the slug, are there any risk of ambiguity?There was a problem hiding this comment.
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 bothtaxonomy-book_type
andtaxonomy
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.
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 termc
.And we have taxonomy
a
with termb-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.
There was a problem hiding this comment.
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.