`wp_get_global_styles`: return the standard format for CSS Custom Properties.

Props samnajian, hellofromtonya, isabel_brison.
Fixes #58467.

Built from https://develop.svn.wordpress.org/trunk@55959


git-svn-id: http://core.svn.wordpress.org/trunk@55471 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
oandregal 2023-06-21 07:56:18 +00:00
parent 06beef84d5
commit 99715f5751
3 changed files with 56 additions and 21 deletions

View File

@ -779,7 +779,7 @@ class WP_Theme_JSON {
if ( empty( $result ) ) { if ( empty( $result ) ) {
unset( $output[ $subtree ] ); unset( $output[ $subtree ] );
} else { } else {
$output[ $subtree ] = $result; $output[ $subtree ] = static::resolve_custom_css_format( $result );
} }
} }
@ -1958,10 +1958,6 @@ class WP_Theme_JSON {
/** /**
* Returns the style property for the given path. * Returns the style property for the given path.
* *
* It also converts CSS Custom Property stored as
* "var:preset|color|secondary" to the form
* "--wp--preset--color--secondary".
*
* It also converts references to a path to the value * It also converts references to a path to the value
* stored at that location, e.g. * stored at that location, e.g.
* { "ref": "style.color.background" } => "#fff". * { "ref": "style.color.background" } => "#fff".
@ -1969,6 +1965,10 @@ class WP_Theme_JSON {
* @since 5.8.0 * @since 5.8.0
* @since 5.9.0 Added support for values of array type, which are returned as is. * @since 5.9.0 Added support for values of array type, which are returned as is.
* @since 6.1.0 Added the `$theme_json` parameter. * @since 6.1.0 Added the `$theme_json` parameter.
* @since 6.3.0 It no longer converts the internal format "var:preset|color|secondary"
* to the standard form "--wp--preset--color--secondary".
* This is already done by the sanitize method,
* so every property will be in the standard form.
* *
* @param array $styles Styles subtree. * @param array $styles Styles subtree.
* @param array $path Which property to process. * @param array $path Which property to process.
@ -2018,20 +2018,6 @@ class WP_Theme_JSON {
return $value; return $value;
} }
// Convert custom CSS properties.
$prefix = 'var:';
$prefix_len = strlen( $prefix );
$token_in = '|';
$token_out = '--';
if ( 0 === strncmp( $value, $prefix, $prefix_len ) ) {
$unwrapped_name = str_replace(
$token_in,
$token_out,
substr( $value, $prefix_len )
);
$value = "var(--wp--$unwrapped_name)";
}
return $value; return $value;
} }
@ -3488,4 +3474,52 @@ class WP_Theme_JSON {
_wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_sizes ); _wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_sizes );
} }
/**
* This is used to convert the internal representation of variables to the CSS representation.
* For example, `var:preset|color|vivid-green-cyan` becomes `var(--wp--preset--color--vivid-green-cyan)`.
*
* @since 6.3.0
* @param string $value The variable such as var:preset|color|vivid-green-cyan to convert.
* @return string The converted variable.
*/
private static function convert_custom_properties( $value ) {
$prefix = 'var:';
$prefix_len = strlen( $prefix );
$token_in = '|';
$token_out = '--';
if ( 0 === strpos( $value, $prefix ) ) {
$unwrapped_name = str_replace(
$token_in,
$token_out,
substr( $value, $prefix_len )
);
$value = "var(--wp--$unwrapped_name)";
}
return $value;
}
/**
* Given a tree, converts the internal representation of variables to the CSS representation.
* It is recursive and modifies the input in-place.
*
* @since 6.3.0
* @param array $tree Input to process.
* @return array The modified $tree.
*/
private static function resolve_custom_css_format( $tree ) {
$prefix = 'var:';
foreach ( $tree as $key => $data ) {
if ( is_string( $data ) && 0 === strpos( $data, $prefix ) ) {
$tree[ $key ] = self::convert_custom_properties( $data );
} elseif ( is_array( $data ) ) {
$tree[ $key ] = self::resolve_custom_css_format( $data );
}
}
return $tree;
}
} }

View File

@ -92,6 +92,8 @@ function wp_get_global_settings( $path = array(), $context = array() ) {
* Gets the styles resulting of merging core, theme, and user data. * Gets the styles resulting of merging core, theme, and user data.
* *
* @since 5.9.0 * @since 5.9.0
* @since 6.3.0 the internal format "var:preset|color|secondary" is always resolved
* to the standard form "var(--wp--preset--font-size--small)".
* *
* @param array $path Path to the specific style to retrieve. Optional. * @param array $path Path to the specific style to retrieve. Optional.
* If empty, will return all styles. * If empty, will return all styles.
@ -115,7 +117,6 @@ function wp_get_global_styles( $path = array(), $context = array() ) {
if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) {
$origin = 'theme'; $origin = 'theme';
} }
$styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles']; $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles'];
return _wp_array_get( $styles, $path, $styles ); return _wp_array_get( $styles, $path, $styles );

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.3-alpha-55958'; $wp_version = '6.3-alpha-55959';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.