Skip to content

Block Supports: Add PHP helpers for block support classes and styles - #14094

Closed
aaronrobertshaw wants to merge 13 commits into
WordPress:trunkfrom
aaronrobertshaw:backport/php-support-style-helpers
Closed

aaronrobertshaw wants to merge 13 commits into
WordPress:trunkfrom
aaronrobertshaw:backport/php-support-style-helpers

Conversation

@aaronrobertshaw

@aaronrobertshaw aaronrobertshaw commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Backport of Gutenberg PRs

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

Adds PHP helpers that turn block attributes into a block support's classes and styles, like the JS get*ClassesAndStyles() functions, and moves the block supports onto them.

This PR backports:

  • wp_get_border_classes_and_styles(), wp_get_shadow_classes_and_styles(), wp_get_spacing_classes_and_styles(), wp_get_color_classes_and_styles() and wp_get_background_classes_and_styles(),
  • wp_apply_border_support(), wp_apply_shadow_support(), wp_apply_spacing_support(), wp_apply_colors_support() and wp_render_background_support() built on them,
  • and the accompanying block supports tests.

The block changes that call these helpers (Avatar, Post Featured Image, Icon, Post Navigation Link, Navigation Submenu, Comment Author Avatar, Calendar and Button) arrive later with the Gutenberg package update.

Testing instructions

The block support output should be identical to trunk. The one exception: a null, false, empty or zero preset color now falls back to the custom color, as in JS. To compare, register a dynamic test block and render it on trunk and on this branch:

npm run env:cli -- eval "register_block_type( 'test/helpers', array( 'api_version' => 3, 'supports' => array( 'color' => array( 'text' => true, 'background' => true ), '__experimentalBorder' => array( 'color' => true, 'radius' => true, 'style' => true, 'width' => true ), 'shadow' => true, 'spacing' => array( 'padding' => true, 'margin' => true ), 'background' => array( 'backgroundImage' => true, 'gradient' => true ) ), 'render_callback' => static function () { return '<div ' . get_block_wrapper_attributes() . '></div>'; } ) ); echo do_blocks( '<!-- wp:test/helpers {\"borderColor\":\"contrast\",\"textColor\":\"base\",\"backgroundColor\":\"accent\",\"style\":{\"border\":{\"radius\":\"8px\",\"width\":\"2px\",\"style\":\"solid\",\"top\":{\"width\":\"4px\",\"color\":\"#3858e9\",\"style\":\"dashed\"}},\"shadow\":\"6px 6px 9px #00000033\",\"spacing\":{\"padding\":{\"top\":\"20px\",\"left\":\"var:preset|spacing|40\"},\"margin\":{\"top\":\"10px\"}},\"background\":{\"backgroundImage\":{\"url\":\"https://example.com/image.jpg\"},\"backgroundSize\":\"contain\"}}} /-->' );"

Both branches output:

<div style="border-radius:8px;border-style:solid;border-width:2px;border-top-width:4px;border-top-color:#3858e9;border-top-style:dashed;padding-top:20px;padding-left:var(--wp--preset--spacing--40);margin-top:10px;box-shadow:6px 6px 9px #00000033;background-image:url(&apos;https://example.com/image.jpg&apos;);background-position:50% 50%;background-size:contain;" class="wp-block-test-helpers has-text-color has-base-color has-background has-accent-background-color has-border-color has-contrast-border-color"></div>

Use of AI

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: the port from Gutenberg and the tests; I reviewed and edited them.

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

I've been held up on some other priorities so this one will now be aimed for WP 7.3

@aaronrobertshaw
aaronrobertshaw force-pushed the backport/php-support-style-helpers branch from fee7818 to 1d36d13 Compare October 20, 2026 14:19
@aaronrobertshaw
aaronrobertshaw force-pushed the backport/php-support-style-helpers branch from f8e168a to 3ebd5d1 Compare October 30, 2026 10:07
@aaronrobertshaw
aaronrobertshaw force-pushed the backport/php-support-style-helpers branch from 3ebd5d1 to 4020b55 Compare November 2, 2026 03:12
@aaronrobertshaw
aaronrobertshaw marked this pull request as ready for review November 5, 2026 00:28
@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props aaronrobertshaw, westonruter, ramonopoly, talldanwp, andrewserong.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the changes to src through PHPStan level 10 and it reported back these opportunities for further refinement.

I added a suggestion to fix the issues in background.php.

The report:

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/background.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  47     Function wp_get_background_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  47     Function wp_get_background_classes_and_styles() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  52     Cannot access offset 'background' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  184    Part $styles['style'] (mixed) of encapsed string cannot be cast to string.
         🪪  encapsedStringPart.nonString
  192    Parameter #1 $class_name of method WP_HTML_Tag_Processor::add_class() expects string, mixed given.
         🪪  argument.type
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/border.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  65     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  70     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  70     Cannot access offset 'radius' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  74     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  74     Cannot access offset 'style' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  78     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  78     Cannot access offset 'width' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  82     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  82     Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  87     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  87     Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  90     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  90     Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  99     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  99     Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  99     Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  103    Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  103    Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  103    Cannot access offset 'style' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  120    Function wp_get_border_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  120    Function wp_get_border_classes_and_styles() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  125    Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/colors.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  120    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  125    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  125    Cannot access offset 'text' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  129    Cannot access offset 'background' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  129    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  133    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  133    Cannot access offset 'gradient' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  137    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  137    Cannot access offset 'gradient' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  140    Parameter #2 $str of function explode expects string, mixed given.
         🪪  argument.type
  148    Function wp_apply_colors_support() should return array<string, mixed> but returns array.
         🪪  return.type
  161    Function wp_get_color_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  161    Function wp_get_color_classes_and_styles() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  178    Cannot access offset 'background'|'gradient'|'text' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  178    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/shadow.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  76     Function wp_get_shadow_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  76     Function wp_get_shadow_classes_and_styles() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  81     Cannot access offset 'shadow' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/spacing.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  52     Cannot access offset 'spacing' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  66     Cannot access offset 'margin' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  66     Cannot access offset 'spacing' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  82     Function wp_get_spacing_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  82     Function wp_get_spacing_classes_and_styles() return type has no value type specified in iterable type array.
         💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
         🪪  missingType.iterableValue
  87     Cannot access offset 'spacing' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 [ERROR] Found 51 errors on changed lines (base: trunk @ 2f8310cf91) 

@aaronrobertshaw

aaronrobertshaw commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for running this through level 10 @westonruter.

I've added @phpstan-param and @phpstan-return shapes to the five new wp_get_*_classes_and_styles() helpers. They follow the shapes wp_get_block_supports_classes_and_styles() already has in #15485. Gutenberg gets the same change in #87095.

I've kept the is_array() guards instead of a native array type. Plugin blocks can call these public helpers. A non-array should return an empty result, not a fatal TypeError.

The remaining errors are in the existing wp_apply_*_support() functions. Block supports aren't on Core's strict PHPStan list yet. I'd rather leave a level 10 pass on those to its own ticket.

For anyone re-reviewing: Core's PHPStan check runs at level 5, which this PR passes.

Level 10 only applies to the paths in phpstan-strict.neon.dist. Block supports isn't one of them.

So I've left the remaining wp_apply_*_support() errors for a separate ticket, if we want block supports on the strict list.

@talldan

talldan commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I ran the changes to src through PHPStan level 10 and it reported back these opportunities for further refinement.

@westonruter Should this be run as part of CI if it's a requirement?

Using the 'Request changes' feature in github isn't ideal because it makes your review blocking. You have to be around to dismiss it, or another maintainer has to decide to dismiss your review on your behalf. For me personally, dismissing someone else's review always feels uncomfortable.

On this PR we're still waiting four days later for you to do something.

@ramonjd

ramonjd commented Nov 23, 2026

Copy link
Copy Markdown
Member

I think this is okay to commit?

I ran PHPStan level 10 on the five block-supports files.

The new wp_get_*_classes_and_styles() helpers are clean.

I reckon the errors left over are fine to leave for follow ups for reasons mentioned:

  • 31 are in the existing wp_apply_*_support() code that this PR moves so they were there before
  • Block supports isn't on the strict list. Should it be? If the PHPStan CI jobs pass here, then I don't think it's a blocker

There are 4 new ones as far as I can tell, where the wp_apply_*_support() functions call the helpers. The callers pass an array where the new shapes expect array{ style?: … }. Could the wp_apply_*_support() docblocks get the right shape here, with a corresponding change in Gutenberg later? I still think it's okay for a separate PR.

@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback everyone 👍

Agreed on the caller errors. As noted, the fix is tweaking the shape in each of their docblocks. I'd rather do that in one follow-up that also covers the other 31 errors in those functions. Keep this PR focused on the new helpers.

We can get to this extra work either before or after block supports join the strict list.

Unless we have genuine blockers, once approved, I'll commit this one.

@ramonjd ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backports are faithful to Gutenberg source PRs (all five)

I smoke tested this in the browser and the frontend supports appear as expected.

No other errors as far as I can see.

@westonruter

Copy link
Copy Markdown
Member

On this PR we're still waiting four days later for you to do something.

We can't do anything right now anyway because we're in RC and we haven't branched for 7.2.

@westonruter

Copy link
Copy Markdown
Member

Should this be run as part of CI if it's a requirement?

It's not a requirement, but we're moving in that direction. Currently the codebase is at PHPStan rule level 5, but as of #16021 we can now opt-in a subset of paths to rule level 10. So we could actually include the paths here as well, but I don't want to hold this up further. The remaining issues identified are all related to under-specified array shapes which is an issue everywhere in core, so we'll need to follow up later.

Found 35 errors on changed lines


 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/border.php
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  65     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  70     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  70     Cannot access offset 'radius' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  74     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  74     Cannot access offset 'style' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  78     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  78     Cannot access offset 'width' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  82     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  82     Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  87     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  87     Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  90     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  90     Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  99     Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  99     Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  99     Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  103    Cannot access offset 'border' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  103    Cannot access offset 'bottom'|'left'|'right'|'top' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  103    Cannot access offset 'style' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  107    Parameter #1 $block_attributes of function wp_get_border_classes_and_styles expects array{style?: array<string, mixed>, ...<string, mixed>}, array given.
         🪪  argument.type
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/colors.php
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  120    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  125    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  125    Cannot access offset 'text' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  129    Cannot access offset 'background' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  129    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  133    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  133    Cannot access offset 'gradient' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  136    Parameter #1 $block_attributes of function wp_get_color_classes_and_styles expects array{style?: array{color?: array<string, mixed>, ...<string, mixed>}, ...<string, mixed>}, array<string, mixed> given.
         🪪  argument.type
  137    Cannot access offset 'color' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  137    Cannot access offset 'gradient' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/shadow.php
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  63     Parameter #1 $block_attributes of function wp_get_shadow_classes_and_styles expects array{style?: array<string, mixed>, ...<string, mixed>}, array given.
         🪪  argument.type
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/spacing.php
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  52     Cannot access offset 'spacing' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  66     Cannot access offset 'margin' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  66     Cannot access offset 'spacing' on mixed.
         🪪  offsetAccess.nonOffsetAccessible
  69     Parameter #1 $block_attributes of function wp_get_spacing_classes_and_styles expects array{style?: array<string, mixed>, ...<string, mixed>}, non-empty-array given.
         🪪  argument.type
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 [ERROR] Found 35 errors on changed lines (base: trunk @ 8d1741e0e0) 


@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some suggestions to remove unnecessary is_array() checks in the new functions.

@andrewserong

Copy link
Copy Markdown
Contributor

with some suggestions to remove unnecessary is_array() checks in the new functions.

What makes the is_array() checks unnecessary? My understanding is that switching to array type annotations would result in a fatal if called with something that isn't an array, rather than an early return with an empty array. I mostly mention this because in previous projects in a production environment I've run into tricky issues when functions have switched to being stricter, as it changes runtime behaviour, and not just linting/issues that get picked up in CI.

For a little more context, I believe the parser will default to returning null for invalid values in block attributes markup:

$attrs = $processor->allocate_and_return_parsed_attributes() ?? (
JSON_ERROR_NONE === $processor->get_last_json_error()
? array()
: null
);

In general our functions should be resilient to poor markup, especially as we see more content being produced by agents rather than the block editor code.

@westonruter

Copy link
Copy Markdown
Member

What makes the is_array() checks unnecessary?

I couldn't see anything but arrays being passed in to these functions. For example, wp_render_background_support() has this code:

$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();

So it is already guaranteed to be an array by the time it passes $block_attributes into wp_get_background_classes_and_styles() a bit later:

$styles = wp_get_background_classes_and_styles( $block_attributes );

@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

Thanks for the approvals and the is_array() discussion.

@westonruter, you're right that every Core caller already passes an array. I'd still like to keep the checks, since they protect callers outside Core.

A plugin could pass $block['attrs'] straight from a render_block filter. The parser sets that to null when the attribute JSON is invalid.

With the check, that block gets no classes. With a native array type, bad content causes a fatal error and the whole page fails to render. As @andrewserong noted, more content now comes from agents, we're already seeing these sorts of fatals.

The @phpstan-param shapes still let static analysis flag a wrong type.

So I'll leave the native type suggestions unapplied. Now that 7.2 has branched, I'll commit this one unless there are objections.

wporg-sync pushed a commit that referenced this pull request Nov 26, 2026
Adds `wp_get_border_classes_and_styles()`, `wp_get_shadow_classes_and_styles()`, `wp_get_spacing_classes_and_styles()`, `wp_get_color_classes_and_styles()` and `wp_get_background_classes_and_styles()`. Each takes a block's attributes and returns the classes and styles for that block support, similar to the JavaScript `get*ClassesAndStyles()` functions.

Blocks that skip serialization of a block support can call these helpers instead of rebuilding the classes and styles by hand. The border, shadow, spacing, color and background block supports now build their output with them.

The block support output is unchanged with one exception. A null, false, empty or zero preset color now falls back to the custom color, as it does in the editor.

Developed in: #14094

Props westonruter, ramonopoly, andrewserong, talldanwp, sakibmd, testerrahul.
Fixes #66269.


git-svn-id: https://develop.svn.wordpress.org/trunk@65798 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

Copy link
Copy Markdown

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

SVN changeset: 65798
GitHub commit: 90582b1

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Nov 26, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 26, 2026
Adds `wp_get_border_classes_and_styles()`, `wp_get_shadow_classes_and_styles()`, `wp_get_spacing_classes_and_styles()`, `wp_get_color_classes_and_styles()` and `wp_get_background_classes_and_styles()`. Each takes a block's attributes and returns the classes and styles for that block support, similar to the JavaScript `get*ClassesAndStyles()` functions.

Blocks that skip serialization of a block support can call these helpers instead of rebuilding the classes and styles by hand. The border, shadow, spacing, color and background block supports now build their output with them.

The block support output is unchanged with one exception. A null, false, empty or zero preset color now falls back to the custom color, as it does in the editor.

Developed in: WordPress/wordpress-develop#14094

Props westonruter, ramonopoly, andrewserong, talldanwp, sakibmd, testerrahul.
Fixes #66269.

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


git-svn-id: http://core.svn.wordpress.org/trunk@64827 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants