Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cache theme supports data for use when caching merged data
  • Loading branch information
joemcgill committed Sep 18, 2023
commit 6811e6d1c22ade5dcb9be77f83c130674a7cf9ed
39 changes: 36 additions & 3 deletions src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class WP_Theme_JSON_Resolver {
*/
protected static $theme = null;

/**
* Container to cache theme support data.
*
* @since n.e.x.t
*/
protected static $theme_support_data = null;

/**
* Container for data coming from the user.
*
Expand Down Expand Up @@ -293,13 +300,37 @@ public static function get_theme_data( $deprecated = array(), $options = array()
return static::$theme;
}

$theme_support_data = self::get_theme_supports_data();

// If no support data has changed, return the previously cached object.
if ( $theme_support_data === static::$theme_support_data ) {
return static::$theme;
}

// Cache the merged theme support data for future use.
static::$theme_support_data = $theme_support_data;

$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
$with_theme_supports->merge( static::$theme );
return $with_theme_supports;
}

/**
* Get merged theme supports data
*
* @since n.e.x.t
*
* @return array Config that adheres to the theme.json schema.
*/
private static function get_theme_supports_data() {
/*
* We want the presets and settings declared in theme.json
* to override the ones declared via theme supports.
* So we take theme supports, transform it to theme.json shape
* and merge the static::$theme upon that.
*/
$theme_support_data = WP_Theme_JSON::get_from_editor_settings( get_classic_theme_supports_block_editor_settings() );

if ( ! wp_theme_has_theme_json() ) {
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
$theme_support_data['settings']['color'] = array();
Expand Down Expand Up @@ -341,9 +372,8 @@ public static function get_theme_data( $deprecated = array(), $options = array()
$theme_support_data['settings']['border']['width'] = true;
}
}
$with_theme_supports = new WP_Theme_JSON( $theme_support_data );
$with_theme_supports->merge( static::$theme );
return $with_theme_supports;

return $theme_support_data;
}

/**
Expand Down Expand Up @@ -585,6 +615,8 @@ public static function get_user_data() {
* @return WP_Theme_JSON
*/
public static function get_merged_data( $origin = 'custom' ) {
global $_wp_theme_features;

if ( is_array( $origin ) ) {
_deprecated_argument( __FUNCTION__, '5.9.0' );
}
Expand All @@ -604,6 +636,7 @@ public static function get_merged_data( $origin = 'custom' ) {
if (
null !== static::$merged[ $origin ]
&& static::has_same_registered_blocks( $cache_map[ $origin ] )
&& static::get_theme_supports_data() === static::$theme_support_data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theme support data only matters for the theme and custom origins, so I think we should only apply this check if the given origin is one of those, to keep caches warm for the other two origins.

) {
return static::$merged[ $origin ];
}
Expand Down