-
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 24 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,122 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Archives | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Archives | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Archives extends WP_Widget_Archives { | ||
|
||
/** | ||
* AMP_Widget_Archives constructor. | ||
* | ||
* @return void. | ||
*/ | ||
public function __construct() { | ||
parent::__construct(); | ||
add_filter( 'amp_component_scripts', array( $this, 'form_script' ) ); | ||
} | ||
|
||
/** | ||
* Add a form extension script for AMP to output. | ||
* | ||
* @param array $scripts The AMP scripts. | ||
* @return array $scripts The filtered AMP scripts. | ||
*/ | ||
public function form_script( $scripts ) { | ||
if ( ! isset( $scripts['amp-form'] ) ) { | ||
$scripts['amp-form'] = 'https://cdn.ampproject.org/v0/amp-form-latest.js'; | ||
} | ||
return $scripts; | ||
} | ||
|
||
/** | ||
* Echoes the markup of the widget. | ||
* | ||
* Mainly copied from WP_Widget_Archives::widget() | ||
* Changes include: | ||
* An id for the <form>. | ||
* More escaping. | ||
* 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 ) { | ||
if ( ! is_amp_endpoint() ) { | ||
parent::widget( $args, $instance ); | ||
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. Excellent move to defer the |
||
return; | ||
} | ||
|
||
$c = ! empty( $instance['count'] ) ? '1' : '0'; | ||
$d = ! empty( $instance['dropdown'] ) ? '1' : '0'; | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ | ||
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Archives', 'default' ) : $instance['title'], $instance, $this->id_base ); | ||
echo wp_kses_post( $args['before_widget'] ); | ||
if ( $title ) : | ||
echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] ); | ||
endif; | ||
|
||
if ( $d ) : | ||
$dropdown_id = "{$this->id_base}-dropdown-{$this->number}"; | ||
?> | ||
<form action="<?php echo esc_url( home_url() ); ?>" method="get" id="widget-archives-dropdown-<?php echo esc_attr( $this->number ); ?>"> | ||
<label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo esc_html( $title ); ?></label> | ||
<select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" on="change:widget-archives-dropdown-<?php echo esc_attr( $this->number ); ?>.submit"> | ||
<?php | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ | ||
$dropdown_args = apply_filters( 'widget_archives_dropdown_args', array( | ||
'type' => 'monthly', | ||
'format' => 'option', | ||
'show_post_count' => $c, | ||
) ); | ||
|
||
switch ( $dropdown_args['type'] ) { | ||
case 'yearly': | ||
$label = __( 'Select Year', 'default' ); | ||
break; | ||
case 'monthly': | ||
$label = __( 'Select Month', 'default' ); | ||
break; | ||
case 'daily': | ||
$label = __( 'Select Day', 'default' ); | ||
break; | ||
case 'weekly': | ||
$label = __( 'Select Week', 'default' ); | ||
break; | ||
default: | ||
$label = __( 'Select Post', 'default' ); | ||
break; | ||
} | ||
?> | ||
<option value=""><?php echo esc_attr( $label ); ?></option> | ||
<?php wp_get_archives( $dropdown_args ); ?> | ||
</select> | ||
</form> | ||
<?php else : ?> | ||
<ul> | ||
<?php | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-archives.php */ | ||
wp_get_archives( apply_filters( 'widget_archives_args', array( | ||
'type' => 'monthly', | ||
'show_post_count' => $c, | ||
) ) ); | ||
?> | ||
</ul> | ||
<?php | ||
endif; | ||
echo wp_kses_post( $args['after_widget'] ); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Categories extends WP_Widget_Categories { | ||
|
||
/** | ||
* AMP_Widget_Categories constructor. | ||
* | ||
* @return void. | ||
*/ | ||
public function __construct() { | ||
parent::__construct(); | ||
add_filter( 'amp_component_scripts', array( $this, 'form_script' ) ); | ||
} | ||
|
||
/** | ||
* Add a form extension script for AMP to output. | ||
* | ||
* @param array $scripts The AMP scripts. | ||
* @return array $scripts The filtered AMP scripts. | ||
*/ | ||
public function form_script( $scripts ) { | ||
if ( ! isset( $scripts['amp-form'] ) ) { | ||
$scripts['amp-form'] = 'https://cdn.ampproject.org/v0/amp-form-latest.js'; | ||
} | ||
return $scripts; | ||
} | ||
|
||
/** | ||
* 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 ) { | ||
if ( ! is_amp_endpoint() ) { | ||
parent::widget( $args, $instance ); | ||
return; | ||
} | ||
|
||
static $first_dropdown = true; | ||
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories', 'default' ); | ||
/** 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>'; | ||
$cat_args['show_option_none'] = __( 'Select Category', 'default' ); | ||
$cat_args['id'] = $dropdown_id; | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-categories.php */ | ||
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'] = ''; | ||
|
||
/** This filter is documented in wp-includes/widgets/class-wp-widget-categories.php */ | ||
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,25 @@ | ||
<?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(); | ||
if ( is_amp_endpoint() ) { | ||
add_filter( 'show_recent_comments_widget_style', '__return_false' ); | ||
} | ||
} | ||
|
||
} |
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.
This is unnecessary once you merged from
develop
in 62a924f