Skip to content

Commit

Permalink
Issue #864: Support 'Categories' widget dropdown.
Browse files Browse the repository at this point in the history
Props @westonruter for the details of how to do this.
Mainly copy WP_Widget_Categories::widget() into the subclass widget().
Add an 'id' attribute to the <form>.
And use the 'id' in the dropdown <select>.
This dropdown now redirects to the category pages, as expected.
Also, bootstrap the widget support class.
  • Loading branch information
Ryan Kienstra committed Jan 19, 2018
1 parent 9bd0815 commit fb9b451
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
1 change: 1 addition & 0 deletions amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ 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
10 changes: 10 additions & 0 deletions includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ 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() {
$amp_widgets = new AMP_Widgets();
$amp_widgets->init();
}

/**
* Bootstrap AMP post meta box.
*
Expand Down
81 changes: 75 additions & 6 deletions includes/widgets/class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,89 @@
*/
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 ) );
return str_replace( '<select', $new_select, $dropdown );
}

/**
* Echoes the markup of the widget.
*
* @todo filter $output, to strip the <script> tag.
* @see https://github.com/Automattic/amp-wp/issues/864
* 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 ) {
ob_start();
parent::widget( $args, $instance );
$output = ob_get_clean();
echo AMP_Theme_Support::filter_the_content( $output ); // WPCS: XSS ok.
static $first_dropdown = true;
$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' ); // phpcs:ignore.
/** 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' ); // phpcs:ignore.
$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.
*/
wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
?>
</ul>
<?php
endif;
echo wp_kses_post( $args['after_widget'] );
}

}
14 changes: 14 additions & 0 deletions tests/test-class-amp-widget-categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ public function test_construct() {
$this->assertEquals( 'A list or dropdown of categories.', $amp_categories->widget_options['description'] );
}

/**
* Test modify_select().
*
* @see AMP_Widget_Categories::modify_select().
*/
public function test_modify_select() {
$categories = wp_dropdown_categories( array(
'echo' => 0,
) );
$number = 3;
$this->instance->number = $number;
$this->assertContains( strval( $number ), $this->instance->modify_select( $categories ) );
}

/**
* Test widget().
*
Expand Down

0 comments on commit fb9b451

Please sign in to comment.