From 85e9170f396e5751b95d0e401aec6b4af7eb6762 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 24 Oct 2023 23:05:28 +0100 Subject: [PATCH 1/7] Add object caching for `WP_Theme_JSON::compute_style_properties` --- src/wp-includes/class-wp-theme-json.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index e5f174138de35..0554f37a63883 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1934,6 +1934,14 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } + $args = func_get_args(); + $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); + $cache = wp_cache_get( $cache_key, 'wp-styles' ); + + if ( $cache ) { + return $cache; + } + $root_variable_duplicates = array(); foreach ( $properties as $css_property => $value_path ) { @@ -2000,6 +2008,8 @@ protected static function compute_style_properties( $styles, $settings = array() } } + wp_cache_set( $cache_key, $declarations, 'wp-styles' ); + return $declarations; } From 7450d26879dab16639602d41ede7adf6c7704e23 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 24 Oct 2023 23:14:53 +0100 Subject: [PATCH 2/7] The values of the args are only useful for the cache key --- src/wp-includes/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 0554f37a63883..c7985c25abb51 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1934,7 +1934,7 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } - $args = func_get_args(); + $args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); $cache = wp_cache_get( $cache_key, 'wp-styles' ); From c9b36eac2f7bceff5f4bf98495e8498b8ff22646 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 24 Oct 2023 23:27:27 +0100 Subject: [PATCH 3/7] Deal with cache before updating `$properties` --- src/wp-includes/class-wp-theme-json.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index c7985c25abb51..2c1082672e203 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1925,6 +1925,14 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) { * @return array Returns the modified $declarations. */ protected static function compute_style_properties( $styles, $settings = array(), $properties = null, $theme_json = null, $selector = null, $use_root_padding = null ) { + $args = func_get_args(); + $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); + $cache = wp_cache_get( $cache_key, 'wp-styles' ); + + if ( $cache ) { + return $cache; + } + if ( null === $properties ) { $properties = static::PROPERTIES_METADATA; } @@ -1934,14 +1942,6 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } - $args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection - $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); - $cache = wp_cache_get( $cache_key, 'wp-styles' ); - - if ( $cache ) { - return $cache; - } - $root_variable_duplicates = array(); foreach ( $properties as $css_property => $value_path ) { From dd8c83e6a169eee7f8f09ee86b8932a1d4b38f9a Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 3 Nov 2023 09:58:02 +0000 Subject: [PATCH 4/7] Only use cache if not in dev mode for theme. Ensure we do not process empty `$styles` --- src/wp-includes/class-wp-theme-json.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 2c1082672e203..dc67613b08fdc 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1925,14 +1925,6 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) { * @return array Returns the modified $declarations. */ protected static function compute_style_properties( $styles, $settings = array(), $properties = null, $theme_json = null, $selector = null, $use_root_padding = null ) { - $args = func_get_args(); - $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); - $cache = wp_cache_get( $cache_key, 'wp-styles' ); - - if ( $cache ) { - return $cache; - } - if ( null === $properties ) { $properties = static::PROPERTIES_METADATA; } @@ -1942,6 +1934,16 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } + $can_use_cached = ! wp_is_development_mode( 'theme' ); + + $args = func_get_args(); + $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); + $cache = wp_cache_get( $cache_key, 'wp-styles' ); + + if ( $can_use_cached && $cache ) { + return $cache; + } + $root_variable_duplicates = array(); foreach ( $properties as $css_property => $value_path ) { @@ -2008,7 +2010,9 @@ protected static function compute_style_properties( $styles, $settings = array() } } - wp_cache_set( $cache_key, $declarations, 'wp-styles' ); + if ( $can_use_cached ) { + wp_cache_set( $cache_key, $declarations, 'wp-styles', DAY_IN_SECONDS ); + } return $declarations; } From 7d974337c9816aa05d61230ef2343597a1ca4ca7 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Fri, 3 Nov 2023 10:18:42 +0000 Subject: [PATCH 5/7] Address PHPCS warning. It really does not matter if the value of `$properties` changes --- src/wp-includes/class-wp-theme-json.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index dc67613b08fdc..8082f3c72a934 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1936,7 +1936,7 @@ protected static function compute_style_properties( $styles, $settings = array() $can_use_cached = ! wp_is_development_mode( 'theme' ); - $args = func_get_args(); + $args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); $cache = wp_cache_get( $cache_key, 'wp-styles' ); From 23773482c858cbaf88f941cf2fceec52299aadb2 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Thu, 4 Jan 2024 00:59:15 +0000 Subject: [PATCH 6/7] Remove unecessary dev mode check for cache --- src/wp-includes/class-wp-theme-json.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 0be15f60599bf..3746864632a70 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1934,13 +1934,11 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } - $can_use_cached = ! wp_is_development_mode( 'theme' ); - $args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); $cache = wp_cache_get( $cache_key, 'wp-styles' ); - if ( $can_use_cached && $cache ) { + if ( $cache ) { return $cache; } @@ -2010,9 +2008,7 @@ protected static function compute_style_properties( $styles, $settings = array() } } - if ( $can_use_cached ) { - wp_cache_set( $cache_key, $declarations, 'wp-styles', DAY_IN_SECONDS ); - } + wp_cache_set( $cache_key, $declarations, 'wp-styles', DAY_IN_SECONDS ); return $declarations; } From d707a79b5eb8d9ba81fb808e234138d2ead8264a Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Thu, 4 Jan 2024 01:03:27 +0000 Subject: [PATCH 7/7] Add the global settings to the cache key recipe, so that when it changes, it invalidates the cache --- src/wp-includes/class-wp-theme-json.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 3746864632a70..aae319ea944ec 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -1934,7 +1934,8 @@ protected static function compute_style_properties( $styles, $settings = array() return $declarations; } - $args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed + // The global settings can include dynamic data related to typography. We need evaluate it so that the cache is invalidated when it changes. + $args = array( func_get_args(), wp_get_global_settings() ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.Changed $cache_key = 'compute_style_properties_' . md5( wp_json_encode( $args ) ); $cache = wp_cache_get( $cache_key, 'wp-styles' );