Make WordPress Core


Ignore:
Timestamp:
10/20/2023 07:06:46 PM (20 months ago)
Author:
joemcgill
Message:

Themes: Make caches for block patterns clearable.

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.

File:
1 moved

Legend:

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

    r56977 r56978  
    11<?php
    22/**
    3  * Tests for _wp_get_block_patterns.
     3 * Tests for WP_Theme::get_block_patterns.
    44 *
    55 * @package WordPress
     
    88 *
    99 * @group blocks
     10 * @group themes
    1011 *
    11  * @covers ::_wp_get_block_patterns
     12 * @covers WP_Theme::get_block_patterns
    1213 */
    13 class Tests_Blocks_WpGetBlockPatterns extends WP_UnitTestCase {
     14class Tests_Theme_WPThemeGetBlockPatterns extends WP_UnitTestCase {
     15
     16    public static function wpSetUpBeforeClass() {
     17        // Ensure development mode is reset before running these tests.
     18        unset( $GLOBALS['_wp_tests_development_mode'] );
     19    }
     20
     21    public static function wpTearDownAfterClass() {
     22        // Ensure development mode is reset after running these tests.
     23        unset( $GLOBALS['_wp_tests_development_mode'] );
     24    }
     25
     26    /**
     27     * Test helper to access the private get_pattern_cache method of a theme.
     28     *
     29     * @param WP_Theme $wp_theme A WP_Theme object.
     30     * @return array|false Returns an array of patterns if cache is found, otherwise false.
     31     */
     32    private function get_pattern_cache( $wp_theme ) {
     33        $reflection = new ReflectionMethod( $wp_theme, 'get_pattern_cache' );
     34        $reflection->setAccessible( true );
     35
     36        $pattern_cache = $reflection->invoke( $wp_theme, 'get_pattern_cache' );
     37        $reflection->setAccessible( false );
     38
     39        return $pattern_cache;
     40    }
     41
    1442    /**
    1543     * @ticket 59490
    1644     *
    17      * @dataProvider data_wp_get_block_patterns
     45     * @dataProvider data_get_block_patterns
    1846     *
    19      * @param string $theme    The theme's slug.
    20      * @param array  $expected The expected pattern data.
     47     * @param string $theme_slug The theme's slug.
     48     * @param array  $expected   The expected pattern data.
    2149     */
    22     public function test_should_return_block_patterns( $theme, $expected ) {
    23         $patterns = _wp_get_block_patterns( wp_get_theme( $theme ) );
     50    public function test_should_return_block_patterns( $theme_slug, $expected ) {
     51        $theme    = wp_get_theme( $theme_slug );
     52        $patterns = $theme->get_block_patterns();
    2453        $this->assertSameSets( $expected, $patterns );
    2554    }
     
    2756    /**
    2857     * @ticket 59490
     58     *
     59     * @covers WP_Theme::delete_pattern_cache
    2960     */
    30     public function test_delete_theme_cache() {
     61    public function test_delete_pattern_cache() {
    3162        $theme = wp_get_theme( 'block-theme-patterns' );
    32         _wp_get_block_patterns( $theme );
     63
     64        $this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
     65
     66        $theme->get_block_patterns();
     67
    3368        $this->assertSameSets(
    3469            array(
     
    4075                ),
    4176            ),
    42             $theme->get_pattern_cache(),
    43             'The transient for block theme patterns should be set'
     77            $this->get_pattern_cache( $theme ),
     78            'The cache for block theme patterns should match the expected.'
    4479        );
    4580        $theme->delete_pattern_cache();
    4681        $this->assertFalse(
    47             $theme->get_pattern_cache(),
    48             'The transient for block theme patterns should have been cleared'
     82            $this->get_pattern_cache( $theme ),
     83            'The cache for block theme patterns should have been cleared.'
    4984        );
    5085    }
     
    5388     * @ticket 59490
    5489     */
    55     public function test_should_clear_transient_after_switching_theme() {
     90    public function test_should_clear_cache_after_switching_theme() {
    5691        switch_theme( 'block-theme' );
    5792        $theme1 = wp_get_theme();
    58         _wp_get_block_patterns( $theme1 );
     93
     94        $this->assertTrue( $theme1->exists(), 'The block-theme test theme could not be found.' );
     95
     96        $theme1->get_block_patterns();
    5997        $this->assertSameSets(
    6098            array(),
    61             $theme1->get_pattern_cache(),
    62             'The transient for block theme should be set'
     99            $this->get_pattern_cache( $theme1 ),
     100            'The cache for block theme should be empty.'
    63101        );
     102
    64103        switch_theme( 'block-theme-patterns' );
    65         $this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' );
     104
    66105        $theme2 = wp_get_theme();
    67         $this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' );
    68         _wp_get_block_patterns( $theme2 );
     106        $this->assertTrue( $theme2->exists(), 'The block-theme-patterns test theme could not be found.' );
     107
     108        $this->assertFalse( $this->get_pattern_cache( $theme1 ), 'Cache should not be set for block theme after switch theme.' );
     109        $this->assertFalse( $this->get_pattern_cache( $theme2 ), 'Cache should not be set for block theme patterns before being requested.' );
     110
     111        $theme2->get_block_patterns( $theme2 );
    69112        $this->assertSameSets(
    70113            array(
     
    77120
    78121            ),
    79             $theme2->get_pattern_cache(),
    80             'The transient for block theme patterns should be set'
     122            $this->get_pattern_cache( $theme2 ),
     123            'The cache for block theme patterns should match the expected.'
    81124        );
    82125    }
     
    87130     * @return array[]
    88131     */
    89     public function data_wp_get_block_patterns() {
     132    public function data_get_block_patterns() {
    90133        return array(
    91134            array(
     
    120163
    121164    /**
    122      * Tests that _wp_get_block_patterns() clears existing transient when in theme development mode.
     165     * Tests that WP_Theme::get_block_patterns() clears existing cache when in theme development mode.
    123166     *
    124167     * @ticket 59591
    125168     */
    126     public function test_should_clear_existing_transient_when_in_development_mode() {
     169    public function test_should_clear_existing_cache_when_in_development_mode() {
    127170        $theme = wp_get_theme( 'block-theme-patterns' );
    128171
     172        $this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
     173
    129174        // Calling the function should set the cache.
    130         _wp_get_block_patterns( $theme );
     175        $theme->get_block_patterns();
    131176        $this->assertSameSets(
    132177            array(
     
    138183                ),
    139184            ),
    140             $theme->get_pattern_cache(),
    141             'The transient for block theme patterns should be set'
     185            $this->get_pattern_cache( $theme ),
     186            'The cache for block theme patterns should be set.'
    142187        );
    143188
    144189        // Calling the function while in theme development mode should clear the cache.
    145190        $GLOBALS['_wp_tests_development_mode'] = 'theme';
    146         _wp_get_block_patterns( $theme );
     191        $theme->get_block_patterns( $theme );
    147192        unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests.
    148193        $this->assertFalse(
    149             $theme->get_pattern_cache(),
    150             'The transient for block theme patterns should have been cleared due to theme development mode'
     194            $this->get_pattern_cache( $theme ),
     195            'The cache for block theme patterns should have been cleared due to theme development mode.'
    151196        );
    152197    }
Note: See TracChangeset for help on using the changeset viewer.