Skip to content

Commit

Permalink
Issue #864: Workaround to include amp-form extension JS.
Browse files Browse the repository at this point in the history
Before, this wasn't included via the sanitizer.
The ideal solution would probably involve editing the sanitizer.
But this adds a filter to add the 'amp-form.'
And it removes the AMP sanitization of 'Categories' and 'Archives' widgets.
  • Loading branch information
Ryan Kienstra committed Jan 23, 2018
1 parent e0456e1 commit 40a73c6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 29 deletions.
26 changes: 23 additions & 3 deletions includes/widgets/class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@
*/
class AMP_Widget_Archives extends WP_Widget_Archives {

/**
* AMP_Widget_Archives constructor.
*
* @return void.
*/
public function __construct() {
parent::__construct();
add_filter( 'amp_component_scripts', array( $this, 'form_script' ) );
}

/**
* Add a form extension script for AMP to output.
*
* @param array $scripts The AMP scripts.
* @return array $scripts The filtered AMP scripts.
*/
public function form_script( $scripts ) {
if ( ! isset( $scripts['amp-form'] ) ) {
$scripts['amp-form'] = 'https://cdn.ampproject.org/v0/amp-form-latest.js';
}
return $scripts;
}

/**
* Echoes the markup of the widget.
*
Expand All @@ -33,7 +56,6 @@ public function widget( $args, $instance ) {
return;
}

ob_start();
$c = ! empty( $instance['count'] ) ? '1' : '0';
$d = ! empty( $instance['dropdown'] ) ? '1' : '0';

Expand Down Expand Up @@ -95,8 +117,6 @@ public function widget( $args, $instance ) {
<?php
endif;
echo wp_kses_post( $args['after_widget'] );
$output = ob_get_clean();
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
26 changes: 23 additions & 3 deletions includes/widgets/class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@
*/
class AMP_Widget_Categories extends WP_Widget_Categories {

/**
* AMP_Widget_Categories constructor.
*
* @return void.
*/
public function __construct() {
parent::__construct();
add_filter( 'amp_component_scripts', array( $this, 'form_script' ) );
}

/**
* Add a form extension script for AMP to output.
*
* @param array $scripts The AMP scripts.
* @return array $scripts The filtered AMP scripts.
*/
public function form_script( $scripts ) {
if ( ! isset( $scripts['amp-form'] ) ) {
$scripts['amp-form'] = 'https://cdn.ampproject.org/v0/amp-form-latest.js';
}
return $scripts;
}

/**
* Adds an 'on' attribute to the category dropdown's <select>.
*
Expand Down Expand Up @@ -42,7 +65,6 @@ public function widget( $args, $instance ) {
return;
}

ob_start();
static $first_dropdown = true;
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories', 'default' );
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
Expand Down Expand Up @@ -85,8 +107,6 @@ public function widget( $args, $instance ) {
<?php
endif;
echo wp_kses_post( $args['after_widget'] );
$output = ob_get_clean();
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
40 changes: 31 additions & 9 deletions tests/test-class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class Test_AMP_Widget_Archives extends WP_UnitTestCase {

/**
* Instance of the widget.
*
* @var object.
*/
public $widget;

/**
* Setup.
*
Expand All @@ -22,6 +29,7 @@ public function setUp() {
add_theme_support( 'amp' );
wp_maybe_load_widgets();
AMP_Theme_Support::init();
$this->widget = new AMP_Widget_Archives();
}

/**
Expand All @@ -30,13 +38,28 @@ public function setUp() {
* @see AMP_Widget_Archives::__construct().
*/
public function test_construct() {
$widget = new AMP_Widget_Archives();
$this->assertEquals( 'AMP_Widget_Archives', get_class( $widget ) );
$this->assertEquals( 'archives', $widget->id_base );
$this->assertEquals( 'Archives', $widget->name );
$this->assertEquals( 'widget_archive', $widget->widget_options['classname'] );
$this->assertEquals( true, $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'A monthly archive of your site&#8217;s Posts.', $widget->widget_options['description'] );
$this->assertEquals( 10, has_action( 'amp_component_scripts', array( $this->widget, 'form_script' ) ) );
$this->assertEquals( 'AMP_Widget_Archives', get_class( $this->widget ) );
$this->assertEquals( 'archives', $this->widget->id_base );
$this->assertEquals( 'Archives', $this->widget->name );
$this->assertEquals( 'widget_archive', $this->widget->widget_options['classname'] );
$this->assertEquals( true, $this->widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'A monthly archive of your site&#8217;s Posts.', $this->widget->widget_options['description'] );
}

/**
* Test form_script().
*
* @see AMP_Widget_Archives::form_script().
*/
public function test_form_script() {
$expected_key = 'amp-form';
$expected_script = 'https://cdn.ampproject.org/v0/amp-form-latest.js';
$scripts = array(
$expected_key => $expected_script,
);
$this->assertEquals( $scripts, $this->widget->form_script( $scripts ) );
$this->assertEquals( $scripts, $this->widget->form_script( array() ) );
}

/**
Expand All @@ -45,7 +68,6 @@ public function test_construct() {
* @see AMP_Widget_Archives::widget().
*/
public function test_widget() {
$widget = new AMP_Widget_Archives();
$arguments = array(
'before_widget' => '<div>',
'after_widget' => '</div>',
Expand All @@ -57,7 +79,7 @@ public function test_widget() {
'dropdown' => 1,
);
ob_start();
$widget->widget( $arguments, $instance );
$this->widget->widget( $arguments, $instance );
$output = ob_get_clean();

$this->assertNotContains( 'onchange=', $output );
Expand Down
49 changes: 35 additions & 14 deletions tests/test-class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class Test_AMP_Widget_Categories extends WP_UnitTestCase {

/**
* Instance of the widget.
*
* @var object.
*/
public $widget;

/**
* Setup.
*
Expand All @@ -22,6 +29,7 @@ public function setUp() {
add_theme_support( 'amp ' );
wp_maybe_load_widgets();
AMP_Theme_Support::init();
$this->widget = new AMP_Widget_Categories();
}

/**
Expand All @@ -30,13 +38,28 @@ public function setUp() {
* @see AMP_Widget_Categories::__construct().
*/
public function test_construct() {
$widget = new AMP_Widget_Categories();
$this->assertEquals( 'AMP_Widget_Categories', get_class( $widget ) );
$this->assertEquals( 'categories', $widget->id_base );
$this->assertEquals( 'Categories', $widget->name );
$this->assertEquals( 'widget_categories', $widget->widget_options['classname'] );
$this->assertEquals( true, $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'A list or dropdown of categories.', $widget->widget_options['description'] );
$this->assertEquals( 10, has_action( 'amp_component_scripts', array( $this->widget, 'form_script' ) ) );
$this->assertEquals( 'AMP_Widget_Categories', get_class( $this->widget ) );
$this->assertEquals( 'categories', $this->widget->id_base );
$this->assertEquals( 'Categories', $this->widget->name );
$this->assertEquals( 'widget_categories', $this->widget->widget_options['classname'] );
$this->assertEquals( true, $this->widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'A list or dropdown of categories.', $this->widget->widget_options['description'] );
}

/**
* Test form_script().
*
* @see AMP_Widget_Categories::form_script().
*/
public function test_form_script() {
$expected_key = 'amp-form';
$expected_script = 'https://cdn.ampproject.org/v0/amp-form-latest.js';
$scripts = array(
$expected_key => $expected_script,
);
$this->assertEquals( $scripts, $this->widget->form_script( $scripts ) );
$this->assertEquals( $scripts, $this->widget->form_script( array() ) );
}

/**
Expand All @@ -45,13 +68,12 @@ public function test_construct() {
* @see AMP_Widget_Categories::modify_select().
*/
public function test_modify_select() {
$widget = new AMP_Widget_Categories();
$categories = wp_dropdown_categories( array(
$categories = wp_dropdown_categories( array(
'echo' => 0,
) );
$number = 3;
$widget->number = $number;
$this->assertContains( strval( $number ), $widget->modify_select( $categories ) );
$number = 3;
$this->widget->number = $number;
$this->assertContains( strval( $number ), $this->widget->modify_select( $categories ) );
}

/**
Expand All @@ -60,7 +82,6 @@ public function test_modify_select() {
* @see AMP_Widget_Categories::widget().
*/
public function test_widget() {
$widget = new AMP_Widget_Categories();
$arguments = array(
'before_widget' => '<div>',
'after_widget' => '</div>',
Expand All @@ -72,7 +93,7 @@ public function test_widget() {
'dropdown' => 1,
);
ob_start();
$widget->widget( $arguments, $instance );
$this->widget->widget( $arguments, $instance );
$output = ob_get_clean();

$this->assertNotContains( '<script type=', $output );
Expand Down

0 comments on commit 40a73c6

Please sign in to comment.