-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Make theme.json object caches non persistent
#46150
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
Changes from all commits
5a9921d
6802de1
b93314e
2f5b23a
6616e9c
cf99597
83af27a
0dd6a78
f654f6f
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 |
|---|---|---|
|
|
@@ -17,18 +17,9 @@ | |
| * @package gutenberg | ||
| */ | ||
|
|
||
| add_action( 'switch_theme', 'wp_theme_has_theme_json_clean_cache' ); | ||
| add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); | ||
| add_action( 'start_previewing_theme', 'wp_theme_has_theme_json_clean_cache' ); | ||
| add_action( 'start_previewing_theme', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); | ||
| add_action( 'upgrader_process_complete', '_wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme', 10, 2 ); | ||
| add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); | ||
| add_action( 'activated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); | ||
| add_action( 'deactivated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); | ||
| add_action( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading', 10, 2 ) ); | ||
| add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); | ||
| add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); | ||
| add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); | ||
| add_action( 'activated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); | ||
| add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); | ||
| add_action( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading', 10, 2 ); | ||
| /** | ||
| * 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' ); | ||
|
Member
Author
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've only left the These new events were introduced in 14.7 (stylesheet PR) when we aimed to make this persistent and can be removed: 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)
Member
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. 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.
Member
Author
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. It requires more changes than that: data cached by
Member
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. Switching themes, mean we do not have cache to clear, as each theme would have it's own cache.
Member
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 agree. However... clearing the cache won't be a bad thing since we'll be saving some memory 👍
Member
Author
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. 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 |
||
| add_action( 'switch_theme', '_gutenberg_clean_theme_json_caches' ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,32 +51,11 @@ function wp_theme_has_theme_json() { | |
| if ( ! function_exists( 'wp_theme_has_theme_json_clean_cache' ) ) { | ||
| /** | ||
| * Function to clean the cache used by wp_theme_has_theme_json method. | ||
| */ | ||
| function wp_theme_has_theme_json_clean_cache() { | ||
| wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); | ||
| } | ||
| } | ||
|
|
||
| if ( ! function_exists( '_wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme' ) ) { | ||
| /** | ||
| * Private function to clean the cache used by wp_theme_has_theme_json method. | ||
| * | ||
| * It is hooked into the `upgrader_process_complete` action. | ||
| * | ||
| * @see default-filters.php | ||
| * | ||
| * @param WP_Upgrader $upgrader Instance of WP_Upgrader class. | ||
| * @param array $options Metadata that identifies the data that is updated. | ||
| * Not to backport to core. Delete it instead. | ||
| */ | ||
| function _wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme( $upgrader, $options ) { | ||
| // The cache only needs cleaning when the active theme was updated. | ||
| if ( | ||
| 'update' === $options['action'] && | ||
| 'theme' === $options['type'] && | ||
| ( isset( $options['themes'][ get_stylesheet() ] ) || isset( $options['themes'][ get_template() ] ) ) | ||
| ) { | ||
| wp_theme_has_theme_json_clean_cache(); | ||
| } | ||
| function wp_theme_has_theme_json_clean_cache() { | ||
|
Member
Author
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. This was introduced in 14.5 at #45543 so I'd rather deprecate it to be safe. |
||
| _deprecated_function( __METHOD__, '14.7' ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -157,38 +136,6 @@ function gutenberg_get_global_stylesheet( $types = array() ) { | |
| return $stylesheet; | ||
| } | ||
|
|
||
| /** | ||
| * Clean the cache used by the `gutenberg_get_global_stylesheet` function. | ||
| */ | ||
| function gutenberg_get_global_stylesheet_clean_cache() { | ||
|
Member
Author
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. 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_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); | ||
| } | ||
|
|
||
| /** | ||
| * Private function to clean the cache used by the `gutenberg_get_global_stylesheet` function after an upgrade. | ||
| * | ||
| * It is hooked into the `upgrader_process_complete` action. | ||
| * | ||
| * @see default-filters.php | ||
| * | ||
| * @param WP_Upgrader $upgrader WP_Upgrader instance. | ||
| * @param array $options Array of bulk item update data. | ||
| */ | ||
| function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, $options ) { | ||
| if ( 'update' !== $options['action'] ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| 'core' === $options['type'] || | ||
| 'plugin' === $options['type'] || | ||
| // Clean cache only if the active theme was updated. | ||
| ( 'theme' === $options['type'] && ( isset( $options['themes'][ get_stylesheet() ] ) || isset( $options['themes'][ get_template() ] ) ) ) | ||
| ) { | ||
| gutenberg_get_global_stylesheet_clean_cache(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Function to get the settings resulting of merging core, theme, and user data. | ||
| * | ||
|
|
@@ -227,3 +174,41 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { | |
| $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); | ||
| return _wp_array_get( $settings, $path, $settings ); | ||
| } | ||
|
|
||
| /** | ||
| * Private function to clean the caches used by gutenberg_get_global_settings method. | ||
| * | ||
| * @access private | ||
| */ | ||
| function _gutenberg_clean_theme_json_caches() { | ||
| wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); | ||
| wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); | ||
| wp_cache_delete( 'gutenberg_get_global_settings_theme', 'theme_json' ); | ||
| WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); | ||
| } | ||
|
|
||
| /** | ||
| * Tell the cache mechanisms not to persist theme.json data across requests. | ||
| * The data stored under this cache group: | ||
| * | ||
| * - wp_theme_has_theme_json | ||
| * - gutenberg_get_global_stylesheet | ||
| * | ||
| * There is some hooks consumers can use to modify parts | ||
| * of the theme.json logic. | ||
| * See https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ | ||
| * | ||
| * The rationale to make this cache group non persistent is to make sure 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, | ||
| * or settings depending on user permissions, etc.). | ||
| * | ||
| * A different alternative considered was to invalidate the cache upon certain | ||
| * events such as options add/update/delete, user meta, etc. | ||
| * It was judged not enough, hence this approach. | ||
| * See https://github.com/WordPress/gutenberg/pull/45372 | ||
| */ | ||
| function _gutenberg_add_non_persistent_theme_json_cache_group() { | ||
| wp_cache_add_non_persistent_groups( 'theme_json' ); | ||
| } | ||
| add_action( 'plugins_loaded', '_gutenberg_add_non_persistent_theme_json_cache_group' ); | ||
Uh oh!
There was an error while loading. Please reload this page.