From ac4a7eb021970a0a71ce1f81f4d9f8bc647d206f Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 12 Jun 2024 22:42:30 +0200 Subject: [PATCH 1/3] Improve performance of `compute_style_properties` method. Improve logic in compute_style_properties to improve performance. This commit fixes a number of performance issues including. - Expensive repeated call to `str_starts_with`. - Return easy if not array. - Repeated call to `strlen`. - Move call to `get_property_value` where it is needed. - Move call to implode to after empty check. --- lib/class-wp-theme-json-gutenberg.php | 48 +++++++++++++++------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index a1e79a56a683bf..62ed70fac0d145 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -2253,43 +2253,37 @@ 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 ) { - if ( null === $properties ) { - $properties = static::PROPERTIES_METADATA; - } - - $declarations = array(); if ( empty( $styles ) ) { - return $declarations; + return array(); } + if ( null === $properties ) { + $properties = static::PROPERTIES_METADATA; + } + $declarations = array(); $root_variable_duplicates = array(); + $string_length = strlen( '--wp--style--root--' ); foreach ( $properties as $css_property => $value_path ) { - $value = static::get_property_value( $styles, $value_path, $theme_json ); + if ( ! is_array( $value_path ) ) { + continue; + } - if ( str_starts_with( $css_property, '--wp--style--root--' ) && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) { + $is_root_style = str_starts_with( $css_property, '--wp--style--root--' ); + if ( $is_root_style && ( 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. if ( '--wp--style--root--padding' === $css_property && is_string( $value ) ) { continue; } - if ( str_starts_with( $css_property, '--wp--style--root--' ) && $use_root_padding ) { - $root_variable_duplicates[] = substr( $css_property, strlen( '--wp--style--root--' ) ); - } - - // Look up protected properties, keyed by value path. - // Skip protected properties that are explicitly set to `null`. - if ( is_array( $value_path ) ) { - $path_string = implode( '.', $value_path ); - if ( - isset( static::PROTECTED_PROPERTIES[ $path_string ] ) && - _wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null - ) { - continue; - } + if ( $is_root_style && $use_root_padding ) { + $root_variable_duplicates[] = substr( $css_property, $string_length ); } // Processes background styles. @@ -2304,6 +2298,16 @@ protected static function compute_style_properties( $styles, $settings = array() continue; } + // Look up protected properties, keyed by value path. + // Skip protected properties that are explicitly set to `null`. + $path_string = implode( '.', $value_path ); + if ( + isset( static::PROTECTED_PROPERTIES[ $path_string ] ) && + _wp_array_get( $settings, static::PROTECTED_PROPERTIES[ $path_string ], null ) === null + ) { + continue; + } + // Calculates fluid typography rules where available. if ( 'font-size' === $css_property ) { /* From 82f479fda5a0eebf83144fd156e0dbb088ab9915 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Wed, 12 Jun 2024 22:48:32 +0200 Subject: [PATCH 2/3] Fix lint. --- lib/class-wp-theme-json-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 62ed70fac0d145..73c7dc3c855209 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -2270,7 +2270,7 @@ protected static function compute_style_properties( $styles, $settings = array() } $is_root_style = str_starts_with( $css_property, '--wp--style--root--' ); - if ( $is_root_style && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) { + if ( $is_root_style && ( static::ROOT_BLOCK_SELECTOR !== $selector || ! $use_root_padding ) ) { continue; } From eca89a5ca87e83e635ca6feb1adc96aabe8d562d Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 14 Jun 2024 10:10:00 +0200 Subject: [PATCH 3/3] Apply suggestions from code review Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com> --- lib/class-wp-theme-json-gutenberg.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 73c7dc3c855209..c75c1560da1c17 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -2262,7 +2262,7 @@ protected static function compute_style_properties( $styles, $settings = array() } $declarations = array(); $root_variable_duplicates = array(); - $string_length = strlen( '--wp--style--root--' ); + $root_style_length = strlen( '--wp--style--root--' ); foreach ( $properties as $css_property => $value_path ) { if ( ! is_array( $value_path ) ) { @@ -2283,7 +2283,7 @@ protected static function compute_style_properties( $styles, $settings = array() } if ( $is_root_style && $use_root_padding ) { - $root_variable_duplicates[] = substr( $css_property, $string_length ); + $root_variable_duplicates[] = substr( $css_property, $root_style_length ); } // Processes background styles.