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

FSE: Verify if php template exists for a hybrid/universal theme with a block-based parent #31123

Merged
merged 7 commits into from
Apr 29, 2021
Merged
Changes from 3 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
7 changes: 5 additions & 2 deletions lib/full-site-editing/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function gutenberg_add_template_loader_filters() {
if ( 'embed' === $template_type ) { // Skip 'embed' for now because it is not a regular template type.
continue;
}
add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
// Use block template if there is no php template exists OR it's a child theme with no corresponding block template.
if ( ! file_exists( get_stylesheet_directory() . '/' . $template_type . '.php' ) || ( ! is_child_theme() && null !== _gutenberg_get_template_file( 'wp_template', $template_type ) ) ) {
add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

For better legibility, I would rewrite this as:

$has_php_template = file_exists( get_stylesheet_directory() . '/' . $template_type . '.php' );
$has_block_template = null !== _gutenberg_get_template_file( 'wp_template', $template_type );

if ( ! $has_php_template || ( ! is_child_theme() && $has_block_template ) ) {
	add_filter( str_replace( '-', '', $template_type ) . '_template', 'gutenberg_override_query_template', 20, 3 );
}

}
}
add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' );
Expand Down Expand Up @@ -129,7 +132,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.
*
Expand Down