Skip to content

Commit

Permalink
Ensure comment submission on AMP post redirects back to AMP and not n…
Browse files Browse the repository at this point in the history
…on-AMP
  • Loading branch information
westonruter committed Mar 22, 2018
1 parent f3218ea commit 2364049
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
12 changes: 8 additions & 4 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static function add_hooks() {
add_filter( 'comment_form_defaults', array( __CLASS__, 'filter_comment_form_defaults' ) );
add_filter( 'comment_reply_link', array( __CLASS__, 'filter_comment_reply_link' ), 10, 4 );
add_filter( 'cancel_comment_reply_link', array( __CLASS__, 'filter_cancel_comment_reply_link' ), 10, 3 );
add_action( 'comment_form', array( __CLASS__, 'add_amp_comment_form_templates' ), 100 );
add_action( 'comment_form', array( __CLASS__, 'amend_comment_form' ), 100 );
remove_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );

if ( AMP_Validation_Utils::should_validate_response() ) {
Expand Down Expand Up @@ -444,7 +444,7 @@ public static function filter_comment_post_redirect( $url, $comment ) {
*/
$message = apply_filters( 'amp_comment_posted_message', $message, $comment );

// Message will be shown in template defined by AMP_Theme_Support::add_amp_comment_form_templates().
// Message will be shown in template defined by AMP_Theme_Support::amend_comment_form().
wp_send_json( array(
'message' => self::wp_kses_amp_mustache( $message ),
) );
Expand Down Expand Up @@ -484,7 +484,7 @@ public static function handle_wp_die( $error, $title = '', $args = array() ) {
$error = $error->get_error_message();
}

// Message will be shown in template defined by AMP_Theme_Support::add_amp_comment_form_templates().
// Message will be shown in template defined by AMP_Theme_Support::amend_comment_form().
wp_send_json( array(
'error' => self::wp_kses_amp_mustache( $error ),
) );
Expand Down Expand Up @@ -643,8 +643,12 @@ public static function amp_set_comments_walker( $args ) {
/**
* Adds the form submit success and fail templates.
*/
public static function add_amp_comment_form_templates() {
public static function amend_comment_form() {
?>
<?php if ( is_singular() && ! amp_is_canonical() ) : ?>
<input type="hidden" name="redirect_to" value="<?php echo esc_url( amp_get_permalink( get_the_ID() ) ); ?>">
<?php endif; ?>

<div submit-success>
<template type="amp-mustache">
<p>{{{message}}}</p>
Expand Down
33 changes: 33 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ public function test_register_widgets() {
$this->assertArrayHasKey( 'AMP_Widget_Categories', $wp_widget_factory->widgets );
}

/**
* Test amend_comment_form().
*
* @covers AMP_Theme_Support::amend_comment_form()
*/
public function test_amend_comment_form() {
$post_id = $this->factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
$this->assertTrue( is_singular() );

// Test native AMP.
add_theme_support( 'amp' );
$this->assertTrue( amp_is_canonical() );
ob_start();
AMP_Theme_Support::amend_comment_form();
$output = ob_get_clean();
$this->assertNotContains( '<input type="hidden" name="redirect_to"', $output );
$this->assertContains( '<div submit-success>', $output );
$this->assertContains( '<div submit-error>', $output );

// Test paired AMP.
add_theme_support( 'amp', array(
'template_dir' => 'amp-templates',
) );
$this->assertFalse( amp_is_canonical() );
ob_start();
AMP_Theme_Support::amend_comment_form();
$output = ob_get_clean();
$this->assertContains( '<input type="hidden" name="redirect_to"', $output );
$this->assertContains( '<div submit-success>', $output );
$this->assertContains( '<div submit-error>', $output );
}

/**
* Test prepare_response.
*
Expand Down

0 comments on commit 2364049

Please sign in to comment.