Skip to content

Commit 4414d8b

Browse files
author
Felix Arntz
committed
Clear existing cache while in theme development mode.
1 parent ed704e6 commit 4414d8b

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,18 @@ function _get_block_template_file_content( $template_file_path ) {
537537
return file_get_contents( $template_file_path );
538538
}
539539

540-
// Bypass cache while developing a theme.
540+
/*
541+
* Bypass cache while developing a theme.
542+
* If there is an existing cache, it should be deleted.
543+
* This ensures that no stale cache values can be served when temporarily
544+
* enabling "theme" development mode and then disabling it again.
545+
*/
541546
if ( wp_is_development_mode( 'theme' ) ) {
547+
$template_data = get_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet() );
548+
if ( false !== $template_data ) {
549+
delete_transient( 'wp_theme_template_contents_' . $theme->get_stylesheet() );
550+
}
551+
542552
return file_get_contents( $template_file_path );
543553
}
544554

tests/phpunit/tests/block-template-utils.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,4 +778,36 @@ public function test_get_block_template_file_content_with_current_theme_file_and
778778
$content = _get_block_template_file_content( $template_file );
779779
$this->assertSame( file_get_contents( $template_file ), $content );
780780
}
781+
782+
/**
783+
* Tests `_get_block_template_file_content()` while using 'theme' development mode clears the existing cache for the current theme.
784+
*
785+
* @ticket 59600
786+
*
787+
* @covers ::_get_block_template_file_content
788+
*/
789+
public function test_get_block_template_file_content_with_theme_development_mode_clears_existing_cache() {
790+
global $_wp_tests_development_mode;
791+
792+
$_wp_tests_development_mode = 'theme';
793+
794+
switch_theme( 'block-theme' );
795+
796+
$template_file = DIR_TESTDATA . '/themedir1/block-theme/parts/small-header.html';
797+
set_transient(
798+
'wp_theme_template_contents_block-theme',
799+
array(
800+
'version' => '1.0.0',
801+
'template_content' => array(
802+
'parts/small-header.html' => '<div>Some content.</div>',
803+
),
804+
)
805+
);
806+
807+
// We don't care about the value here as it is already covered by the test above.
808+
_get_block_template_file_content( $template_file );
809+
810+
// Ensure the relevant transient was deleted.
811+
$this->assertFalse( get_transient( 'wp_theme_template_contents_block-theme' ) );
812+
}
781813
}

0 commit comments

Comments
 (0)