Skip to content

Commit 77dcb17

Browse files
committed
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
1 parent ed9ade3 commit 77dcb17

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/wp-includes/block-template-utils.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,21 @@ static function ( $item ) {
224224
* @return string[] A list of paths to all template part files.
225225
*/
226226
function _get_block_templates_paths( $base_directory ) {
227+
static $template_path_list = array();
228+
if ( isset( $template_path_list[ $base_directory ] ) ) {
229+
return $template_path_list[ $base_directory ];
230+
}
227231
$path_list = array();
228-
if ( file_exists( $base_directory ) ) {
232+
try {
229233
$nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) );
230234
$nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH );
231235
foreach ( $nested_html_files as $path => $file ) {
232236
$path_list[] = $path;
233237
}
238+
} catch ( Exception $e ) {
239+
// Do nothing.
234240
}
241+
$template_path_list[ $base_directory ] = $path_list;
235242
return $path_list;
236243
}
237244

tests/phpunit/tests/block-template.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,49 @@ public function data_get_block_theme_folders() {
387387
);
388388
}
389389

390+
/**
391+
* Tests `_get_block_templates_paths()` for an invalid directory.
392+
*
393+
* @ticket 58196
394+
*
395+
* @covers ::_get_block_templates_paths
396+
*/
397+
public function test_get_block_templates_paths_dir_exists() {
398+
$theme_dir = get_template_directory();
399+
// Templates in the current theme.
400+
$templates = array(
401+
'parts/small-header.html',
402+
'templates/custom-single-post-template.html',
403+
'templates/index.html',
404+
'templates/page-home.html',
405+
'templates/page.html',
406+
'templates/single.html',
407+
);
408+
409+
$expected_template_paths = array_map(
410+
static function ( $template ) use ( $theme_dir ) {
411+
return $theme_dir . '/' . $template;
412+
},
413+
$templates
414+
);
415+
416+
$template_paths = _get_block_templates_paths( $theme_dir );
417+
$this->assertSameSets( $expected_template_paths, $template_paths );
418+
}
419+
420+
/**
421+
* Test _get_block_templates_paths() for a invalid dir.
422+
*
423+
* @ticket 58196
424+
*
425+
* @covers ::_get_block_templates_paths
426+
*/
427+
public function test_get_block_templates_paths_dir_doesnt_exists() {
428+
// Should return empty array for invalid path.
429+
$template_paths = _get_block_templates_paths( '/tmp/random-invalid-theme-path' );
430+
$this->assertSame( array(), $template_paths );
431+
}
432+
390433
/**
391434
* Registers a test block to log `in_the_loop()` results.
392435
*

0 commit comments

Comments
 (0)