Skip to content

Commit

Permalink
Editor: Simplify return shape and logic of _wp_get_block_patterns().
Browse files Browse the repository at this point in the history
Follow up to [56765].

Props spacedmonkey.
Fixes #59490.


git-svn-id: https://develop.svn.wordpress.org/trunk@56771 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
felixarntz committed Oct 3, 2023
1 parent e624c12 commit d8206d2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 84 deletions.
41 changes: 14 additions & 27 deletions src/wp-includes/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,11 @@ function _register_theme_block_patterns() {
$registry = WP_Block_Patterns_Registry::get_instance();

foreach ( $themes as $theme ) {
$pattern_data = _wp_get_block_patterns( $theme );
$dirpath = $theme->get_stylesheet_directory() . '/patterns/';
$text_domain = $theme->get( 'TextDomain' );
$patterns = _wp_get_block_patterns( $theme );
$dirpath = $theme->get_stylesheet_directory() . '/patterns/';
$text_domain = $theme->get( 'TextDomain' );

foreach ( $pattern_data['patterns'] as $file => $pattern_data ) {
foreach ( $patterns as $file => $pattern_data ) {
if ( $registry->is_registered( $pattern_data['slug'] ) ) {
continue;
}
Expand Down Expand Up @@ -405,42 +405,29 @@ function _register_theme_block_patterns() {
* @param WP_Theme $theme Theme object.
* @return array Block pattern data.
*/

function _wp_get_block_patterns( WP_Theme $theme ) {
if ( ! $theme->exists() ) {
return array(
'version' => false,
'patterns' => array(),
);
}

$transient_name = 'wp_theme_patterns_' . $theme->get_stylesheet();
$version = $theme->get( 'Version' );
$can_use_cached = ! wp_is_development_mode( 'theme' );

if ( $can_use_cached ) {
$pattern_data = get_transient( $transient_name );
if ( is_array( $pattern_data ) && $pattern_data['version'] === $version ) {
$pattern_data = $theme->get_pattern_cache();
if ( is_array( $pattern_data ) ) {
return $pattern_data;
}
}

$pattern_data = array(
'version' => $version,
'patterns' => array(),
);
$dirpath = $theme->get_stylesheet_directory() . '/patterns/';
$pattern_data = array();

if ( ! file_exists( $dirpath ) ) {
if ( $can_use_cached ) {
set_transient( $transient_name, $pattern_data );
$theme->set_pattern_cache( $pattern_data );
}
return $pattern_data;
}
$files = glob( $dirpath . '*.php' );
if ( ! $files ) {
if ( $can_use_cached ) {
set_transient( $transient_name, $pattern_data );
$theme->set_pattern_cache( $pattern_data );
}
return $pattern_data;
}
Expand Down Expand Up @@ -473,7 +460,7 @@ function _wp_get_block_patterns( WP_Theme $theme ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: file name. */
/* translators: 1: file name. */
__( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
$file
),
Expand All @@ -486,7 +473,7 @@ function _wp_get_block_patterns( WP_Theme $theme ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %1s: file name; %2s: slug value found. */
/* translators: 1: file name; 2: slug value found. */
__( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ),
$file,
$pattern['slug']
Expand All @@ -500,7 +487,7 @@ function _wp_get_block_patterns( WP_Theme $theme ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %1s: file name. */
/* translators: 1: file name. */
__( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
$file
),
Expand Down Expand Up @@ -540,11 +527,11 @@ function _wp_get_block_patterns( WP_Theme $theme ) {

$key = str_replace( $dirpath, '', $file );

$pattern_data['patterns'][ $key ] = $pattern;
$pattern_data[ $key ] = $pattern;
}

if ( $can_use_cached ) {
set_transient( $transient_name, $pattern_data );
$theme->set_pattern_cache( $pattern_data );
}

return $pattern_data;
Expand Down
35 changes: 34 additions & 1 deletion src/wp-includes/class-wp-theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,40 @@ public function cache_delete() {
}

/**
* Clear block pattern cache.
* Gets block pattern cache.
*
* @since 6.4.0
*
* @return array|false Returns an array of patterns if cache is found, otherwise false.
*/
public function get_pattern_cache() {
if ( ! $this->exists() ) {
return false;
}
$pattern_data = get_transient( 'wp_theme_patterns_' . $this->stylesheet );
if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) {
return $pattern_data['patterns'];
}
return false;
}

/**
* Sets block pattern cache.
*
* @since 6.4.0
*
* @param array $patterns Block patterns data to set in cache.
*/
public function set_pattern_cache( array $patterns ) {
$pattern_data = array(
'version' => $this->get( 'Version' ),
'patterns' => $patterns,
);
set_transient( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data );
}

/**
* Clears block pattern cache.
*
* @since 6.4.0
*/
Expand Down
88 changes: 32 additions & 56 deletions tests/phpunit/tests/blocks/wpGetBlockPatterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,21 @@ public function test_should_return_block_patterns( $theme, $expected ) {
public function test_delete_theme_cache() {
$theme = wp_get_theme( 'block-theme-patterns' );
_wp_get_block_patterns( $theme );
$transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );
$this->assertSameSets(
array(
'version' => '1.0.0',
'patterns' => array(
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),
),
$transient,
$theme->get_pattern_cache(),
'The transient for block theme patterns should be set'
);
$theme->cache_delete();
$transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );
$theme->delete_pattern_cache();
$this->assertFalse(
$transient,
$theme->get_pattern_cache(),
'The transient for block theme patterns should have been cleared'
);
}
Expand All @@ -60,33 +55,29 @@ public function test_delete_theme_cache() {
*/
public function test_should_clear_transient_after_switching_theme() {
switch_theme( 'block-theme' );
_wp_get_block_patterns( wp_get_theme() );
$theme1 = wp_get_theme();
_wp_get_block_patterns( $theme1 );
$this->assertSameSets(
array(
'version' => '1.0.0',
'patterns' => array(),
),
get_transient( 'wp_theme_patterns_block-theme' ),
array(),
$theme1->get_pattern_cache(),
'The transient for block theme should be set'
);
switch_theme( 'block-theme-patterns' );
$this->assertFalse( get_transient( 'wp_theme_patterns_block-theme' ), 'Transient should not be set for block theme after switch theme' );
$this->assertFalse( get_transient( 'wp_theme_patterns_block-theme-patterns' ), 'Transient should not be set for block theme patterns before being requested' );
_wp_get_block_patterns( wp_get_theme() );
$transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );
$this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' );
$theme2 = wp_get_theme();
$this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' );
_wp_get_block_patterns( $theme2 );
$this->assertSameSets(
array(
'version' => '1.0.0',
'patterns' => array(
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),

),
$transient,
$theme2->get_pattern_cache(),
'The transient for block theme patterns should be set'
);
}
Expand All @@ -100,45 +91,30 @@ public function data_wp_get_block_patterns() {
return array(
array(
'theme' => 'block-theme',
'patterns' => array(
'version' => '1.0.0',
'patterns' => array(),
),
'patterns' => array(),
),
array(
'theme' => 'block-theme-child',
'patterns' => array(
'version' => '1.0.0',
'patterns' => array(),
),
'patterns' => array(),
),
array(
'theme' => 'block-theme-patterns',
'patterns' => array(
'version' => '1.0.0',
'patterns' => array(
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),
'cta.php' => array(
'title' => 'Centered Call To Action',
'slug' => 'block-theme-patterns/cta',
'description' => '',
'categories' => array( 'call-to-action' ),
),
),
),
array(
'theme' => 'broken-theme',
'patterns' => array(
'version' => false,
'patterns' => array(),
),
'patterns' => array(),
),
array(
'theme' => 'invalid',
'patterns' => array(
'version' => false,
'patterns' => array(),
),
'patterns' => array(),
),
);
}
Expand Down

0 comments on commit d8206d2

Please sign in to comment.