Make WordPress Core

Ticket #54910: 54910_cache.diff

File 54910_cache.diff, 1.7 KB (added by costdev, 3 years ago)

Cache the result of WP_Theme->is_block_theme().

  • src/wp-includes/class-wp-theme.php

    diff --git a/src/wp-includes/class-wp-theme.php b/src/wp-includes/class-wp-theme.php
    index 9fa101906a..fd52275dc8 100644
    a b final class WP_Theme implements ArrayAccess { 
    14761476         * @return bool
    14771477         */
    14781478        public function is_block_theme() {
     1479                $is_block_theme = wp_cache_get( 'is_block_theme' );
     1480
     1481                if ( false !== $is_block_theme ) {
     1482                        return (bool) $is_block_theme;
     1483                }
     1484
    14791485                $paths_to_index_block_template = array(
    14801486                        $this->get_file_path( '/block-templates/index.html' ),
    14811487                        $this->get_file_path( '/templates/index.html' ),
    final class WP_Theme implements ArrayAccess { 
    14831489
    14841490                foreach ( $paths_to_index_block_template as $path_to_index_block_template ) {
    14851491                        if ( is_file( $path_to_index_block_template ) && is_readable( $path_to_index_block_template ) ) {
    1486                                 return true;
     1492                                $content = file_get_contents( $path_to_index_block_template );
     1493                                $blocks  = parse_blocks( $content );
     1494
     1495                                if ( $blocks && array_filter( wp_list_pluck( $blocks, 'blockName' ) ) ) {
     1496                                        wp_cache_set( 'is_block_theme', 1 );
     1497                                        return true;
     1498                                }
    14871499                        }
    14881500                }
    14891501
     1502                wp_cache_set( 'is_block_theme', 0 );
     1503
    14901504                return false;
    14911505        }
    14921506
  • src/wp-includes/template.php

    diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php
    index 891e77748d..4d43d69237 100644
    a b function get_query_template( $type, $templates = array() ) { 
    6363
    6464        $template = locate_template( $templates );
    6565
    66         $template = locate_block_template( $template, $type, $templates );
     66        if ( wp_is_block_theme() ) {
     67                $template = locate_block_template( $template, $type, $templates );
     68        }
    6769
    6870        /**
    6971         * Filters the path of the queried template by type.