Make WordPress Core

Opened 6 months ago

Closed 4 months ago

#64848 closed defect (bug) (fixed)

WP_Theme_JSON: Prevent implicit coercion in to_ruleset

Reported by: ramonopoly Owned by: ramonopoly
Priority: normal Milestone: 7.1
Component: Themes Version: 5.8
Severity: minor Keywords: has-patch gutenberg-merge has-unit-tests
Cc: Focuses:

Description

WP_Theme_JSON::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.

I caught this while debugging the test_get_styles_with_appearance_tools() test, which was outputting values like gap: 1 where 1 was the result of coercing a bool.

The bool was the block support value, not a gap value.

Attachments (1)

deprecated.png (41.4 KB ) - added by Presskopp 4 months ago.

Download all attachments as: .zip

Change History (18)

This ticket was mentioned in PR #11236 on WordPress/wordpress-develop by @ramonopoly.


6 months ago
#1

Brings over changes from:

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.

## The fix

  • Cast numeric values to string explicitly.
  • Skip declarations whose value is not a string or number (booleans, arrays, objects).

## Testing

Tests should pass!

Trac ticket: https://core.trac.wordpress.org/ticket/64848

## Use of AI Tools

Diagnostics

@isabel_brison commented on PR #11236:


6 months ago
#2

This LGTM but if we're aiming for 7.1 we'll have to wait until the 7.0 release branch is created at RC1 to commit it to trunk.

@isabel_brison commented on PR #11236:


6 months ago
#3

Also, PHP tests seem to be complaining about your use of setAccessible()

There was 1 error:

1) Tests_Theme_wpThemeJson::test_to_ruleset_skips_non_scalar_values_and_casts_numerics
Method ReflectionMethod::setAccessible() is deprecated since 8.5, as it has no effect since PHP 8.1

/var/www/tests/phpunit/tests/theme/wpThemeJson.php:7066
/var/www/vendor/bin/phpunit:122

@ramonopoly commented on PR #11236:


6 months ago
#4

PHP tests seem to be complaining about your use of setAccessible()

Argh, < 8.5 wants it, > 8.5 hates it. 🤣

This LGTM but if we're aiming for 7.1 we'll have to wait until the 7.0 release branch is created at RC1 to commit it to trunk.

Thanks for the heads up. I'll leave it brewing until then.

#5 @Presskopp
4 months ago

The warning mentioned in #3 is appearing for a split second when you edit a post under PHP 8.5.1 (WP 7.0-RC3-62340, WP_DEBUG set to true)

EDIT: Gutenberg Plugin 23.1.1 must be installed/active

Last edited 4 months ago by Presskopp (previous) (diff)

@Presskopp
4 months ago

#7 @ramonopoly
4 months ago

see https://github.com/WordPress/gutenberg/issues/78124

Thanks @Presskopp I suspect it's unrelated directly to this issue.

I created a PR in GB to wrap those calls

#8 @ramonopoly
4 months ago

  • Owner set to ramonopoly
  • Resolutionfixed
  • Status newclosed

In 62347:

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

@ramonopoly commented on PR #11236:


4 months ago
#9

Committed in r62347 and 8a4a40ee7ce5d1bda592dc5213c4338cae1733fe

#10 @ramonopoly
4 months ago

  • Resolution fixed
  • Status closedreopened

Reopening. There's a block on committing to trunk until 7.0. I'll revert

@ramonopoly commented on PR #11236:


4 months ago
#11

Reopening. There's a block on committing to trunk until 7.0. I'll revert

This ticket was mentioned in PR #11791 on WordPress/wordpress-develop by @ramonopoly.


4 months ago
#13

Reverts:

And therefore https://core.trac.wordpress.org/changeset/62347

This is because there's a block on commits to trunk for 7.1 patches until 7.0 is released.

Related trac ticket: core.trac.wordpress.org/ticket/64848

#14 @ramonopoly
4 months ago

In 62348:

[REVERT] WP_Theme_JSON: Prevent implicit coercion in to_ruleset

Reverts r62347. The fix the patch proposes is destined for 7.1. There is a general block for such patches until the 7.0 general release.

Props ramonopoly, andrewserong, westonruter.

See #64848.

@ramonopoly commented on PR #11791:


4 months ago
#15

Committed in r62348 / 8b45167597be0efe35083e2a1ca620caee66fc1f

@ramonopoly commented on PR #11236:


4 months ago
#16

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

This still needs to go in, bot. It was reverted.

#17 @ramonopoly
4 months ago

  • Resolutionfixed
  • Status reopenedclosed

In 62415:

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

Note: See TracTickets for help on using tickets.