Skip to content

Commit

Permalink
Issue #864: Add filtering to widget output, more subclasses.
Browse files Browse the repository at this point in the history
Use AMP_Theme_Support::filter_the_content().
Still, I need to ensure that this is the best way to filter.
Add RSS and Audio widget subclasses.
And PHPUnit classes for these.
  • Loading branch information
Ryan Kienstra committed Jan 18, 2018
1 parent 16caae9 commit 9bd0815
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 11 deletions.
2 changes: 2 additions & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ class AMP_Autoloader {
'AMP_WP_Utils' => 'includes/utils/class-amp-wp-utils',
'AMP_Widget_Archives' => 'includes/widgets/class-amp-widget-archives',
'AMP_Widget_Categories' => 'includes/widgets/class-amp-widget-categories',
'AMP_Widget_Media_Audio' => 'includes/widgets/class-amp-widget-media-audio',
'AMP_Widget_Media_Gallery' => 'includes/widgets/class-amp-widget-media-gallery',
'AMP_Widget_Media_Image' => 'includes/widgets/class-amp-widget-media-image',
'AMP_Widget_Media_Video' => 'includes/widgets/class-amp-widget-media-video',
'AMP_Widget_Recent_Comments' => 'includes/widgets/class-amp-widget-recent-comments',
'AMP_Widget_RSS' => 'includes/widgets/class-amp-widget-rss',
'AMP_Widgets' => 'includes/widgets/class-amp-widgets',
'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed',
'AMP_Test_Stub_Sanitizer' => 'tests/stubs',
Expand Down
2 changes: 1 addition & 1 deletion includes/widgets/class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function widget( $args, $instance ) {
ob_start();
parent::widget( $args, $instance );
$output = ob_get_clean();
echo $output; // WPCS: XSS ok.
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
2 changes: 1 addition & 1 deletion includes/widgets/class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function widget( $args, $instance ) {
ob_start();
parent::widget( $args, $instance );
$output = ob_get_clean();
echo $output; // WPCS: XSS ok.
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
30 changes: 30 additions & 0 deletions includes/widgets/class-amp-widget-media-audio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Class AMP_Widget_Media_Audio
*
* @package AMP
*/

/**
* Class AMP_Widget_Media_Audio
*
* @package AMP
*/
class AMP_Widget_Media_Audio extends WP_Widget_Media_Audio {

/**
* Echoes the markup of the widget.
*
* @todo filter $output, to convert <audio> to <amp-audio>.
* @see https://github.com/Automattic/amp-wp/issues/864
* @param array $instance Data for widget.
* @return void.
*/
public function render_media( $instance ) {
ob_start();
parent::render_media( $instance );
$output = ob_get_clean();
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
2 changes: 1 addition & 1 deletion includes/widgets/class-amp-widget-media-gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function render_media( $instance ) {
ob_start();
parent::render_media( $instance );
$output = ob_get_clean();
echo $output; // WPCS: XSS ok.
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
2 changes: 1 addition & 1 deletion includes/widgets/class-amp-widget-media-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function render_media( $instance ) {
ob_start();
parent::render_media( $instance );
$output = ob_get_clean();
echo $output; // WPCS: XSS ok.
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
2 changes: 1 addition & 1 deletion includes/widgets/class-amp-widget-media-video.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function render_media( $instance ) {
ob_start();
parent::render_media( $instance );
$output = ob_get_clean();
echo $output; // WPCS: XSS ok.
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
31 changes: 31 additions & 0 deletions includes/widgets/class-amp-widget-rss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Class AMP_Widget_RSS
*
* @package AMP
*/

/**
* Class AMP_Widget_RSS
*
* @package AMP
*/
class AMP_Widget_RSS extends WP_Widget_RSS {

/**
* Echoes the markup of the widget.
*
* @todo filter $output, to convert <img> to <amp-img>.
* @see https://github.com/Automattic/amp-wp/issues/864
* @param array $args Widget display data.
* @param array $instance Data for widget.
* @return void.
*/
public function widget( $args, $instance ) {
ob_start();
parent::widget( $args, $instance );
$output = ob_get_clean();
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
}

}
2 changes: 2 additions & 0 deletions includes/widgets/class-amp-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ public function get_widgets() {
return array(
'WP_Widget_Archives' => 'AMP_Widget_Archives',
'WP_Widget_Categories' => 'AMP_Widget_Categories',
'WP_Widget_Media_Audio' => 'AMP_Widget_Media_Audio',
'WP_Widget_Media_Gallery' => 'AMP_Widget_Media_Gallery',
'WP_Widget_Media_Image' => 'AMP_Widget_Media_Image',
'WP_Widget_Media_Video' => 'AMP_Widget_Media_Video',
'WP_Widget_Recent_Comments' => 'AMP_Widget_Recent_Comments',
'WP_Widget_RSS' => 'AMP_Widget_RSS',
);
}

Expand Down
1 change: 1 addition & 0 deletions tests/test-class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Test_AMP_Widget_Archives extends WP_UnitTestCase {
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Archives();
Expand Down
1 change: 1 addition & 0 deletions tests/test-class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Test_AMP_Widget_Categories extends WP_UnitTestCase {
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Categories();
Expand Down
82 changes: 82 additions & 0 deletions tests/test-class-amp-widget-media-audio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Tests for class AMP_Widget_Media_Audio.
*
* @package AMP
*/

/**
* Tests for class AMP_Widget_Media_Audio.
*
* @package AMP
*/
class Test_AMP_Widget_Media_Audio extends WP_UnitTestCase {

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

/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Media_Audio();
}

/**
* Test construct().
*
* @see AMP_Widget_Media_Audio::__construct().
*/
public function test_construct() {
global $wp_widget_factory;
$amp_widget = $wp_widget_factory->widgets['AMP_Widget_Media_Audio'];

$this->assertEquals( 'media_audio', $amp_widget->id_base );
$this->assertEquals( 'Audio', $amp_widget->name );
$this->assertEquals( 'widget_media_audio', $amp_widget->widget_options['classname'] );
$this->assertEquals( true, $amp_widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Displays an audio player.', $amp_widget->widget_options['description'] );
}

/**
* Test render_media().
*
* Mock audio logic mainly copied from Test_WP_Widget_Media_image::test_render_media().
*
* @see AMP_Widget_Media_Audio::render_media().
*/
public function test_render_media() {
$audio = '/tmp/small-audio.mp3';
copy( DIR_TESTDATA . '/uploads/small-audio.mp3', $audio );
$attachment_id = self::factory()->attachment->create_object( array(
'file' => $audio,
'post_parent' => 0,
'post_mime_type' => 'audio/mp3',
'post_title' => 'Test Audio',
) );
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $audio ) );
$instance = array(
'title' => 'Test Audio Widget',
'attachment_id' => $attachment_id,
'url' => 'https://example.com/amp',
);

ob_start();
$this->instance->render_media( $instance );
$output = ob_get_clean();

$this->assertFalse( strpos( $output, '<audio' ) );
$this->assertContains( '<amp-audio', $output );
}

}
5 changes: 3 additions & 2 deletions tests/test-class-amp-widget-media-gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Test_AMP_Widget_Media_Gallery extends WP_UnitTestCase {
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Media_Gallery();
Expand All @@ -48,11 +49,11 @@ public function test_construct() {
}

/**
* Test widget().
* Test render_media().
*
* Mock image logic mainly copied from Test_WP_Widget_Media_image::test_render_media().
*
* @see AMP_Widget_Media_Gallery::widget().
* @see AMP_Widget_Media_Gallery::render_media().
*/
public function test_render_media() {
$first_test_image = '/tmp/test-image.jpg';
Expand Down
5 changes: 3 additions & 2 deletions tests/test-class-amp-widget-media-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Test_AMP_Widget_Media_Image extends WP_UnitTestCase {
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Media_Image();
Expand All @@ -48,11 +49,11 @@ public function test_construct() {
}

/**
* Test widget().
* Test render_media().
*
* Mock image logic mainly copied from Test_WP_Widget_Media_image::test_render_media().
*
* @see AMP_Widget_Media_Image::widget().
* @see AMP_Widget_Media_Image::render_media().
*/
public function test_render_media() {
$first_test_image = '/tmp/test-image.jpg';
Expand Down
5 changes: 3 additions & 2 deletions tests/test-class-amp-widget-media-video.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Test_AMP_Widget_Media_Video extends WP_UnitTestCase {
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Media_Video();
Expand All @@ -48,11 +49,11 @@ public function test_construct() {
}

/**
* Test widget().
* Test render_media().
*
* Mock video logic mainly copied from Test_WP_Widget_Media_image::test_render_media().
*
* @see AMP_Widget_Media_Video::widget().
* @see AMP_Widget_Media_Video::render_media().
*/
public function test_render_media() {
$video = '/tmp/small-video.mp4';
Expand Down
1 change: 1 addition & 0 deletions tests/test-class-amp-widget-recent-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Test_AMP_Widget_Recent_Comments extends WP_UnitTestCase {
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_Recent_Comments();
Expand Down
81 changes: 81 additions & 0 deletions tests/test-class-amp-widget-rss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Tests for class AMP_Widget_RSS.
*
* @package AMP
*/

/**
* Tests for class AMP_Widget_RSS.
*
* @package AMP
*/
class Test_AMP_Widget_RSS extends WP_UnitTestCase {

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

/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
AMP_Theme_Support::init();
$amp_widgets = new AMP_Widgets();
$amp_widgets->register_widgets();
$this->instance = new AMP_Widget_RSS();
}

/**
* Test construct().
*
* @see AMP_Widget_RSS::__construct().
*/
public function test_construct() {
global $wp_widget_factory;
$amp_widget = $wp_widget_factory->widgets['AMP_Widget_RSS'];

$this->assertEquals( 'rss', $amp_widget->id_base );
$this->assertEquals( 'RSS', $amp_widget->name );
$this->assertEquals( 'widget_rss', $amp_widget->widget_options['classname'] );
$this->assertEquals( true, $amp_widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Entries from any RSS or Atom feed.', $amp_widget->widget_options['description'] );
}

/**
* Test widget().
*
* Mock video logic mainly copied from Test_WP_Widget_Media_image::test_render_media().
*
* @see AMP_Widget_RSS::widget().
*/
public function test_widget() {
$args = array(
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
);
$instance = array(
'title' => 'Test RSS Widget: With Content, Author, Date',
'url' => 'https://amphtml.wordpress.com/feed/',
'show_author' => 1,
'show_date' => 1,
'show_summary' => 1,
);

ob_start();
$this->instance->widget( $args, $instance );
$output = ob_get_clean();

$this->assertFalse( strpos( $output, '<img' ) );
$this->assertContains( '<amp-img', $output );
}

}

0 comments on commit 9bd0815

Please sign in to comment.