Make WordPress Core


Ignore:
Timestamp:
04/19/2024 05:57:43 PM (2 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php

    r57608 r58025  
    1313 */
    1414class Tests_Theme_WPThemeGetBlockPatterns extends WP_UnitTestCase {
     15    /**
     16     * The initial cache object.
     17     *
     18     * @var object
     19     */
     20    private $initial_cache_object;
     21
     22    public function set_up() {
     23        parent::set_up();
     24
     25        $this->initial_cache_object = wp_using_ext_object_cache();
     26    }
     27
     28    public function tear_down() {
     29        wp_using_ext_object_cache( $this->initial_cache_object );
     30        parent::tear_down();
     31    }
    1532
    1633    public static function wpSetUpBeforeClass() {
     
    3855
    3956        return $pattern_cache;
     57    }
     58
     59    /**
     60     * Test helper to access the private cache_hash propery of a theme.
     61     *
     62     * @param WP_Theme $wp_theme A WP_Theme object.
     63     * @return array|false Returns an array of patterns if cache is found, otherwise false.
     64     */
     65    private function get_cache_hash( $wp_theme ) {
     66        $reflection = new ReflectionProperty( get_class( $wp_theme ), 'cache_hash' );
     67        $reflection->setAccessible( true );
     68        $cache_hash = $reflection->getValue( $wp_theme );
     69        $reflection->setAccessible( false );
     70        return $cache_hash;
    4071    }
    4172
     
    197228        );
    198229    }
     230
     231    /**
     232     * @ticket 59600
     233     *
     234     * @covers WP_Theme::delete_pattern_cache
     235     */
     236    public function test_delete_pattern_cache_non_obj_cache() {
     237        // Ensure object cache is disabled.
     238        wp_using_ext_object_cache( false );
     239
     240        $theme = wp_get_theme( 'block-theme-patterns' );
     241
     242        $this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
     243
     244        $theme->get_block_patterns();
     245
     246        $this->assertSameSets(
     247            array(
     248                'cta.php' => array(
     249                    'title'       => 'Centered Call To Action',
     250                    'slug'        => 'block-theme-patterns/cta',
     251                    'description' => '',
     252                    'categories'  => array( 'call-to-action' ),
     253                ),
     254            ),
     255            $this->get_pattern_cache( $theme ),
     256            'The cache for block theme patterns should match the expected.'
     257        );
     258        $theme->delete_pattern_cache();
     259        $this->assertFalse(
     260            $this->get_pattern_cache( $theme ),
     261            'The cache for block theme patterns should have been cleared.'
     262        );
     263    }
     264
     265    /**
     266     * Check if the pattern cache is stored in transient if object cache is not present.
     267     *
     268     * @ticket 59600
     269     */
     270    public function test_pattern_transient_cache_for_non_cache_site() {
     271        // Ensure object cache is disabled.
     272        wp_using_ext_object_cache( false );
     273
     274        $theme = wp_get_theme( 'block-theme-patterns' );
     275        $theme->get_block_patterns();
     276
     277        $transient_key   = 'wp_theme_files_patterns-' . $this->get_cache_hash( $theme );
     278        $transient_value = get_site_transient( $transient_key );
     279
     280        $this->assertSameSets(
     281            array(
     282                'cta.php' => array(
     283                    'title'       => 'Centered Call To Action',
     284                    'slug'        => 'block-theme-patterns/cta',
     285                    'description' => '',
     286                    'categories'  => array( 'call-to-action' ),
     287                ),
     288            ),
     289            $transient_value['patterns'],
     290            'The transient value should match the expected.'
     291        );
     292
     293        $this->assertNotEmpty(
     294            $this->get_pattern_cache( $theme ),
     295            'The cache for block theme patterns is empty.'
     296        );
     297    }
    199298}
Note: See TracChangeset for help on using the changeset viewer.