-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #864: Add filtering to widget output, more subclasses.
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
Showing
17 changed files
with
245 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} |