Skip to content

Commit

Permalink
Issue #864: Make 'Archives' widget dropdown AMP-compliant.
Browse files Browse the repository at this point in the history
Props @westonruter for describing how to do this.
Like before, it mainly copies WP_Widget_Archives::widget().
It adds an id to the <form>.
And an 'on' attribute to the <select> element.
  • Loading branch information
Ryan Kienstra committed Jan 23, 2018
1 parent 28b5cdd commit a0b9848
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
75 changes: 69 additions & 6 deletions includes/widgets/class-amp-widget-archives.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,80 @@ 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
* 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 ) {
ob_start();
parent::widget( $args, $instance );
$output = ob_get_clean();
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
$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'] );
}

}
11 changes: 2 additions & 9 deletions includes/widgets/class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,8 @@ public function widget( $args, $instance ) {
<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.
*/

/** 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>
Expand Down
2 changes: 1 addition & 1 deletion tests/test-class-amp-widget-media-audio.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function test_construct() {
*/
public function test_render_media() {
$widget = new AMP_Widget_Media_Audio();
$audio = '/tmp/small-audio.mp3';
$audio = '/tmp/small-audio.mp3';
copy( DIR_TESTDATA . '/uploads/small-audio.mp3', $audio );
$attachment_id = self::factory()->attachment->create_object( array(
'file' => $audio,
Expand Down

0 comments on commit a0b9848

Please sign in to comment.