From d805e9cc0316dd941c365ba5e9bd382785caacee Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Oct 2023 12:49:31 -0500 Subject: [PATCH 01/10] Introduce function to get cached theme template file contents. --- src/wp-includes/block-template-utils.php | 60 +++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index c5953e1d4ce2c..47685e9d64fef 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -507,6 +507,64 @@ function _remove_theme_attribute_from_template_part_block( &$block ) { } } +/** + * Gets the contents for the given template file path, using a transient cache. + * + * @since 6.4.0 + * @access private + * + * @param string $template_file_path Absolute path to a theme template file. + * @return string The template file contents. + */ +function _get_template_file_content( $template_file_path ) { + $theme = wp_get_theme(); + + $template_file_path = wp_normalize_path( $template_file_path ); + $theme_dir = wp_normalize_path( $theme->get_stylesheet_directory() ); + + if ( str_starts_with( $template_file_path, $theme_dir ) ) { + $relative_path = substr( $template_file_path, strlen( $theme_dir ) ); + } elseif ( $theme->parent() ) { + $theme = $theme->parent(); + $theme_dir = wp_normalize_path( $theme->get_stylesheet_directory() ); + if ( str_starts_with( $template_file_path, $theme_dir ) ) { + $relative_path = substr( $template_file_path, strlen( $theme_dir ) ); + } + } + + // Bypass cache if the file is not within the theme directory. + if ( ! isset( $relative_path ) ) { + return file_get_contents( $template_file_path ); + } + + // Bypass cache while developing a theme. + $can_use_cache = ! wp_is_development_mode( 'theme' ); + if ( ! $can_use_cache ) { + return file_get_contents( $template_file_path ); + } + + // Check theme template cache first (if cached version matches the current theme version). + $template_data = get_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet() ); + if ( is_array( $template_data ) && $template_data['version'] === $theme->get( 'Version' ) ) { + if ( isset( $template_data['template_content'][ $relative_path ] ) ) { + return $template_data['template_content'][ $relative_path ]; + } + } else { + $template_data = array( + 'version' => $theme->get( 'Version' ), + 'template_content' => array(), + ); + } + + // Retrieve fresh file contents if not found in cache. + $template_data['template_content'][ $relative_path ] = file_get_contents( $template_file_path ); + + // Update the cache. + set_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet(), $template_data ); + + return $template_data['template_content'][ $relative_path ]; +} + /** * Builds a unified template object based on a theme file. * @@ -520,7 +578,7 @@ function _remove_theme_attribute_from_template_part_block( &$block ) { */ function _build_block_template_result_from_file( $template_file, $template_type ) { $default_template_types = get_default_block_template_types(); - $template_content = file_get_contents( $template_file['path'] ); + $template_content = _get_template_file_content( $template_file['path'] ); $theme = get_stylesheet(); $template = new WP_Block_Template(); From b2408fcb3191f229c7ecc0472e1db7c1f3061a1d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Oct 2023 12:56:07 -0500 Subject: [PATCH 02/10] Rename function to be specific to block templates. --- src/wp-includes/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 47685e9d64fef..950aa191ee122 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -516,7 +516,7 @@ function _remove_theme_attribute_from_template_part_block( &$block ) { * @param string $template_file_path Absolute path to a theme template file. * @return string The template file contents. */ -function _get_template_file_content( $template_file_path ) { +function _get_block_template_file_content( $template_file_path ) { $theme = wp_get_theme(); $template_file_path = wp_normalize_path( $template_file_path ); @@ -578,7 +578,7 @@ function _get_template_file_content( $template_file_path ) { */ function _build_block_template_result_from_file( $template_file, $template_type ) { $default_template_types = get_default_block_template_types(); - $template_content = _get_template_file_content( $template_file['path'] ); + $template_content = _get_block_template_file_content( $template_file['path'] ); $theme = get_stylesheet(); $template = new WP_Block_Template(); From ad18c944d61a79dce8a0b15e8fdd612309b7a5a3 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Oct 2023 16:19:09 -0500 Subject: [PATCH 03/10] Clear template content caches when switching theme. --- src/wp-includes/theme.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index b5fba76159dad..7fca8fe178c6a 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -877,6 +877,12 @@ function switch_theme( $stylesheet ) { $new_theme->delete_pattern_cache(); $old_theme->delete_pattern_cache(); + // Clear template content caches. + delete_transient( 'wp_theme_template_contents_' . $new_theme->get_stylesheet() ); + delete_transient( 'wp_theme_template_contents_' . $new_theme->get_template() ); + delete_transient( 'wp_theme_template_contents_' . $old_theme->get_stylesheet() ); + delete_transient( 'wp_theme_template_contents_' . $old_theme->get_template() ); + /** * Fires after the theme is switched. * From fd3671f0e149b7ba3bcdc7cd7d17d098eee55438 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Oct 2023 17:01:40 -0500 Subject: [PATCH 04/10] Fix bug where theme slugs starting with same slug as another theme would be incorrectly cached. --- src/wp-includes/block-template-utils.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 950aa191ee122..696cb87e03364 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -520,13 +520,13 @@ function _get_block_template_file_content( $template_file_path ) { $theme = wp_get_theme(); $template_file_path = wp_normalize_path( $template_file_path ); - $theme_dir = wp_normalize_path( $theme->get_stylesheet_directory() ); + $theme_dir = wp_normalize_path( $theme->get_stylesheet_directory() ) . '/'; if ( str_starts_with( $template_file_path, $theme_dir ) ) { $relative_path = substr( $template_file_path, strlen( $theme_dir ) ); } elseif ( $theme->parent() ) { $theme = $theme->parent(); - $theme_dir = wp_normalize_path( $theme->get_stylesheet_directory() ); + $theme_dir = wp_normalize_path( $theme->get_stylesheet_directory() ) . '/'; if ( str_starts_with( $template_file_path, $theme_dir ) ) { $relative_path = substr( $template_file_path, strlen( $theme_dir ) ); } From af0de47c54a3076dbe38e7dcd2108a06a0bf47d7 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 11 Oct 2023 17:43:31 -0500 Subject: [PATCH 05/10] Add test coverage. --- tests/phpunit/tests/block-template-utils.php | 237 +++++++++++++++++++ 1 file changed, 237 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index b06e931529f1e..64461dfa1039a 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -86,6 +86,12 @@ public function set_up() { switch_theme( self::TEST_THEME ); } + public function tear_down() { + parent::tear_down(); + + unset( $GLOBALS['_wp_tests_development_mode'] ); + } + public function test_build_block_template_result_from_post() { $template = _build_block_template_result_from_post( self::$template_post, @@ -523,4 +529,235 @@ public function test_wp_generate_block_templates_export_file() { } $this->assertTrue( $has_html_files, 'contains at least one html file' ); } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the current theme. + * + * This should store the file content in cache. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_current_theme_file() { + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content, 'Unexpected file content' ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme' ); + $this->assertArrayHasKey( 'template_content', $cache_result, 'Invalid cache value' ); + $this->assertArrayHasKey( 'parts/small-header.html', $cache_result['template_content'], 'File not set in cache' ); + $this->assertSame( $content, $cache_result['template_content']['parts/small-header.html'], 'File has incorrect content in cache' ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the current parent theme. + * + * This should store the file content in cache. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_current_parent_theme_file() { + switch_theme( 'block-theme-child' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/templates/index.html'; + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content, 'Unexpected file content' ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme' ); + $this->assertArrayHasKey( 'template_content', $cache_result, 'Invalid cache value' ); + $this->assertArrayHasKey( 'templates/index.html', $cache_result['template_content'], 'File not set in cache' ); + $this->assertSame( $content, $cache_result['template_content']['templates/index.html'], 'File has incorrect content in cache' ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is not part of the current theme. + * + * This should not set any cache. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_another_theme_file() { + switch_theme( 'block-theme-patterns' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme-child/templates/page-1.html'; + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content, 'Unexpected file content' ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme-patterns' ); + $this->assertFalse( $cache_result, 'Cache unexpectedly set for current theme' ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme' ); + $this->assertFalse( $cache_result, 'Cache unexpectedly set for current parent theme' ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme-child' ); + $this->assertFalse( $cache_result, 'Cache unexpectedly set for non-current theme' ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the current theme while using 'theme' development mode. + * + * This should not set any cache. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_current_theme_file_and_theme_development_mode() { + global $_wp_tests_development_mode; + + $_wp_tests_development_mode = 'theme'; + + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content, 'Unexpected file content' ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme' ); + $this->assertFalse( $cache_result, 'Cache unexpectedly set despite theme development mode' ); + } + + /** + * Tests `_get_block_template_file_content()` with files that are part of the current theme expands the existing cache. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_expands_existing_cache() { + switch_theme( 'block-theme' ); + + $template_file1 = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + $template_file2 = DIR_TESTDATA . '/themedir1/block-theme/templates/index.html'; + $template_file3 = DIR_TESTDATA . '/themedir1/block-theme/templates/page-home.html'; + + $content1 = _get_block_template_file_content( $template_file1 ); + $content2 = _get_block_template_file_content( $template_file2 ); + $content3 = _get_block_template_file_content( $template_file3 ); + + $cache_result = get_transient( 'wp_theme_template_contents_block-theme' ); + $this->assertSame( + array( + 'version' => '1.0.0', + 'template_content' => array( + 'parts/small-header.html' => $content1, + 'templates/index.html' => $content2, + 'templates/page-home.html' => $content3, + ), + ), + $cache_result + ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the current theme relies on cached values. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_current_theme_file_relies_on_cache() { + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + $forced_content = '
Some cache content that is not actually the file content.
'; + set_transient( + 'wp_theme_template_contents_block-theme', + array( + 'version' => '1.0.0', + 'template_content' => array( + 'parts/small-header.html' => $forced_content, + ), + ) + ); + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( $forced_content, $content ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the another theme ignores cached values. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_another_theme_file_ignores_cache() { + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme-child/templates/page-1.html'; + $forced_content = '
Some cache content that is not actually the file content.
'; + set_transient( + 'wp_theme_template_contents_block-theme-child', + array( + 'version' => '1.0.0', + 'template_content' => array( + 'templates/page-1.html' => $forced_content, + ), + ) + ); + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the current theme refreshes existing cache when version is outdated. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_current_theme_file_refreshes_cache_when_version_outdated() { + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + $forced_content = '
Some cache content that is not actually the file content.
'; + set_transient( + 'wp_theme_template_contents_block-theme', + array( + 'version' => '0.9.0', + 'template_content' => array( + 'parts/small-header.html' => $forced_content, + ), + ) + ); + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content, 'Cached file content unexpectedly returned' ); + + $this->assertSame( + array( + 'version' => '1.0.0', + 'template_content' => array( + 'parts/small-header.html' => $content, + ), + ), + get_transient( 'wp_theme_template_contents_block-theme' ), + 'Cached transient was not updated' + ); + } + + /** + * Tests `_get_block_template_file_content()` with a file that is part of the current theme ignores cached values while using 'theme' development mode. + * + * @ticket 59600 + */ + public function test_get_block_template_file_content_with_current_theme_file_and_theme_development_mode_ignores_cache() { + global $_wp_tests_development_mode; + + $_wp_tests_development_mode = 'theme'; + + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + $forced_content = '
Some cache content that is not actually the file content.
'; + set_transient( + 'wp_theme_template_contents_block-theme', + array( + 'version' => '1.0.0', + 'template_content' => array( + 'parts/small-header.html' => $forced_content, + ), + ) + ); + + $content = _get_block_template_file_content( $template_file ); + $this->assertSame( file_get_contents( $template_file ), $content ); + } } From fa387c8b2a8a9058e202b7bfc6ae3c8b82c829d0 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 12 Oct 2023 10:25:16 -0500 Subject: [PATCH 06/10] Add covers annotations. --- tests/phpunit/tests/block-template-utils.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 64461dfa1039a..2238314d1698e 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -536,6 +536,8 @@ public function test_wp_generate_block_templates_export_file() { * This should store the file content in cache. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_current_theme_file() { switch_theme( 'block-theme' ); @@ -557,6 +559,8 @@ public function test_get_block_template_file_content_with_current_theme_file() { * This should store the file content in cache. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_current_parent_theme_file() { switch_theme( 'block-theme-child' ); @@ -578,6 +582,8 @@ public function test_get_block_template_file_content_with_current_parent_theme_f * This should not set any cache. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_another_theme_file() { switch_theme( 'block-theme-patterns' ); @@ -603,6 +609,8 @@ public function test_get_block_template_file_content_with_another_theme_file() { * This should not set any cache. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_current_theme_file_and_theme_development_mode() { global $_wp_tests_development_mode; @@ -624,6 +632,8 @@ public function test_get_block_template_file_content_with_current_theme_file_and * Tests `_get_block_template_file_content()` with files that are part of the current theme expands the existing cache. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_expands_existing_cache() { switch_theme( 'block-theme' ); @@ -654,6 +664,8 @@ public function test_get_block_template_file_content_expands_existing_cache() { * Tests `_get_block_template_file_content()` with a file that is part of the current theme relies on cached values. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_current_theme_file_relies_on_cache() { switch_theme( 'block-theme' ); @@ -678,6 +690,8 @@ public function test_get_block_template_file_content_with_current_theme_file_rel * Tests `_get_block_template_file_content()` with a file that is part of the another theme ignores cached values. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_another_theme_file_ignores_cache() { switch_theme( 'block-theme' ); @@ -702,6 +716,8 @@ public function test_get_block_template_file_content_with_another_theme_file_ign * Tests `_get_block_template_file_content()` with a file that is part of the current theme refreshes existing cache when version is outdated. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_current_theme_file_refreshes_cache_when_version_outdated() { switch_theme( 'block-theme' ); @@ -737,6 +753,8 @@ public function test_get_block_template_file_content_with_current_theme_file_ref * Tests `_get_block_template_file_content()` with a file that is part of the current theme ignores cached values while using 'theme' development mode. * * @ticket 59600 + * + * @covers ::_get_block_template_file_content */ public function test_get_block_template_file_content_with_current_theme_file_and_theme_development_mode_ignores_cache() { global $_wp_tests_development_mode; From a262dab4ee6cdd1d0445a8f5994bcdc939111a3b Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 12 Oct 2023 10:27:26 -0500 Subject: [PATCH 07/10] Eliminate useless variable. --- src/wp-includes/block-template-utils.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 696cb87e03364..c44dec81e1069 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -538,8 +538,7 @@ function _get_block_template_file_content( $template_file_path ) { } // Bypass cache while developing a theme. - $can_use_cache = ! wp_is_development_mode( 'theme' ); - if ( ! $can_use_cache ) { + if ( wp_is_development_mode( 'theme' ) ) { return file_get_contents( $template_file_path ); } From ed704e60f093a01ebbcd31868c49a5d18bf3bf3c Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 12 Oct 2023 10:28:18 -0500 Subject: [PATCH 08/10] Remove unnecessary detail from function description. --- src/wp-includes/block-template-utils.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index c44dec81e1069..cbcf7e5cf1800 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -508,7 +508,7 @@ function _remove_theme_attribute_from_template_part_block( &$block ) { } /** - * Gets the contents for the given template file path, using a transient cache. + * Gets the contents for the given template file path. * * @since 6.4.0 * @access private From 4414d8b1c5c6e4245ff47e791e820f30d1f62e44 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 12 Oct 2023 10:41:19 -0500 Subject: [PATCH 09/10] Clear existing cache while in theme development mode. --- src/wp-includes/block-template-utils.php | 12 +++++++- tests/phpunit/tests/block-template-utils.php | 32 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index cbcf7e5cf1800..eb60ee1784de7 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -537,8 +537,18 @@ function _get_block_template_file_content( $template_file_path ) { return file_get_contents( $template_file_path ); } - // Bypass cache while developing a theme. + /* + * Bypass cache while developing a theme. + * If there is an existing cache, it should be deleted. + * This ensures that no stale cache values can be served when temporarily + * enabling "theme" development mode and then disabling it again. + */ if ( wp_is_development_mode( 'theme' ) ) { + $template_data = get_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet() ); + if ( false !== $template_data ) { + delete_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet() ); + } + return file_get_contents( $template_file_path ); } diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 2238314d1698e..7ab5961e86df2 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -778,4 +778,36 @@ public function test_get_block_template_file_content_with_current_theme_file_and $content = _get_block_template_file_content( $template_file ); $this->assertSame( file_get_contents( $template_file ), $content ); } + + /** + * Tests `_get_block_template_file_content()` while using 'theme' development mode clears the existing cache for the current theme. + * + * @ticket 59600 + * + * @covers ::_get_block_template_file_content + */ + public function test_get_block_template_file_content_with_theme_development_mode_clears_existing_cache() { + global $_wp_tests_development_mode; + + $_wp_tests_development_mode = 'theme'; + + switch_theme( 'block-theme' ); + + $template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html'; + set_transient( + 'wp_theme_template_contents_block-theme', + array( + 'version' => '1.0.0', + 'template_content' => array( + 'parts/small-header.html' => '
Some content.
', + ), + ) + ); + + // We don't care about the value here as it is already covered by the test above. + _get_block_template_file_content( $template_file ); + + // Ensure the relevant transient was deleted. + $this->assertFalse( get_transient( 'wp_theme_template_contents_block-theme' ) ); + } } From d0f971ffc2808de62fc5198275298de679beab02 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Thu, 12 Oct 2023 10:45:22 -0500 Subject: [PATCH 10/10] Add expiration to transient to prevent it from being autoloaded. --- src/wp-includes/block-template-utils.php | 2 +- tests/phpunit/tests/block-template-utils.php | 15 ++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index eb60ee1784de7..6c15135f6a361 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -569,7 +569,7 @@ function _get_block_template_file_content( $template_file_path ) { $template_data['template_content'][ $relative_path ] = file_get_contents( $template_file_path ); // Update the cache. - set_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet(), $template_data ); + set_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet(), $template_data, WEEK_IN_SECONDS ); return $template_data['template_content'][ $relative_path ]; } diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 7ab5961e86df2..ccc22f724bd8e 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -679,7 +679,8 @@ public function test_get_block_template_file_content_with_current_theme_file_rel 'template_content' => array( 'parts/small-header.html' => $forced_content, ), - ) + ), + WEEK_IN_SECONDS ); $content = _get_block_template_file_content( $template_file ); @@ -705,7 +706,8 @@ public function test_get_block_template_file_content_with_another_theme_file_ign 'template_content' => array( 'templates/page-1.html' => $forced_content, ), - ) + ), + WEEK_IN_SECONDS ); $content = _get_block_template_file_content( $template_file ); @@ -731,7 +733,8 @@ public function test_get_block_template_file_content_with_current_theme_file_ref 'template_content' => array( 'parts/small-header.html' => $forced_content, ), - ) + ), + WEEK_IN_SECONDS ); $content = _get_block_template_file_content( $template_file ); @@ -772,7 +775,8 @@ public function test_get_block_template_file_content_with_current_theme_file_and 'template_content' => array( 'parts/small-header.html' => $forced_content, ), - ) + ), + WEEK_IN_SECONDS ); $content = _get_block_template_file_content( $template_file ); @@ -801,7 +805,8 @@ public function test_get_block_template_file_content_with_theme_development_mode 'template_content' => array( 'parts/small-header.html' => '
Some content.
', ), - ) + ), + WEEK_IN_SECONDS ); // We don't care about the value here as it is already covered by the test above.