-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
FSE: Verify if php template exists for a hybrid/universal theme with a block-based parent #31123
Changes from all commits
4c26fac
5032b23
5ca2c43
87ecee6
be5e239
2ffddb2
a0b8f9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ function gutenberg_add_template_loader_filters() { | |
add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 ); | ||
} | ||
} | ||
|
||
add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' ); | ||
|
||
/** | ||
|
@@ -69,6 +70,19 @@ function gutenberg_override_query_template( $template, $type, array $templates = | |
$current_template_slug = basename( $template, '.php' ); | ||
$current_block_template_slug = is_object( $current_template ) ? $current_template->slug : false; | ||
foreach ( $templates as $template_item ) { | ||
|
||
// if the theme is a child theme we want to check if a php template exists | ||
// and that a corresponding block template from the theme and not the parent doesn't exist. | ||
$has_php_template = file_exists( get_stylesheet_directory() . '/' . $type . '.php' ); | ||
$has_block_template = false; | ||
$block_template = _gutenberg_get_template_file( 'wp_template', $type ); | ||
if ( null !== $block_template && wp_get_theme()->get_stylesheet() === $block_template['theme'] ) { | ||
$has_block_template = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if lines 77-81 would be better encapsulated in a separate function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about but I didn't do it because I thought it was very unlikely that such a function would be reused since it would be very specific. And the rest of the gutenberg_override_query_template function is using similar checks so I thought this way was a bit more consistent. I'm happy to do it but I'm not sure what we gain from doing so. |
||
} | ||
if ( is_child_theme() && ( $has_php_template && ! $has_block_template ) ) { | ||
return $template; | ||
} | ||
|
||
$template_item_slug = gutenberg_strip_php_suffix( $template_item ); | ||
|
||
// Break the loop if the block-template matches the template slug. | ||
|
@@ -129,7 +143,7 @@ function gutenberg_override_query_template( $template, $type, array $templates = | |
} | ||
|
||
/** | ||
* Return the correct 'wp_template' to render fot the request template type. | ||
* Return the correct 'wp_template' to render for the request template type. | ||
* | ||
* Accepts an optional $template_hierarchy argument as a hint. | ||
* | ||
|
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.
is this a better option than using $template?
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.
$template will pull from the parent if the child doesn't have a template defined for a specific page since it uses locate_template(). With get_stylesheet_directory() we will be sure the path is not from the parent.