-
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: Dequeue some disallowed scripts, and begin subclasses.
The build will fail, as the filtering in widget() isn't present. There isn't yet a way to instantiate or init() class AMP_Widgets(). But it unregisters the 'Archives' and 'Categories' widgets. And registers new widgets that subclass them. @todo: filter the output of widget() in these subclasses. And add support for the remaining widgets.
- Loading branch information
Ryan Kienstra
committed
Jan 17, 2018
1 parent
a4c8f36
commit 3faab47
Showing
7 changed files
with
337 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Archives | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Archives | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Archives extends WP_Widget_Archives { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to strip the onchange attribute | ||
* @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 $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Categories extends WP_Widget_Categories { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to strip the <script> tag. | ||
* @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 $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Register widgets, and add actions. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Register the widgets. | ||
*/ | ||
class AMP_Widgets { | ||
|
||
/** | ||
* Add the actions. | ||
* | ||
* @return void. | ||
*/ | ||
public function init() { | ||
add_action( 'widgets_init', array( $this, 'register_widgets' ) ); | ||
add_action( 'show_recent_comments_widget_style', '__return_false' ); | ||
add_action( 'wp_footer', array( $this, 'dequeue_scripts' ) ); | ||
} | ||
|
||
/** | ||
* Add the filters. | ||
* | ||
* @return void. | ||
*/ | ||
public function register_widgets() { | ||
$widgets = self::get_widgets(); | ||
foreach ( $widgets as $native_wp_widget => $amp_widget ) { | ||
unregister_widget( $native_wp_widget ); | ||
register_widget( $amp_widget ); | ||
} | ||
} | ||
|
||
/** | ||
* Get the widgets to unregister and register. | ||
* | ||
* @return array $widgets An associative array, with the previous WP widget mapped to the new AMP widget. | ||
*/ | ||
public function get_widgets() { | ||
return array( | ||
'WP_Widget_Archives' => 'AMP_Widget_Archives', | ||
'WP_Widget_Categories' => 'AMP_Widget_Categories', | ||
); | ||
} | ||
|
||
/** | ||
* Dequeue widget scripts and styles, which aren't allowed in AMP. | ||
* | ||
* Uses the action 'wp_footer' in order to prevent | ||
* 'wp_print_footer_scripts' from outputting the scripts. | ||
* | ||
* @return void. | ||
*/ | ||
public function dequeue_scripts() { | ||
wp_dequeue_script( 'wp-mediaelement' ); | ||
wp_dequeue_script( 'mediaelement-vimeo' ); | ||
wp_dequeue_style( 'wp-mediaelement' ); | ||
} | ||
|
||
} |
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,71 @@ | ||
<?php | ||
/** | ||
* Tests for class AMP_Widget_Archives. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Tests for class AMP_Widget_Archives. | ||
* | ||
* @package WidgetLiveEditor | ||
*/ | ||
class Test_AMP_Widget_Archives extends WP_UnitTestCase { | ||
/** | ||
* Instance of the widget. | ||
* | ||
* @var object | ||
*/ | ||
public $instance; | ||
|
||
/** | ||
* Setup. | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$amp_widgets = new AMP_Widgets(); | ||
$amp_widgets->register_widgets(); | ||
$this->instance = new AMP_Widget_Archives(); | ||
} | ||
|
||
/** | ||
* Test construct(). | ||
* | ||
* @see AMP_Widget_Archives::__construct(). | ||
*/ | ||
public function test_construct() { | ||
global $wp_widget_factory; | ||
$amp_archives = $wp_widget_factory->widgets['AMP_Widget_Archives']; | ||
$this->assertEquals( 'AMP_Widget_Archives', get_class( $amp_archives ) ); | ||
$this->assertEquals( 'archives', $amp_archives->id_base ); | ||
$this->assertEquals( 'Archives', $amp_archives->name ); | ||
$this->assertEquals( 'widget_archive', $amp_archives->widget_options['classname'] ); | ||
$this->assertEquals( true, $amp_archives->widget_options['customize_selective_refresh'] ); | ||
$this->assertEquals( 'A monthly archive of your site’s Posts.', $amp_archives->widget_options['description'] ); | ||
} | ||
|
||
/** | ||
* Test widget(). | ||
* | ||
* @see AMP_Widget_Archives::widget(). | ||
*/ | ||
public function test_widget() { | ||
$arguments = array( | ||
'before_widget' => '<div>', | ||
'after_widget' => '</div>', | ||
'before_title' => '<h2>', | ||
'after_title' => '</h2>', | ||
); | ||
$instance = array( | ||
'title' => 'Test Archives Widget', | ||
'dropdown' => 1, | ||
); | ||
ob_start(); | ||
$this->instance->widget( $arguments, $instance ); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertFalse( strpos( $output, 'onchange=' ) ); | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
/** | ||
* Tests for class AMP_Widget_Categories. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Tests for class AMP_Widget_Categories. | ||
* | ||
* @package WidgetLiveEditor | ||
*/ | ||
class Test_AMP_Widget_Categories extends WP_UnitTestCase { | ||
/** | ||
* Instance of the widget. | ||
* | ||
* @var object | ||
*/ | ||
public $instance; | ||
|
||
/** | ||
* Setup. | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$amp_widgets = new AMP_Widgets(); | ||
$amp_widgets->register_widgets(); | ||
$this->instance = new AMP_Widget_Categories(); | ||
} | ||
|
||
/** | ||
* Test construct(). | ||
* | ||
* @see AMP_Widget_Categories::__construct(). | ||
*/ | ||
public function test_construct() { | ||
global $wp_widget_factory; | ||
$amp_categories = $wp_widget_factory->widgets['AMP_Widget_Categories']; | ||
$this->assertEquals( 'AMP_Widget_Categories', get_class( $amp_categories ) ); | ||
$this->assertEquals( 'categories', $amp_categories->id_base ); | ||
$this->assertEquals( 'Categories', $amp_categories->name ); | ||
$this->assertEquals( 'widget_categories', $amp_categories->widget_options['classname'] ); | ||
$this->assertEquals( true, $amp_categories->widget_options['customize_selective_refresh'] ); | ||
$this->assertEquals( 'A list or dropdown of categories.', $amp_categories->widget_options['description'] ); | ||
} | ||
|
||
/** | ||
* Test widget(). | ||
* | ||
* @see AMP_Widget_Categories::widget(). | ||
*/ | ||
public function test_widget() { | ||
$arguments = array( | ||
'before_widget' => '<div>', | ||
'after_widget' => '</div>', | ||
'before_title' => '<h2>', | ||
'after_title' => '</h2>', | ||
); | ||
$instance = array( | ||
'title' => 'Test Categories Widget', | ||
'dropdown' => 1, | ||
); | ||
ob_start(); | ||
$this->instance->widget( $arguments, $instance ); | ||
$output = ob_get_clean(); | ||
|
||
$this->assertFalse( strpos( $output, '<script type=' ) ); | ||
} | ||
} |
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,67 @@ | ||
<?php | ||
/** | ||
* Tests for class AMP_Widgets. | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Tests for class AMP_Widgets. | ||
*/ | ||
class Test_AMP_Widgets extends WP_UnitTestCase { | ||
/** | ||
* Instance of AMP_Widgets. | ||
* | ||
* @var object | ||
*/ | ||
public $instance; | ||
|
||
/** | ||
* Setup. | ||
* | ||
* @inheritdoc | ||
*/ | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->instance = new AMP_Widgets(); | ||
} | ||
|
||
/** | ||
* Test init(). | ||
* | ||
* @see AMP_Widgets::init(). | ||
*/ | ||
public function test_init() { | ||
$this->instance->init(); | ||
$this->assertEquals( 10, has_filter( 'widgets_init', array( $this->instance, 'register_widgets' ) ) ); | ||
$this->assertEquals( 10, has_filter( 'show_recent_comments_widget_style', '__return_false' ) ); | ||
$this->assertEquals( 10, has_action( 'wp_footer', array( $this->instance, 'dequeue_scripts' ) ) ); | ||
} | ||
|
||
/** | ||
* Test register_widgets(). | ||
* | ||
* @covers AMP_Widgets::get_widgets(). | ||
* @see AMP_Widgets::register_widgets(). | ||
*/ | ||
public function test_register_widgets() { | ||
global $wp_widget_factory; | ||
$this->instance->register_widgets(); | ||
$this->assertFalse( isset( $wp_widget_factory->widgets['WP_Widget_Archives'] ) ); | ||
$this->assertEquals( 'AMP_Widget_Archives', get_class( $wp_widget_factory->widgets['AMP_Widget_Archives'] ) ); | ||
$this->assertFalse( isset( $wp_widget_factory->widgets['WP_Widget_Categories'] ) ); | ||
$this->assertEquals( 'AMP_Widget_Categories', get_class( $wp_widget_factory->widgets['AMP_Widget_Categories'] ) ); | ||
} | ||
|
||
/** | ||
* Test dequeue_scripts(). | ||
* | ||
* @see AMP_Widgets::dequeue_scripts(). | ||
*/ | ||
public function test_dequeue_scripts() { | ||
$this->assertFalse( wp_script_is( 'wp-mediaelement' ) ); | ||
$this->assertFalse( wp_script_is( 'mediaelement-vimeo' ) ); | ||
$this->assertFalse( wp_style_is( 'wp-mediaelement' ) ); | ||
} | ||
|
||
} |