From dc3f5b43d94b13086690b76a1ee81808beb8828c Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 22 Jan 2018 16:40:34 -0800 Subject: [PATCH] Merge AMP_Widgets into AMP_Theme_Support --- amp.php | 1 - includes/admin/functions.php | 12 ---- includes/class-amp-autoloader.php | 1 - includes/class-amp-theme-support.php | 25 ++++++++ includes/widgets/class-amp-widgets.php | 62 ------------------- tests/test-class-amp-theme-support.php | 17 +++++ tests/test-class-amp-widget-archives.php | 30 +++------ tests/test-class-amp-widget-categories.php | 39 +++++------- tests/test-class-amp-widget-media-audio.php | 29 +++------ tests/test-class-amp-widget-media-gallery.php | 31 +++------- tests/test-class-amp-widget-media-image.php | 32 +++------- tests/test-class-amp-widget-media-video.php | 34 ++++------ .../test-class-amp-widget-recent-comments.php | 36 ++--------- tests/test-class-amp-widget-rss.php | 39 +++--------- tests/test-class-amp-widgets.php | 57 ----------------- 15 files changed, 122 insertions(+), 323 deletions(-) delete mode 100644 includes/widgets/class-amp-widgets.php delete mode 100644 tests/test-class-amp-widgets.php diff --git a/amp.php b/amp.php index 49fa2597e14..449a814ddc9 100644 --- a/amp.php +++ b/amp.php @@ -71,7 +71,6 @@ function amp_after_setup_theme() { } add_action( 'init', 'amp_init' ); - add_action( 'init', 'amp_add_widget_support' ); add_action( 'admin_init', 'AMP_Options_Manager::register_settings' ); add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' ); add_action( 'wp_loaded', 'amp_post_meta_box' ); diff --git a/includes/admin/functions.php b/includes/admin/functions.php index 957d3f2e0b5..6a1d4536e76 100644 --- a/includes/admin/functions.php +++ b/includes/admin/functions.php @@ -136,18 +136,6 @@ function amp_add_custom_analytics( $analytics ) { return $analytics; } -/** - * Bootstraps AMP widget support class, which registers AMP-compliant widgets. - * - * @since 0.7 - */ -function amp_add_widget_support() { - if ( is_amp_endpoint() ) { - $amp_widgets = new AMP_Widgets(); - $amp_widgets->init(); - } -} - /** * Bootstrap AMP post meta box. * diff --git a/includes/class-amp-autoloader.php b/includes/class-amp-autoloader.php index 5b7f208265c..ee480b09278 100644 --- a/includes/class-amp-autoloader.php +++ b/includes/class-amp-autoloader.php @@ -86,7 +86,6 @@ class AMP_Autoloader { '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', 'AMP_Test_World_Sanitizer' => 'tests/stubs', diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index 885f116b6ee..bf6c4f5b2a9 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -154,6 +154,8 @@ public static function register_paired_hooks() { */ public static function register_hooks() { + add_action( 'widgets_init', array( __CLASS__, 'register_widgets' ) ); + // Remove core actions which are invalid AMP. remove_action( 'wp_head', 'locale_stylesheet' ); remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); @@ -196,6 +198,29 @@ public static function register_hooks() { // @todo Add character conversion. } + /** + * Register/override widgets. + * + * @global WP_Widget_Factory + * @return void + */ + public static function register_widgets() { + global $wp_widget_factory; + foreach ( $wp_widget_factory->widgets as $registered_widget ) { + $registered_widget_class_name = get_class( $registered_widget ); + if ( ! preg_match( '/^WP_Widget_(.+)$/', $registered_widget_class_name, $matches ) ) { + continue; + } + $amp_class_name = 'AMP_Widget_' . $matches[1]; + if ( ! class_exists( $amp_class_name ) || is_a( $amp_class_name, $registered_widget_class_name ) ) { + continue; + } + + unregister_widget( $registered_widget_class_name ); + register_widget( $amp_class_name ); + } + } + /** * Register content embed handlers. * diff --git a/includes/widgets/class-amp-widgets.php b/includes/widgets/class-amp-widgets.php deleted file mode 100644 index 16dd0b6ed73..00000000000 --- a/includes/widgets/class-amp-widgets.php +++ /dev/null @@ -1,62 +0,0 @@ - $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() { - $widgets = array( - 'Archives', - 'Categories', - 'Media_Audio', - 'Media_Gallery', - 'Media_Image', - 'Media_Video', - 'Recent_Comments', - 'RSS', - ); - $mapped_widgets = array(); - - foreach ( $widgets as $widget ) { - $wp_widget = 'WP_Widget_' . $widget; - if ( class_exists( $wp_widget ) ) { - $mapped_widgets[ $wp_widget ] = 'AMP_Widget_' . $widget; - } - } - return $mapped_widgets; - } - -} diff --git a/tests/test-class-amp-theme-support.php b/tests/test-class-amp-theme-support.php index 5a10663bfb7..2e5f3a29d47 100644 --- a/tests/test-class-amp-theme-support.php +++ b/tests/test-class-amp-theme-support.php @@ -67,4 +67,21 @@ public function test_is_paired_available() { $this->assertTrue( is_search() ); $this->assertFalse( AMP_Theme_Support::is_paired_available() ); } + + /** + * Test register_widgets(). + * + * @covers AMP_Theme_Support::register_widgets() + * @global WP_Widget_Factory $wp_widget_factory + */ + public function test_register_widgets() { + global $wp_widget_factory; + remove_all_actions( 'widgets_init' ); + $wp_widget_factory->widgets = array(); + wp_widgets_init(); + AMP_Theme_Support::register_widgets(); + + $this->assertArrayNotHasKey( 'WP_Widget_Categories', $wp_widget_factory->widgets ); + $this->assertArrayHasKey( 'AMP_Widget_Categories', $wp_widget_factory->widgets ); + } } diff --git a/tests/test-class-amp-widget-archives.php b/tests/test-class-amp-widget-archives.php index 7ba55052f14..c90e834fe01 100644 --- a/tests/test-class-amp-widget-archives.php +++ b/tests/test-class-amp-widget-archives.php @@ -12,13 +12,6 @@ */ class Test_AMP_Widget_Archives extends WP_UnitTestCase { - /** - * Instance of the widget. - * - * @var object - */ - public $instance; - /** * Setup. * @@ -28,9 +21,6 @@ public function setUp() { parent::setUp(); wp_maybe_load_widgets(); AMP_Theme_Support::init(); - $amp_widgets = new AMP_Widgets(); - $amp_widgets->register_widgets(); - $this->instance = new AMP_Widget_Archives(); } /** @@ -39,14 +29,13 @@ public function setUp() { * @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'] ); + $widget = new AMP_Widget_Archives(); + $this->assertEquals( 'AMP_Widget_Archives', get_class( $widget ) ); + $this->assertEquals( 'archives', $widget->id_base ); + $this->assertEquals( 'Archives', $widget->name ); + $this->assertEquals( 'widget_archive', $widget->widget_options['classname'] ); + $this->assertEquals( true, $widget->widget_options['customize_selective_refresh'] ); + $this->assertEquals( 'A monthly archive of your site’s Posts.', $widget->widget_options['description'] ); } /** @@ -55,6 +44,7 @@ public function test_construct() { * @see AMP_Widget_Archives::widget(). */ public function test_widget() { + $widget = new AMP_Widget_Archives(); $arguments = array( 'before_widget' => '
', 'after_widget' => '
', @@ -66,10 +56,10 @@ public function test_widget() { 'dropdown' => 1, ); ob_start(); - $this->instance->widget( $arguments, $instance ); + $widget->widget( $arguments, $instance ); $output = ob_get_clean(); - $this->assertFalse( strpos( $output, 'onchange=' ) ); + $this->assertNotContains( 'onchange=', $output ); } } diff --git a/tests/test-class-amp-widget-categories.php b/tests/test-class-amp-widget-categories.php index 40b1170a4ba..9c73ce6e553 100644 --- a/tests/test-class-amp-widget-categories.php +++ b/tests/test-class-amp-widget-categories.php @@ -12,13 +12,6 @@ */ class Test_AMP_Widget_Categories extends WP_UnitTestCase { - /** - * Instance of the widget. - * - * @var object - */ - public $instance; - /** * Setup. * @@ -28,9 +21,6 @@ public function setUp() { parent::setUp(); wp_maybe_load_widgets(); AMP_Theme_Support::init(); - $amp_widgets = new AMP_Widgets(); - $amp_widgets->register_widgets(); - $this->instance = new AMP_Widget_Categories(); } /** @@ -39,14 +29,13 @@ public function setUp() { * @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'] ); + $widget = new AMP_Widget_Categories(); + $this->assertEquals( 'AMP_Widget_Categories', get_class( $widget ) ); + $this->assertEquals( 'categories', $widget->id_base ); + $this->assertEquals( 'Categories', $widget->name ); + $this->assertEquals( 'widget_categories', $widget->widget_options['classname'] ); + $this->assertEquals( true, $widget->widget_options['customize_selective_refresh'] ); + $this->assertEquals( 'A list or dropdown of categories.', $widget->widget_options['description'] ); } /** @@ -55,12 +44,13 @@ public function test_construct() { * @see AMP_Widget_Categories::modify_select(). */ public function test_modify_select() { - $categories = wp_dropdown_categories( array( + $widget = new AMP_Widget_Categories(); + $categories = wp_dropdown_categories( array( 'echo' => 0, ) ); - $number = 3; - $this->instance->number = $number; - $this->assertContains( strval( $number ), $this->instance->modify_select( $categories ) ); + $number = 3; + $widget->number = $number; + $this->assertContains( strval( $number ), $widget->modify_select( $categories ) ); } /** @@ -69,6 +59,7 @@ public function test_modify_select() { * @see AMP_Widget_Categories::widget(). */ public function test_widget() { + $widget = new AMP_Widget_Categories(); $arguments = array( 'before_widget' => '
', 'after_widget' => '
', @@ -80,10 +71,10 @@ public function test_widget() { 'dropdown' => 1, ); ob_start(); - $this->instance->widget( $arguments, $instance ); + $widget->widget( $arguments, $instance ); $output = ob_get_clean(); - $this->assertFalse( strpos( $output, '