Skip to content

Commit

Permalink
Merge pull request #1033 from Automattic/fix/paired-mode-xhr
Browse files Browse the repository at this point in the history
Fix enqueueing amp-runtime and handling comment submissions in paired mode
  • Loading branch information
westonruter authored Mar 22, 2018
2 parents f957712 + 213d195 commit 7870892
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
26 changes: 18 additions & 8 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ public static function init() {
* action to template_redirect--the wp action--is used instead.
*/
add_action( 'wp', array( __CLASS__, 'finish_init' ), PHP_INT_MAX );

add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
}

/**
Expand Down Expand Up @@ -226,6 +224,7 @@ public static function add_hooks() {
add_action( 'wp_print_styles', array( __CLASS__, 'print_amp_styles' ), 0 ); // Print boilerplate before theme and plugin stylesheets.
add_action( 'wp_head', 'amp_add_generator_metadata', 20 );

add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
if ( is_customize_preview() ) {
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'dequeue_customize_preview_scripts' ), 1000 );
}
Expand All @@ -250,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 @@ -364,7 +363,14 @@ public static function send_header( $name, $value, $args = array() ) {
* @since 0.7.0
*/
public static function handle_xhr_request() {
if ( empty( self::$purged_amp_query_vars['__amp_source_origin'] ) || empty( $_SERVER['REQUEST_METHOD'] ) || 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
$is_amp_xhr = (
! empty( self::$purged_amp_query_vars['_wp_amp_action_xhr_converted'] )
&&
! empty( self::$purged_amp_query_vars['__amp_source_origin'] )
&&
( ! empty( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] )
);
if ( ! $is_amp_xhr ) {
return;
}

Expand Down Expand Up @@ -438,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 @@ -478,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 @@ -637,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 Expand Up @@ -1146,8 +1156,8 @@ public static function prepare_response( $response, $args = array() ) {
* @return void
*/
public static function enqueue_assets() {
// Enqueue AMP runtime.
wp_enqueue_script( 'amp-runtime' );

// Enqueue default styles expected by sanitizer.
wp_enqueue_style( 'amp-default', amp_get_asset_url( 'css/amp-default.css' ), array(), AMP__VERSION );
}
Expand Down
35 changes: 35 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 Expand Up @@ -279,6 +312,8 @@ public function test_handle_xhr_request() {
AMP_Theme_Support::handle_xhr_request();
$this->assertEmpty( AMP_Theme_Support::$headers_sent );

$_GET['_wp_amp_action_xhr_converted'] = '1';

// Try bad source origin.
$_GET['__amp_source_origin'] = 'http://evil.example.com/';
$_SERVER['REQUEST_METHOD'] = 'POST';
Expand Down

0 comments on commit 7870892

Please sign in to comment.