Forms: default the Button block to a submit element inside a Form - #51046
Forms: default the Button block to a submit element inside a Form#51046enejb wants to merge 1 commit into
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
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! Jetpack plugin: No scheduled milestone found for this plugin. If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
Code Coverage SummaryCoverage changed in 2 files.
|
315dae0 to
ee29d98
Compare
There was a problem hiding this comment.
Pull request overview
This PR fixes legacy Jetpack Forms posts where an inner Jetpack Button block is missing the element attribute, causing it to render as an anchor (<a>) and therefore never submit the form. It introduces a filterable fallback element for the Button block and scopes a Forms override to only the duration of a form render.
Changes:
- Add a new
jetpack_button_default_elementfilter used when the Jetpack Button block has no explicitelementattribute. - In Jetpack Forms, temporarily hook that filter during form rendering so element-less Buttons inside a form default to a submit-capable
<button>, then remove the filter afterward. - Add PHPUnit coverage to verify the filter is applied during form render and removed afterward.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| projects/plugins/jetpack/extensions/blocks/button/button.php | Adds a filter for the default element fallback used by the Button block when the attribute is absent. |
| projects/plugins/jetpack/changelog/fix-forms-button-default-element | Adds Jetpack plugin changelog entry for the new filter. |
| projects/packages/forms/src/blocks/contact-form/class-contact-form-block.php | Adds/removes the Button default-element filter around form rendering; refactors render to ensure cleanup occurs. |
| projects/packages/forms/tests/php/contact-form/Contact_Form_Block_Test.php | Adds unit tests to confirm filter scoping behavior. |
| projects/packages/forms/changelog/fix-forms-button-default-element | Adds Forms package changelog entry describing the user-facing fix. |
| // A Button block that doesn't specify an element renders as an `a`, which can | ||
| // never submit the form. Default it to `button` for as long as this form is | ||
| // rendering; gutenblock_render_form() drops the filter again once it is done. | ||
| add_filter( 'jetpack_button_default_element', array( __CLASS__, 'submit_button_element' ) ); | ||
|
|
| Significance: patch | ||
| Type: other | ||
|
|
||
| Button block: add a filter for the default HTML element the block falls back to when it does not specify one. |
Fixes FORMS-741
Proposed changes
A Jetpack Button block that has no
elementattribute falls back to rendering an<a>(seeget_attribute()inprojects/plugins/jetpack/extensions/blocks/button/button.php). Inside a Form that produces a submit control that looks right but is actually an anchor:Clicking it opens a blank tab and the form is never submitted. The Form block's inner-block template does set
element: "button", so this only bites forms whose saved markup predates that or lost the attribute along the way.jetpack_button_default_elementfilter, instead of hardcoding'a'. Nothing changes on its own — the default is stilla, and the render callback's existing allowlist still ignores anything outsidea/button/input.buttonfor the duration of a form's render — added inpre_render_contact_form()(before the inner blocks render) and removed ingutenblock_render_form()(once they have). A Button that setselementexplicitly is untouched either way, since the filter only supplies the fallback.Scoping to the form's render span means it covers a button nested in a Group or any other wrapper inside the form, and stops applying the moment the form is done — a Calendly, Eventbrite, Payment Button or Subscribe block elsewhere on the page still renders its anchor.
gutenblock_render_form()has several early returns, so its body moved to a privaterender_form()and the public method became a thin wrapper that removes the filter. The public signature is unchanged.Two deliberate non-changes:
edit.jsxdraws aRichTextregardless ofelement) and saved post content is not rewritten — PHP simply supplies the fallback on every render, so no posts are dirtied on load.saveInPostContentmode still always renders an<a>. That is intentional and documented in the Button block's README (stricter WordPress.com sanitization rules), and the render callback returns early before the fallback is consulted.One known limitation worth a reviewer's eye: if a third-party
pre_render_blockfilter at a priority above 10 short-circuits the form block, the render callback never runs and the filter would stay attached for the rest of the request. Our own synced-form (ref) path callsgutenblock_render_form()directly, so it still cleans up after itself.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
You need a Form whose Button block has no
elementattribute — this is the legacy shape, and the editor will not produce it for you.<a class="wp-block-button__link" href="#" target="_blank" role="button">. Fill in the Name field and click Submit — a blank tab opens, nothing is submitted, and no new entry appears under Feedback / Form Responses.<a ... href="#"><button class="wp-block-button__link" type="submit"><a ... href="#"><button type="submit">.<!-- wp:jetpack/button {"element":"a","text":"Link"} /-->, must still render as an<a>.jp test php packages/formsVerified live on a Jurassic Ninja site: the three buttons render as anchor / button / anchor exactly as above, and a real form submission created a response (0 → 1).