From 5b5e874ed5202e3b4355d74b5f7b24a5c6aaae0c Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Wed, 20 Dec 2023 20:02:16 +0000 Subject: [PATCH] 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 --- wp-includes/block-template-utils.php | 9 ++++++++- wp-includes/version.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/wp-includes/block-template-utils.php b/wp-includes/block-template-utils.php index 265758b922..f47c649fff 100644 --- a/wp-includes/block-template-utils.php +++ b/wp-includes/block-template-utils.php @@ -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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index c16e679d4c..9789a2e029 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.