-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #870 from Automattic/add/864-native-widget-support
Add native WordPress widget support
- Loading branch information
Showing
12 changed files
with
502 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Archives | ||
* | ||
* @since 0.7.0 | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Archives | ||
* | ||
* @since 0.7.0 | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Archives extends WP_Widget_Archives { | ||
|
||
/** | ||
* 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. | ||
* | ||
* @since 0.7.0 | ||
* | ||
* @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; | ||
} | ||
|
||
$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" target="_top"> | ||
<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:AMP.navigateTo(url=event.value)"> | ||
<?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'] ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @since 0.7.0 | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Categories | ||
* | ||
* @since 0.7.0 | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Categories extends WP_Widget_Categories { | ||
|
||
/** | ||
* 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. | ||
* | ||
* @since 0.7.0 | ||
* | ||
* @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 ) : | ||
$form_id = sprintf( 'widget-categories-dropdown-%d', $this->number ); | ||
printf( '<form action="%s" method="get" target="_top" id="%s">', esc_url( home_url() ), esc_attr( $form_id ) ); | ||
$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; | ||
|
||
$dropdown = wp_dropdown_categories( array_merge( | ||
/** This filter is documented in wp-includes/widgets/class-wp-widget-categories.php */ | ||
apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ), | ||
array( 'echo' => false ) | ||
) ); | ||
$dropdown = preg_replace( | ||
'/(?<=<select\b)/', | ||
sprintf( '<select on="change:%s.submit"', esc_attr( $form_id ) ), | ||
$dropdown, | ||
1 | ||
); | ||
echo $dropdown; // WPCS: XSS OK. | ||
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'] ); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Class AMP_Widget_Recent_Comments | ||
* | ||
* @since 0.7.0 | ||
* @package AMP | ||
*/ | ||
|
||
/** | ||
* Class AMP_Widget_Recent_Comments | ||
* | ||
* @since 0.7.0 | ||
* @package AMP | ||
*/ | ||
class AMP_Widget_Recent_Comments extends WP_Widget_Recent_Comments { | ||
|
||
/** | ||
* Instantiates the widget, and prevents inline styling. | ||
* | ||
* @since 0.7.0 | ||
*/ | ||
public function __construct() { | ||
parent::__construct(); | ||
add_filter( 'wp_head', array( $this, 'remove_head_style_in_amp' ), 0 ); | ||
} | ||
|
||
/** | ||
* Prevent recent comments widget style from printing in AMP, | ||
* | ||
* @since 0.7.0 | ||
*/ | ||
public function remove_head_style_in_amp() { | ||
if ( is_amp_endpoint() ) { | ||
add_filter( 'show_recent_comments_widget_style', '__return_false' ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.