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

@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Small Header Template Part</p>
<!-- /wp:paragraph -->

View File

@@ -0,0 +1,8 @@
/*
Theme Name: Block Theme Child Deprecated Path
Theme URI: https://wordpress.org/
Description: For testing purposes only.
Template: block-theme
Version: 1.0.0
Text Domain: block-theme-child-deprecated-path
*/

View File

@@ -0,0 +1,3 @@
<!-- wp:paragraph -->
<p>Index Template</p>
<!-- /wp:paragraph -->

View File

@@ -0,0 +1,71 @@
{
"version": 1,
"settings": {
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f5f7f9"
},
{
"slug": "dark",
"name": "Dark",
"color": "#000"
}
],
"gradients": [
{
"name": "Custom gradient",
"gradient": "linear-gradient(135deg,rgba(0,0,0) 0%,rgb(0,0,0) 100%)",
"slug": "custom-gradient"
}
],
"custom": false,
"customGradient": false
},
"typography": {
"fontSizes": [
{
"name": "Custom",
"slug": "custom",
"size": "100px"
}
],
"customFontSize": false,
"customLineHeight": true
},
"spacing": {
"units": [
"rem"
],
"customPadding": true
},
"blocks": {
"core/paragraph": {
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f5f7f9"
}
]
}
}
}
},
"customTemplates": [
{
"name": "page-home",
"title": "Homepage template"
}
],
"templateParts": [
{
"name": "small-header",
"title": "Small Header",
"area": "header"
}
]
}

View File

@@ -275,6 +275,82 @@ class Tests_Block_Template extends WP_UnitTestCase {
$this->assertSame( array( false, true ), $in_the_loop_logs, 'Main query loop was triggered incorrectly' );
}
/**
* @ticket 58319
*
* @covers ::get_block_theme_folders
*
* @dataProvider data_get_block_theme_folders
*
* @param string $theme The theme's stylesheet.
* @param string[] $expected The expected associative array of block theme folders.
*/
public function test_get_block_theme_folders( $theme, $expected ) {
$wp_theme = wp_get_theme( $theme );
$wp_theme->cache_delete(); // Clear cache.
$this->assertSame( $expected, get_block_theme_folders( $theme ), 'Incorrect block theme folders were retrieved.' );
$reflection = new ReflectionMethod( $wp_theme, 'cache_get' );
$reflection->setAccessible( true );
$theme_cache = $reflection->invoke( $wp_theme, 'theme' );
$cached_value = $theme_cache['block_template_folders'];
$reflection->setAccessible( false );
$this->assertSame( $expected, $cached_value, 'The cached value is incorrect.' );
}
/**
* Data provider.
*
* @return array[]
*/
public function data_get_block_theme_folders() {
return array(
'block-theme' => array(
'block-theme',
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
'block-theme-deprecated-path' => array(
'block-theme-deprecated-path',
array(
'wp_template' => 'block-templates',
'wp_template_part' => 'block-template-parts',
),
),
'block-theme-child' => array(
'block-theme-child',
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
'block-theme-child-deprecated-path' => array(
'block-theme-child-deprecated-path',
array(
'wp_template' => 'block-templates',
'wp_template_part' => 'block-template-parts',
),
),
'this-is-an-invalid-theme' => array(
'this-is-an-invalid-theme',
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
'null' => array(
null,
array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
),
),
);
}
/**
* Registers a test block to log `in_the_loop()` results.
*

View File

@@ -176,6 +176,7 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase {
'REST Theme',
'Block Theme',
'Block Theme Child Theme',
'Block Theme Child Deprecated Path',
'Block Theme Child with no theme.json',
'Block Theme Child Theme With Fluid Layout',
'Block Theme Child Theme With Fluid Typography',