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.

Built from https://develop.svn.wordpress.org/trunk@55236


git-svn-id: http://core.svn.wordpress.org/trunk@54769 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2023-02-06 19:59:17 +00:00
parent ee7b88a862
commit 8a805daa9a
2 changed files with 62 additions and 36 deletions

View File

@ -113,6 +113,14 @@ final class WP_Theme implements ArrayAccess {
*/
private $headers_sanitized;
/**
* Is this theme a block theme.
*
* @since 6.2.0
* @var bool
*/
private $block_theme;
/**
* Header name from the theme's style.css after being translated.
*
@ -250,7 +258,7 @@ final class WP_Theme implements ArrayAccess {
$cache = $this->cache_get( 'theme' );
if ( is_array( $cache ) ) {
foreach ( array( 'errors', 'headers', 'template' ) as $key ) {
foreach ( array( 'block_theme', 'errors', 'headers', 'template' ) as $key ) {
if ( isset( $cache[ $key ] ) ) {
$this->$key = $cache[ $key ];
}
@ -276,9 +284,11 @@ final class WP_Theme implements ArrayAccess {
$this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
}
$this->template = $this->stylesheet;
$this->block_theme = false;
$this->cache_add(
'theme',
array(
'block_theme' => $this->block_theme,
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -293,9 +303,11 @@ final class WP_Theme implements ArrayAccess {
$this->headers['Name'] = $this->stylesheet;
$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
$this->template = $this->stylesheet;
$this->block_theme = false;
$this->cache_add(
'theme',
array(
'block_theme' => $this->block_theme,
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -327,6 +339,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add(
'theme',
array(
'block_theme' => $this->is_block_theme(),
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -363,6 +376,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add(
'theme',
array(
'block_theme' => $this->is_block_theme(),
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -399,6 +413,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add(
'theme',
array(
'block_theme' => $this->is_block_theme(),
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -426,6 +441,7 @@ final class WP_Theme implements ArrayAccess {
$_child->cache_add(
'theme',
array(
'block_theme' => $_child->is_block_theme(),
'headers' => $_child->headers,
'errors' => $_child->errors,
'stylesheet' => $_child->stylesheet,
@ -445,6 +461,7 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add(
'theme',
array(
'block_theme' => $this->is_block_theme(),
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -465,6 +482,7 @@ final class WP_Theme implements ArrayAccess {
// We're good. If we didn't retrieve from cache, set it.
if ( ! is_array( $cache ) ) {
$cache = array(
'block_theme' => $this->is_block_theme(),
'headers' => $this->headers,
'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
@ -762,6 +780,7 @@ final class WP_Theme implements ArrayAccess {
$this->errors = null;
$this->headers_sanitized = null;
$this->name_translated = null;
$this->block_theme = null;
$this->headers = array();
$this->__construct( $this->stylesheet, $this->theme_root );
}
@ -1491,18 +1510,25 @@ final class WP_Theme implements ArrayAccess {
* @return bool
*/
public function is_block_theme() {
if ( isset( $this->block_theme ) ) {
return $this->block_theme;
}
$paths_to_index_block_template = array(
$this->get_file_path( '/block-templates/index.html' ),
$this->get_file_path( '/templates/index.html' ),
);
$this->block_theme = false;
foreach ( $paths_to_index_block_template as $path_to_index_block_template ) {
if ( is_file( $path_to_index_block_template ) && is_readable( $path_to_index_block_template ) ) {
return true;
$this->block_theme = true;
break;
}
}
return false;
return $this->block_theme;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-55235';
$wp_version = '6.2-alpha-55236';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.