Make WordPress Core

Opened 6 weeks ago

Last modified 22 hours ago

#66273 new defect (bug)

wp_get_state_declarations_with_fallback_border_styles does not always return an array

Reported by: marian1 Owned by:
Priority: normal Milestone: 7.3
Component: General Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

wp_get_state_declarations_with_fallback_border_styles() is documented to return an array:

/**
 * ...
 *
 * @param array $declarations CSS declarations generated by the style engine.
 * @return array CSS declarations with fallback border styles applied where needed.
 */
function wp_get_state_declarations_with_fallback_border_styles( $declarations ) { ... }

However, when a non-array argument is passed, the function returns that argument unchanged:

if ( ! is_array( $declarations ) ) {
    return $declarations;
}

The function can therefore return a value of any type, contrary to its documented return type.

I am unsure of the potential side effects, but the function should probably either return an empty array:

if ( ! is_array( $declarations ) ) {
    return array();
}

or set $declarations to an empty array so that the fallback logic is applied:

if ( ! is_array( $declarations ) ) {
    $declarations = array();
}

The same applies to wp_get_state_style_with_fallback_dimension_styles().

Change History (7)

#1 @westonruter
6 weeks ago

  • Keywords needs-patch added
  • Milestone Awaiting Review → 7.2

This ticket was mentioned in ​PR #14125 on ​WordPress/wordpress-develop by ​@rishabhwp.


6 weeks ago
#2

  • Keywords has-patch has-unit-tests added; needs-patch removed

Four helpers in the states block support are documented with @return array, but returned their argument unchanged when it was not an array, so the return value could be of any type.

They now return an empty array for non-array input:

  • wp_get_state_declarations_with_fallback_border_styles()
  • wp_get_state_declarations_with_background_resets()
  • wp_get_state_style_with_fallback_dimension_styles()
  • wp_get_root_state_style()

The ticket names the first and third. The other two have the identical guard in the same file, so they are included for consistency.

Core only calls these functions with arrays, so the generated CSS is unchanged. The one plugin on WordPress.org that calls them (Brandy Blocks) also only passes arrays.

Unit tests are added for non-array and empty-array input.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Adding unit tests and code review.

​@therssoftware commented on ​PR #14125:


6 weeks ago
#3

Test & Review Report

Ticket: https://core.trac.wordpress.org/ticket/66273
PR: ​https://github.com/WordPress/wordpress-develop/pull/14125

---

Environment

  • WordPress: 7.2-alpha-63166-src (trunk)
  • PHP: 8.3.33
  • Server: macOS Darwin 24.6.0 (arm64)
  • Database: MariaDB 10.11
  • PHPUnit: 9.6.37
  • PHPCS: 3.13.5 (WordPress-Core)

---

Summary of Proposed Changes in PR #14125

PR #14125 updates four helper functions in src/wp-includes/block-supports/states.php:

  • wp_get_state_declarations_with_fallback_border_styles()
  • wp_get_state_declarations_with_background_resets()
  • wp_get_state_style_with_fallback_dimension_styles()
  • wp_get_root_state_style()

When a non-array value is supplied as the first parameter, each function previously returned that value unchanged (e.g. null, false, or a string), violating their documented @return array contract. PR #14125 changes them to return array(). It also adds a data provider covering null, booleans, integers, strings, and objects.

---

Verification & Test Results

1. Automated Unit Tests

Executed the test suite for states block supports:

vendor/bin/phpunit tests/phpunit/tests/block-supports/states.php

Result:

OK (55 tests, 108 assertions)

All 9 data provider test cases in test_state_style_helpers_return_empty_array_for_non_array_value passed cleanly across all four functions.

2. Code Standards Verification
vendor/bin/phpcs src/wp-includes/block-supports/states.php tests/phpunit/tests/block-supports/states.php

Result:

.. 2 / 2 (100%)
Time: 199ms; Memory: 8MB

0 errors, 0 warnings.

---

Critical Code & Architectural Findings

1. Fatal Error in Sister Function: wp_normalize_state_style_for_css_output()

In the same file (src/wp-includes/block-supports/states.php:63), wp_normalize_state_style_for_css_output( $style ) also documents:

/**
 * Normalizes a state style object before generating CSS declarations.
 *
 * @since 7.1.0
 *
 * @param array $style State style object.
 * @return array Normalized state style object.
 */
function wp_normalize_state_style_for_css_output( $style ) {
    unset( $style['layout'] );
    $style = wp_normalize_state_preset_vars( $style );
    return $style;
}

Edge-case behavior uncovered during testing:

  • If null is passed: returns null (not an array, violating @return array).
  • If a string is passed (e.g. wp_normalize_state_style_for_css_output( 'color: red' )): PHP 8 throws a fatal error:
    Fatal error: Uncaught Error: Cannot unset string offsets in states.php:65
    

Recommendation: Add the same defensive guard to wp_normalize_state_style_for_css_output():

if ( ! is_array( $style ) ) {
    return array();
}
2. Warning / TypeError in wp_get_root_state_style() when $nested_keys is non-array

In wp_get_root_state_style( $state_style, $nested_keys ) (states.php:299):

function wp_get_root_state_style( $state_style, $nested_keys ) {
    if ( ! is_array( $state_style ) ) {
        return array();
    }

    $root_style = $state_style;
    foreach ( $nested_keys as $key ) {
        unset( $root_style[ $key ] );
    }

    return $root_style;
}

While $state_style is now guarded, $nested_keys is iterated without a guard. Passing a non-array (such as null or a string):

wp_get_root_state_style( array( 'color' => 'red' ), null );

Triggers:

Warning: foreach() argument must be of type array|object, null given in states.php on line 305

Recommendation: Guard $nested_keys as well:

if ( ! is_array( $nested_keys ) ) {
    return $state_style;
}
3. Interplay with Trac #66274 / PR #14121

wp_get_state_style_with_fallback_dimension_styles() currently has zero callers across WordPress core (its single call site was removed in ​r60832 / commit 1caffb5277). Trac #66274 / PR ​#14121 is actively open proposing to remove this unused function. While keeping PR #14125 consistent is fine, committers should be aware that #66274 may supersede that specific function's change.

4. Modern PHPUnit Data Provider Standards

In tests/phpunit/tests/block-supports/states.php:350, data_non_array_values() is declared as:

public function data_non_array_values()

For PHPUnit 10+ readiness and adherence to Core conventions (see #66211), data providers should be declared public static function.

---

Suggested Commit / Next Steps

The core intent of PR #14125 is solid and ready. Including the missing guards for wp_normalize_state_style_for_css_output() and $nested_keys in wp_get_root_state_style(), and updating the data provider to public static function, would make this a complete, bulletproof hardening of states.php.

This ticket was mentioned in ​Slack in #core by im3dabasia1. ​View the logs.


5 days ago

#5 @im3dabasia1
5 days ago

As per the pre-RC1 7.2 bug scrub:

the ​PR has a good test report with a few extra suggestions. @rishabhwp, would you be able to look at them so it’s ready for review before RC1?

​@rishabhwp commented on ​PR #14125:


5 days ago
#6

Thanks for testing this, @robelsust!

  1. Good catch on wp_normalize_state_style_for_css_output(). It now returns an empty array for non-array input too (0d7e9ce5da).
  2. $nested_keys in wp_get_root_state_style(): I've left this one as is. A non-array there only raises a warning; the function still returns an array. All three callers in core pass an array (array_merge() results, or VALID_ELEMENT_PSEUDO_SELECTORS[ ... ] ?? array()). And I think the warning is also useful if a third party calls it incorrectly.
  3. FYI, wp_get_state_style_with_fallback_dimension_styles() is used again since ccf582a08ce747069019fdeb7996963196c943e7.
  4. Made the data provider static (ce025b71d9).

#7 @audrasjb
22 hours ago

  • Milestone 7.2 → 7.3

This ticket is pretty close from its resolution, but we're too close from 7.2 RC1 and it still needs to be reviewed and committed. Moving to 7.3.

Note: See TracTickets for help on using tickets.