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; 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. * 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' ); $cache = $this->cache_get( 'theme' );
if ( is_array( $cache ) ) { if ( is_array( $cache ) ) {
foreach ( array( 'errors', 'headers', 'template' ) as $key ) { foreach ( array( 'block_theme', 'errors', 'headers', 'template' ) as $key ) {
if ( isset( $cache[ $key ] ) ) { if ( isset( $cache[ $key ] ) ) {
$this->$key = $cache[ $key ]; $this->$key = $cache[ $key ];
} }
@ -275,14 +283,16 @@ final class WP_Theme implements ArrayAccess {
} else { } else {
$this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
} }
$this->template = $this->stylesheet; $this->template = $this->stylesheet;
$this->block_theme = false;
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'headers' => $this->headers, 'block_theme' => $this->block_theme,
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'template' => $this->template, 'stylesheet' => $this->stylesheet,
'template' => $this->template,
) )
); );
if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one. if ( ! file_exists( $this->theme_root ) ) { // Don't cache this one.
@ -293,13 +303,15 @@ final class WP_Theme implements ArrayAccess {
$this->headers['Name'] = $this->stylesheet; $this->headers['Name'] = $this->stylesheet;
$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
$this->template = $this->stylesheet; $this->template = $this->stylesheet;
$this->block_theme = false;
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'headers' => $this->headers, 'block_theme' => $this->block_theme,
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'template' => $this->template, 'stylesheet' => $this->stylesheet,
'template' => $this->template,
) )
); );
return; return;
@ -327,9 +339,10 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'headers' => $this->headers, 'block_theme' => $this->is_block_theme(),
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'stylesheet' => $this->stylesheet,
) )
); );
@ -363,10 +376,11 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'headers' => $this->headers, 'block_theme' => $this->is_block_theme(),
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'template' => $this->template, 'stylesheet' => $this->stylesheet,
'template' => $this->template,
) )
); );
return; return;
@ -399,10 +413,11 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'headers' => $this->headers, 'block_theme' => $this->is_block_theme(),
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'template' => $this->template, 'stylesheet' => $this->stylesheet,
'template' => $this->template,
) )
); );
$this->parent = new WP_Theme( $this->template, $this->theme_root, $this ); $this->parent = new WP_Theme( $this->template, $this->theme_root, $this );
@ -426,10 +441,11 @@ final class WP_Theme implements ArrayAccess {
$_child->cache_add( $_child->cache_add(
'theme', 'theme',
array( array(
'headers' => $_child->headers, 'block_theme' => $_child->is_block_theme(),
'errors' => $_child->errors, 'headers' => $_child->headers,
'stylesheet' => $_child->stylesheet, 'errors' => $_child->errors,
'template' => $_child->template, 'stylesheet' => $_child->stylesheet,
'template' => $_child->template,
) )
); );
// The two themes actually reference each other with the Template header. // The two themes actually reference each other with the Template header.
@ -445,10 +461,11 @@ final class WP_Theme implements ArrayAccess {
$this->cache_add( $this->cache_add(
'theme', 'theme',
array( array(
'headers' => $this->headers, 'block_theme' => $this->is_block_theme(),
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'template' => $this->template, 'stylesheet' => $this->stylesheet,
'template' => $this->template,
) )
); );
} }
@ -465,10 +482,11 @@ final class WP_Theme implements ArrayAccess {
// We're good. If we didn't retrieve from cache, set it. // We're good. If we didn't retrieve from cache, set it.
if ( ! is_array( $cache ) ) { if ( ! is_array( $cache ) ) {
$cache = array( $cache = array(
'headers' => $this->headers, 'block_theme' => $this->is_block_theme(),
'errors' => $this->errors, 'headers' => $this->headers,
'stylesheet' => $this->stylesheet, 'errors' => $this->errors,
'template' => $this->template, 'stylesheet' => $this->stylesheet,
'template' => $this->template,
); );
// If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above. // If the parent theme is in another root, we'll want to cache this. Avoids an entire branch of filesystem calls above.
if ( isset( $theme_root_template ) ) { if ( isset( $theme_root_template ) ) {
@ -762,6 +780,7 @@ final class WP_Theme implements ArrayAccess {
$this->errors = null; $this->errors = null;
$this->headers_sanitized = null; $this->headers_sanitized = null;
$this->name_translated = null; $this->name_translated = null;
$this->block_theme = null;
$this->headers = array(); $this->headers = array();
$this->__construct( $this->stylesheet, $this->theme_root ); $this->__construct( $this->stylesheet, $this->theme_root );
} }
@ -1491,18 +1510,25 @@ final class WP_Theme implements ArrayAccess {
* @return bool * @return bool
*/ */
public function is_block_theme() { public function is_block_theme() {
if ( isset( $this->block_theme ) ) {
return $this->block_theme;
}
$paths_to_index_block_template = array( $paths_to_index_block_template = array(
$this->get_file_path( '/block-templates/index.html' ), $this->get_file_path( '/block-templates/index.html' ),
$this->get_file_path( '/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 ) { 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 ) ) { 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 * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.