Forms: Fix device visibility CSS classes not applied to field wrapper on frontend#49945
Forms: Fix device visibility CSS classes not applied to field wrapper on frontend#49945agent-sandbox-automattic[bot] wants to merge 5 commits into
Conversation
…y support When a user applies device visibility settings (or any custom CSS classes) to a Jetpack form field block via the block editor, the resulting CSS classes (stored in the block's className attribute) were not being propagated to the outermost wrapper div of the rendered field on the frontend. Instead, the classes were only applied to the inner input element and were also transformed with a -wrap suffix on the outer div (e.g. wp-hide-on-desktop became wp-hide-on-desktop-wrap), which breaks the CSS selectors used by the block visibility feature. This fix ensures that custom CSS classes (after extracting the is-style-* block style variation classes) are also added to fieldwrapperclasses, so they appear on the outermost field wrapper element where device visibility CSS rules expect them. Co-authored-by: CGastrell <christian.gastrell@automattic.com>
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Code Coverage SummaryCannot generate coverage summary while tests are failing. 🤐 Please fix the tests, or re-run the Code coverage job if it was something being flaky. |
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
|
Thanks for picking this up — but I don't think this hides fields on the frontend. Device visibility isn't a Direction's still open in FORMS-694: either replicate core's class+CSS generation (support it), or disable the control on fields so the non-working option disappears (drafted in #49973). Probably worth settling that first. |
There was a problem hiding this comment.
Pull request overview
This PR fixes Jetpack Forms frontend rendering so block className values (including device visibility classes like wp-hide-on-desktop) are propagated to the outermost form-field wrapper element, aligning frontend markup with block visibility CSS expectations.
Changes:
- Update
block_attributes_to_shortcode_attributes()to add remaining (non-is-style-*) classes tofieldwrapperclasses. - Extend PHPUnit coverage to assert device visibility classes are present on the wrapper classes.
- Add a patch-level changelog entry documenting the fix.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| projects/packages/forms/src/contact-form/class-contact-form-plugin.php | Adds remaining custom classes to fieldwrapperclasses so wrapper markup receives device visibility classes. |
| projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php | Updates/extends test expectations to cover wrapper class propagation (including device visibility classes). |
| projects/packages/forms/changelog/ai-agent-forms-694-1782340429 | Adds changelog entry for the user-facing fix. |
| // Also apply remaining custom classes (e.g. device visibility classes) directly | ||
| // to the field wrapper so they take effect on the outermost wrapper element. | ||
| if ( ! empty( $atts['class'] ) ) { | ||
| $atts['fieldwrapperclasses'] .= ' ' . $atts['class']; | ||
| } |
…trol on inner field sub-blocks
Proposed changes
Form fields were not respecting the block editor's device visibility settings ("Hide on desktop / tablet / mobile") on the front end.
Root cause: Core's block-visibility support stores the setting in the block's
metadata.blockVisibilityand applies awp-block-hidden-{viewport}class via arender_blockfilter that writes the class onto the first tag of the block's rendered output (alongside the@media { … display: none !important }CSS it emits). Jetpack form fields render through the shortcode pipeline rather thanrender_block, so the class never reaches the rendered field wrapper — core still outputs the hide CSS, but nothing on the field carries the class, so the field stays visible.Fixes:
block_attributes_to_shortcode_attributes()re-derives thewp-block-hidden-{viewport}classes from the field block'smetadata.blockVisibilityand adds them to the field wrapper, where the CSS that core already outputs expects them.Device visibility only makes sense at the field (and label) level, so the per-block visibility control is disabled (
supports.visibility: false) on the inner field sub-blocks —input,phone-input,input-range,input-rating,option,options. Thelabelsub-block keeps the control.A required field that is hidden for the visitor's current device no longer blocks submission. The client-side validation skips fields whose element is not rendered for the current viewport (
offsetParent === null), and the server skips required validation for an empty field whose wrapper carries awp-block-hidden-*class (the server can't know the viewport). Required fields that are visible are still enforced.An empty device-hidden field is no longer listed on the post-submission confirmation page (a device-visibility field that was filled in on a viewport where it shows still appears). Storage and email notifications are unchanged.
Testing instructions
<div class="wp-block-jetpack-field-text wp-block-hidden-tablet …">carries the class, and core's@mediarule hides it.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.