Themes: Improve performance of get_block_theme_folders function

This commit enhances the performance of the get_block_theme_folders function by introducing a new method called get_block_template_folders within the WP_Theme class. Previously, this function suffered from poor performance due to repeated file lookups using file_exists. The new method implements basic caching, storing the result in the theme's cache, similar to how block themes are cached in the block_theme property (see [55236]).

Additionally, this change improves error handling by checking if a theme exists before attempting to look up the file. It also enhances test coverage. 

Props spacedmonkey, thekt12, swissspidy, flixos90, costdev, mukesh27.
Fixes #58319.

git-svn-id: https://develop.svn.wordpress.org/trunk@56621 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-09-19 16:15:52 +00:00
parent 48b9b6cbab
commit 9a734f3751
8 changed files with 284 additions and 67 deletions

View File

@@ -37,21 +37,15 @@ if ( ! defined( 'WP_TEMPLATE_PART_AREA_UNCATEGORIZED' ) ) {
* }
*/
function get_block_theme_folders( $theme_stylesheet = null ) {
$theme_name = null === $theme_stylesheet ? get_stylesheet() : $theme_stylesheet;
$root_dir = get_theme_root( $theme_name );
$theme_dir = "$root_dir/$theme_name";
if ( file_exists( $theme_dir . '/block-templates' ) || file_exists( $theme_dir . '/block-template-parts' ) ) {
$theme = wp_get_theme( (string) $theme_stylesheet );
if ( ! $theme->exists() ) {
// Return the default folders if the theme doesn't exist.
return array(
'wp_template' => 'block-templates',
'wp_template_part' => 'block-template-parts',
'wp_template' => 'templates',
'wp_template_part' => 'parts',
);
}
return array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
);
return $theme->get_block_template_folders();
}
/**