Skip to content

Commit

Permalink
Themes: Clear existing pattern cache when in theme development mode a…
Browse files Browse the repository at this point in the history
…nd prevent PHP warning due to missing file.

Follow up to [56765].

Props spacedmonkey, afercia, jrf, flixos90.
Fixes #59591.
See #59490.


git-svn-id: https://develop.svn.wordpress.org/trunk@56931 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Oct 13, 2023
1 parent 93782b5 commit 8382683
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
26 changes: 22 additions & 4 deletions src/wp-includes/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,25 @@ function _register_theme_block_patterns() {
continue;
}

$file_path = $dirpath . $file;

if ( ! file_exists( $file_path ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: file name. */
__( 'Could not register file "%s" as a block pattern as the file does not exist.' ),
$file
),
'6.4.0'
);
$theme->delete_pattern_cache();
continue;
}

// The actual pattern content is the output of the file.
ob_start();
include $dirpath . $file;
include $file_path;
$pattern_data['content'] = ob_get_clean();
if ( ! $pattern_data['content'] ) {
continue;
Expand Down Expand Up @@ -408,11 +424,13 @@ function _register_theme_block_patterns() {
function _wp_get_block_patterns( WP_Theme $theme ) {
$can_use_cached = ! wp_is_development_mode( 'theme' );

if ( $can_use_cached ) {
$pattern_data = $theme->get_pattern_cache();
if ( is_array( $pattern_data ) ) {
$pattern_data = $theme->get_pattern_cache();
if ( is_array( $pattern_data ) ) {
if ( $can_use_cached ) {
return $pattern_data;
}
// If in development mode, clear pattern cache.
$theme->delete_pattern_cache();
}

$dirpath = $theme->get_stylesheet_directory() . '/patterns/';
Expand Down
34 changes: 33 additions & 1 deletion tests/phpunit/tests/blocks/wpGetBlockPatterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Tests_Blocks_WpGetBlockPatterns extends WP_UnitTestCase {
*
* @param string $theme The theme's slug.
* @param array $expected The expected pattern data.
*/
public function test_should_return_block_patterns( $theme, $expected ) {
$patterns = _wp_get_block_patterns( wp_get_theme( $theme ) );
Expand Down Expand Up @@ -118,4 +117,37 @@ public function data_wp_get_block_patterns() {
),
);
}

/**
* Tests that _wp_get_block_patterns() clears existing transient when in theme development mode.
*
* @ticket 59591
*/
public function test_should_clear_existing_transient_when_in_development_mode() {
$theme = wp_get_theme( 'block-theme-patterns' );

// Calling the function should set the cache.
_wp_get_block_patterns( $theme );
$this->assertSameSets(
array(
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),
),
$theme->get_pattern_cache(),
'The transient for block theme patterns should be set'
);

// Calling the function while in theme development mode should clear the cache.
$GLOBALS['_wp_tests_development_mode'] = 'theme';
_wp_get_block_patterns( $theme );
unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests.
$this->assertFalse(
$theme->get_pattern_cache(),
'The transient for block theme patterns should have been cleared due to theme development mode'
);
}
}

0 comments on commit 8382683

Please sign in to comment.