From 244a8cf308f2ee02197276f1f249cbe437052bd9 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 19 Apr 2024 17:59:16 +0000 Subject: [PATCH] Themes: Cache block theme patterns in a transient. This extends the benefits of persistent caching added in [56978] for block theme patterns to sites that are not using a persistent object cache. By default, these caches expire using the value of the `WP_Theme::cache_expiration` property. The transient cache TTL can be overridden using the newly introduced `wp_theme_files_cache_ttl` filter. Props thekt12, joemcgill, flixos90, peterwilsoncc, spacedmonkey. See #59600, #59719. Built from https://develop.svn.wordpress.org/trunk@58025 git-svn-id: http://core.svn.wordpress.org/trunk@57491 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-theme.php | 37 +++++++++++++++++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/wp-includes/class-wp-theme.php b/wp-includes/class-wp-theme.php index 9833981dfe..6db75e4058 100644 --- a/wp-includes/class-wp-theme.php +++ b/wp-includes/class-wp-theme.php @@ -1972,6 +1972,7 @@ final class WP_Theme implements ArrayAccess { * Gets block pattern cache. * * @since 6.4.0 + * @since 6.6.0 Uses transients to cache regardless of site environment. * * @return array|false Returns an array of patterns if cache is found, otherwise false. */ @@ -1979,7 +1980,9 @@ final class WP_Theme implements ArrayAccess { if ( ! $this->exists() ) { return false; } - $pattern_data = wp_cache_get( 'wp_theme_patterns_' . $this->stylesheet, 'theme_files' ); + + $pattern_data = get_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash ); + if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) { return $pattern_data['patterns']; } @@ -1990,6 +1993,7 @@ final class WP_Theme implements ArrayAccess { * Sets block pattern cache. * * @since 6.4.0 + * @since 6.6.0 Uses transients to cache regardless of site environment. * * @param array $patterns Block patterns data to set in cache. */ @@ -1998,16 +2002,43 @@ final class WP_Theme implements ArrayAccess { 'version' => $this->get( 'Version' ), 'patterns' => $patterns, ); - wp_cache_set( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data, 'theme_files' ); + + /** + * Filters the cache expiration time for theme files. + * + * @since 6.6.0 + * + * @param int $cache_expiration Cache expiration time in seconds. + * @param string $cache_type Type of cache being set. + */ + $cache_expiration = (int) apply_filters( 'wp_theme_files_cache_ttl', self::$cache_expiration, 'theme_block_patterns' ); + + // We don't want to cache patterns infinitely. + if ( $cache_expiration <= 0 ) { + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: %1$s: The filter name.*/ + __( 'The %1$s filter must return an integer value greater than 0.' ), + 'wp_theme_files_cache_ttl' + ), + '6.6.0' + ); + + $cache_expiration = self::$cache_expiration; + } + + set_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash, $pattern_data, $cache_expiration ); } /** * Clears block pattern cache. * * @since 6.4.0 + * @since 6.6.0 Uses transients to cache regardless of site environment. */ public function delete_pattern_cache() { - wp_cache_delete( 'wp_theme_patterns_' . $this->stylesheet, 'theme_files' ); + delete_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index acaa1018ca..a6c033f079 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.6-alpha-58024'; +$wp_version = '6.6-alpha-58025'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.