-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Make theme.json
object caches non persistent
#46150
Conversation
Personally, I'd like to revisit this decision later, and try using a persistent approach plus flushing the cache upon certain site events (plugin de/activation, options add/update/remove, etc.). Given the great gains of #45372 I'd rather prioritize shipping it and look into this later. |
function _gutenberg_add_non_persistent_theme_json_cache_group() { | ||
wp_cache_add_non_persistent_groups( 'theme_json' ); | ||
} | ||
add_action( 'init', '_gutenberg_add_non_persistent_theme_json_cache_group' ); |
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.
I'm not sure what hook would be best to set this group to non-persistent. Any ideas?
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.
Maybe plugins_loaded
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.
🤔 At that point, any plugin that implement persistent cache should be loaded, hence calling this method should be effective. Sounds good.
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.
- upon plugin de/activation - upon upgrading core, plugins, themes - upon saving global styles posts
bb0266f
to
83af27a
Compare
* When backporting to core, the existing filters hooked to WP_Theme_JSON_Resolver::clean_cached_data() | ||
* need to be removed. | ||
*/ | ||
add_action( 'start_previewing_theme', '_gutenberg_clean_theme_json_caches' ); |
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.
I've only left the start_previewing_theme
and switch_theme
events as they are necessary for certain flows (preview themes, theme directory) that may access the theme data before they are dispatched.
These new events were introduced in 14.7 (stylesheet PR) when we aimed to make this persistent and can be removed: activated_plugin
, deactivated_plugin
, upgrader_process_complete
.
With this change, all caches need the same invalidation logic, so I've consolidated it into a single place as per feedback at #45372 (comment)
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.
If we merge #45955, it will mean that no cache invalidation will be needed for start_previewing_theme / switch_theme, as the act of changing / filter the stylesheet option, will cache the cache key.
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.
It requires more changes than that: data cached by wp_theme_has_theme_json
, gutenberg_get_global_styles
, and WP_Theme_JSON_Resolver
all needs to be cached by stylesheet. If/When we do that, we can remove these hooks.
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.
Switching themes, mean we do not have cache to clear, as each theme would have it's own cache.
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.
Switching themes, mean we do not have cache to clear, as each theme would have it's own cache.
I agree. However... clearing the cache won't be a bad thing since we'll be saving some memory 👍
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.
My point is that to be able to remove these hooks, we need we make changes to those three places. It cannot be done in this PR or #45955 that only addresses wp_theme_has_theme_json
as far as I could see.
/** | ||
* Clean the cache used by the `gutenberg_get_global_stylesheet` function. | ||
*/ | ||
function gutenberg_get_global_stylesheet_clean_cache() { |
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.
This was introduced at #45679 and is slated for the 14.7 release. I've marked this PR for that release as well, so we can remove it. If this does not make it to the 14.7 we should deprecate it instead.
) { | ||
wp_theme_has_theme_json_clean_cache(); | ||
} | ||
function wp_theme_has_theme_json_clean_cache() { |
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.
This was introduced in 14.5 at #45543 so I'd rather deprecate it to be safe.
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.
LGTM
The main bulk of this PR is being backported at WordPress/wordpress-develop#3712 |
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file. For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.). Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`. References: * [WordPress/gutenberg#45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`. * [WordPress/gutenberg#45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`. * [WordPress/gutenberg#46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent. * [WordPress/gutenberg#45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined. * [WordPress/gutenberg#45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`. Follow-up to [54493], [53282], [52744], [52049], [50959]. Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey. Fixes #56975. git-svn-id: https://develop.svn.wordpress.org/trunk@55086 602fd350-edb4-49c9-b593-d223f7449a82
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file. For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.). Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`. References: * [WordPress/gutenberg#45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`. * [WordPress/gutenberg#45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`. * [WordPress/gutenberg#46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent. * [WordPress/gutenberg#45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined. * [WordPress/gutenberg#45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`. Follow-up to [54493], [53282], [52744], [52049], [50959]. Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey. Fixes #56975. Built from https://develop.svn.wordpress.org/trunk@55086 git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file. For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.). Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`. References: * [WordPress/gutenberg#45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`. * [WordPress/gutenberg#45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`. * [WordPress/gutenberg#46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent. * [WordPress/gutenberg#45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined. * [WordPress/gutenberg#45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`. Follow-up to [54493], [53282], [52744], [52049], [50959]. Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey. Fixes #56975. Built from https://develop.svn.wordpress.org/trunk@55086 git-svn-id: https://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Adds `wp_theme_has_theme_json()` for public consumption, to replace the private internal Core-only `WP_Theme_JSON_Resolver::theme_has_support()` method. This new global function checks if a theme or its parent has a `theme.json` file. For performance, results are cached as an integer `1` or `0` in the `'theme_json'` group with `'wp_theme_has_theme_json'` key. This is a non-persistent cache. Why? To make the derived data from `theme.json` is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.). Also adds a new public function `wp_clean_theme_json_cache()` to clear the cache on `'switch_theme'` and `start_previewing_theme'`. References: * [WordPress/gutenberg#45168 Gutenberg PR 45168] Add `wp_theme_has_theme_json` as a public API to know whether a theme has a `theme.json`. * [WordPress/gutenberg#45380 Gutenberg PR 45380] Deprecate `WP_Theme_JSON_Resolver:theme_has_support()`. * [WordPress/gutenberg#46150 Gutenberg PR 46150] Make `theme.json` object caches non-persistent. * [WordPress/gutenberg#45979 Gutenberg PR 45979] Don't check if constants set by `wp_initial_constants()` are defined. * [WordPress/gutenberg#45950 Gutenberg PR 45950] Cleaner logic in `wp_theme_has_theme_json`. Follow-up to [54493], [53282], [52744], [52049], [50959]. Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey. Fixes #56975. Built from https://develop.svn.wordpress.org/trunk@55086 git-svn-id: http://core.svn.wordpress.org/trunk@54619 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Related #45372
Part of #45171
What?
This PR makes the object caches in use for theme.json related data non-persistent.
Why?
Upon some conversations at #45372 people find that there are a couple of cases for which the invalidation of cache may be not enough:
To unblock that PR, this makes the data non-persistent.
How?
theme_json
to the list of cache groups not to persist.Testing Instructions