Skip to content

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

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

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.

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.

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.

2 participants