-
Notifications
You must be signed in to change notification settings - Fork 2k
[WIP] Limit Global Styles: Invalidate cache after change / during preview #69703
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 1 commit
4bb2baa
14b6266
2b0bd77
273aeb5
aa8cb27
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 |
|---|---|---|
|
|
@@ -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() { | |
| <?php | ||
| } | ||
| add_action( 'launch_bar_extra_tooltip_toggle_global_styles', '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(); | ||
mmtr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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_action( | ||
| 'save_post_wp_global_styles', | ||
| function () use ( $transient_name ) { | ||
| delete_transient( $transient_name ); | ||
| } | ||
| ); | ||
mmtr marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
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 would really love if we could actually fix the issue in Gutenberg instead of circumventing it. With this code, we aren't preventing the issue from happening to other components/systems that use global styles. Gutenberg should be purging the cache whenever the Global Styles CPT gets updated.
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. Sure, I'll create an equivalent PR for both Core and Gutenberg tomorrow that will fix https://core.trac.wordpress.org/ticket/56910 and WordPress/gutenberg#43914. But as noted above, we would still need this PR since otherwise we would have the cache problems for weeks.
Contributor
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 think that we can wait, Gating Global Styles isn't live so technically the cache problem doesn't exist yet. There still needs to be some conversations before we can actually resume working on Gating Global Styles. We won't be launching the feature any time soon, but I'm ok with merging this as long as we add a follow-up task to remove the code once the issue is fixed in Core/Gutenberg. I really believe that we aren't in a hurry with this and should prioritize fixing the underlying problem rather than making it work for our specific case and then cleaning up the tech debt, but as I said above I don't mind releasing this as long as we take ownership and clean up after.
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 does exist, since the cache currently prevents all WP.com users from immediately see style changes done in the site editor. Said that, the preferred approach by Core seems to cache the styles forever (instead of a minute) and correctly invalidate them. This makes the invalidation harder, since we need to account for many more scenarios like WordPress updates, theme updates, etc. However, this is currently investigated in this PR that seeks to cache the
Contributor
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 is the key issue we need to address, in my view. Marin and I talked about WordPress/gutenberg#41201 (comment), and I'd love this is addressed in addition to migrate away from transients to object cache.
Contributor
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 also want to share WordPress/gutenberg#45171 for you to see. I've seen many issues with caches, so I created that issue to seek some clarity as to what needs fixing. |
||
| } | ||
| add_action( 'init', 'wpcom_invalidate_global_styles_cache' ); | ||
Uh oh!
There was an error while loading. Please reload this page.