diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 05bbf021b9d..89d6b6a8131 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -78,6 +78,9 @@ class AMP_Autoloader { 'AMP_Image_Dimension_Extractor' => 'includes/utils/class-amp-image-dimension-extractor', 'AMP_String_Utils' => 'includes/utils/class-amp-string-utils', '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_Widgets' => 'includes/widgets/class-amp-widgets', 'WPCOM_AMP_Polldaddy_Embed' => 'wpcom/class-amp-polldaddy-embed', 'AMP_Test_Stub_Sanitizer' => 'tests/stubs', 'AMP_Test_World_Sanitizer' => 'tests/stubs', diff --git a/includes/widgets/class-amp-widget-archives.php b/includes/widgets/class-amp-widget-archives.php new file mode 100644 index 00000000000..3fe2a7cbe2f --- /dev/null +++ b/includes/widgets/class-amp-widget-archives.php @@ -0,0 +1,31 @@ + 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. + } + +} diff --git a/includes/widgets/class-amp-widgets.php b/includes/widgets/class-amp-widgets.php new file mode 100644 index 00000000000..255466d6f6c --- /dev/null +++ b/includes/widgets/class-amp-widgets.php @@ -0,0 +1,63 @@ + $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' ); + } + +} diff --git a/tests/test-class-amp-widget-archives.php b/tests/test-class-amp-widget-archives.php new file mode 100644 index 00000000000..3a3b27f037f --- /dev/null +++ b/tests/test-class-amp-widget-archives.php @@ -0,0 +1,71 @@ +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' => '
', + 'after_widget' => '
', + 'before_title' => '

', + 'after_title' => '

', + ); + $instance = array( + 'title' => 'Test Archives Widget', + 'dropdown' => 1, + ); + ob_start(); + $this->instance->widget( $arguments, $instance ); + $output = ob_get_clean(); + + $this->assertFalse( strpos( $output, 'onchange=' ) ); + } +} diff --git a/tests/test-class-amp-widget-categories.php b/tests/test-class-amp-widget-categories.php new file mode 100644 index 00000000000..c21d7ccd495 --- /dev/null +++ b/tests/test-class-amp-widget-categories.php @@ -0,0 +1,71 @@ +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' => '
', + 'after_widget' => '
', + 'before_title' => '

', + 'after_title' => '

', + ); + $instance = array( + 'title' => 'Test Categories Widget', + 'dropdown' => 1, + ); + ob_start(); + $this->instance->widget( $arguments, $instance ); + $output = ob_get_clean(); + + $this->assertFalse( strpos( $output, '