From bda8b79686b712537031884f96bd8ba39d41a19b Mon Sep 17 00:00:00 2001 From: Ben Keith Date: Wed, 16 Mar 2016 22:03:09 -0400 Subject: [PATCH 01/15] create new options, remove option to include only chosen terms, include ability to exclude chosen terms, make the taxonomies option a select instead of a text field, improve docs inside and outside the plugin --- inc/widgets/largo-taxonomy-list.php | 165 +++++++++++++++++++++++----- 1 file changed, 137 insertions(+), 28 deletions(-) diff --git a/inc/widgets/largo-taxonomy-list.php b/inc/widgets/largo-taxonomy-list.php index e4cf01828..168372234 100644 --- a/inc/widgets/largo-taxonomy-list.php +++ b/inc/widgets/largo-taxonomy-list.php @@ -1,9 +1,14 @@ element of terms, or 2) a customizable UL of terms. */ class largo_taxonomy_list_widget extends WP_Widget { + /** + * Constructor + */ function __construct() { $widget_ops = array( 'classname' => 'largo-taxonomy-list', @@ -12,6 +17,13 @@ function __construct() { parent::__construct( 'largo-taxonomy-list-widget', __('Largo Taxonomy List', 'largo'), $widget_ops); } + /** + * Output the widget + * + * @param array $args Sidebar-related args + * @param array $instance Instance-specific widget arguments + * @link https://developer.wordpress.org/reference/functions/get_terms/ + */ function widget( $args, $instance ) { extract( $args ); @@ -25,23 +37,34 @@ function widget( $args, $instance ) { if ( $title ) echo $before_title . $title . $after_title; - /* - * The widget - */ - $cat_args = array( - 'orderby' => 'name', + // Set us up the term args + $term_args = array( 'taxonomy' => $instance['taxonomy'], 'number' => $instance['count'], - 'include' => $instance['include'], + 'exclude' => $instance['exclude'], + 'orderby' => $instance['orderby'], + 'order' => $instance['order'], + ); + $defaults = array( + 'taxonomy' => 'series', + 'number' => 5, + 'exclude' => null, + 'orderby' => 'name', + 'order' => 'ASC' ); + $term_args = wp_parse_args( $term_args, $defaults ); + /* + * The dropdown option + */ if ( $is_dropdown ) { - $cats = get_categories($cat_args); ?> + $term_args['orderby'] = 'name'; + $terms = get_categories($term_args); ?> @@ -53,11 +76,14 @@ function widget( $args, $instance ) { }); - '; - $cat_args['title_li'] = ''; - $tax_items = get_categories($cat_args); + $tax_items = get_categories($term_args); switch ($instance['taxonomy']) { case 'series': @@ -112,6 +138,7 @@ private function render_li($item, $thumbnail = '', $headline = '') { * @uses largo_taxonomy_list_widget::render_li * @uses largo_featured_thumbnail_in_post_array * @uses largo_first_headline_in_post_array + * @since 0.5.3 */ private function render_series_list($tax_items, $instance) { foreach ($tax_items as $item) { @@ -160,6 +187,7 @@ private function render_series_list($tax_items, $instance) { * @uses largo_taxonomy_list_widget::render_li * @uses largo_featured_thumbnail_in_post_array * @uses largo_first_headline_in_post_array + * @since 0.5.3 */ private function render_cat_list($tax_items, $instance) { foreach ($tax_items as $item) { @@ -190,6 +218,7 @@ private function render_cat_list($tax_items, $instance) { * @uses largo_taxonomy_list_widget::render_li * @uses largo_featured_thumbnail_in_post_array * @uses largo_first_headline_in_post_array + * @since 0.5.3 */ private function render_tag_list($tax_items, $instance) { foreach ($tax_items as $item) { @@ -220,6 +249,7 @@ private function render_tag_list($tax_items, $instance) { * @uses largo_taxonomy_list_widget::render_li * @uses largo_featured_thumbnail_in_post_array * @uses largo_first_headline_in_post_array + * @since 0.5.3 */ private function render_term_list($tax_items, $instance) { foreach ($tax_items as $item) { @@ -250,58 +280,137 @@ private function render_term_list($tax_items, $instance) { } } + /** + * Sanitize and save widget arguments + */ function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = sanitize_text_field($new_instance['title']); - $instance['taxonomy'] = strtolower(strip_tags($new_instance['taxonomy'])); + $instance['taxonomy'] = isset($new_instance['taxonomy']) ? strtolower(strip_tags($new_instance['taxonomy'])) : 'series' ; + $instance['orderby'] = isset($new_instance['orderby']) ? strtolower(strip_tags($new_instance['orderby'])) : 'name' ; + $instance['order'] = isset($new_instance['order']) ? strtolower(strip_tags($new_instance['order'])) : 'ASC' ; + $instance['order'] = sanitize_text_field($new_instance['order']); $instance['count'] = sanitize_text_field($new_instance['count']); + + // Default is 5 as of 0.5.5, not infinite: see discussion on http://jira.inn.org/browse/HELPDESK-589 if ($instance['count'] == '' ) { - $instance['count'] = ''; + $instance['count'] = 5; } else if ($instance['count'] < 1) { $instance['count'] = 1; } - $instance['include'] = sanitize_text_field($new_instance['include']); $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0; $instance['thumbnails'] = !empty($new_instance['thumbnails']) ? 1 : 0; $instance['use_headline'] = !empty($new_instance['use_headline']) ? 1 : 0; + $instance['exclude'] = sanitize_text_field($new_instance['exclude']); return $instance; } + /** + * Render the widget form + */ function form( $instance ) { //Defaults $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'taxonomy' => '' ) ); $title = esc_attr( $instance['title'] ); - $taxonomy = esc_attr( $instance['taxonomy'] ); - $count = $instance['count']; - $include = $instance['include']; + $count = isset($instance['count']) ? esc_attr( $instance['count'] ) : 5; + $orderby = esc_attr( $instance['orderby'] ); + $order = esc_attr( $instance['order'] ); + $instance['taxonomy'] = isset( $instance['taxonomy'] ) ? $instance['taxonomy'] : 'series'; $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false; $thumbnails = isset( $instance['thumbnails'] ) ? (bool) $instance['thumbnails'] : false; $use_headline = isset( $instance['use_headline'] ) ? (bool) $instance['use_headline'] : false; + $exclude = $instance['exclude']; + + // Create ', + $sort_orderby, + selected( $instance['orderby'], $sort_orderby, false ), + __(ucwords($sort_orderby), 'largo') + ); + } + + // Create