Make WordPress Core

Changeset 58025

Timestamp:
04/19/2024 05:57:43 PM (3 months ago)
Author:
joemcgill
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme.php

    r57847 r58025  
    19731973     *
    19741974     * @since 6.4.0
     1975
    19751976     *
    19761977     * @return array|false Returns an array of patterns if cache is found, otherwise false.
     
    19801981            return false;
    19811982        }
    1982         $pattern_data = wp_cache_get( 'wp_theme_patterns_' . $this->stylesheet, 'theme_files' );
     1983
     1984        $pattern_data = get_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash );
     1985
    19831986        if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) {
    19841987            return $pattern_data['patterns'];
     
    19911994     *
    19921995     * @since 6.4.0
     1996
    19931997     *
    19941998     * @param array $patterns Block patterns data to set in cache.
     
    19992003            'patterns' => $patterns,
    20002004        );
    2001         wp_cache_set( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data, 'theme_files' );
     2005
     2006        /**
     2007         * Filters the cache expiration time for theme files.
     2008         *
     2009         * @since 6.6.0
     2010         *
     2011         * @param int    $cache_expiration Cache expiration time in seconds.
     2012         * @param string $cache_type       Type of cache being set.
     2013         */
     2014        $cache_expiration = (int) apply_filters( 'wp_theme_files_cache_ttl', self::$cache_expiration, 'theme_block_patterns' );
     2015
     2016        // We don't want to cache patterns infinitely.
     2017        if ( $cache_expiration <= 0 ) {
     2018            _doing_it_wrong(
     2019                __METHOD__,
     2020                sprintf(
     2021                    /* translators: %1$s: The filter name.*/
     2022                    __( 'The %1$s filter must return an integer value greater than 0.' ),
     2023                    '<code>wp_theme_files_cache_ttl</code>'
     2024                ),
     2025                '6.6.0'
     2026            );
     2027
     2028            $cache_expiration = self::$cache_expiration;
     2029        }
     2030
     2031        set_site_transient( 'wp_theme_files_patterns-' . $this->cache_hash, $pattern_data, $cache_expiration );
    20022032    }
    20032033
     
    20062036     *
    20072037     * @since 6.4.0
     2038
    20082039     */
    20092040    public function delete_pattern_cache() {
    2010         wp_cache_delete( 'wp_theme_patterns_' . $this->stylesheet, 'theme_files' );
     2041        );
    20112042    }
    20122043
  • trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php

    r57608 r58025  
    1313 */
    1414class Tests_Theme_WPThemeGetBlockPatterns extends WP_UnitTestCase {
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
    1532
    1633    public static function wpSetUpBeforeClass() {
     
    3855
    3956        return $pattern_cache;
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
    4071    }
    4172
     
    197228        );
    198229    }
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
    199298}
Note: See TracChangeset for help on using the changeset viewer.