Skip to content

Commit

Permalink
Remove support for handling non-redirecting post requests, for now
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Feb 2, 2018
1 parent 92f3a76 commit 1593f88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 52 deletions.
31 changes: 4 additions & 27 deletions includes/amp-helper-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,49 +400,26 @@ function amp_print_schemaorg_metadata() {
}

/**
* Hook into a comment submission of an AMP XHR post request.
*
* This only runs on wp-comments-post.php.
* Hook into a form submissions, such as comment the form or some other .
*
* @since 0.7.0
* @global string $pagenow
*/
function amp_handle_xhr_request() {
global $pagenow;
if ( isset( $_GET['__amp_redirect'] ) ) { // WPCS: CSRF ok.
add_action( 'template_redirect', function() {
// grab post data.
$transint_name = wp_unslash( $_GET['__amp_redirect'] ); // WPCS: CSRF ok, input var ok.
$_POST = get_transient( $transint_name );
delete_transient( $transint_name );
}, 0 );
}
if ( ! isset( $_GET['__amp_source_origin'] ) || ! isset( $pagenow ) ) { // WPCS: CSRF ok. Beware of AMP_Theme_Support::purge_amp_query_vars().
if ( ! isset( $_GET['__amp_source_origin'] ) ) { // WPCS: CSRF ok. Beware of AMP_Theme_Support::purge_amp_query_vars().
return;
}

if ( 'wp-comments-post.php' === $pagenow ) {
if ( isset( $pagenow ) && 'wp-comments-post.php' === $pagenow ) {
// This only runs on wp-comments-post.php.
add_filter( 'comment_post_redirect', function() {
// We don't need any data, so just send a success.
wp_send_json_success();
}, PHP_INT_MAX, 2 );
amp_handle_xhr_headers_output();
} elseif ( isset( $_GET['_wp_amp_action_xhr_converted'] ) ) { // WPCS: CSRF ok.
// Add amp redirect hooks.
add_filter( 'wp_redirect', 'amp_intercept_post_request_redirect', PHP_INT_MAX, 2 );
add_action( 'template_redirect', function() {
// grab post data.
$transient_name = uniqid();
set_transient( $transient_name, wp_unslash( $_POST ), 60 ); // WPCS: CSRF ok, input var ok.

/*
* Buffering starts here, so unlikely the form has a redirect,
* so force a redirect to the same page.
*/
$location = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ); // WPCS: CSRF ok, input var ok.
$location = add_query_arg( '__amp_redirect', $transient_name, $location );
amp_intercept_post_request_redirect( $location );
}, 0 );
amp_handle_xhr_headers_output();
}

Expand Down
37 changes: 12 additions & 25 deletions includes/sanitizers/class-amp-form-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function sanitize() {
$action_url = add_query_arg( '_wp_amp_action_xhr_converted', 1, $action_url );
$node->setAttribute( 'action-xhr', $action_url );
// Append error handler if not found.
$this->error_handler( $node );
$this->ensure_submit_error_element( $node );
} elseif ( 'http://' === substr( $xhr_action, 0, 7 ) ) {
$node->setAttribute( 'action-xhr', substr( $xhr_action, 5 ) );
}
Expand All @@ -118,37 +118,24 @@ public function sanitize() {
*
* @link https://www.ampproject.org/docs/reference/components/amp-form#success/error-response-rendering
* @since 0.7
* @param DOMElement $node The form node to check.
*
* @param DOMElement $form The form node to check.
*/
public function error_handler( $node ) {
$templates = $node->getElementsByTagName( 'template' );
if ( $templates->length ) {
for ( $i = $templates->length - 1; $i >= 0; $i-- ) {
if ( $templates->item( $i )->parentNode->hasAttribute( 'submit-error' ) ) {
return; // Found error template, do nothing.
}
public function ensure_submit_error_element( $form ) {
$templates = $form->getElementsByTagName( 'template' );
for ( $i = $templates->length - 1; $i >= 0; $i-- ) {
if ( $templates->item( $i )->parentNode->hasAttribute( 'submit-error' ) ) {
return; // Found error template, do nothing.
}
}
$node->appendChild( $this->create_error_template() );
}

/**
* Creates a error handler element node.
*
* @link https://www.ampproject.org/docs/reference/components/amp-form#success/error-response-rendering
* @since 0.7
*
* @return DOMElement The div[submit-error] element.
*/
public function create_error_template() {
$node = $this->dom->createElement( 'div' );
$div = $this->dom->createElement( 'div' );
$template = $this->dom->createElement( 'template' );
$mustache = $this->dom->createTextNode( '{{{error}}}' );
$node->setAttribute( 'submit-error', '' );
$div->setAttribute( 'submit-error', '' );
$template->setAttribute( 'type', 'amp-mustache' );
$template->appendChild( $mustache );
$node->appendChild( $template );

return $node;
$div->appendChild( $template );
$form->appendChild( $div );
}
}

0 comments on commit 1593f88

Please sign in to comment.