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.
Built from https://develop.svn.wordpress.org/trunk@56621


git-svn-id: http://core.svn.wordpress.org/trunk@56133 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-09-19 16:17:22 +00:00
parent d3a8869891
commit 8fdddd7b6d
3 changed files with 123 additions and 68 deletions

View File

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

View File

@ -194,6 +194,25 @@ final class WP_Theme implements ArrayAccess {
*/ */
private $cache_hash; private $cache_hash;
/**
* Block template folders.
*
* @since 6.4.0
* @var string[]
*/
private $block_template_folders;
/**
* Default values for template folders.
*
* @since 6.4.0
* @var string[]
*/
private $default_template_folders = array(
'wp_template' => 'templates',
'wp_template_part' => 'parts',
);
/** /**
* Flag for whether the themes cache bucket should be persistently cached. * Flag for whether the themes cache bucket should be persistently cached.
* *
@ -262,7 +281,7 @@ final class WP_Theme implements ArrayAccess {
$cache = $this->cache_get( 'theme' ); $cache = $this->cache_get( 'theme' );
if ( is_array( $cache ) ) { if ( is_array( $cache ) ) {
foreach ( array( 'block_theme', 'errors', 'headers', 'template' ) as $key ) { foreach ( array( 'block_template_folders', 'block_theme', 'errors', 'headers', 'template' ) as $key ) {
if ( isset( $cache[ $key ] ) ) { if ( isset( $cache[ $key ] ) ) {
$this->$key = $cache[ $key ]; $this->$key = $cache[ $key ];
} }
@ -289,9 +308,11 @@ final class WP_Theme implements ArrayAccess {
} }
$this->template = $this->stylesheet; $this->template = $this->stylesheet;
$this->block_theme = false; $this->block_theme = false;
$this->block_template_folders = $this->default_template_folders;
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $this->block_template_folders,
'block_theme' => $this->block_theme, 'block_theme' => $this->block_theme,
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
@ -308,9 +329,11 @@ final class WP_Theme implements ArrayAccess {
$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
$this->template = $this->stylesheet; $this->template = $this->stylesheet;
$this->block_theme = false; $this->block_theme = false;
$this->block_template_folders = $this->default_template_folders;
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $this->block_template_folders,
'block_theme' => $this->block_theme, 'block_theme' => $this->block_theme,
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
@ -345,6 +368,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $this->get_block_template_folders(),
'block_theme' => $this->is_block_theme(), 'block_theme' => $this->is_block_theme(),
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
@ -378,6 +402,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $this->get_block_template_folders(),
'block_theme' => $this->block_theme, 'block_theme' => $this->block_theme,
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
@ -419,6 +444,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $this->get_block_template_folders(),
'block_theme' => $this->is_block_theme(), 'block_theme' => $this->is_block_theme(),
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
@ -447,6 +473,7 @@ final class WP_Theme implements ArrayAccess {
$_child->cache_add( $_child->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $_child->get_block_template_folders(),
'block_theme' => $_child->is_block_theme(), 'block_theme' => $_child->is_block_theme(),
'headers' => $_child->headers, 'headers' => $_child->headers,
'errors' => $_child->errors, 'errors' => $_child->errors,
@ -467,6 +494,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'block_template_folders' => $this->get_block_template_folders(),
'block_theme' => $this->is_block_theme(), 'block_theme' => $this->is_block_theme(),
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
@ -489,6 +517,7 @@ final class WP_Theme implements ArrayAccess {
if ( ! is_array( $cache ) ) { if ( ! is_array( $cache ) ) {
$cache = array( $cache = array(
'block_theme' => $this->is_block_theme(), 'block_theme' => $this->is_block_theme(),
'block_template_folders' => $this->get_block_template_folders(),
'headers' => $this->headers, 'headers' => $this->headers,
'errors' => $this->errors, 'errors' => $this->errors,
'stylesheet' => $this->stylesheet, 'stylesheet' => $this->stylesheet,
@ -787,6 +816,7 @@ final class WP_Theme implements ArrayAccess {
$this->headers_sanitized = null; $this->headers_sanitized = null;
$this->name_translated = null; $this->name_translated = null;
$this->block_theme = null; $this->block_theme = null;
$this->block_template_folders = null;
$this->headers = array(); $this->headers = array();
$this->__construct( $this->stylesheet, $this->theme_root ); $this->__construct( $this->stylesheet, $this->theme_root );
} }
@ -1714,6 +1744,37 @@ final class WP_Theme implements ArrayAccess {
return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id ); return (array) apply_filters( 'site_allowed_themes', $allowed_themes[ $blog_id ], $blog_id );
} }
/**
* Returns the folder names of the block template directories.
*
* @since 6.4.0
*
* @return string[] {
* Folder names used by block themes.
*
* @type string $wp_template Theme-relative directory name for block templates.
* @type string $wp_template_part Theme-relative directory name for block template parts.
* }
*/
public function get_block_template_folders() {
// Return set/cached value if available.
if ( isset( $this->block_template_folders ) ) {
return $this->block_template_folders;
}
$this->block_template_folders = $this->default_template_folders;
$stylesheet_directory = $this->get_stylesheet_directory();
// If the theme uses deprecated block template folders.
if ( file_exists( $stylesheet_directory . '/block-templates' ) || file_exists( $stylesheet_directory . '/block-template-parts' ) ) {
$this->block_template_folders = array(
'wp_template' => 'block-templates',
'wp_template_part' => 'block-template-parts',
);
}
return $this->block_template_folders;
}
/** /**
* Enables a theme for all sites on the current network. * Enables a theme for all sites on the current network.
* *

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.4-alpha-56620'; $wp_version = '6.4-alpha-56621';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.