Make WordPress Core

Changeset 55236


Ignore:
Timestamp:
02/06/2023 07:57:41 PM (4 years ago)
Author:
flixos90
Message:

Themes: Add caching to WP_Theme::is_block_theme().

This changeset adds a block_theme entry in the theme cache data, similar to the existing entries headers, errors, stylesheet, and template.

Props spacedmonkey, costdev, joemcgill, flixos90, mukesh27, adamsilverstein.
Fixes #57114.

Location:
trunk
Files:
2 edited

Legend:

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

    r54236 r55236  
    115115
    116116        /**
     117         * Is this theme a block theme.
     118         *
     119         * @since 6.2.0
     120         * @var bool
     121         */
     122        private $block_theme;
     123
     124        /**
    117125         * Header name from the theme's style.css after being translated.
    118126         *
     
    251259
    252260                if ( is_array( $cache ) ) {
    253                         foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
     261                        foreach ( array( 'block_theme', 'errors', 'headers', 'template' ) as $key ) {
    254262                                if ( isset( $cache[ $key ] ) ) {
    255263                                        $this->$key = $cache[ $key ];
     
    276284                                $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
    277285                        }
    278                         $this->template = $this->stylesheet;
     286                        $this->template    = $this->stylesheet;
     287                        $this->block_theme = false;
    279288                        $this->cache_add(
    280289                                'theme',
    281290                                array(
    282                                         'headers'    => $this->headers,
    283                                         'errors'     => $this->errors,
    284                                         'stylesheet' => $this->stylesheet,
    285                                         'template'   => $this->template,
     291                                        'block_theme' => $this->block_theme,
     292                                        'headers'     => $this->headers,
     293                                        'errors'      => $this->errors,
     294                                        'stylesheet'  => $this->stylesheet,
     295                                        'template'    => $this->template,
    286296                                )
    287297                        );
     
    294304                        $this->errors          = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
    295305                        $this->template        = $this->stylesheet;
     306                        $this->block_theme     = false;
    296307                        $this->cache_add(
    297308                                'theme',
    298309                                array(
    299                                         'headers'    => $this->headers,
    300                                         'errors'     => $this->errors,
    301                                         'stylesheet' => $this->stylesheet,
    302                                         'template'   => $this->template,
     310                                        'block_theme' => $this->block_theme,
     311                                        'headers'     => $this->headers,
     312                                        'errors'      => $this->errors,
     313                                        'stylesheet'  => $this->stylesheet,
     314                                        'template'    => $this->template,
    303315                                )
    304316                        );
     
    328340                                'theme',
    329341                                array(
    330                                         'headers'    => $this->headers,
    331                                         'errors'     => $this->errors,
    332                                         'stylesheet' => $this->stylesheet,
     342                                        'block_theme' => $this->is_block_theme(),
     343                                        'headers'     => $this->headers,
     344                                        'errors'      => $this->errors,
     345                                        'stylesheet'  => $this->stylesheet,
    333346                                )
    334347                        );
     
    364377                                        'theme',
    365378                                        array(
    366                                                 'headers'    => $this->headers,
    367                                                 'errors'     => $this->errors,
    368                                                 'stylesheet' => $this->stylesheet,
    369                                                 'template'   => $this->template,
     379                                                'block_theme' => $this->is_block_theme(),
     380                                                'headers'     => $this->headers,
     381                                                'errors'      => $this->errors,
     382                                                'stylesheet'  => $this->stylesheet,
     383                                                'template'    => $this->template,
    370384                                        )
    371385                                );
     
    400414                                        'theme',
    401415                                        array(
    402                                                 'headers'    => $this->headers,
    403                                                 'errors'     => $this->errors,
    404                                                 'stylesheet' => $this->stylesheet,
    405                                                 'template'   => $this->template,
     416                                                'block_theme' => $this->is_block_theme(),
     417                                                'headers'     => $this->headers,
     418                                                'errors'      => $this->errors,
     419                                                'stylesheet'  => $this->stylesheet,
     420                                                'template'    => $this->template,
    406421                                        )
    407422                                );
     
    427442                                        'theme',
    428443                                        array(
    429                                                 'headers'    => $_child->headers,
    430                                                 'errors'     => $_child->errors,
    431                                                 'stylesheet' => $_child->stylesheet,
    432                                                 'template'   => $_child->template,
     444                                                'block_theme' => $_child->is_block_theme(),
     445                                                'headers'     => $_child->headers,
     446                                                'errors'      => $_child->errors,
     447                                                'stylesheet'  => $_child->stylesheet,
     448                                                'template'    => $_child->template,
    433449                                        )
    434450                                );
     
    446462                                                'theme',
    447463                                                array(
    448                                                         'headers'    => $this->headers,
    449                                                         'errors'     => $this->errors,
    450                                                         'stylesheet' => $this->stylesheet,
    451                                                         'template'   => $this->template,
     464                                                        'block_theme' => $this->is_block_theme(),
     465                                                        'headers'     => $this->headers,
     466                                                        'errors'      => $this->errors,
     467                                                        'stylesheet'  => $this->stylesheet,
     468                                                        'template'    => $this->template,
    452469                                                )
    453470                                        );
     
    466483                if ( ! is_array( $cache ) ) {
    467484                        $cache = array(
    468                                 'headers'    => $this->headers,
    469                                 'errors'     => $this->errors,
    470                                 'stylesheet' => $this->stylesheet,
    471                                 'template'   => $this->template,
     485                                'block_theme' => $this->is_block_theme(),
     486                                'headers'     => $this->headers,
     487                                'errors'      => $this->errors,
     488                                'stylesheet'  => $this->stylesheet,
     489                                'template'    => $this->template,
    472490                        );
    473491                        // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above.
     
    763781                $this->headers_sanitized = null;
    764782                $this->name_translated   = null;
     783                $this->block_theme       = null;
    765784                $this->headers           = array();
    766785                $this->__construct( $this->stylesheet, $this->theme_root );
     
    14921511         */
    14931512        public function is_block_theme() {
     1513                if ( isset( $this->block_theme ) ) {
     1514                        return $this->block_theme;
     1515                }
     1516
    14941517                $paths_to_index_block_template = array(
    14951518                        $this->get_file_path( '/block-templates/index.html' ),
     
    14971520                );
    14981521
     1522                $this->block_theme = false;
     1523
    14991524                foreach ( $paths_to_index_block_template as $path_to_index_block_template ) {
    15001525                        if ( is_file( $path_to_index_block_template ) && is_readable( $path_to_index_block_template ) ) {
    1501                                 return true;
    1502                         }
    1503                 }
    1504 
    1505                 return false;
     1526                                $this->block_theme = true;
     1527                                break;
     1528                        }
     1529                }
     1530
     1531                return $this->block_theme;
    15061532        }
    15071533
  • trunk/tests/phpunit/tests/theme/wpTheme.php

    r53933 r55236  
    277277
    278278        /**
     279         * @ticket 57114
     280         *
     281         * @covers WP_Theme::is_block_theme
     282         *
     283         * @dataProvider data_is_block_theme
     284         */
     285        public function test_is_block_theme_property( $theme_dir, $expected ) {
     286                $theme = new WP_Theme( $theme_dir, $this->theme_root );
     287                $theme->is_block_theme();
     288                $reflection          = new ReflectionClass( $theme );
     289                $reflection_property = $reflection->getProperty( 'block_theme' );
     290                $reflection_property->setAccessible( true );
     291
     292                $this->assertSame( $expected, $reflection_property->getValue( $theme ) );
     293        }
     294
     295        /**
     296         * @ticket 57114
     297         *
     298         * @covers WP_Theme::is_block_theme
     299         * @covers WP_Theme::cache_get
     300         */
     301        public function test_is_block_theme_check_cache() {
     302                $filter = new MockAction();
     303                add_filter( 'theme_file_path', array( $filter, 'filter' ) );
     304
     305                $theme1 = new WP_Theme( 'block-theme', $this->theme_root );
     306                // First run.
     307                $this->assertTrue( $theme1->is_block_theme(), 'is_block_theme should return true on first run' );
     308
     309                $theme2 = new WP_Theme( 'block-theme', $this->theme_root );
     310                // Second run.
     311                $this->assertTrue( $theme2->is_block_theme(), 'is_block_theme should return true on second run' );
     312                $this->assertCount( 0, $filter->get_events(), 'Should only be 0, as second run should be cached' );
     313        }
     314
     315        /**
     316         * @ticket 57114
     317         *
     318         * @covers WP_Theme::is_block_theme
     319         * @covers WP_Theme::cache_delete
     320         */
     321        public function test_is_block_theme_delete_cache() {
     322                $filter = new MockAction();
     323                add_filter( 'theme_file_path', array( $filter, 'filter' ) );
     324
     325                $theme = new WP_Theme( 'block-theme', $this->theme_root );
     326                // First run.
     327                $this->assertTrue( $theme->is_block_theme(), 'is_block_theme should return true on first run' );
     328                // Clear cache.
     329                $theme->cache_delete();
     330                // Second run.
     331                $this->assertTrue( $theme->is_block_theme(), 'is_block_theme should return true on second run' );
     332                $this->assertCount( 2, $filter->get_events(), 'Should only be 4, as second run should not be cached' );
     333        }
     334
     335        /**
    279336         * Test get_files for an existing theme.
    280337         *
Note: See TracChangeset for help on using the changeset viewer.