From 2723e2c7043492a6d29a7e15e279fd699139c85d Mon Sep 17 00:00:00 2001 From: Jonny Harrus Date: Mon, 24 Jun 2024 20:06:38 +0100 Subject: [PATCH] Refactor style properties computation in WP Theme JSON class The method compute_style_properties() in class-wp-theme-json.php has been refactored for more efficient theme style computation. The flow has been improved by reordering conditions and breaking down complex ones into simpler bits. Now the code is easier to read, and the handling of protected properties and root styles are clearer. --- src/wp-includes/class-wp-theme-json.php | 52 +++++++++++++------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 63e24340732cf..5f39af1487205 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -2281,23 +2281,29 @@ 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(); + $root_style_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. @@ -2306,22 +2312,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--' ) ); - } - - /* - * 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, $root_style_length ); } // Processes background styles. @@ -2336,6 +2328,18 @@ 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 ) { /*