Skip to content

Commit

Permalink
Themes: Make caches for block patterns clearable.
Browse files Browse the repository at this point in the history
In [56765], theme block pattern files were cached to a transient as a performance enhancement. However, transients are not easily clearable when caches are flushed on environments not using a persistent cache, which can lead to errors if the theme files are renamed, edited, or moved.

This changes the caching mechanism to use `wp_cache_set()` instead, and caches these values to the global group so they are still persistent on environments using an object cache, and will be cleared by a cache flush.

In addition, the helper `_wp_get_block_patterns` has been moved `WP_Theme::get_block_patterns` for consistency with other block related theme methods and cache helpers for these values, `WP_Theme::get_pattern_cache` and `WP_Theme::set_pattern_cache`, have been made private.

Relevant unit tests updated.

Props: afercia, flixos90, mukesh27, joemcgill.
Fixes #59633. See #59591, #59490.


git-svn-id: https://develop.svn.wordpress.org/trunk@56978 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
joemcgill committed Oct 20, 2023
1 parent df21bdf commit 03e46a7
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 363 deletions.
169 changes: 1 addition & 168 deletions src/wp-includes/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function _register_theme_block_patterns() {
$registry = WP_Block_Patterns_Registry::get_instance();

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

Expand Down Expand Up @@ -387,170 +387,3 @@ function _register_theme_block_patterns() {
}
}
add_action( 'init', '_register_theme_block_patterns' );

/**
* Gets block pattern data for a specified theme.
* Each pattern is defined as a PHP file and defines
* its metadata using plugin-style headers. The minimum required definition is:
*
* /**
* * Title: My Pattern
* * Slug: my-theme/my-pattern
* *
*
* The output of the PHP source corresponds to the content of the pattern, e.g.:
*
* <main><p><?php echo "Hello"; ?></p></main>
*
* If applicable, this will collect from both parent and child theme.
*
* Other settable fields include:
*
* - Description
* - Viewport Width
* - Inserter (yes/no)
* - Categories (comma-separated values)
* - Keywords (comma-separated values)
* - Block Types (comma-separated values)
* - Post Types (comma-separated values)
* - Template Types (comma-separated values)
*
* @since 6.4.0
* @access private
*
* @param WP_Theme $theme Theme object.
* @return array Block pattern data.
*/
function _wp_get_block_patterns( WP_Theme $theme ) {
$can_use_cached = ! wp_is_development_mode( 'theme' );

$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/';
$pattern_data = array();

if ( ! file_exists( $dirpath ) ) {
if ( $can_use_cached ) {
$theme->set_pattern_cache( $pattern_data );
}
return $pattern_data;
}
$files = glob( $dirpath . '*.php' );
if ( ! $files ) {
if ( $can_use_cached ) {
$theme->set_pattern_cache( $pattern_data );
}
return $pattern_data;
}

$default_headers = array(
'title' => 'Title',
'slug' => 'Slug',
'description' => 'Description',
'viewportWidth' => 'Viewport Width',
'inserter' => 'Inserter',
'categories' => 'Categories',
'keywords' => 'Keywords',
'blockTypes' => 'Block Types',
'postTypes' => 'Post Types',
'templateTypes' => 'Template Types',
);

$properties_to_parse = array(
'categories',
'keywords',
'blockTypes',
'postTypes',
'templateTypes',
);

foreach ( $files as $file ) {
$pattern = get_file_data( $file, $default_headers );

if ( empty( $pattern['slug'] ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: file name. */
__( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
$file
),
'6.0.0'
);
continue;
}

if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* 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']
),
'6.0.0'
);
}

// Title is a required property.
if ( ! $pattern['title'] ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: 1: file name. */
__( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
$file
),
'6.0.0'
);
continue;
}

// For properties of type array, parse data as comma-separated.
foreach ( $properties_to_parse as $property ) {
if ( ! empty( $pattern[ $property ] ) ) {
$pattern[ $property ] = array_filter( wp_parse_list( (string) $pattern[ $property ] ) );
} else {
unset( $pattern[ $property ] );
}
}

// Parse properties of type int.
$property = 'viewportWidth';
if ( ! empty( $pattern[ $property ] ) ) {
$pattern[ $property ] = (int) $pattern[ $property ];
} else {
unset( $pattern[ $property ] );
}

// Parse properties of type bool.
$property = 'inserter';
if ( ! empty( $pattern[ $property ] ) ) {
$pattern[ $property ] = in_array(
strtolower( $pattern[ $property ] ),
array( 'yes', 'true' ),
true
);
} else {
unset( $pattern[ $property ] );
}

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

$pattern_data[ $key ] = $pattern;
}

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

return $pattern_data;
}
Loading

0 comments on commit 03e46a7

Please sign in to comment.