Repository navigation
Block Supports: Add PHP helpers for block support classes and styles - #14094
aaronrobertshaw wants to merge 13 commits into
Conversation
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
a4d85e7 to
fee7818
Compare
|
I've been held up on some other priorities so this one will now be aimed for WP 7.3 |
fee7818 to
1d36d13
Compare
f8e168a to
3ebd5d1
Compare
… is null or empty.
…nd spacing helpers.
3ebd5d1 to
4020b55
Compare
|
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: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
westonruter
left a comment
There was a problem hiding this comment.
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)
|
Thanks for running this through level 10 @westonruter. I've added I've kept the The remaining errors are in the existing For anyone re-reviewing: Core's PHPStan check runs at level 5, which this PR passes. Level 10 only applies to the paths in So I've left the remaining |
@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. |
|
I think this is okay to commit? I ran PHPStan level 10 on the five block-supports files. The new I reckon the errors left over are fine to leave for follow ups for reasons mentioned:
There are 4 new ones as far as I can tell, where the |
|
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
left a comment
There was a problem hiding this comment.
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.
We can't do anything right now anyway because we're in RC and we haven't branched for 7.2. |
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
|
westonruter
left a comment
There was a problem hiding this comment.
LGTM, with some suggestions to remove unnecessary is_array() checks in the new functions.
What makes the For a little more context, I believe the parser will default to returning wordpress-develop/src/wp-includes/class-wp-block-parser.php Lines 180 to 184 in 748d6d3 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. |
I couldn't see anything but arrays being passed in to these functions. For example, So it is already guaranteed to be an array by the time it passes |
|
Thanks for the approvals and the @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 With the check, that block gets no classes. With a native The So I'll leave the native type suggestions unapplied. Now that 7.2 has branched, I'll commit this one unless there are objections. |
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
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
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()andwp_get_background_classes_and_styles(),wp_apply_border_support(),wp_apply_shadow_support(),wp_apply_spacing_support(),wp_apply_colors_support()andwp_render_background_support()built on them,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:
Both branches output:
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.