Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve _get_block_templates_paths performace #5281

Closed
15 changes: 9 additions & 6 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,17 @@ static function ( $item ) {
* @return string[] A list of paths to all template part files.
*/
function _get_block_templates_paths( $base_directory ) {
static $wp_path_list = array();
joemcgill marked this conversation as resolved.
Show resolved Hide resolved
if ( isset( $wp_path_list[ $base_directory ] ) ) {
return $wp_path_list[ $base_directory ];
}
$path_list = array();
if ( file_exists( $base_directory ) ) {
kt-12 marked this conversation as resolved.
Show resolved Hide resolved
$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;
}
$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;
}
$wp_path_list[ $base_directory ] = $path_list;
return $path_list;
}

Expand Down