Make WordPress Core

Changeset 55236

Timestamp:
02/06/2023 07:57:41 PM (18 months 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
     118
     119
     120
     121
     122
     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( '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
    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
    765784        $this->headers           = array();
    766785        $this->__construct( $this->stylesheet, $this->theme_root );
     
    14921511     */
    14931512    public function is_block_theme() {
     1513
     1514
     1515
     1516
    14941517        $paths_to_index_block_template = array(
    14951518            $this->get_file_path( '/block-templates/index.html' ),
     
    14971520        );
    14981521
     1522
     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
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
    279336     * Test get_files for an existing theme.
    280337     *
Note: See TracChangeset for help on using the changeset viewer.