Skip to content
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

[WIP] Limit Global Styles: Invalidate cache after change / during preview #69703

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ 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' ) ) {
/*
* 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' ) ) {
return new WP_Theme_JSON_Data( array(), 'custom' );
mmtr marked this conversation as resolved.
Show resolved Hide resolved
}

/*
Expand All @@ -130,7 +134,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' );

/**
Expand Down Expand Up @@ -184,7 +187,19 @@ 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();

/*
* 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' ) ) {
$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' );
Expand Down Expand Up @@ -286,3 +301,43 @@ 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() {
// 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_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',
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' );