From 1d5836281964c1e71af26395f6e3a6f9dd79847a Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 21 Sep 2023 10:40:57 +0100 Subject: [PATCH 1/9] temporary cache --- src/wp-includes/block-template-utils.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 9e0eafcc99e31..cff559ab23ed9 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -224,6 +224,12 @@ static function ( $item ) { * @return string[] A list of paths to all template part files. */ function _get_block_templates_paths( $base_directory ) { + $cache_key = 'block_templates_paths_'. $base_directory; + $path_list = wp_cache_get( $cache_key ); + if ( false !== $path_list ) { + return $path_list; + } + $path_list = array(); if ( file_exists( $base_directory ) ) { $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); @@ -231,6 +237,7 @@ function _get_block_templates_paths( $base_directory ) { foreach ( $nested_html_files as $path => $file ) { $path_list[] = $path; } + wp_cache_set( $cache_key, $path_list ); } return $path_list; } From c09979d5a21e4ede4c6f1044664c24a1d5109f1c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 22 Sep 2023 11:43:17 +0100 Subject: [PATCH 2/9] Introducing static for caching and removing file check as Internally RecursiveDirectoryIterator has a file check, Ref: https://github.com/php/php-src/blob/62e2402534d5e51faba41e30c79f8e4af1783370/ext/spl/spl_directory.c#L1475 `spl_filesystem_is_invalid_or_dot` --- src/wp-includes/block-template-utils.php | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index cff559ab23ed9..27148e1df3e71 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -224,21 +224,17 @@ static function ( $item ) { * @return string[] A list of paths to all template part files. */ function _get_block_templates_paths( $base_directory ) { - $cache_key = 'block_templates_paths_'. $base_directory; - $path_list = wp_cache_get( $cache_key ); - if ( false !== $path_list ) { - return $path_list; + static $wp_path_list = array(); + if ( isset( $wp_path_list[ $base_directory ] ) ) { + return $wp_path_list[ $base_directory ]; } - $path_list = array(); - if ( file_exists( $base_directory ) ) { - $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_cache_set( $cache_key, $path_list ); + $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; } From 360effb31b75220385e244ec3b319f623d0070d8 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 Nov 2023 11:34:47 +0000 Subject: [PATCH 3/9] try catch for fileexists --- src/wp-includes/block-template-utils.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 1c6db3a24871a..fad65b8c66419 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -229,10 +229,14 @@ function _get_block_templates_paths( $base_directory ) { return $wp_path_list[ $base_directory ]; } $path_list = array(); - $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; + 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. } $wp_path_list[ $base_directory ] = $path_list; return $path_list; From 300413579597d2ac5e3b4b2f91ef2efbe29b4151 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 11 Dec 2023 13:41:12 +0000 Subject: [PATCH 4/9] Test cases --- .../tests/blocks/getBlockTemplates.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 9b1729f93bd8a..2a8e1b464f936 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -221,4 +221,41 @@ public function data_get_block_templates_should_respect_posttypes_property() { ), ); } + + /** + * @ticket 58196 + */ + 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->assertSame( $expected_template_paths, $template_paths ); + } + + /** + * @ticket 58196 + */ + public function test_get_block_templates_paths_dir_doesnt_exists() { + $theme_dir = '/tmp/random-invalid-theme-path'; + $expected_template_paths = array(); + // should return empty array for invalid path. + $template_paths = _get_block_templates_paths( $theme_dir ); + $this->assertSame( $expected_template_paths, $template_paths ); + } } From 918e20a836126ba00e55f2a9e4e1278bec6df15f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 11 Dec 2023 14:58:03 +0000 Subject: [PATCH 5/9] fix ordering issue --- tests/phpunit/tests/blocks/getBlockTemplates.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 2a8e1b464f936..1734b07065317 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -245,6 +245,8 @@ static function ( $template ) use ( $theme_dir ) { ); $template_paths = _get_block_templates_paths( $theme_dir ); + sort( $expected_template_paths ); + sort( $template_paths ); $this->assertSame( $expected_template_paths, $template_paths ); } @@ -256,6 +258,8 @@ public function test_get_block_templates_paths_dir_doesnt_exists() { $expected_template_paths = array(); // should return empty array for invalid path. $template_paths = _get_block_templates_paths( $theme_dir ); + sort( $expected_template_paths ); + sort( $template_paths ); $this->assertSame( $expected_template_paths, $template_paths ); } } From f0389556b1c3565aa0836f21f2855c5008601028 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 11 Dec 2023 15:01:51 +0000 Subject: [PATCH 6/9] update doc block --- tests/phpunit/tests/blocks/getBlockTemplates.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index 1734b07065317..eb61882be0bde 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -223,7 +223,10 @@ public function data_get_block_templates_should_respect_posttypes_property() { } /** + * Test _get_block_templates_paths() for a valid theme dir. + * * @ticket 58196 + * @covers ::_get_block_templates_paths */ public function test_get_block_templates_paths_dir_exists() { $theme_dir = get_template_directory(); @@ -251,7 +254,10 @@ static function ( $template ) use ( $theme_dir ) { } /** + * 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() { $theme_dir = '/tmp/random-invalid-theme-path'; From 400f0b1e1bba6c8a571f814037fe7da9c9d0a418 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 19 Dec 2023 15:30:57 +0000 Subject: [PATCH 7/9] change variable name --- src/wp-includes/block-template-utils.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index fad65b8c66419..7c9edbc55d33b 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -224,9 +224,9 @@ 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(); - if ( isset( $wp_path_list[ $base_directory ] ) ) { - return $wp_path_list[ $base_directory ]; + static $template_path_list = array(); + if ( isset( $template_path_list[ $base_directory ] ) ) { + return $template_path_list[ $base_directory ]; } $path_list = array(); try { @@ -238,7 +238,7 @@ function _get_block_templates_paths( $base_directory ) { } catch ( Exception $e ) { // Do nothing. } - $wp_path_list[ $base_directory ] = $path_list; + $template_path_list[ $base_directory ] = $path_list; return $path_list; } From 71dc4436c361b7da684a883ffc9bfae5338ba6e7 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 19 Dec 2023 15:44:40 +0000 Subject: [PATCH 8/9] Address review comment and move function to proper file --- tests/phpunit/tests/block-template.php | 44 +++++++++++++++++ .../tests/blocks/getBlockTemplates.php | 47 ------------------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/tests/phpunit/tests/block-template.php b/tests/phpunit/tests/block-template.php index 7b4cbf33220a7..0693e531996e7 100644 --- a/tests/phpunit/tests/block-template.php +++ b/tests/phpunit/tests/block-template.php @@ -351,6 +351,50 @@ public function data_get_block_theme_folders() { ); } + + /** + * 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. * diff --git a/tests/phpunit/tests/blocks/getBlockTemplates.php b/tests/phpunit/tests/blocks/getBlockTemplates.php index eb61882be0bde..9b1729f93bd8a 100644 --- a/tests/phpunit/tests/blocks/getBlockTemplates.php +++ b/tests/phpunit/tests/blocks/getBlockTemplates.php @@ -221,51 +221,4 @@ public function data_get_block_templates_should_respect_posttypes_property() { ), ); } - - /** - * Test _get_block_templates_paths() for a valid theme dir. - * - * @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 ); - sort( $expected_template_paths ); - sort( $template_paths ); - $this->assertSame( $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() { - $theme_dir = '/tmp/random-invalid-theme-path'; - $expected_template_paths = array(); - // should return empty array for invalid path. - $template_paths = _get_block_templates_paths( $theme_dir ); - sort( $expected_template_paths ); - sort( $template_paths ); - $this->assertSame( $expected_template_paths, $template_paths ); - } } From 5622ba7cec8746c3bb3df9bf27f938f314c3056e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Wed, 20 Dec 2023 10:40:22 +0000 Subject: [PATCH 9/9] Update tests/phpunit/tests/block-template.php Remove extra line Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/block-template.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/block-template.php b/tests/phpunit/tests/block-template.php index 0693e531996e7..4b6b98964d726 100644 --- a/tests/phpunit/tests/block-template.php +++ b/tests/phpunit/tests/block-template.php @@ -351,7 +351,6 @@ public function data_get_block_theme_folders() { ); } - /** * Tests `_get_block_templates_paths()` for an invalid directory. *