Skip to content

Commit

Permalink
Merge AMP_Widgets into AMP_Theme_Support
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jan 23, 2018
1 parent da6cee6 commit dc3f5b4
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 323 deletions.
1 change: 0 additions & 1 deletion amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
12 changes: 0 additions & 12 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 0 additions & 1 deletion includes/class-amp-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
25 changes: 25 additions & 0 deletions includes/class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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.
*
Expand Down
62 changes: 0 additions & 62 deletions includes/widgets/class-amp-widgets.php

This file was deleted.

17 changes: 17 additions & 0 deletions tests/test-class-amp-theme-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}
30 changes: 10 additions & 20 deletions tests/test-class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
*/
class Test_AMP_Widget_Archives extends WP_UnitTestCase {

/**
* Instance of the widget.
*
* @var object
*/
public $instance;

/**
* Setup.
*
Expand All @@ -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();
}

/**
Expand All @@ -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'] );
}

/**
Expand All @@ -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' => '<div>',
'after_widget' => '</div>',
Expand All @@ -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 );
}

}
39 changes: 15 additions & 24 deletions tests/test-class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
*/
class Test_AMP_Widget_Categories extends WP_UnitTestCase {

/**
* Instance of the widget.
*
* @var object
*/
public $instance;

/**
* Setup.
*
Expand All @@ -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();
}

/**
Expand All @@ -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'] );
}

/**
Expand All @@ -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 ) );
}

/**
Expand All @@ -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' => '<div>',
'after_widget' => '</div>',
Expand All @@ -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, '<script type=' ) );
$this->assertNotContains( '<script type=', $output );
}

}
29 changes: 9 additions & 20 deletions tests/test-class-amp-widget-media-audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
*/
class Test_AMP_Widget_Media_Audio extends WP_UnitTestCase {

/**
* Instance of the widget.
*
* @var object
*/
public $instance;

/**
* Setup.
*
Expand All @@ -32,9 +25,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_Media_Audio();
}

/**
Expand All @@ -43,14 +33,12 @@ public function setUp() {
* @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'] );
$widget = new AMP_Widget_Media_Audio();
$this->assertEquals( 'media_audio', $widget->id_base );
$this->assertEquals( 'Audio', $widget->name );
$this->assertEquals( 'widget_media_audio', $widget->widget_options['classname'] );
$this->assertEquals( true, $widget->widget_options['customize_selective_refresh'] );
$this->assertEquals( 'Displays an audio player.', $widget->widget_options['description'] );
}

/**
Expand All @@ -61,6 +49,7 @@ public function test_construct() {
* @see AMP_Widget_Media_Audio::render_media().
*/
public function test_render_media() {
$widget = new AMP_Widget_Media_Audio();
$audio = '/tmp/small-audio.mp3';
copy( DIR_TESTDATA . '/uploads/small-audio.mp3', $audio );
$attachment_id = self::factory()->attachment->create_object( array(
Expand All @@ -77,10 +66,10 @@ public function test_render_media() {
);

ob_start();
$this->instance->render_media( $instance );
$widget->render_media( $instance );
$output = ob_get_clean();

$this->assertFalse( strpos( $output, '<audio' ) );
$this->assertNotContains( '<audio', $output );
$this->assertContains( '<amp-audio', $output );
}

Expand Down
Loading

0 comments on commit dc3f5b4

Please sign in to comment.