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 eadbc759e0ced..023c03080b9cc 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 4df9ad39dc608..4698ead7f9bc3 100644 --- a/lib/compat/wordpress-6.2/default-filters.php +++ b/lib/compat/wordpress-6.2/default-filters.php @@ -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' ); +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 923c07b3f6e9a..5e2b0a3d6d6f7 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,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() { + _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() { - 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' );