From 4bb2baa857fa231fcca396c3d0cb0ce842e9cef8 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 2 Nov 2022 15:44:36 +0100 Subject: [PATCH 1/5] Limit Global Styles: Invalidate cache after change / during preview --- .../wpcom-global-styles/index.php | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php index fcaddb2957668..d7fe4e3377686 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php @@ -117,10 +117,10 @@ function wpcom_block_global_styles_frontend( $theme_json ) { return $theme_json; } - if ( class_exists( 'WP_Theme_JSON_Data' ) ) { - return new WP_Theme_JSON_Data( array(), 'custom' ); - } elseif ( class_exists( 'WP_Theme_JSON_Data_Gutenberg' ) ) { + if ( class_exists( 'WP_Theme_JSON_Data_Gutenberg' ) ) { return new WP_Theme_JSON_Data_Gutenberg( array(), 'custom' ); + } elseif ( class_exists( 'WP_Theme_JSON_Data' ) ) { + return new WP_Theme_JSON_Data( array(), 'custom' ); } /* @@ -130,7 +130,6 @@ function wpcom_block_global_styles_frontend( $theme_json ) { */ return $theme_json; } -add_filter( 'theme_json_user', 'wpcom_block_global_styles_frontend' ); add_filter( 'wp_theme_json_data_user', 'wpcom_block_global_styles_frontend' ); /** @@ -184,7 +183,14 @@ function wpcom_track_global_styles( $blog_id, $post, $updated ) { * @return bool Returns true if custom styles are in use. */ function wpcom_global_styles_in_use() { - $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( wp_get_theme() ); + $current_theme = wp_get_theme(); + if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) { + $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $current_theme ); + } elseif ( class_exists( 'WP_Theme_JSON_Resolver' ) ) { + $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $current_theme ); + } else { + return false; + } if ( ! isset( $user_cpt['post_content'] ) ) { do_action( 'global_styles_log', 'global_styles_not_in_use' ); @@ -286,3 +292,23 @@ function wpcom_display_global_styles_banner_extra_tooltip() { Date: Wed, 2 Nov 2022 16:47:02 +0100 Subject: [PATCH 2/5] Update apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php Co-authored-by: Richard Ortiz --- .../editing-toolkit-plugin/wpcom-global-styles/index.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php index d7fe4e3377686..539d75c0c2f38 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php @@ -304,11 +304,6 @@ function wpcom_invalidate_global_styles_cache() { add_filter( 'pre_set_transient_' . $transient_name, '__return_false' ); } - add_action( - 'save_post_wp_global_styles', - function () use ( $transient_name ) { - delete_transient( $transient_name ); - } - ); + add_action( 'save_post_wp_global_styles', fn() => delete_transient( $transient_name ) ); } add_action( 'init', 'wpcom_invalidate_global_styles_cache' ); From 2b0bd7788c46adc157c91885a338d854d4bffc0f Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 2 Nov 2022 16:52:13 +0100 Subject: [PATCH 3/5] Add comment explaining why we give priority to Gutenberg classes --- .../editing-toolkit-plugin/wpcom-global-styles/index.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php index 539d75c0c2f38..0ec6910fc3d88 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php @@ -117,6 +117,10 @@ function wpcom_block_global_styles_frontend( $theme_json ) { return $theme_json; } + /* + * Because Gutenberg overrides Core, the order in which we check the existence of + * the classes below is important in order to give Gutenberg priority if available. + */ if ( class_exists( 'WP_Theme_JSON_Data_Gutenberg' ) ) { return new WP_Theme_JSON_Data_Gutenberg( array(), 'custom' ); } elseif ( class_exists( 'WP_Theme_JSON_Data' ) ) { @@ -184,6 +188,11 @@ function wpcom_track_global_styles( $blog_id, $post, $updated ) { */ function wpcom_global_styles_in_use() { $current_theme = wp_get_theme(); + + /* + * Because Gutenberg overrides Core, the order in which we check the existence of + * the classes below is important in order to give Gutenberg priority if available. + */ if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) { $user_cpt = WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_wp_global_styles( $current_theme ); } elseif ( class_exists( 'WP_Theme_JSON_Resolver' ) ) { From 273aeb5972580ffd04a40246464602eb55b05e92 Mon Sep 17 00:00:00 2001 From: mmtr Date: Wed, 2 Nov 2022 18:12:50 +0100 Subject: [PATCH 4/5] Rename variable --- .../editing-toolkit-plugin/wpcom-global-styles/index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php index 0ec6910fc3d88..adfb9cc3b758b 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php @@ -306,13 +306,13 @@ function wpcom_display_global_styles_banner_extra_tooltip() { * Invalidates the cached Global Styles after changing them or when previewing them. */ function wpcom_invalidate_global_styles_cache() { - $transient_name = 'gutenberg_global_styles_' . get_stylesheet(); + $gutenberg_global_styles_transient_name = 'gutenberg_global_styles_' . get_stylesheet(); if ( wpcom_should_limit_global_styles() && wpcom_is_previewing_global_styles() ) { - add_filter( 'pre_transient_' . $transient_name, '__return_null' ); - add_filter( 'pre_set_transient_' . $transient_name, '__return_false' ); + add_filter( 'pre_transient_' . $gutenberg_global_styles_transient_name, '__return_null' ); + add_filter( 'pre_set_transient_' . $gutenberg_global_styles_transient_name, '__return_false' ); } - add_action( 'save_post_wp_global_styles', fn() => delete_transient( $transient_name ) ); + add_action( 'save_post_wp_global_styles', fn() => delete_transient( $gutenberg_global_styles_transient_name ) ); } add_action( 'init', 'wpcom_invalidate_global_styles_cache' ); From aa8cb27a1d8b15a554a35a37f146a95cbc8b4330 Mon Sep 17 00:00:00 2001 From: mmtr Date: Fri, 4 Nov 2022 15:21:14 +0100 Subject: [PATCH 5/5] Also invalidate wp_global_styles_%s cache --- .../wpcom-global-styles/index.php | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php index adfb9cc3b758b..a1334056436b4 100644 --- a/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php +++ b/apps/editing-toolkit/editing-toolkit-plugin/wpcom-global-styles/index.php @@ -306,13 +306,38 @@ function wpcom_display_global_styles_banner_extra_tooltip() { * Invalidates the cached Global Styles after changing them or when previewing them. */ function wpcom_invalidate_global_styles_cache() { + // This is copied from https://github.com/WordPress/gutenberg/blob/0d9d73f3282e4a0bcd4e0400176d65fcbd5dccb8/lib/compat/wordpress-6.0/class-wp-theme-json-resolver-6-0.php#L171-L186. + $args = array( + 'numberposts' => 1, + 'orderby' => 'date', + 'order' => 'desc', + 'post_type' => 'wp_global_styles', + 'post_status' => array( 'publish' ), + 'tax_query' => array( + array( + 'taxonomy' => 'wp_theme', + 'field' => 'name', + 'terms' => wp_get_theme()->get_stylesheet(), + ), + ), + ); + // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize -- needed to mimic Core behavior. + $core_global_styles_cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) ); + + // This is copied from https://github.com/WordPress/gutenberg/blob/9ff5888a1fcd5aa218cbdab5e58c09953bd9bd8b/lib/compat/wordpress-6.1/get-global-styles-and-settings.php#L78. $gutenberg_global_styles_transient_name = 'gutenberg_global_styles_' . get_stylesheet(); - if ( wpcom_should_limit_global_styles() && wpcom_is_previewing_global_styles() ) { + if ( wpcom_is_previewing_global_styles() && wpcom_should_limit_global_styles() ) { add_filter( 'pre_transient_' . $gutenberg_global_styles_transient_name, '__return_null' ); add_filter( 'pre_set_transient_' . $gutenberg_global_styles_transient_name, '__return_false' ); } - add_action( 'save_post_wp_global_styles', fn() => delete_transient( $gutenberg_global_styles_transient_name ) ); + add_action( + 'save_post_wp_global_styles', + function () use ( $core_global_styles_cache_key, $gutenberg_global_styles_transient_name ) { + wp_cache_delete( $core_global_styles_cache_key ); + delete_transient( $gutenberg_global_styles_transient_name ); + } + ); } add_action( 'init', 'wpcom_invalidate_global_styles_cache' );