Make WordPress Core


Ignore:
Timestamp:
06/22/2023 08:42:42 AM (3 years ago)
Author:
oandregal
Message:

wp_get_global_styles: allow transforming the CSS Custom Properties into the values they represent.

Props samnajian, ramonopoly, isabel_brison.
Fixes #58588.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json.php

    r55985 r55986  
    35303530    }
    35313531
     3532    /**
     3533     * Replaces CSS variables with their values in place.
     3534     *
     3535     * @since 6.3.0
     3536     * @param array $styles CSS declarations to convert.
     3537     * @param array $values key => value pairs to use for replacement.
     3538     * @return array
     3539     */
     3540    private static function convert_variables_to_value( $styles, $values ) {
     3541        foreach ( $styles as $key => $style ) {
     3542            if ( is_array( $style ) ) {
     3543                $styles[ $key ] = self::convert_variables_to_value( $style, $values );
     3544                continue;
     3545            }
     3546
     3547            if ( 0 <= strpos( $style, 'var(' ) ) {
     3548                // find all the variables in the string in the form of var(--variable-name, fallback), with fallback in the second capture group.
     3549
     3550                $has_matches = preg_match_all( '/var\(([^),]+)?,?\s?(\S+)?\)/', $style, $var_parts );
     3551
     3552                if ( $has_matches ) {
     3553                    $resolved_style = $styles[ $key ];
     3554                    foreach ( $var_parts[1] as $index => $var_part ) {
     3555                        $key_in_values   = 'var(' . $var_part . ')';
     3556                        $rule_to_replace = $var_parts[0][ $index ]; // the css rule to replace e.g. var(--wp--preset--color--vivid-green-cyan).
     3557                        $fallback        = $var_parts[2][ $index ]; // the fallback value.
     3558                        $resolved_style  = str_replace(
     3559                            array(
     3560                                $rule_to_replace,
     3561                                $fallback,
     3562                            ),
     3563                            array(
     3564                                isset( $values[ $key_in_values ] ) ? $values[ $key_in_values ] : $rule_to_replace,
     3565                                isset( $values[ $fallback ] ) ? $values[ $fallback ] : $fallback,
     3566                            ),
     3567                            $resolved_style
     3568                        );
     3569                    }
     3570                    $styles[ $key ] = $resolved_style;
     3571                }
     3572            }
     3573        }
     3574
     3575        return $styles;
     3576    }
     3577
     3578    /**
     3579     * Resolves the values of CSS variables in the given styles.
     3580     *
     3581     * @since 6.3.0
     3582     * @param WP_Theme_JSON $theme_json The theme json resolver.
     3583     *
     3584     * @return WP_Theme_JSON The $theme_json with resolved variables.
     3585     */
     3586    public static function resolve_variables( $theme_json ) {
     3587        $settings    = $theme_json->get_settings();
     3588        $styles      = $theme_json->get_raw_data()['styles'];
     3589        $preset_vars = static::compute_preset_vars( $settings, static::VALID_ORIGINS );
     3590        $theme_vars  = static::compute_theme_vars( $settings );
     3591        $vars        = array_reduce(
     3592            array_merge( $preset_vars, $theme_vars ),
     3593            function( $carry, $item ) {
     3594                $name                    = $item['name'];
     3595                $carry[ "var({$name})" ] = $item['value'];
     3596                return $carry;
     3597            },
     3598            array()
     3599        );
     3600
     3601        $theme_json->theme_json['styles'] = self::convert_variables_to_value( $styles, $vars );
     3602        return $theme_json;
     3603    }
     3604
    35323605}
Note: See TracChangeset for help on using the changeset viewer.