Repository navigation
Spacing: Add a PHP helper for block support classes and styles - #84208
Conversation
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. 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:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: 0 B Total Size: 8.61 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in
|
92d5e78 to
cbada45
Compare
3f96758 to
474d78b
Compare
9022a44 to
0a22cd0
Compare
9ba6cbc to
d425f2c
Compare
d425f2c to
90d99a8
Compare
andrewserong
left a comment
There was a problem hiding this comment.
Comment Author Avatar, Icon, and Post Navigation Link blocks are all smoke testing nicely for me 👍
This set of API changes across these block supports PRs sounds really good to me, I was just bumping into this problem on a hacky server-rendered block I'm playing around with and adding these in will make for much better ergonomics 🎉
A couple of tiny questions:
- I imagine this is slated for 7.3 now instead of 7.2, so shall we change the changelog directory and
sincetags? - Once all these PRs are in, would it make sense to have a wrapper function that can call all the block supports internally rather than needing to call them each one after the other for each block? Don't worry if this comment doesn't make sense (or if I've gotten it wrong), I suspect this will become clearer once they're all in
LGTM! 🚀
b267598 to
0972b97
Compare
|
Thanks for testing these @andrewserong, and glad they're already proving useful 🙂
Yep, I've updated those references.
I like the idea. Most server-rendered blocks will end up calling a few of these in a row. I'd rather do it as a follow-up once all five helpers have landed, so it builds on a settled API. I've also added the same early return for non-array attributes that the other helpers have, and rebased on trunk. PHPStan landed on trunk this week and doesn't know the new helper yet, since it isn't in Core until WordPress/wordpress-develop#14094. So I've added a temporary baseline entry for it. |
cac98bf to
fdf2b52
Compare
23d2664 to
f02d3bc
Compare
Skip the wrapper style attribute when the spacing helper returns no styles, so an empty spacing object no longer prints style="". Drop the numeric spacing case, which tests style engine behaviour that differs from the editor, and trim the remaining tests to the cases that prove this change.
Matches the colour, border and background helpers, and orders the helper test docblock like theirs.
Also drops the Comment Author Avatar $spacing_string entry, which this PR fixes.
The block library calls wp_get_spacing_classes_and_styles(), which the build prefixes to gutenberg_get_spacing_classes_and_styles() for the plugin, but which PHPStan resolves from the WordPress stubs. The stubs were generated from core trunk, which lacks the function, so its calls were baselined as not found. Generate the stubs from wordpress-develop PR 14094, which backports the helper, and drop the three baselined errors. Regenerate them from trunk once the backport is committed. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: cfcf15e8-ccc1-4d78-a7c9-51f73e8e4b92
…ion until the stubs include it
f02d3bc to
330dd22
Compare
Related:
Core backport: wordpress-develop#14094.
What?
Adds a PHP helper that turns a block's padding and margin into inline styles, like the JS
getSpacingClassesAndStyles().The spacing block support and three blocks now use it.
Why?
A spacing preset on a Comment Author Avatar works in the editor but not on the front end. The block builds its spacing by hand as a raw string, so the preset is never resolved:
stylemargin-top: 5px;padding-top: var:preset|spacing|40padding-top:var(--wp--preset--spacing--40);margin-top:5px;Icon and Post Navigation Link also build spacing by hand. A shared helper keeps them in step.
How?
The helper doesn't check block supports, so each caller passes the values it applies.
divprefixFunctions, so the plugin callsgutenberg_get_spacing_classes_and_styles()Output changes
Only in Comment Author Avatar, now matching the spacing block support:
"top":10is dropped, as the block support already does"padding":"10px"outputs, not a PHP warningstyle=""PHPStan's WordPress stubs are built from Core trunk, which won't have this helper until wordpress-develop#14094 lands. So this PR adds a temporary baseline entry for it. PHPStan will flag the entry for removal once the stubs move to a Core trunk with the helper.
Testing Instructions
Comment Author Avatar
{"style":{"spacing":{"margin":{"top":"5px"},"padding":{"top":"var:preset|spacing|40"}}}}.padding-top:var(--wp--preset--spacing--40);margin-top:5px;.Markup matches trunk
Use of AI Tools
Implemented and tested by a Claude Code agent from a predefined task.