mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@57215 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
ed9ade3997
commit
77dcb1771a
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -387,6 +387,49 @@ class Tests_Block_Template extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests `_get_block_templates_paths()` for an invalid directory.
|
||||
*
|
||||
* @ticket 58196
|
||||
*
|
||||
* @covers ::_get_block_templates_paths
|
||||
*/
|
||||
public function test_get_block_templates_paths_dir_exists() {
|
||||
$theme_dir = get_template_directory();
|
||||
// Templates in the current theme.
|
||||
$templates = array(
|
||||
'parts/small-header.html',
|
||||
'templates/custom-single-post-template.html',
|
||||
'templates/index.html',
|
||||
'templates/page-home.html',
|
||||
'templates/page.html',
|
||||
'templates/single.html',
|
||||
);
|
||||
|
||||
$expected_template_paths = array_map(
|
||||
static function ( $template ) use ( $theme_dir ) {
|
||||
return $theme_dir . '/' . $template;
|
||||
},
|
||||
$templates
|
||||
);
|
||||
|
||||
$template_paths = _get_block_templates_paths( $theme_dir );
|
||||
$this->assertSameSets( $expected_template_paths, $template_paths );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test _get_block_templates_paths() for a invalid dir.
|
||||
*
|
||||
* @ticket 58196
|
||||
*
|
||||
* @covers ::_get_block_templates_paths
|
||||
*/
|
||||
public function test_get_block_templates_paths_dir_doesnt_exists() {
|
||||
// Should return empty array for invalid path.
|
||||
$template_paths = _get_block_templates_paths( '/tmp/random-invalid-theme-path' );
|
||||
$this->assertSame( array(), $template_paths );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a test block to log `in_the_loop()` results.
|
||||
*
|
||||
|
||||
Loading…
Reference in New Issue
Block a user