From 1593f882256d6d835db9a0e73ec9d0128f297a8c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 2 Feb 2018 14:33:01 -0800 Subject: [PATCH] Remove support for handling non-redirecting post requests, for now --- includes/amp-helper-functions.php | 31 ++-------------- .../sanitizers/class-amp-form-sanitizer.php | 37 ++++++------------- 2 files changed, 16 insertions(+), 52 deletions(-) diff --git a/includes/amp-helper-functions.php b/includes/amp-helper-functions.php index bf25bd7ec4d..fc09db8580d 100644 --- a/includes/amp-helper-functions.php +++ b/includes/amp-helper-functions.php @@ -400,27 +400,18 @@ 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. @@ -428,21 +419,7 @@ function amp_handle_xhr_request() { }, 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(); } diff --git a/includes/sanitizers/class-amp-form-sanitizer.php b/includes/sanitizers/class-amp-form-sanitizer.php index c3cdfe437b1..05c6252f0cc 100644 --- a/includes/sanitizers/class-amp-form-sanitizer.php +++ b/includes/sanitizers/class-amp-form-sanitizer.php @@ -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 ) ); } @@ -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 ); } }