WP_Theme_JSON: Prevent implicit coercion in to_ruleset

`to_ruleset` used string concatenation (`$element['name'] . ': ' . $element['value'] . ';'`), so PHP implicitly coerced non-string values (e.g. booleans → `'1'`/`''`, arrays → `'Array'`). That could emit invalid or misleading CSS.

At the same time, pass a `style` theme.json path in `test_get_styles_with_appearance_tools()` to simulate a style node. Before it was `settings`.

Props ramonopoly, andrewserong, isabel_brison.

Fixes #64848.

 --This line, and those below, will be ignored--

M    src/wp-includes/class-wp-theme-json.php
M    tests/phpunit/tests/theme/wpThemeJson.php

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


git-svn-id: http://core.svn.wordpress.org/trunk@61628 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ramonopoly
2026-05-11 02:05:43 +00:00
parent c69a2d8f54
commit b41ae0e5f0
2 changed files with 14 additions and 2 deletions
+13 -1
View File
@@ -1982,6 +1982,7 @@ class WP_Theme_JSON {
* creates the corresponding ruleset.
*
* @since 5.8.0
* @since 7.1.0 Skip declarations whose value is not a plain string (booleans, arrays, objects, etc.).
*
* @param string $selector CSS selector.
* @param array $declarations List of declarations.
@@ -1995,7 +1996,18 @@ class WP_Theme_JSON {
$declaration_block = array_reduce(
$declarations,
static function ( $carry, $element ) {
return $carry .= $element['name'] . ': ' . $element['value'] . ';'; },
$value = $element['value'];
if ( is_numeric( $value ) ) {
$value = (string) $value;
}
if ( ! is_string( $value ) ) {
return $carry;
}
return $carry .= $element['name'] . ': ' . $value . ';';
},
''
);
+1 -1
View File
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '7.1-alpha-62346';
$wp_version = '7.1-alpha-62347';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.