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..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 @@ -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' ); } /* @@ -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' ); /** @@ -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' ); @@ -286,3 +301,43 @@ function wpcom_display_global_styles_banner_extra_tooltip() { 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' );