-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add object caching for WP_Theme_JSON::compute_style_properties
#5567
base: trunk
Are you sure you want to change the base?
Changes from all commits
85e9170
7450d26
c9b36ea
dd8c83e
7d97433
bd6f08c
2377348
d707a79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1934,6 +1934,15 @@ protected static function compute_style_properties( $styles, $settings = array() | |||||||||
return $declarations; | ||||||||||
} | ||||||||||
|
||||||||||
// The global settings can include dynamic data related to typography. We need evaluate it so that the cache is invalidated when it changes. | ||||||||||
$args = array( func_get_args(), wp_get_global_settings() ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed | ||||||||||
$cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); | ||||||||||
Comment on lines
+1938
to
+1939
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion/nitpick: rename
Suggested change
|
||||||||||
$cache = wp_cache_get( $cache_key, 'wp-styles' ); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than introducing a new cache group, I think this should be a part of the existing
Suggested change
|
||||||||||
|
||||||||||
if ( $cache ) { | ||||||||||
return $cache; | ||||||||||
} | ||||||||||
|
||||||||||
$root_variable_duplicates = array(); | ||||||||||
|
||||||||||
foreach ( $properties as $css_property => $value_path ) { | ||||||||||
|
@@ -2000,6 +2009,8 @@ protected static function compute_style_properties( $styles, $settings = array() | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
wp_cache_set( $cache_key, $declarations, 'wp-styles', DAY_IN_SECONDS ); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm unsure about the best expiration time here. If there is a side effect that we've not accounted for in the cache key value, showing stale styles for a full day without flushing the cache is pretty long. Maybe limiting this to once an hour for now would be safer?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Theme json is a none presient cache, so there is no need for an expiry here. |
||||||||||
|
||||||||||
return $declarations; | ||||||||||
} | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way the theme.json code is organized is as follows:
WP_Theme_JSON
: private class that deals with atheme.json
structure. It doesn't depend on anything else.WP_Theme_JSON_Resolver
: private class that pulls data (theme.json files, theme.json from database) and merges it together appropriately (get_merged_data
). It depends on theWP_Theme_JSON
class.wp_get_global_settings
,wp_get_global_styles
, etc. They use and depend on the two previous private classes.By using a public function such as
wp_get_global_settings
in theWP_Theme_JSON
class, this code is creating a circular dependency. This code is saying: when processing any theme.json structure, read all existing theme.json (core, blocks, theme, user) first.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback @oandregal. This makes perfect sense. I think we would either need to add the caching to the public function that relies on
WP_Theme_JSON::compute_style_properties
or find a different approach to invalidation withinWP_Theme_JSON
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback @oandregal. I'll have a closer look at this