Skip to content

Commit

Permalink
Issue #864: Remove dequeuing of scripts, and create comment subclass.
Browse files Browse the repository at this point in the history
As Weston mentioned, the plugin already prevents scripts in:
'wp_print_footer_scripts'.
Also, move the comment filter to a subclass of WP_Widget_Recent_Comments.
Includes PHPUnit tests for all of these changes.
  • Loading branch information
Ryan Kienstra committed Jan 17, 2018
1 parent 3faab47 commit be58d02
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 35 deletions.
1 change: 1 addition & 0 deletions includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ 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_Recent_Comments' => 'includes/widgets/class-amp-widget-recent-comments',
'AMP_Widgets' => 'includes/widgets/class-amp-widgets',
'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed',
'AMP_Test_Stub_Sanitizer' => 'tests/stubs',
Expand Down
23 changes: 23 additions & 0 deletions includes/widgets/class-amp-widget-recent-comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Class AMP_Widget_Recent_Comments
*
* @package AMP
*/

/**
* Class AMP_Widget_Recent_Comments
*
* @package AMP
*/
class AMP_Widget_Recent_Comments extends WP_Widget_Recent_Comments {

/**
* Instantiates the widget, and prevents inline styling.
*/
public function __construct() {
parent::__construct();
add_filter( 'show_recent_comments_widget_style', '__return_false' );
}

}
21 changes: 3 additions & 18 deletions includes/widgets/class-amp-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class AMP_Widgets {
*/
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' ) );
}

/**
Expand All @@ -41,23 +39,10 @@ public function register_widgets() {
*/
public function get_widgets() {
return array(
'WP_Widget_Archives' => 'AMP_Widget_Archives',
'WP_Widget_Categories' => 'AMP_Widget_Categories',
'WP_Widget_Archives' => 'AMP_Widget_Archives',
'WP_Widget_Categories' => 'AMP_Widget_Categories',
'WP_Widget_Recent_Comments' => 'AMP_Widget_Recent_Comments',
);
}

/**
* 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' );
}

}
4 changes: 3 additions & 1 deletion tests/test-class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
/**
* Tests for class AMP_Widget_Archives.
*
* @package WidgetLiveEditor
* @package AMP
*/
class Test_AMP_Widget_Archives extends WP_UnitTestCase {

/**
* Instance of the widget.
*
Expand Down Expand Up @@ -68,4 +69,5 @@ public function test_widget() {

$this->assertFalse( strpos( $output, 'onchange=' ) );
}

}
4 changes: 3 additions & 1 deletion tests/test-class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
/**
* Tests for class AMP_Widget_Categories.
*
* @package WidgetLiveEditor
* @package AMP
*/
class Test_AMP_Widget_Categories extends WP_UnitTestCase {

/**
* Instance of the widget.
*
Expand Down Expand Up @@ -68,4 +69,5 @@ public function test_widget() {

$this->assertFalse( strpos( $output, '<script type=' ) );
}

}
51 changes: 51 additions & 0 deletions tests/test-class-amp-widget-recent-comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Tests for class AMP_Widget_Recent_Comments.
*
* @package AMP
*/

/**
* Tests for class AMP_Widget_Recent_Comments.
*
* @package AMP
*/
class Test_AMP_Widget_Recent_Comments 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_Recent_Comments();
}

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

$this->assertEquals( 'recent-comments', $amp_categories->id_base );
$this->assertEquals( 'Recent Comments', $amp_categories->name );
$this->assertEquals( 'widget_recent_comments', $amp_categories->widget_options['classname'] );
$this->assertEquals( true, $amp_categories->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Your site&#8217;s most recent comments.', $amp_categories->widget_options['description'] );
$this->assertFalse( apply_filters( 'show_recent_comments_widget_style', true ) );
}

}
20 changes: 5 additions & 15 deletions tests/test-class-amp-widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ 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' ) ) );
}

/**
Expand All @@ -47,21 +46,12 @@ public function test_init() {
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'] ) );
}
$widgets = $this->instance->get_widgets();

/**
* 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' ) );
foreach ( $widgets as $native_wp_widget => $amp_widget ) {
$this->assertFalse( isset( $wp_widget_factory->widgets[ $native_wp_widget ] ) );
$this->assertEquals( $amp_widget, get_class( $wp_widget_factory->widgets[ $amp_widget ] ) );
}
}

}

0 comments on commit be58d02

Please sign in to comment.