From 5a9921d9030dc9ac1086aec1faabea1758ec73de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 10:48:42 +0100 Subject: [PATCH 1/9] Make theme_json group non persistent --- .../get-global-styles-and-settings.php | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 923c07b3f6e9a1..abaabc7c7d91aa 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -227,3 +227,29 @@ 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 ); } + +/** + * 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( 'init', '_gutenberg_add_non_persistent_theme_json_cache_group' ); From 6802de12825fd393eabf5eed40cfa332038d5683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:57:12 +0100 Subject: [PATCH 2/9] Sort hooks for readibility --- lib/compat/wordpress-6.2/default-filters.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 4df9ad39dc6084..64218d879d7c43 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -17,18 +17,18 @@ * @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', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'activated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); +add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); 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( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); 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( '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( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); +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( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading', 10, 2 ); +add_action( 'upgrader_process_complete', '_wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme', 10, 2 ); +add_action( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading', 10, 2 ) ); From b93314ecc3636d577aa404b0f479a1ee604454f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 11:59:45 +0100 Subject: [PATCH 3/9] No need to delete cache keys upon upgrading if it is non persistent --- lib/compat/wordpress-6.2/default-filters.php | 2 - .../get-global-styles-and-settings.php | 48 ------------------- 2 files changed, 50 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 64218d879d7c43..6ad00a45f53e33 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -29,6 +29,4 @@ add_action( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); 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( 'upgrader_process_complete', '_gutenberg_get_global_stylesheet_clean_cache_upon_upgrading', 10, 2 ); -add_action( 'upgrader_process_complete', '_wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme', 10, 2 ); add_action( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading', 10, 2 ) ); diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index abaabc7c7d91aa..78e82981ee31a2 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -57,29 +57,6 @@ function wp_theme_has_theme_json_clean_cache() { } } -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. - */ - 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(); - } - } -} - /** * Returns the stylesheet resulting of merging core, theme, and user data. * @@ -164,31 +141,6 @@ function gutenberg_get_global_stylesheet_clean_cache() { 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. * From 2f5b23a161cdf8beb2312d4fa6ab516f32202590 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:00:10 +0100 Subject: [PATCH 4/9] No need to delete cache keys upon plugin de/activation if it is non persistent --- lib/compat/wordpress-6.2/default-filters.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 6ad00a45f53e33..42ab5688088823 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -17,9 +17,7 @@ * @package gutenberg */ -add_action( 'activated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'activated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); -add_action( 'deactivated_plugin', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'deactivated_plugin', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); From 6616e9cf9b6227f527c49c8ab1bf7cdd712f961d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:02:14 +0100 Subject: [PATCH 5/9] No need to delete cache keys upon post saving if it is non persistent --- lib/compat/wordpress-6.2/default-filters.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 42ab5688088823..701d61131611c3 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -19,7 +19,6 @@ 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( 'save_post_wp_global_styles', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); add_action( 'start_previewing_theme', 'wp_theme_has_theme_json_clean_cache' ); From cf99597de286ad4fcef996efd58d843561d9e920 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:03:22 +0100 Subject: [PATCH 6/9] Remove filters for clearing WP_Theme_JSON_Resolver cache - upon plugin de/activation - upon upgrading core, plugins, themes - upon saving global styles posts --- .../class-wp-theme-json-resolver-6-2.php | 25 ------------------- lib/compat/wordpress-6.2/default-filters.php | 4 --- 2 files changed, 29 deletions(-) diff --git a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php index eadbc759e0ced1..023c03080b9cce 100644 --- a/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php +++ b/lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php @@ -32,31 +32,6 @@ public static function theme_has_support() { return wp_theme_has_theme_json(); } - /** - * Private method to clean the cached data 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. - */ - public static function _clean_cached_data_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() ] ) ) ) - ) { - static::clean_cached_data(); - } - } - /** * Returns the data merged from multiple origins. * diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 701d61131611c3..94cfc343e414d5 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -17,13 +17,9 @@ * @package gutenberg */ -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( 'save_post_wp_global_styles', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); 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( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); 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( 'upgrader_process_complete', array( 'WP_Theme_JSON_Resolver_Gutenberg', '_clean_cached_data_upon_upgrading', 10, 2 ) ); From 83af27a42182cfc60cc3ed86b5ab74b4442c55e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:51:39 +0100 Subject: [PATCH 7/9] Deprecate old methods to clear the cache --- lib/compat/wordpress-6.2/default-filters.php | 12 +++++------ .../get-global-styles-and-settings.php | 20 +++++++++++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/compat/wordpress-6.2/default-filters.php b/lib/compat/wordpress-6.2/default-filters.php index 94cfc343e414d5..4698ead7f9bc30 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -17,9 +17,9 @@ * @package gutenberg */ -add_action( 'start_previewing_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); -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( 'switch_theme', 'gutenberg_get_global_stylesheet_clean_cache' ); -add_action( 'switch_theme', 'wp_theme_has_theme_json_clean_cache' ); -add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver_Gutenberg', 'clean_cached_data' ) ); +/** + * 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' ); +add_action( 'switch_theme', '_gutenberg_clean_theme_json_caches' ); diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 78e82981ee31a2..727b7893ccc284 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -51,9 +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. + * + * Not to backport to core. Delete it instead. */ function wp_theme_has_theme_json_clean_cache() { - wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); + _deprecated_function( __METHOD__, '14.7' ); } } @@ -136,9 +138,11 @@ function gutenberg_get_global_stylesheet( $types = array() ) { /** * Clean the cache used by the `gutenberg_get_global_stylesheet` function. + * + * Not to backport to core. Delete it instead. */ function gutenberg_get_global_stylesheet_clean_cache() { - wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); + _deprecated_function( __METHOD__, '14.7' ); } /** @@ -180,6 +184,18 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { 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: From 0dd6a782343833925c65b23c3f35aec593e496c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 12:59:48 +0100 Subject: [PATCH 8/9] Remove method as it was introduced in this same release --- .../wordpress-6.2/get-global-styles-and-settings.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 727b7893ccc284..89d396de08b0e4 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -136,15 +136,6 @@ function gutenberg_get_global_stylesheet( $types = array() ) { return $stylesheet; } -/** - * Clean the cache used by the `gutenberg_get_global_stylesheet` function. - * - * Not to backport to core. Delete it instead. - */ -function gutenberg_get_global_stylesheet_clean_cache() { - _deprecated_function( __METHOD__, '14.7' ); -} - /** * Function to get the settings resulting of merging core, theme, and user data. * From f654f6f99da1262a11f4b61586f93faddb1c23b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 29 Nov 2022 13:14:41 +0100 Subject: [PATCH 9/9] Use plugins_loaded instead of init as hook --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 89d396de08b0e4..5e2b0a3d6d6f75 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -211,4 +211,4 @@ function _gutenberg_clean_theme_json_caches() { 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' ); +add_action( 'plugins_loaded', '_gutenberg_add_non_persistent_theme_json_cache_group' );