Skip to content

Commit

Permalink
Clear existing cache while in theme development mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Oct 12, 2023
1 parent ed704e6 commit 4414d8b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<div>Some content.</div>',
),
)
);

// 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' ) );
}
}

0 comments on commit 4414d8b

Please sign in to comment.