Make WordPress Core

Changeset 56771


Ignore:
Timestamp:
10/03/2023 06:17:03 PM (3 years ago)
Author:
flixos90
Message:

Editor: Simplify return shape and logic of _wp_get_block_patterns().

Follow up to [56765].

Props spacedmonkey.
Fixes #59490.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-patterns.php

    r56765 r56771  
    342342
    343343        foreach ( $themes as $theme ) {
    344                 $pattern_data = _wp_get_block_patterns( $theme );
    345                 $dirpath      = $theme->get_stylesheet_directory() . '/patterns/';
    346                 $text_domain  = $theme->get( 'TextDomain' );
    347 
    348                 foreach ( $pattern_data['patterns'] as $file => $pattern_data ) {
     344                $patterns    = _wp_get_block_patterns( $theme );
     345                $dirpath     = $theme->get_stylesheet_directory() . '/patterns/';
     346                $text_domain = $theme->get( 'TextDomain' );
     347
     348                foreach ( $patterns as $file => $pattern_data ) {
    349349                        if ( $registry->is_registered( $pattern_data['slug'] ) ) {
    350350                                continue;
     
    406406 * @return array Block pattern data.
    407407 */
    408 
    409408function _wp_get_block_patterns( WP_Theme $theme ) {
    410         if ( ! $theme->exists() ) {
    411                 return array(
    412                         'version'  => false,
    413                         'patterns' => array(),
    414                 );
    415         }
    416 
    417         $transient_name = 'wp_theme_patterns_' . $theme->get_stylesheet();
    418         $version        = $theme->get( 'Version' );
    419409        $can_use_cached = ! wp_is_development_mode( 'theme' );
    420410
    421411        if ( $can_use_cached ) {
    422                 $pattern_data = get_transient( $transient_name );
    423                 if ( is_array( $pattern_data ) && $pattern_data['version'] === $version ) {
     412                $pattern_data = $theme->get_pattern_cache();
     413                if ( is_array( $pattern_data ) ) {
    424414                        return $pattern_data;
    425415                }
    426416        }
    427417
    428         $pattern_data = array(
    429                 'version'  => $version,
    430                 'patterns' => array(),
    431         );
    432418        $dirpath      = $theme->get_stylesheet_directory() . '/patterns/';
     419        $pattern_data = array();
    433420
    434421        if ( ! file_exists( $dirpath ) ) {
    435422                if ( $can_use_cached ) {
    436                         set_transient( $transient_name, $pattern_data );
     423                        $theme->set_pattern_cache( $pattern_data );
    437424                }
    438425                return $pattern_data;
     
    441428        if ( ! $files ) {
    442429                if ( $can_use_cached ) {
    443                         set_transient( $transient_name, $pattern_data );
     430                        $theme->set_pattern_cache( $pattern_data );
    444431                }
    445432                return $pattern_data;
     
    474461                                __FUNCTION__,
    475462                                sprintf(
    476                                         /* translators: %s: file name. */
     463                                        /* translators: 1: file name. */
    477464                                        __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
    478465                                        $file
     
    487474                                __FUNCTION__,
    488475                                sprintf(
    489                                         /* translators: %1s: file name; %2s: slug value found. */
     476                                        /* translators: 1: file name; 2: slug value found. */
    490477                                        __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ),
    491478                                        $file,
     
    501488                                __FUNCTION__,
    502489                                sprintf(
    503                                         /* translators: %1s: file name. */
     490                                        /* translators: 1: file name. */
    504491                                        __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
    505492                                        $file
     
    541528                $key = str_replace( $dirpath, '', $file );
    542529
    543                 $pattern_data['patterns'][ $key ] = $pattern;
     530                $pattern_data[ $key ] = $pattern;
    544531        }
    545532
    546533        if ( $can_use_cached ) {
    547                 set_transient( $transient_name, $pattern_data );
     534                $theme->set_pattern_cache( $pattern_data );
    548535        }
    549536
  • trunk/src/wp-includes/class-wp-theme.php

    r56765 r56771  
    826826
    827827        /**
    828          * Clear block pattern cache.
     828         * Gets block pattern cache.
     829         *
     830         * @since 6.4.0
     831         *
     832         * @return array|false Returns an array of patterns if cache is found, otherwise false.
     833         */
     834        public function get_pattern_cache() {
     835                if ( ! $this->exists() ) {
     836                        return false;
     837                }
     838                $pattern_data = get_transient( 'wp_theme_patterns_' . $this->stylesheet );
     839                if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) {
     840                        return $pattern_data['patterns'];
     841                }
     842                return false;
     843        }
     844
     845        /**
     846         * Sets block pattern cache.
     847         *
     848         * @since 6.4.0
     849         *
     850         * @param array $patterns Block patterns data to set in cache.
     851         */
     852        public function set_pattern_cache( array $patterns ) {
     853                $pattern_data = array(
     854                        'version'  => $this->get( 'Version' ),
     855                        'patterns' => $patterns,
     856                );
     857                set_transient( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data );
     858        }
     859
     860        /**
     861         * Clears block pattern cache.
    829862         *
    830863         * @since 6.4.0
  • trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php

    r56765 r56771  
    3232                $theme = wp_get_theme( 'block-theme-patterns' );
    3333                _wp_get_block_patterns( $theme );
    34                 $transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );
    3534                $this->assertSameSets(
    3635                        array(
    37                                 'version'  => '1.0.0',
    38                                 'patterns' => array(
    39                                         'cta.php' => array(
    40                                                 'title'       => 'Centered Call To Action',
    41                                                 'slug'        => 'block-theme-patterns/cta',
    42                                                 'description' => '',
    43                                                 'categories'  => array( 'call-to-action' ),
    44                                         ),
     36                                'cta.php' => array(
     37                                        'title'       => 'Centered Call To Action',
     38                                        'slug'        => 'block-theme-patterns/cta',
     39                                        'description' => '',
     40                                        'categories'  => array( 'call-to-action' ),
    4541                                ),
    4642                        ),
    47                         $transient,
     43                        $theme->get_pattern_cache(),
    4844                        'The transient for block theme patterns should be set'
    4945                );
    50                 $theme->cache_delete();
    51                 $transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );
     46                $theme->delete_pattern_cache();
    5247                $this->assertFalse(
    53                         $transient,
     48                        $theme->get_pattern_cache(),
    5449                        'The transient for block theme patterns should have been cleared'
    5550                );
     
    6156        public function test_should_clear_transient_after_switching_theme() {
    6257                switch_theme( 'block-theme' );
    63                 _wp_get_block_patterns( wp_get_theme() );
     58                $theme1 = wp_get_theme();
     59                _wp_get_block_patterns( $theme1 );
    6460                $this->assertSameSets(
    65                         array(
    66                                 'version'  => '1.0.0',
    67                                 'patterns' => array(),
    68                         ),
    69                         get_transient( 'wp_theme_patterns_block-theme' ),
     61                        array(),
     62                        $theme1->get_pattern_cache(),
    7063                        'The transient for block theme should be set'
    7164                );
    7265                switch_theme( 'block-theme-patterns' );
    73                 $this->assertFalse( get_transient( 'wp_theme_patterns_block-theme' ), 'Transient should not be set for block theme after switch theme' );
    74                 $this->assertFalse( get_transient( 'wp_theme_patterns_block-theme-patterns' ), 'Transient should not be set for block theme patterns before being requested' );
    75                 _wp_get_block_patterns( wp_get_theme() );
    76                 $transient = get_transient( 'wp_theme_patterns_block-theme-patterns' );
     66                $this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' );
     67                $theme2 = wp_get_theme();
     68                $this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' );
     69                _wp_get_block_patterns( $theme2 );
    7770                $this->assertSameSets(
    7871                        array(
    79                                 'version'  => '1.0.0',
    80                                 'patterns' => array(
    81                                         'cta.php' => array(
    82                                                 'title'       => 'Centered Call To Action',
    83                                                 'slug'        => 'block-theme-patterns/cta',
    84                                                 'description' => '',
    85                                                 'categories'  => array( 'call-to-action' ),
    86                                         ),
     72                                'cta.php' => array(
     73                                        'title'       => 'Centered Call To Action',
     74                                        'slug'        => 'block-theme-patterns/cta',
     75                                        'description' => '',
     76                                        'categories'  => array( 'call-to-action' ),
    8777                                ),
     78
    8879                        ),
    89                         $transient,
     80                        $theme2->get_pattern_cache(),
    9081                        'The transient for block theme patterns should be set'
    9182                );
     
    10192                        array(
    10293                                'theme'    => 'block-theme',
    103                                 'patterns' => array(
    104                                         'version'  => '1.0.0',
    105                                         'patterns' => array(),
    106                                 ),
     94                                'patterns' => array(),
    10795                        ),
    10896                        array(
    10997                                'theme'    => 'block-theme-child',
    110                                 'patterns' => array(
    111                                         'version'  => '1.0.0',
    112                                         'patterns' => array(),
    113                                 ),
     98                                'patterns' => array(),
    11499                        ),
    115100                        array(
    116101                                'theme'    => 'block-theme-patterns',
    117102                                'patterns' => array(
    118                                         'version'  => '1.0.0',
    119                                         'patterns' => array(
    120                                                 'cta.php' => array(
    121                                                         'title'       => 'Centered Call To Action',
    122                                                         'slug'        => 'block-theme-patterns/cta',
    123                                                         'description' => '',
    124                                                         'categories'  => array( 'call-to-action' ),
    125                                                 ),
     103                                        'cta.php' => array(
     104                                                'title'       => 'Centered Call To Action',
     105                                                'slug'        => 'block-theme-patterns/cta',
     106                                                'description' => '',
     107                                                'categories'  => array( 'call-to-action' ),
    126108                                        ),
    127109                                ),
     
    129111                        array(
    130112                                'theme'    => 'broken-theme',
    131                                 'patterns' => array(
    132                                         'version'  => false,
    133                                         'patterns' => array(),
    134                                 ),
     113                                'patterns' => array(),
    135114                        ),
    136115                        array(
    137116                                'theme'    => 'invalid',
    138                                 'patterns' => array(
    139                                         'version'  => false,
    140                                         'patterns' => array(),
    141                                 ),
     117                                'patterns' => array(),
    142118                        ),
    143119                );
Note: See TracChangeset for help on using the changeset viewer.