From b7ba582722c08889064da8f1df6fc5f6a17dc9ef Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 15 Apr 2024 11:14:05 +0100 Subject: [PATCH 01/34] add caching --- src/wp-includes/class-wp-theme-json.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 7ab626ce8f96e..2a33bfa68b984 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2053,6 +2053,16 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } + // The global settings can include dynamic data related to typography. We need evaluate it so that the cache is invalidated when it changes. + $cache_args = array( func_get_args(), wp_get_global_settings() ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed + $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $cache_args ) ); + $cache_group = 'theme_json'; + $cache = wp_cache_get( $cache_key, $cache_group ); + + if ( $cache ) { + return $cache; + } + $root_variable_duplicates = array(); foreach ( $properties as $css_property => $value_path ) { @@ -2128,6 +2138,8 @@ protected static function compute_style_properties( $styles, $settings = array() } } + wp_cache_set( $cache_key, $declarations, $cache_group ); + return $declarations; } From 3bba97f64b17f69692912faff6c9605221c2c598 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 15 Apr 2024 12:23:15 +0100 Subject: [PATCH 02/34] transient implementation --- src/wp-includes/class-wp-theme-json.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 2a33bfa68b984..8997974f39b04 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2054,10 +2054,9 @@ protected static function compute_style_properties( $styles, $settings = array() } // The global settings can include dynamic data related to typography. We need evaluate it so that the cache is invalidated when it changes. - $cache_args = array( func_get_args(), wp_get_global_settings() ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed - $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $cache_args ) ); - $cache_group = 'theme_json'; - $cache = wp_cache_get( $cache_key, $cache_group ); + $cache_args = array( func_get_args(), wp_get_global_settings() ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed + $cache_key = 'theme_json_compute_style_properties_' . md5( wp_json_encode( $cache_args ) ); + $cache = get_site_transient( $cache_key ); if ( $cache ) { return $cache; @@ -2138,7 +2137,7 @@ protected static function compute_style_properties( $styles, $settings = array() } } - wp_cache_set( $cache_key, $declarations, $cache_group ); + set_site_transient( $cache_key, $declarations, HOUR_IN_SECONDS ); return $declarations; } From 97806ea81d87626c4934c08f08556305085d6673 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 01:19:30 +0100 Subject: [PATCH 03/34] Improve caching key computation --- src/wp-includes/class-wp-theme-json.php | 43 ++++++++++++++++--- .../global-styles-and-settings.php | 1 + 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 8997974f39b04..e00dc0f41b74c 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2053,13 +2053,20 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } - // The global settings can include dynamic data related to typography. We need evaluate it so that the cache is invalidated when it changes. - $cache_args = array( func_get_args(), wp_get_global_settings() ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed - $cache_key = 'theme_json_compute_style_properties_' . md5( wp_json_encode( $cache_args ) ); - $cache = get_site_transient( $cache_key ); + $can_use_cached = ! wp_is_development_mode( 'theme' ); + if ( $can_use_cached ) { + $func_args_hash = md5( wp_json_encode( func_get_args() ) ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed + /* + * The global settings can include dynamic data related to typography. + * We need to evaluate it so that the cache is invalidated when it changes. + * Computing global settings hash is costly, so we cache it in a non-persistent cache. + */ + $cache_key = "compute_style_properties_{$func_args_hash}_" . $this->wp_get_global_settings_hash(); + $cache = get_site_transient( $cache_key ); - if ( $cache ) { - return $cache; + if ( $cache ) { + return $cache; + } } $root_variable_duplicates = array(); @@ -2137,11 +2144,33 @@ protected static function compute_style_properties( $styles, $settings = array() } } - set_site_transient( $cache_key, $declarations, HOUR_IN_SECONDS ); + if ( $can_use_cached ) { + set_site_transient( $cache_key, $declarations, HOUR_IN_SECONDS ); + } return $declarations; } + /** + * Gets the hash of the global settings. + * + * @since 6.6.0 + * + * @return string The hash of the global settings. + */ + private function wp_get_global_settings_hash() { + $cache_group = 'theme_json'; + $cache_key = 'wp_get_global_settings_hash'; + $cache = wp_cache_get( $cache_key, $cache_group ); + if ( $cache ) { + return $cache; + } + + $hash = md5( wp_json_encode( $cache_args ) ); + wp_cache_set( $cache_key, $hash, $cache_group ); + return $hash; + } + /** * Returns the style property for the given path. * diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index fae7ae6ffb6ea..0e89f44d1b717 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -445,6 +445,7 @@ function wp_theme_has_theme_json() { function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' ); + wp_cache_delete( 'wp_get_global_settings_hash', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); From 341e21865a06a770b98f22449fa1737b737364d5 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 02:53:26 +0100 Subject: [PATCH 04/34] improving performace for non persistance cache. --- src/wp-includes/class-wp-theme-json.php | 62 ++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index e00dc0f41b74c..3b6b57f13d738 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -38,6 +38,14 @@ class WP_Theme_JSON { */ protected static $blocks_metadata = array(); + /** + * Holds the cache of computed style properties for the theme.json. + * + * @since 6.6.0 + * @var array + */ + private static $cached_compute_style_properties = array(); + /** * The CSS selector for the top-level styles. * @@ -734,6 +742,8 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { } } } + + $this->get_compute_style_properties_cache(); } /** @@ -2061,11 +2071,10 @@ protected static function compute_style_properties( $styles, $settings = array() * We need to evaluate it so that the cache is invalidated when it changes. * Computing global settings hash is costly, so we cache it in a non-persistent cache. */ - $cache_key = "compute_style_properties_{$func_args_hash}_" . $this->wp_get_global_settings_hash(); - $cache = get_site_transient( $cache_key ); + $cache_key = "{$func_args_hash}_" . self::wp_get_global_settings_hash(); - if ( $cache ) { - return $cache; + if ( isset( self::$cached_compute_style_properties[ $cache_key ] ) ) { + return self::$cached_compute_style_properties[ $cache_key ]; } } @@ -2145,12 +2154,51 @@ protected static function compute_style_properties( $styles, $settings = array() } if ( $can_use_cached ) { - set_site_transient( $cache_key, $declarations, HOUR_IN_SECONDS ); + self::set_compute_style_properties_cache( $cache_key, $declarations ); } return $declarations; } + /** + * Sets the cache for the compute style properties. + * + * @since 6.6.0 + * + * @param string $cache_key The cache key. + * @param array $declarations The declarations to cache. + * + * @return array The cached declarations. + */ + private static function set_compute_style_properties_cache( $cache_key, $declarations ) { + self::$cached_compute_style_properties[ $cache_key ] = $declarations; + set_site_transient( 'wp_compute_style_properties', self::$cached_compute_style_properties, HOUR_IN_SECONDS ); + self::$cached_compute_style_properties; + } + + /** + * Gets the cache for the compute style properties. + * + * @since 6.6.0 + * + * @return array The cached declarations. + */ + private function get_compute_style_properties_cache() { + self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); + if ( ! is_array( self::$cached_compute_style_properties ) ) { + self::$cached_compute_style_properties = array(); + } + } + + /** + * Clears the cache for the compute style properties. + * + * @since 6.6.0 + */ + public static function clear_compute_style_properties_cache() { + delete_site_transient( 'wp_compute_style_properties' ); + } + /** * Gets the hash of the global settings. * @@ -2158,7 +2206,7 @@ protected static function compute_style_properties( $styles, $settings = array() * * @return string The hash of the global settings. */ - private function wp_get_global_settings_hash() { + private static function wp_get_global_settings_hash() { $cache_group = 'theme_json'; $cache_key = 'wp_get_global_settings_hash'; $cache = wp_cache_get( $cache_key, $cache_group ); @@ -2166,7 +2214,7 @@ private function wp_get_global_settings_hash() { return $cache; } - $hash = md5( wp_json_encode( $cache_args ) ); + $hash = md5( wp_json_encode( wp_get_global_settings() ) ); wp_cache_set( $cache_key, $hash, $cache_group ); return $hash; } From 9c92f654e7fbde3f8555dfdd7830bce23a517a3c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 02:55:19 +0100 Subject: [PATCH 05/34] cache related call only for non development mode --- src/wp-includes/class-wp-theme-json.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 3b6b57f13d738..ff0c6a345e577 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -743,7 +743,10 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { } } - $this->get_compute_style_properties_cache(); + $can_use_cached = ! wp_is_development_mode( 'theme' ); + if ( $can_use_cached ) { + $this->get_compute_style_properties_cache(); + } } /** From cce7c9aadf6101ff8a508b599e90b4675d0e38c4 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 03:02:27 +0100 Subject: [PATCH 06/34] cache clear code --- src/wp-includes/class-wp-theme-json.php | 1 + src/wp-includes/global-styles-and-settings.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index ff0c6a345e577..88b10e4228d24 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2199,6 +2199,7 @@ private function get_compute_style_properties_cache() { * @since 6.6.0 */ public static function clear_compute_style_properties_cache() { + self::$cached_compute_style_properties = array(); delete_site_transient( 'wp_compute_style_properties' ); } diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 0e89f44d1b717..696484beb38f2 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -451,6 +451,8 @@ function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); + // Indirectly `compute_style_properties_cache` relies on `theme_json` cache group for cache key. So clear it. + WP_Theme_JSON::clear_compute_style_properties_cache(); } /** From 7c7bcecae695125e68edd0a2e184a88a9f958817 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 12:44:15 +0100 Subject: [PATCH 07/34] change cache invalidating logic --- src/wp-includes/class-wp-theme-json.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 88b10e4228d24..5e30c9e48cb9c 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -42,9 +42,9 @@ class WP_Theme_JSON { * Holds the cache of computed style properties for the theme.json. * * @since 6.6.0 - * @var array + * @var array|null */ - private static $cached_compute_style_properties = array(); + private static $cached_compute_style_properties = null; /** * The CSS selector for the top-level styles. @@ -2075,6 +2075,9 @@ protected static function compute_style_properties( $styles, $settings = array() * Computing global settings hash is costly, so we cache it in a non-persistent cache. */ $cache_key = "{$func_args_hash}_" . self::wp_get_global_settings_hash(); + if ( ! isset( self::$cached_compute_style_properties ) ) { + self::get_compute_style_properties_cache(); + } if ( isset( self::$cached_compute_style_properties[ $cache_key ] ) ) { return self::$cached_compute_style_properties[ $cache_key ]; @@ -2186,7 +2189,7 @@ private static function set_compute_style_properties_cache( $cache_key, $declara * * @return array The cached declarations. */ - private function get_compute_style_properties_cache() { + private static function get_compute_style_properties_cache() { self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); if ( ! is_array( self::$cached_compute_style_properties ) ) { self::$cached_compute_style_properties = array(); @@ -2199,7 +2202,7 @@ private function get_compute_style_properties_cache() { * @since 6.6.0 */ public static function clear_compute_style_properties_cache() { - self::$cached_compute_style_properties = array(); + self::$cached_compute_style_properties = null; delete_site_transient( 'wp_compute_style_properties' ); } From b55aa6fc293cacec29b7511b5f59267816bc7001 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 13:12:32 +0100 Subject: [PATCH 08/34] remove get compute style from constructor --- src/wp-includes/class-wp-theme-json.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 5e30c9e48cb9c..51608887cfb75 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -742,11 +742,6 @@ public function __construct( $theme_json = array(), $origin = 'theme' ) { } } } - - $can_use_cached = ! wp_is_development_mode( 'theme' ); - if ( $can_use_cached ) { - $this->get_compute_style_properties_cache(); - } } /** @@ -2190,10 +2185,15 @@ private static function set_compute_style_properties_cache( $cache_key, $declara * @return array The cached declarations. */ private static function get_compute_style_properties_cache() { - self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); + if ( ! isset( self::$cached_compute_style_properties ) ) { + self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); + } + if ( ! is_array( self::$cached_compute_style_properties ) ) { self::$cached_compute_style_properties = array(); } + + return self::$cached_compute_style_properties; } /** From 2732050788dc1fc81ef25974631acc717af111f3 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 13:16:16 +0100 Subject: [PATCH 09/34] remove uncessary check --- src/wp-includes/class-wp-theme-json.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 51608887cfb75..186ea3ea8049a 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2185,9 +2185,7 @@ private static function set_compute_style_properties_cache( $cache_key, $declara * @return array The cached declarations. */ private static function get_compute_style_properties_cache() { - if ( ! isset( self::$cached_compute_style_properties ) ) { - self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); - } + self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); if ( ! is_array( self::$cached_compute_style_properties ) ) { self::$cached_compute_style_properties = array(); From 2afb765a66bf24eeb0118594d6882149f1f4ffa6 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Apr 2024 20:00:43 +0100 Subject: [PATCH 10/34] attach clear_compute_style_properties_cache directly to hook --- src/wp-includes/default-filters.php | 3 +++ src/wp-includes/global-styles-and-settings.php | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 1bcd85e79aaf5..15b00b2077d87 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -357,6 +357,9 @@ add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); add_action( 'switch_theme', 'wp_clean_theme_json_cache' ); add_action( 'start_previewing_theme', 'wp_clean_theme_json_cache' ); +//`compute_style_properties_cache` relies on `theme_json` for cache key, so clear it whenever `theme_json` is cleared. +add_action( 'switch_theme', array( 'WP_Theme_JSON', 'clear_compute_style_properties_cache' ) ); +add_action( 'start_previewing_theme', array( 'WP_Theme_JSON', 'clear_compute_style_properties_cache' ) ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_enqueue_scripts', 'wp_enqueue_emoji_styles' ); diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 696484beb38f2..0e89f44d1b717 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -451,8 +451,6 @@ function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); - // Indirectly `compute_style_properties_cache` relies on `theme_json` cache group for cache key. So clear it. - WP_Theme_JSON::clear_compute_style_properties_cache(); } /** From 974a292c7747251a285cbe958abb510b509c43f0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Tue, 23 Apr 2024 12:55:15 +0100 Subject: [PATCH 11/34] Update src/wp-includes/class-wp-theme-json.php cs fixes Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- src/wp-includes/class-wp-theme-json.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 186ea3ea8049a..25f257cdc4006 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2065,10 +2065,10 @@ protected static function compute_style_properties( $styles, $settings = array() if ( $can_use_cached ) { $func_args_hash = md5( wp_json_encode( func_get_args() ) ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed /* - * The global settings can include dynamic data related to typography. - * We need to evaluate it so that the cache is invalidated when it changes. - * Computing global settings hash is costly, so we cache it in a non-persistent cache. - */ + * The global settings can include dynamic data related to typography. + * We need to evaluate it so that the cache is invalidated when it changes. + * Computing global settings hash is costly, so we cache it in a non-persistent cache. + */ $cache_key = "{$func_args_hash}_" . self::wp_get_global_settings_hash(); if ( ! isset( self::$cached_compute_style_properties ) ) { self::get_compute_style_properties_cache(); From 99ac114beb5ad406ee017d7ba66c0cdb1f3d9dab Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 12:03:50 +0100 Subject: [PATCH 12/34] global style caching --- src/wp-includes/class-wp-theme-json.php | 93 ------------------- src/wp-includes/default-filters.php | 3 - .../global-styles-and-settings.php | 62 ++++++++++++- 3 files changed, 60 insertions(+), 98 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 25f257cdc4006..7ab626ce8f96e 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -38,14 +38,6 @@ class WP_Theme_JSON { */ protected static $blocks_metadata = array(); - /** - * Holds the cache of computed style properties for the theme.json. - * - * @since 6.6.0 - * @var array|null - */ - private static $cached_compute_style_properties = null; - /** * The CSS selector for the top-level styles. * @@ -2061,24 +2053,6 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } - $can_use_cached = ! wp_is_development_mode( 'theme' ); - if ( $can_use_cached ) { - $func_args_hash = md5( wp_json_encode( func_get_args() ) ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed - /* - * The global settings can include dynamic data related to typography. - * We need to evaluate it so that the cache is invalidated when it changes. - * Computing global settings hash is costly, so we cache it in a non-persistent cache. - */ - $cache_key = "{$func_args_hash}_" . self::wp_get_global_settings_hash(); - if ( ! isset( self::$cached_compute_style_properties ) ) { - self::get_compute_style_properties_cache(); - } - - if ( isset( self::$cached_compute_style_properties[ $cache_key ] ) ) { - return self::$cached_compute_style_properties[ $cache_key ]; - } - } - $root_variable_duplicates = array(); foreach ( $properties as $css_property => $value_path ) { @@ -2154,76 +2128,9 @@ protected static function compute_style_properties( $styles, $settings = array() } } - if ( $can_use_cached ) { - self::set_compute_style_properties_cache( $cache_key, $declarations ); - } - return $declarations; } - /** - * Sets the cache for the compute style properties. - * - * @since 6.6.0 - * - * @param string $cache_key The cache key. - * @param array $declarations The declarations to cache. - * - * @return array The cached declarations. - */ - private static function set_compute_style_properties_cache( $cache_key, $declarations ) { - self::$cached_compute_style_properties[ $cache_key ] = $declarations; - set_site_transient( 'wp_compute_style_properties', self::$cached_compute_style_properties, HOUR_IN_SECONDS ); - self::$cached_compute_style_properties; - } - - /** - * Gets the cache for the compute style properties. - * - * @since 6.6.0 - * - * @return array The cached declarations. - */ - private static function get_compute_style_properties_cache() { - self::$cached_compute_style_properties = get_site_transient( 'wp_compute_style_properties' ); - - if ( ! is_array( self::$cached_compute_style_properties ) ) { - self::$cached_compute_style_properties = array(); - } - - return self::$cached_compute_style_properties; - } - - /** - * Clears the cache for the compute style properties. - * - * @since 6.6.0 - */ - public static function clear_compute_style_properties_cache() { - self::$cached_compute_style_properties = null; - delete_site_transient( 'wp_compute_style_properties' ); - } - - /** - * Gets the hash of the global settings. - * - * @since 6.6.0 - * - * @return string The hash of the global settings. - */ - private static function wp_get_global_settings_hash() { - $cache_group = 'theme_json'; - $cache_key = 'wp_get_global_settings_hash'; - $cache = wp_cache_get( $cache_key, $cache_group ); - if ( $cache ) { - return $cache; - } - - $hash = md5( wp_json_encode( wp_get_global_settings() ) ); - wp_cache_set( $cache_key, $hash, $cache_group ); - return $hash; - } - /** * Returns the style property for the given path. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 15b00b2077d87..1bcd85e79aaf5 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -357,9 +357,6 @@ add_action( 'init', array( 'WP_Block_Supports', 'init' ), 22 ); add_action( 'switch_theme', 'wp_clean_theme_json_cache' ); add_action( 'start_previewing_theme', 'wp_clean_theme_json_cache' ); -//`compute_style_properties_cache` relies on `theme_json` for cache key, so clear it whenever `theme_json` is cleared. -add_action( 'switch_theme', array( 'WP_Theme_JSON', 'clear_compute_style_properties_cache' ) ); -add_action( 'start_previewing_theme', array( 'WP_Theme_JSON', 'clear_compute_style_properties_cache' ) ); add_action( 'after_switch_theme', '_wp_menus_changed' ); add_action( 'after_switch_theme', '_wp_sidebars_changed' ); add_action( 'wp_enqueue_scripts', 'wp_enqueue_emoji_styles' ); diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 0e89f44d1b717..b1b2a189cb835 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -306,8 +306,30 @@ function wp_add_global_styles_for_blocks() { $tree = WP_Theme_JSON_Resolver::get_merged_data(); $block_nodes = $tree->get_styles_block_nodes(); + + $can_use_cached = ! wp_is_development_mode( 'theme' ); + if ( $can_use_cached ) { + $settings_hash = _wp_get_global_settings_hash(); + $cache_key = 'wp_styles_for_blocks'; + $cached = get_site_transient( $cache_key ); + if ( ! is_array( $cached ) ) { + $cached = array(); + } + } + foreach ( $block_nodes as $metadata ) { - $block_css = $tree->get_styles_for_block( $metadata ); + if ( $can_use_cached ) { + $metadata_hash = md5( wp_json_encode( $metadata ) ); + $style_cache_key = $settings_hash . '_' . $metadata_hash; + if ( isset( $cached[ $style_cache_key ] ) ) { + $block_css = $cached[ $style_cache_key ]; + } else { + $block_css = $tree->get_styles_for_block( $metadata ); + $cached[ $style_cache_key ] = $block_css; + } + } else { + $block_css = $tree->get_styles_for_block( $metadata ); + } if ( ! wp_should_load_separate_core_block_assets() ) { wp_add_inline_style( 'global-styles', $block_css ); @@ -353,6 +375,10 @@ function wp_add_global_styles_for_blocks() { } } } + + if ( $can_use_cached ) { + set_site_transient( $cache_key, $cached, HOUR_IN_SECONDS ); + } } /** @@ -445,12 +471,13 @@ function wp_theme_has_theme_json() { function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' ); - wp_cache_delete( 'wp_get_global_settings_hash', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_custom', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); + wp_cache_delete( 'wp_get_global_settings_hash', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); + _delete_styles_for_blocks_cache(); } /** @@ -612,3 +639,34 @@ function wp_get_block_css_selector( $block_type, $target = 'root', $fallback = f return null; } + +/** + * Gets the hash of the global settings. + * + * @since 6.6.0 + * @access private + * + * @return string The hash of the global settings. + */ +function _wp_get_global_settings_hash() { + $cache_group = 'theme_json'; + $cache_key = 'wp_get_global_settings_hash'; + $cache = wp_cache_get( $cache_key, $cache_group ); + if ( $cache ) { + return $cache; + } + + $hash = md5( wp_json_encode( wp_get_global_settings() ) ); + wp_cache_set( $cache_key, $hash, $cache_group ); + return $hash; +} + +/** + * Deletes the global settings hash. + * + * @since 6.6.0 + * @access private + */ +function _delete_styles_for_blocks_cache() { + delete_site_transient( 'wp_styles_for_blocks' ); +} From fc6c235793af325dad587290fca2b33e92f17166 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 12:42:49 +0100 Subject: [PATCH 13/34] update old test --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 15e3a9a71dea6..9846bc46716d1 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -703,7 +703,8 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q wp_clean_theme_json_cache(); } $query_count = get_num_queries() - $query_count; - $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); + // we have transient delete query inside `_delete_styles_for_blocks_cache()` which result in 3 queries. + $this->assertSame( 3, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); From d1bd38a7f59b324a807c828ed7816503662298af Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 12:56:35 +0100 Subject: [PATCH 14/34] remove additional function --- src/wp-includes/global-styles-and-settings.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index b1b2a189cb835..7c6f160d4c277 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -477,7 +477,7 @@ function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); wp_cache_delete( 'wp_get_global_settings_hash', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); - _delete_styles_for_blocks_cache(); + delete_site_transient( 'wp_styles_for_blocks' ); } /** @@ -661,12 +661,3 @@ function _wp_get_global_settings_hash() { return $hash; } -/** - * Deletes the global settings hash. - * - * @since 6.6.0 - * @access private - */ -function _delete_styles_for_blocks_cache() { - delete_site_transient( 'wp_styles_for_blocks' ); -} From 2957ae4056dd42d64e869e45779f6857c8ef9c8b Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 12:57:25 +0100 Subject: [PATCH 15/34] update function name --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index 9846bc46716d1..d3f238e96b28e 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -703,7 +703,7 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q wp_clean_theme_json_cache(); } $query_count = get_num_queries() - $query_count; - // we have transient delete query inside `_delete_styles_for_blocks_cache()` which result in 3 queries. + // we have transient delete query inside `wp_clean_theme_json_cache()` which result in 3 queries. $this->assertSame( 3, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); From ffa4771a8a827b0ce2cc370cbeff38e4e9022131 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 13:06:36 +0100 Subject: [PATCH 16/34] cs fixes --- src/wp-includes/global-styles-and-settings.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 7c6f160d4c277..639c99b409846 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -660,4 +660,3 @@ function _wp_get_global_settings_hash() { wp_cache_set( $cache_key, $hash, $cache_group ); return $hash; } - From cbc7be523f6933f59573480306c2d3248f75dfe4 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 13:23:18 +0100 Subject: [PATCH 17/34] cache is set check --- .../theme/wpAddGlobalStylesForBlocks.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index b009ac7233ceb..c84939006d06b 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -73,6 +73,26 @@ public function test_third_party_blocks_inline_styles_get_registered_to_global_s ); } + /** + * @ticket 59595 + */ + public function test_check_cache_is_set() { + $this->set_up_third_party_block(); + + wp_register_style( 'global-styles', false, array(), true, true ); + + $style_for_block = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertFalse( $style_for_block ); + + wp_add_global_styles_for_blocks(); + + $this->assertContains( + '.wp-block-my-third-party-block{background-color: hotpink;}', + $this->get_global_styles(), + 'Third party block inline style should be registered after running wp_add_global_styles_for_blocks()' + ); + } + /** * @ticket 56915 */ From a4fc0b5fa9c519a36a10573a0a1289da3cc3bc23 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 13:25:49 +0100 Subject: [PATCH 18/34] cache is set check --- tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index c84939006d06b..a94a24142e6af 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -81,11 +81,14 @@ public function test_check_cache_is_set() { wp_register_style( 'global-styles', false, array(), true, true ); - $style_for_block = get_site_transient( 'wp_styles_for_blocks' ); - $this->assertFalse( $style_for_block ); + $style_for_block_before = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertFalse( $style_for_block_before ); wp_add_global_styles_for_blocks(); + $style_for_block_before = get_site_transient( 'wp_styles_for_blocks' ); + $this->asserNotEmpty( $style_for_block_before ); + $this->assertContains( '.wp-block-my-third-party-block{background-color: hotpink;}', $this->get_global_styles(), From 23270b2e52fb0213999de2234a01e4164515908c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 17 May 2024 13:28:54 +0100 Subject: [PATCH 19/34] assert if it's not empty after --- tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index a94a24142e6af..a285e4ad2449b 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -86,8 +86,8 @@ public function test_check_cache_is_set() { wp_add_global_styles_for_blocks(); - $style_for_block_before = get_site_transient( 'wp_styles_for_blocks' ); - $this->asserNotEmpty( $style_for_block_before ); + $style_for_block_after = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertNotEmpty( $style_for_block_after ); $this->assertContains( '.wp-block-my-third-party-block{background-color: hotpink;}', From d0b243cdd828e1ae3d03ad206918574de9fab94b Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 24 May 2024 12:10:45 -0500 Subject: [PATCH 20/34] Update unit tests --- .../theme/wpAddGlobalStylesForBlocks.php | 72 ++++++++++++++++--- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index a285e4ad2449b..b528bb50ba0d2 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -74,25 +74,79 @@ public function test_third_party_blocks_inline_styles_get_registered_to_global_s } /** + * Ensure that the block cache is set for global styles. + * * @ticket 59595 */ - public function test_check_cache_is_set() { + public function test_styles_for_blocks_cache_is_set() { $this->set_up_third_party_block(); wp_register_style( 'global-styles', false, array(), true, true ); - $style_for_block_before = get_site_transient( 'wp_styles_for_blocks' ); - $this->assertFalse( $style_for_block_before ); + $styles_for_blocks_before = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertFalse( $styles_for_blocks_before ); wp_add_global_styles_for_blocks(); - $style_for_block_after = get_site_transient( 'wp_styles_for_blocks' ); - $this->assertNotEmpty( $style_for_block_after ); + $styles_for_blocks_after = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertNotEmpty( $styles_for_blocks_after ); + } - $this->assertContains( - '.wp-block-my-third-party-block{background-color: hotpink;}', - $this->get_global_styles(), - 'Third party block inline style should be registered after running wp_add_global_styles_for_blocks()' + /** + * Confirm that the block cache is skipped when in dev mode for themes. + * + * @ticket 59595 + */ + public function test_styles_for_blocks_skips_cache_in_dev_mode() { + global $_wp_tests_development_mode; + + $orig_dev_mode = $_wp_tests_development_mode; + + // Setting development mode to theme should skip the cache. + $_wp_tests_development_mode = 'theme'; + + wp_register_style( 'global-styles', false, array(), true, true ); + + // Initial register of global styles. + wp_add_global_styles_for_blocks(); + + $styles_for_blocks_initial = get_site_transient( 'wp_styles_for_blocks' ); + + // Cleanup. + $_wp_tests_development_mode = $orig_dev_mode; + + $this->assertFalse( $styles_for_blocks_initial, ); + } + + /** + * Confirm that the block cache is updated if the block meta has changed. + * + * @ticket 59595 + */ + public function test_styles_for_blocks_cache_is_skipped() { + wp_register_style( 'global-styles', false, array(), true, true ); + + // Initial register of global styles. + wp_add_global_styles_for_blocks(); + + $styles_for_blocks_initial = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' ); + + $this->set_up_third_party_block(); + + /* + * Call register of global styles again to ensure the cache is updated. + * In normal conditions, this function is only called once per request. + */ + wp_add_global_styles_for_blocks(); + + $styles_for_blocks_updated = get_site_transient( 'wp_styles_for_blocks' ); + $this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' ); + + $this->assertNotEquals( + $styles_for_blocks_initial, + $styles_for_blocks_updated, + 'Block style cache was not updated.' ); } From fbbb4cf8825a2628914116c54a7ccdc6cfb26910 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 24 May 2024 12:15:00 -0500 Subject: [PATCH 21/34] Fix typo --- tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index b528bb50ba0d2..8813d493a4b04 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -115,7 +115,7 @@ public function test_styles_for_blocks_skips_cache_in_dev_mode() { // Cleanup. $_wp_tests_development_mode = $orig_dev_mode; - $this->assertFalse( $styles_for_blocks_initial, ); + $this->assertFalse( $styles_for_blocks_initial ); } /** From 8d8e99de1c3b46ec60f9835c9a3432ba85b6f555 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 May 2024 13:37:43 +0100 Subject: [PATCH 22/34] update cache related chanage --- .../global-styles-and-settings.php | 53 +++++++------------ 1 file changed, 20 insertions(+), 33 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 6c1255b929f83..c2cf7f1282d1d 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -309,23 +309,33 @@ function wp_add_global_styles_for_blocks() { $can_use_cached = ! wp_is_development_mode( 'theme' ); if ( $can_use_cached ) { - $settings_hash = _wp_get_global_settings_hash(); - $cache_key = 'wp_styles_for_blocks'; - $cached = get_site_transient( $cache_key ); + $hash = md5( + wp_json_encode( + array( + 'global_setting' => wp_get_global_settings(), + 'block_nodes' => $block_nodes, + ) + ) + ); + + $cache_key = "wp_styles_for_blocks:$hash"; + $cached = get_site_transient( $cache_key ); if ( ! is_array( $cached ) ) { $cached = array(); } } + $update_cache = false; + foreach ( $block_nodes as $metadata ) { if ( $can_use_cached ) { - $metadata_hash = md5( wp_json_encode( $metadata ) ); - $style_cache_key = $settings_hash . '_' . $metadata_hash; - if ( isset( $cached[ $style_cache_key ] ) ) { - $block_css = $cached[ $style_cache_key ]; + $block_name = str_replace( '/', '-', $metadata['name'] ); + if ( isset( $cached[ $block_name ] ) ) { + $block_css = $cached[ $block_name ]; } else { - $block_css = $tree->get_styles_for_block( $metadata ); - $cached[ $style_cache_key ] = $block_css; + $block_css = $tree->get_styles_for_block( $metadata ); + $cached[ $block_name ] = $block_css; + $update_cache = true; } } else { $block_css = $tree->get_styles_for_block( $metadata ); @@ -376,7 +386,7 @@ function wp_add_global_styles_for_blocks() { } } - if ( $can_use_cached ) { + if ( $update_cache ) { set_site_transient( $cache_key, $cached, HOUR_IN_SECONDS ); } } @@ -475,9 +485,7 @@ function wp_clean_theme_json_cache() { wp_cache_delete( 'wp_get_global_settings_theme', 'theme_json' ); wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); - wp_cache_delete( 'wp_get_global_settings_hash', 'theme_json' ); WP_Theme_JSON_Resolver::clean_cached_data(); - delete_site_transient( 'wp_styles_for_blocks' ); } /** @@ -639,24 +647,3 @@ function wp_get_block_css_selector( $block_type, $target = 'root', $fallback = f return null; } - -/** - * Gets the hash of the global settings. - * - * @since 6.6.0 - * @access private - * - * @return string The hash of the global settings. - */ -function _wp_get_global_settings_hash() { - $cache_group = 'theme_json'; - $cache_key = 'wp_get_global_settings_hash'; - $cache = wp_cache_get( $cache_key, $cache_group ); - if ( $cache ) { - return $cache; - } - - $hash = md5( wp_json_encode( wp_get_global_settings() ) ); - wp_cache_set( $cache_key, $hash, $cache_group ); - return $hash; -} From 5899f63baae1905dc81051b900ae06fa0d4c1ee4 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 27 May 2024 17:48:54 +0100 Subject: [PATCH 23/34] remove repetition --- .../global-styles-and-settings.php | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index c2cf7f1282d1d..e56326d1e9637 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -328,8 +328,14 @@ function wp_add_global_styles_for_blocks() { $update_cache = false; foreach ( $block_nodes as $metadata ) { - if ( $can_use_cached ) { - $block_name = str_replace( '/', '-', $metadata['name'] ); + if ( ! empty( $metadata['name'] ) ) { + $block_name = $metadata['name']; + } elseif ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { + // The likes of block element styles from theme.json do not have $metadata['name'] set. + $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); + } + + if ( $can_use_cached && $block_name ) { if ( isset( $cached[ $block_name ] ) ) { $block_css = $cached[ $block_name ]; } else { @@ -357,9 +363,9 @@ function wp_add_global_styles_for_blocks() { * before adding the inline style. * This conditional loading only applies to core blocks. */ - if ( isset( $metadata['name'] ) ) { - if ( str_starts_with( $metadata['name'], 'core/' ) ) { - $block_name = str_replace( 'core/', '', $metadata['name'] ); + if ( $block_name ) { + if ( str_starts_with( $block_name, 'core/' ) ) { + $block_name = str_replace( 'core/', '', $block_name ); $block_handle = 'wp-block-' . $block_name; if ( in_array( $block_handle, $wp_styles->queue, true ) ) { wp_add_inline_style( $stylesheet_handle, $block_css ); @@ -368,22 +374,6 @@ function wp_add_global_styles_for_blocks() { wp_add_inline_style( $stylesheet_handle, $block_css ); } } - - // The likes of block element styles from theme.json do not have $metadata['name'] set. - if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { - $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); - if ( $block_name ) { - if ( str_starts_with( $block_name, 'core/' ) ) { - $block_name = str_replace( 'core/', '', $block_name ); - $block_handle = 'wp-block-' . $block_name; - if ( in_array( $block_handle, $wp_styles->queue, true ) ) { - wp_add_inline_style( $stylesheet_handle, $block_css ); - } - } else { - wp_add_inline_style( $stylesheet_handle, $block_css ); - } - } - } } if ( $update_cache ) { From 2c26302b989e029b7c6b5d22aa83e2f6bad8a5bc Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 May 2024 22:15:57 +0100 Subject: [PATCH 24/34] update style block caching logic --- src/wp-includes/global-styles-and-settings.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index e56326d1e9637..860a2506c387b 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -309,6 +309,7 @@ function wp_add_global_styles_for_blocks() { $can_use_cached = ! wp_is_development_mode( 'theme' ); if ( $can_use_cached ) { + // md5 is a costly operation, so we hashing global settings and block_node in a single call. $hash = md5( wp_json_encode( array( @@ -328,9 +329,9 @@ function wp_add_global_styles_for_blocks() { $update_cache = false; foreach ( $block_nodes as $metadata ) { - if ( ! empty( $metadata['name'] ) ) { + if ( isset( $metadata['name'] ) ) { $block_name = $metadata['name']; - } elseif ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { + } elseif ( ! empty( $metadata['path'] ) ) { // The likes of block element styles from theme.json do not have $metadata['name'] set. $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); } From 4c8dfe25f61e186d24b131e30cf0dcfe5c5eaaee Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 May 2024 22:35:20 +0100 Subject: [PATCH 25/34] update test cases. --- .../theme/wpAddGlobalStylesForBlocks.php | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index 8813d493a4b04..d2fc077401aec 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -83,12 +83,13 @@ public function test_styles_for_blocks_cache_is_set() { wp_register_style( 'global-styles', false, array(), true, true ); - $styles_for_blocks_before = get_site_transient( 'wp_styles_for_blocks' ); + $cache_key = $this->get_wp_styles_for_blocks_cache_key(); + $styles_for_blocks_before = get_site_transient( $cache_key ); $this->assertFalse( $styles_for_blocks_before ); wp_add_global_styles_for_blocks(); - $styles_for_blocks_after = get_site_transient( 'wp_styles_for_blocks' ); + $styles_for_blocks_after = get_site_transient( $cache_key ); $this->assertNotEmpty( $styles_for_blocks_after ); } @@ -110,7 +111,8 @@ public function test_styles_for_blocks_skips_cache_in_dev_mode() { // Initial register of global styles. wp_add_global_styles_for_blocks(); - $styles_for_blocks_initial = get_site_transient( 'wp_styles_for_blocks' ); + $cache_key = $this->get_wp_styles_for_blocks_cache_key(); + $styles_for_blocks_initial = get_site_transient( $cache_key ); // Cleanup. $_wp_tests_development_mode = $orig_dev_mode; @@ -129,7 +131,8 @@ public function test_styles_for_blocks_cache_is_skipped() { // Initial register of global styles. wp_add_global_styles_for_blocks(); - $styles_for_blocks_initial = get_site_transient( 'wp_styles_for_blocks' ); + $cache_key = $this->get_wp_styles_for_blocks_cache_key(); + $styles_for_blocks_initial = get_site_transient( $cache_key ); $this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' ); $this->set_up_third_party_block(); @@ -140,7 +143,8 @@ public function test_styles_for_blocks_cache_is_skipped() { */ wp_add_global_styles_for_blocks(); - $styles_for_blocks_updated = get_site_transient( 'wp_styles_for_blocks' ); + $cache_key = $this->get_wp_styles_for_blocks_cache_key(); + $styles_for_blocks_updated = get_site_transient( $cache_key ); $this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' ); $this->assertNotEquals( @@ -321,4 +325,25 @@ private function get_global_styles() { $actual = wp_styles()->get_data( 'global-styles', 'after' ); return is_array( $actual ) ? $actual : array(); } + + /** + * Get cache key for `wp_styles_for_blocks`. + * + * @return string The cache key. + */ + private function get_wp_styles_for_blocks_cache_key() { + $tree = WP_Theme_JSON_Resolver::get_merged_data(); + $block_nodes = $tree->get_styles_block_nodes(); + // md5 is a costly operation, so we hashing global settings and block_node in a single call. + $hash = md5( + wp_json_encode( + array( + 'global_setting' => wp_get_global_settings(), + 'block_nodes' => $block_nodes, + ) + ) + ); + + return "wp_styles_for_blocks:$hash"; + } } From b08183e0d63a82d9cea17358a101b32102cc89ee Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 28 May 2024 22:37:33 +0100 Subject: [PATCH 26/34] revert test changes --- tests/phpunit/tests/theme/wpThemeJsonResolver.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/theme/wpThemeJsonResolver.php b/tests/phpunit/tests/theme/wpThemeJsonResolver.php index d3f238e96b28e..15e3a9a71dea6 100644 --- a/tests/phpunit/tests/theme/wpThemeJsonResolver.php +++ b/tests/phpunit/tests/theme/wpThemeJsonResolver.php @@ -703,8 +703,7 @@ public function test_get_user_data_from_wp_global_styles_does_not_use_uncached_q wp_clean_theme_json_cache(); } $query_count = get_num_queries() - $query_count; - // we have transient delete query inside `wp_clean_theme_json_cache()` which result in 3 queries. - $this->assertSame( 3, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); + $this->assertSame( 0, $query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' ); $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme ); $this->assertEmpty( $user_cpt, 'User CPT is expected to be empty.' ); From 48a310c65c0b17dcda9459a2fcde97c9bee687f4 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 29 May 2024 21:35:51 +0100 Subject: [PATCH 27/34] unregster bug fix --- src/wp-includes/class-wp-theme-json.php | 8 ++++++++ tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 8 +++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index d1af7ab6e2591..de18fad84ce10 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1000,6 +1000,14 @@ protected static function get_blocks_metadata() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); + // Unset old blocks from static variable. + $blocks_to_unset = array_diff_key( static::$blocks_metadata, $blocks ); + if ( ! empty( $blocks_to_unset ) ) { + foreach ( $blocks_to_unset as $block_name => $block_meta ) { + unset( static::$blocks_metadata[ $block_name ] ); + } + } + // Is there metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); if ( empty( $blocks ) ) { diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index d2fc077401aec..f009f86e06fe1 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -20,6 +20,7 @@ class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase { public function set_up() { parent::set_up(); + switch_theme( 'default' ); remove_action( 'wp_print_styles', 'print_emoji_styles' ); } @@ -32,6 +33,7 @@ public function tear_down() { $this->test_blocks = array(); } + switch_theme( 'default' ); parent::tear_down(); } @@ -126,6 +128,7 @@ public function test_styles_for_blocks_skips_cache_in_dev_mode() { * @ticket 59595 */ public function test_styles_for_blocks_cache_is_skipped() { + switch_theme( 'block-theme' ); wp_register_style( 'global-styles', false, array(), true, true ); // Initial register of global styles. @@ -136,15 +139,14 @@ public function test_styles_for_blocks_cache_is_skipped() { $this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' ); $this->set_up_third_party_block(); - /* * Call register of global styles again to ensure the cache is updated. * In normal conditions, this function is only called once per request. */ wp_add_global_styles_for_blocks(); - $cache_key = $this->get_wp_styles_for_blocks_cache_key(); - $styles_for_blocks_updated = get_site_transient( $cache_key ); + $cache_key_new = $this->get_wp_styles_for_blocks_cache_key(); + $styles_for_blocks_updated = get_site_transient( $cache_key_new ); $this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' ); $this->assertNotEquals( From 8b16eb5925558c424048874cf4c9e4a6857861e6 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 31 May 2024 13:16:53 -0500 Subject: [PATCH 28/34] Revert "unregster bug fix" This reverts commit 48a310c65c0b17dcda9459a2fcde97c9bee687f4. --- src/wp-includes/class-wp-theme-json.php | 8 -------- tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 8 +++----- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index de18fad84ce10..d1af7ab6e2591 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1000,14 +1000,6 @@ protected static function get_blocks_metadata() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); - // Unset old blocks from static variable. - $blocks_to_unset = array_diff_key( static::$blocks_metadata, $blocks ); - if ( ! empty( $blocks_to_unset ) ) { - foreach ( $blocks_to_unset as $block_name => $block_meta ) { - unset( static::$blocks_metadata[ $block_name ] ); - } - } - // Is there metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); if ( empty( $blocks ) ) { diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index f009f86e06fe1..d2fc077401aec 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -20,7 +20,6 @@ class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase { public function set_up() { parent::set_up(); - switch_theme( 'default' ); remove_action( 'wp_print_styles', 'print_emoji_styles' ); } @@ -33,7 +32,6 @@ public function tear_down() { $this->test_blocks = array(); } - switch_theme( 'default' ); parent::tear_down(); } @@ -128,7 +126,6 @@ public function test_styles_for_blocks_skips_cache_in_dev_mode() { * @ticket 59595 */ public function test_styles_for_blocks_cache_is_skipped() { - switch_theme( 'block-theme' ); wp_register_style( 'global-styles', false, array(), true, true ); // Initial register of global styles. @@ -139,14 +136,15 @@ public function test_styles_for_blocks_cache_is_skipped() { $this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' ); $this->set_up_third_party_block(); + /* * Call register of global styles again to ensure the cache is updated. * In normal conditions, this function is only called once per request. */ wp_add_global_styles_for_blocks(); - $cache_key_new = $this->get_wp_styles_for_blocks_cache_key(); - $styles_for_blocks_updated = get_site_transient( $cache_key_new ); + $cache_key = $this->get_wp_styles_for_blocks_cache_key(); + $styles_for_blocks_updated = get_site_transient( $cache_key ); $this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' ); $this->assertNotEquals( From 76435fc1eea4abb94faf0e95f09a655425107f33 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 31 May 2024 09:46:51 -0500 Subject: [PATCH 29/34] Clear theme json data between theme unit tests --- tests/phpunit/tests/theme/base.php | 1 + tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/tests/phpunit/tests/theme/base.php b/tests/phpunit/tests/theme/base.php index 182efa5df81e4..4c42fa3363b0a 100644 --- a/tests/phpunit/tests/theme/base.php +++ b/tests/phpunit/tests/theme/base.php @@ -44,6 +44,7 @@ public function tear_down() { remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); wp_clean_themes_cache(); + wp_clean_theme_json_cache(); unset( $GLOBALS['wp_themes'] ); parent::tear_down(); diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index d2fc077401aec..e1f25339ed05f 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -207,6 +207,7 @@ public function test_third_party_blocks_inline_styles_get_rendered_when_per_bloc * @ticket 56915 */ public function test_blocks_inline_styles_get_rendered() { + $this->set_up_third_party_block(); wp_register_style( 'global-styles', false, array(), true, true ); wp_enqueue_style( 'global-styles' ); wp_add_global_styles_for_blocks(); @@ -248,6 +249,7 @@ public function test_third_party_blocks_inline_styles_for_elements_get_rendered_ * @ticket 57868 */ public function test_third_party_blocks_inline_styles_for_elements_get_rendered() { + $this->set_up_third_party_block(); wp_register_style( 'global-styles', false, array(), true, true ); wp_enqueue_style( 'global-styles' ); wp_add_global_styles_for_blocks(); From 21c29222e2711dc088dd67d935e2421a4da726b1 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 31 May 2024 16:30:00 -0500 Subject: [PATCH 30/34] Don't cache nodes without names --- .../global-styles-and-settings.php | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 71e6bb6d05407..93e6dc6a0aa86 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -330,20 +330,14 @@ function wp_add_global_styles_for_blocks() { $update_cache = false; foreach ( $block_nodes as $metadata ) { - if ( isset( $metadata['name'] ) ) { - $block_name = $metadata['name']; - } elseif ( ! empty( $metadata['path'] ) ) { - // The likes of block element styles from theme.json do not have $metadata['name'] set. - $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); - } - if ( $can_use_cached && $block_name ) { - if ( isset( $cached[ $block_name ] ) ) { - $block_css = $cached[ $block_name ]; + if ( $can_use_cached && isset( $metadata['name'] ) ) { + if ( isset( $cached[ $metadata['name'] ] ) ) { + $block_css = $cached[ $metadata['name'] ]; } else { - $block_css = $tree->get_styles_for_block( $metadata ); - $cached[ $block_name ] = $block_css; - $update_cache = true; + $block_css = $tree->get_styles_for_block( $metadata ); + $cached[ $metadata['name'] ] = $block_css; + $update_cache = true; } } else { $block_css = $tree->get_styles_for_block( $metadata ); @@ -365,9 +359,9 @@ function wp_add_global_styles_for_blocks() { * before adding the inline style. * This conditional loading only applies to core blocks. */ - if ( $block_name ) { - if ( str_starts_with( $block_name, 'core/' ) ) { - $block_name = str_replace( 'core/', '', $block_name ); + if ( isset( $metadata['name'] ) ) { + if ( str_starts_with( $metadata['name'], 'core/' ) ) { + $block_name = str_replace( 'core/', '', $metadata['name'] ); $block_handle = 'wp-block-' . $block_name; if ( in_array( $block_handle, $wp_styles->queue, true ) ) { wp_add_inline_style( $stylesheet_handle, $block_css ); @@ -376,6 +370,22 @@ function wp_add_global_styles_for_blocks() { wp_add_inline_style( $stylesheet_handle, $block_css ); } } + + // The likes of block element styles from theme.json do not have $metadata['name'] set. + if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { + $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); + if ( $block_name ) { + if ( str_starts_with( $block_name, 'core/' ) ) { + $block_name = str_replace( 'core/', '', $block_name ); + $block_handle = 'wp-block-' . $block_name; + if ( in_array( $block_handle, $wp_styles->queue, true ) ) { + wp_add_inline_style( $stylesheet_handle, $block_css ); + } + } else { + wp_add_inline_style( $stylesheet_handle, $block_css ); + } + } + } } if ( $update_cache ) { From accffb6d3704029c0d8e2ca945c2e922f2f39d21 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 3 Jun 2024 19:27:05 +0100 Subject: [PATCH 31/34] remove test case --- src/wp-includes/class-wp-theme-json.php | 8 -------- tests/phpunit/tests/theme/base.php | 1 + tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php | 2 -- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index dc56cd018abbf..4da9580271ff8 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1052,14 +1052,6 @@ protected static function get_blocks_metadata() { $blocks = $registry->get_all_registered(); $style_registry = WP_Block_Styles_Registry::get_instance(); - // Unset old blocks from static variable. - $blocks_to_unset = array_diff_key( static::$blocks_metadata, $blocks ); - if ( ! empty( $blocks_to_unset ) ) { - foreach ( $blocks_to_unset as $block_name => $block_meta ) { - unset( static::$blocks_metadata[ $block_name ] ); - } - } - // Is there metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); if ( empty( $blocks ) ) { diff --git a/tests/phpunit/tests/theme/base.php b/tests/phpunit/tests/theme/base.php index 4c42fa3363b0a..00824e07d5042 100644 --- a/tests/phpunit/tests/theme/base.php +++ b/tests/phpunit/tests/theme/base.php @@ -45,6 +45,7 @@ public function tear_down() { wp_clean_themes_cache(); wp_clean_theme_json_cache(); + wp_clean_theme_json_cache(); unset( $GLOBALS['wp_themes'] ); parent::tear_down(); diff --git a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php index 6ac9d2567e219..5a967db332be7 100644 --- a/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php +++ b/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php @@ -20,7 +20,6 @@ class Tests_Theme_WpAddGlobalStylesForBlocks extends WP_Theme_UnitTestCase { public function set_up() { parent::set_up(); - switch_theme( 'default' ); remove_action( 'wp_print_styles', 'print_emoji_styles' ); } @@ -33,7 +32,6 @@ public function tear_down() { $this->test_blocks = array(); } - switch_theme( 'default' ); parent::tear_down(); } From b6de61fd560701edb9f5b2218425f73c0d3e9faa Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 3 Jun 2024 20:02:23 +0100 Subject: [PATCH 32/34] cache node with no name --- src/wp-includes/global-styles-and-settings.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index 71e6bb6d05407..e5a9f386a5900 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -337,13 +337,18 @@ function wp_add_global_styles_for_blocks() { $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); } - if ( $can_use_cached && $block_name ) { - if ( isset( $cached[ $block_name ] ) ) { - $block_css = $cached[ $block_name ]; + if ( $can_use_cached ) { + $block_cache_key = $block_name; + if ( ! $block_cache_key ) { + $block_cache_key = md5( wp_json_encode( $metadata ) ); + } + + if ( isset( $cached[ $block_cache_key ] ) ) { + $block_css = $cached[ $block_cache_key ]; } else { - $block_css = $tree->get_styles_for_block( $metadata ); - $cached[ $block_name ] = $block_css; - $update_cache = true; + $block_css = $tree->get_styles_for_block( $metadata ); + $cached[ $block_cache_key ] = $block_css; + $update_cache = true; } } else { $block_css = $tree->get_styles_for_block( $metadata ); From 0947a3886127bf100bb838fb29940a4a273a16cb Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 3 Jun 2024 20:05:51 +0100 Subject: [PATCH 33/34] test cases fixes --- tests/phpunit/tests/theme/base.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/theme/base.php b/tests/phpunit/tests/theme/base.php index 00824e07d5042..4c42fa3363b0a 100644 --- a/tests/phpunit/tests/theme/base.php +++ b/tests/phpunit/tests/theme/base.php @@ -45,7 +45,6 @@ public function tear_down() { wp_clean_themes_cache(); wp_clean_theme_json_cache(); - wp_clean_theme_json_cache(); unset( $GLOBALS['wp_themes'] ); parent::tear_down(); From 561ee415a6bad2ecccb87f7dbdf26af3da461d55 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 4 Jun 2024 09:33:10 -0500 Subject: [PATCH 34/34] Inline docs improvements --- src/wp-includes/global-styles-and-settings.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/wp-includes/global-styles-and-settings.php b/src/wp-includes/global-styles-and-settings.php index bfa5793e8ef67..b413273a64974 100644 --- a/src/wp-includes/global-styles-and-settings.php +++ b/src/wp-includes/global-styles-and-settings.php @@ -310,7 +310,7 @@ function wp_add_global_styles_for_blocks() { $can_use_cached = ! wp_is_development_mode( 'theme' ); if ( $can_use_cached ) { - // md5 is a costly operation, so we hashing global settings and block_node in a single call. + // Hash global settings and block nodes together to optimize performance of key generation. $hash = md5( wp_json_encode( array( @@ -332,10 +332,7 @@ function wp_add_global_styles_for_blocks() { foreach ( $block_nodes as $metadata ) { if ( $can_use_cached ) { - /* - * If the block name is available, use it for the cache key to avoid costly - * hashing processes. Otherwise, cache the data using a hash of the metadata. - */ + // Use the block name as the key for cached CSS data. Otherwise, use a hash of the metadata. $cache_node_key = isset( $metadata['name'] ) ? $metadata['name'] : md5( wp_json_encode( $metadata ) ); if ( isset( $cached[ $cache_node_key ] ) ) {