-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add opt-in to prevent POST
forms from being converted to amp-form
(with action-xhr
)
#6527
Conversation
…n converted to amp-form
…t amp-form script
531de25
to
6550aa1
Compare
Plugin builds for ef4653e are ready 🛎️!
|
Noticed a bug while testing this; given the HTML: <form method="get" action-xhr="https://example.com"></form> It will be sanitized to become: <form method="get" action-xhr="https://example.com" action="//amp-dev.test/test-6527/?amp=1" target="_top" rel="amphtml" novalidate="" class="i-amphtml-form"><input name="amp" value="1" type="hidden"></form> The |
When the <form method="post" action-xhr="https://example.com"></form> Marking the validation error for that markup to "Kept" produces: <form method="post" action-xhr="https://example.com" data-ampdevmode="" novalidate="" class="i-amphtml-form"></form> Should the |
includes/amp-helper-functions.php
Outdated
* | ||
* @param bool $use_native Whether to allow native `POST` forms. | ||
*/ | ||
return (bool) apply_filters( 'amp_allowing_native_post_forms', false ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for using allowing
instead of allow
(or allows
)? Just wondering since we don't have any hooks that use present participle verbs, AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's amp_using_native_img
😄
If you think the filters make more sense as amp_use_native_img
and amp_allow_native_form
then that I'm happy with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had gone with singular for img
since imgs
looks bad.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then what about the function names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you think the filters make more sense as
amp_use_native_img
andamp_allow_native_form
then that I'm happy with that.
I was only concerned with it as it doesn't follow the usual naming conventions we use for our hooks, but either naming works for me 😄.
But then what about the function names?
They can stay if we're keeping the hook names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about:
Function | Filter |
---|---|
amp_is_native_img_used() |
amp_native_img_used |
amp_is_native_post_form_allowed() |
amp_native_post_form_allowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on board with that.
includes/class-amp-theme-support.php
Outdated
@@ -1514,6 +1513,11 @@ public static function ensure_required_markup( Document $dom, $script_handles = | |||
$superfluous_script_handles = array_diff( $superfluous_script_handles, [ 'amp-carousel' ] ); | |||
} | |||
|
|||
// When opting-in to POST forms, omit the amp-form component entirely since it blocks submission. | |||
if ( amp_is_allowing_native_post_forms() && $dom->xpath->query( '//form[ @method = "post" and @action ]' )->length > 0 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the value of @method
be case sensitive here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Fixed in dfb5223.
That's tricky. It turns out in the case of a See the above is valid (playground): Attempting to omit the |
I see, that makes sense. The docs also confirm using both
|
Oops. Yeah, POST forms with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Oh wait there seems to be some failing PHP tests. |
The failing test highlighted a bug in Core. The issue resides in this block of code, where there is no check to verify
|
Right, so it's a network issue. |
Created WordPress/wordpress-develop#1571 to fix the issue and it was just recently merged. |
QA Passed Input for Post method<form method="post" action-xhr="https://example.com"></form> Output for post method<form method="post" action-xhr="https://example.com" target="_top" novalidate="" class="i-amphtml-form"></form> Input for Get Method<form method="get" action-xhr="https://example.com"></form> Output for Post method<form method="get" action-xhr="https://example.com" action="//amp-local.test/form-test/amp/" target="_top" novalidate="" class="i-amphtml-form"></form> |
Summary
See #6443.
One of the very frequent issues encountered by users of the AMP plugin is POST form submissions not behaving properly. In fact, we have a support page all about handling form submissions. The issue is that AMP requires POST forms to be submitted over XHR rather than a traditional page navigation, but this breaks if the endpoint sends back a
Location
header to redirect or if the endpoint is on another host that doesn't send CORS headers. There is a feature request to allow non-XHR POST forms in AMP (ampproject/amphtml#27638), but for now we should allow an opt-in to prevent conversion of such POST forms and to omit theamp-form
component. Thisamp-form
component must be excluded or else it will intercept any submissions on POST forms to block them and show an error message.To opt-in to traditional non-XHR
POST
forms, do the following:When this is done, page that have such a form will now emit a new validation error:
FORM_HAS_POST_METHOD
. When the user marks this validation error as kept, then:form
is skipped.data-ampdevmode
attribute is added to theform
element and thehtml
root element (to prevent removal).amp-form
component script is omitted from the page.A user may decide to opt-in to preventing any such
POST
form from being converted by default via the following code:Checklist