From bde7afe3e228c493c8c9ba8712e7dd1d08b67571 Mon Sep 17 00:00:00 2001 From: Jonny Harrus Date: Sun, 2 Jun 2024 22:17:04 +0100 Subject: [PATCH 1/2] Improve theme file caching in class-wp-theme-json.php Minor changes have been made to class-wp-theme-json.php to better cache the theme files. This includes adding an 'update_cache' functionality that sets 'str_start_with' and 'str_start_with_cache', and removing redundant checks for empty or non-numerical values. These improvements aim to optimize performance by updating cache when necessary. --- src/wp-includes/class-wp-theme-json.php | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 4da9580271ff8..9beb450e50731 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2194,13 +2194,20 @@ protected static function compute_style_properties( $styles, $settings = array() } $root_variable_duplicates = array(); + $update_cache = false; + $str_start_with_cache = (array) wp_cache_get( 'str_start_with', 'theme_files' ); + $str_len_root = strlen( '--wp--style--root--' ); foreach ( $properties as $css_property => $value_path ) { - $value = static::get_property_value( $styles, $value_path, $theme_json ); - - if ( str_starts_with( $css_property, '--wp--style--root--' ) && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) { + if ( ! isset( $str_start_with_cache[ $css_property ] ) ) { + $str_start_with_cache[ $css_property ] = str_starts_with( $css_property, '--wp--style--root--' ); + $update_cache = true; + } + if ( $str_start_with_cache[ $css_property ] && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) { continue; } + + $value = static::get_property_value( $styles, $value_path, $theme_json ); /* * Root-level padding styles don't currently support strings with CSS shorthand values. * This may change: https://github.com/WordPress/gutenberg/issues/40132. @@ -2209,8 +2216,8 @@ protected static function compute_style_properties( $styles, $settings = array() continue; } - if ( str_starts_with( $css_property, '--wp--style--root--' ) && $use_root_padding ) { - $root_variable_duplicates[] = substr( $css_property, strlen( '--wp--style--root--' ) ); + if ( $str_start_with_cache[ $css_property ] && $use_root_padding ) { + $root_variable_duplicates[] = substr( $css_property, $str_len_root ); } /* @@ -2233,9 +2240,7 @@ protected static function compute_style_properties( $styles, $settings = array() $value = isset( $background_styles['declarations'][ $css_property ] ) ? $background_styles['declarations'][ $css_property ] : $value; } - // Skip if empty and not "0" or value represents array of longhand values. - $has_missing_value = empty( $value ) && ! is_numeric( $value ); - if ( $has_missing_value || is_array( $value ) ) { + if ( '' === $value || is_array( $value ) ) { continue; } @@ -2267,6 +2272,10 @@ protected static function compute_style_properties( $styles, $settings = array() ); } + if ( $update_cache ) { + wp_cache_set( 'str_start_with', $str_start_with_cache, 'theme_files' ); + } + // If a variable value is added to the root, the corresponding property should be removed. foreach ( $root_variable_duplicates as $duplicate ) { $discard = array_search( $duplicate, array_column( $declarations, 'name' ), true ); From 96ac25b74a83fef56e062522cbbe47e65bdbfffb Mon Sep 17 00:00:00 2001 From: Jonny Harrus Date: Sun, 2 Jun 2024 23:51:13 +0100 Subject: [PATCH 2/2] Optimize path_string generation in class-wp-theme-json.php The array-to-string conversion in the class-wp-theme-json.php file has been modified for more optimized performance. The original simple implode function has been replaced with a loop to more efficiently concatenate the array elements. This approach also gives more flexible control in handling the dot separator. --- src/wp-includes/class-wp-theme-json.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 9beb450e50731..2704c78c7e33f 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2225,7 +2225,13 @@ protected static function compute_style_properties( $styles, $settings = array() * Skip protected properties that are explicitly set to `null`. */ if ( is_array( $value_path ) ) { - $path_string = implode( '.', $value_path ); + $path_string = ''; + for ( $i = 0; $i < count( $value_path ); $i++ ) { + if ( $i > 0 ) { + $path_string .= '.'; + } + $path_string .= $value_path[ $i ]; + } if ( isset( static::PROTECTED_PROPERTIES[ $path_string ] ) && _wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null