Skip to content

Block Supports: Add a helper that returns every support's classes and styles - #15485

Open
aaronrobertshaw wants to merge 3 commits into
WordPress:trunkfrom
aaronrobertshaw:backport/block-supports-classes-and-styles-helper
Open

aaronrobertshaw wants to merge 3 commits into
WordPress:trunkfrom
aaronrobertshaw:backport/block-supports-classes-and-styles-helper

Conversation

@aaronrobertshaw

@aaronrobertshaw aaronrobertshaw commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Backport of Gutenberg PR WordPress/gutenberg#86337

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

Adds wp_get_block_supports_classes_and_styles(), which returns the combined classes and styles of the background, color, border, shadow and spacing helpers. An optional list limits it to the supports a block applies to one element.

This PR backports:

  • wp_get_block_supports_classes_and_styles(), next to the other block support utilities,
  • and the accompanying block supports tests.

The Avatar and Post Navigation Link changes that call it arrive later with the Gutenberg package update.

Builds on the helpers committed in r65798 for #14094.

Testing instructions

Nothing renders differently, because no block calls the helper in Core yet. To check its output:

npm run env:cli -- eval "var_export( wp_get_block_supports_classes_and_styles( array( 'borderColor' => 'contrast', 'style' => array( 'border' => array( 'width' => '2px' ), 'shadow' => 'var:preset|shadow|natural', 'spacing' => array( 'padding' => array( 'top' => '10px' ) ) ) ), array( 'border', 'shadow' ) ) );"

It outputs only the border and shadow, without the padding:

array (
  'class' => 'has-border-color has-contrast-border-color',
  'style' => 'border-width:2px;box-shadow:var(--wp--preset--shadow--natural);',
)

Without this PR the command fails with a fatal error for an undefined function.

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 Nov 5, 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
aaronrobertshaw marked this pull request as ready for review November 5, 2026 07:12
@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, ramonopoly, westonruter.

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.

@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.

Backport is accurate. I smoke tested a bunch of blocks on the frontend, checked the CSS and classnames.

Tests pass locally.

Might be worth holding off until 7.2 is stable to avoid clashes?

Thanks!

@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.

As with #14094 (review), I ran the changes to src through PHPStan level 8 and it reported back these opportunities for further refinement.

I added a suggestion to fix the issues in border.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
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/border.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  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
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/colors.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  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
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  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
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  Line   src/wp-includes/block-supports/spacing.php
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
  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
 ------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

Thanks @westonruter. The @phpstan-param and @phpstan-return shapes are now on all five helpers here, matching #14094. I've answered the two border suggestions inline.

Would you mind taking another look at both PRs?

… styles.

Adds wp_get_block_supports_classes_and_styles(), which combines the background, color, border, shadow and spacing helpers in the style engine's order. An optional list limits it to the supports a block applies to one element.
@aaronrobertshaw
aaronrobertshaw force-pushed the backport/block-supports-classes-and-styles-helper branch from 986ab3c to aa2f1d5 Compare November 26, 2026 07:01
@aaronrobertshaw

Copy link
Copy Markdown
Contributor Author

#14094 is now committed in r65798, and this PR has been rebased accordingly.

The diff now only has the new wp_get_block_supports_classes_and_styles() and its tests.

@westonruter unless you have concerns with that function, could you approve or dismiss your review so I can commit this one?

7.2 has branched, so Ramon's note about waiting is covered too.

* Default null, for all of them.
* @return array<string, string> Array with `class` and `style` keys, each present only when non-empty.
*
* @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }

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.

Bonus:

Suggested change
* @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }
* @phpstan-param array<'background'|'color'|'border'|'shadow'|'spacing'>|null $supports
* @phpstan-return array{ class?: non-empty-string, style?: non-empty-string }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants