Themes: Improve the performance of `_get_block_templates_paths`.

This avoids redundant recursive lookups for block template paths in the same base directory by implementing a static cache. It also replaces an potentially expensive `file_exists` call in favor of doing recursive iteration of files inside a try/catch block. 

Props thekt12, spacedmonkey, flixos90, mukesh27, joemcgill.
Fixes #58196.

Built from https://develop.svn.wordpress.org/trunk@57215


git-svn-id: http://core.svn.wordpress.org/trunk@56721 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2023-12-20 20:02:16 +00:00
parent f5e051855d
commit 5b5e874ed5
2 changed files with 9 additions and 2 deletions

View File

@ -224,14 +224,21 @@ function _filter_block_template_part_area( $type ) {
* @return string[] A list of paths to all template part files.
*/
function _get_block_templates_paths( $base_directory ) {
static $template_path_list = array();
if ( isset( $template_path_list[ $base_directory ] ) ) {
return $template_path_list[ $base_directory ];
}
$path_list = array();
if ( file_exists( $base_directory ) ) {
try {
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
$nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH );
foreach ( $nested_html_files as $path => $file ) {
$path_list[] = $path;
}
} catch ( Exception $e ) {
// Do nothing.
}
$template_path_list[ $base_directory ] = $path_list;
return $path_list;
}

View File

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