-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #864: Native WordPress widget support, including subclasses #870
Changes from 9 commits
3faab47
be58d02
436722d
44287d8
c7268bf
16caae9
9bd0815
fb9b451
cbdbd4a
6ee75ee
e703850
a355243
99dd862
74d727b
fc48208
da6cee6
dc3f5b4
28b5cdd
a0b9848
6053f9a
e0456e1
40a73c6
62a924f
c28578c
6f82cc7
2081ab0
0a78830
0e26178
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok. | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Categories extends WP_Widget_Categories { | ||
|
||
/** | ||
* Adds an 'on' attribute to the category dropdown's <select>. | ||
* | ||
* @param string $dropdown The markup of the category dropdown. | ||
* @return string $dropdown The filtered markup of the dropdown. | ||
*/ | ||
public function modify_select( $dropdown ) { | ||
$new_select = sprintf( '<select on="change:widget-categories-dropdown-%d.submit"', esc_attr( $this->number ) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Brilliant use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, Weston! |
||
return str_replace( '<select', $new_select, $dropdown ); | ||
} | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* Mainly copied from WP_Widget_Categories::widget() | ||
* There's now an id for the <form>. | ||
* And the dropdown is now filtered with 'wp_dropdown_cats.' | ||
* This enables adding an 'on' attribute, with the id of the form. | ||
* So changing the dropdown value will redirect to the category page, with valid AMP. | ||
* | ||
* @param array $args Widget display data. | ||
* @param array $instance Data for widget. | ||
* @return void. | ||
*/ | ||
public function widget( $args, $instance ) { | ||
static $first_dropdown = true; | ||
// @codingStandardsIgnoreLine | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this for? Is it for the text domain? If so, I suggest editing the <property name="text_domain" value="amp,default" /> And then add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, @westonruter. That |
||
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' ); | ||
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ | ||
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); | ||
$c = ! empty( $instance['count'] ) ? '1' : '0'; | ||
$h = ! empty( $instance['hierarchical'] ) ? '1' : '0'; | ||
$d = ! empty( $instance['dropdown'] ) ? '1' : '0'; | ||
echo wp_kses_post( $args['before_widget'] ); | ||
if ( $title ) { | ||
echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] ); | ||
} | ||
$cat_args = array( | ||
'orderby' => 'name', | ||
'show_count' => $c, | ||
'hierarchical' => $h, | ||
); | ||
if ( $d ) : | ||
echo sprintf( '<form action="%s" method="get" id="widget-categories-dropdown-%d">', esc_url( home_url() ), esc_attr( $this->number ) ); | ||
add_filter( 'wp_dropdown_cats', array( $this, 'modify_select' ) ); | ||
$dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}"; | ||
$first_dropdown = false; | ||
echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . esc_html( $title ) . '</label>'; | ||
// @codingStandardsIgnoreLine | ||
$cat_args['show_option_none'] = __( 'Select Category' ); | ||
$cat_args['id'] = $dropdown_id; | ||
/** | ||
* Filters the arguments for the Categories widget drop-down. | ||
* | ||
* @since 2.8.0 | ||
* @since 4.9.0 Added the `$instance` parameter. | ||
* | ||
* @see wp_dropdown_categories() | ||
* | ||
* @param array $cat_args An array of Categories widget drop-down arguments. | ||
* @param array $instance Array of settings for the current widget. | ||
*/ | ||
wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) ); | ||
remove_filter( 'wp_dropdown_cats', array( $this, 'modify_select' ) ); | ||
echo '</form>'; | ||
else : | ||
?> | ||
<ul> | ||
<?php | ||
$cat_args['title_li'] = ''; | ||
/** | ||
* Filters the arguments for the Categories widget. | ||
* | ||
* @since 2.8.0 | ||
* @since 4.9.0 Added the `$instance` parameter. | ||
* | ||
* @param array $cat_args An array of Categories widget options. | ||
* @param array $instance Array of settings for the current widget. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of copying the phpdoc, you can There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, this line applies your suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do the same with the |
||
*/ | ||
wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) ); | ||
?> | ||
</ul> | ||
<?php | ||
endif; | ||
echo wp_kses_post( $args['after_widget'] ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Media_Audio | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Media_Audio | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Media_Audio extends WP_Widget_Media_Audio { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to convert <audio> to <amp-audio>. | ||
* @see https://github.com/Automattic/amp-wp/issues/864 | ||
* @param array $instance Data for widget. | ||
* @return void. | ||
*/ | ||
public function render_media( $instance ) { | ||
ob_start(); | ||
parent::render_media( $instance ); | ||
$output = ob_get_clean(); | ||
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok. | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Media_Gallery | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Media_Gallery | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Media_Gallery extends WP_Widget_Media_Gallery { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to convert <imp> to <amp-img> and remove the <style>. | ||
* @see https://github.com/Automattic/amp-wp/issues/864 | ||
* @param array $instance Data for widget. | ||
* @return void. | ||
*/ | ||
public function render_media( $instance ) { | ||
ob_start(); | ||
parent::render_media( $instance ); | ||
$output = ob_get_clean(); | ||
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok. | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Media_Image | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Media_Image | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Media_Image extends WP_Widget_Media_Image { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to convert <imp> to <amp-img> and remove the 'style' attribute. | ||
* @see https://github.com/Automattic/amp-wp/issues/864 | ||
* @param array $instance Data for widget. | ||
* @return void. | ||
*/ | ||
public function render_media( $instance ) { | ||
ob_start(); | ||
parent::render_media( $instance ); | ||
$output = ob_get_clean(); | ||
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok. | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Media_Video | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Media_Video | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Media_Video extends WP_Widget_Media_Video { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to convert <video> to <amp-video> and remove the 'style' attribute. | ||
* @see https://github.com/Automattic/amp-wp/issues/864 | ||
* @param array $instance Data for widget. | ||
* @return void. | ||
*/ | ||
public function render_media( $instance ) { | ||
ob_start(); | ||
parent::render_media( $instance ); | ||
$output = ob_get_clean(); | ||
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok. | ||
} | ||
|
||
} |
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' ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_RSS | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_RSS | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_RSS extends WP_Widget_RSS { | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* @todo filter $output, to convert <img> to <amp-img>. | ||
* @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 AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok. | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this only be done if
amp_is_canonical()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @westonruter, good catch. It should at least check that it is_amp_endpoint(). We wouldn't want these subclass widgets on a standard WP page.
If by chance we're going to use the widget templates in this theme pull request, we should probably check that
amp_is_canonical()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The theme should be styling whatever markup the the widgets in core (or the plugin) output. The AMP widget functionality you're adding here should only apply if
is_amp_endpoint()
because this will true both when viewing AMP in paired mode and when canonical.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @westonruter. You probably saw it, but this commit applies your suggestion.
I had tried to wrap the action hook in the conditional:
add_action( 'init', 'amp_add_widget_support' )
But this produced a notice:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed a suggested change in dc3f5b4 to fix.