Make WordPress Core


Ignore:
Timestamp:
09/17/2015 10:10:46 PM (9 years ago)
Author:
wonderboymusic
Message:

Widgets: don't show a dropdown if there is only 1 taxonomy or zero taxonomies available to the Tag Cloud widget form. Don't output the widget if there are no terms in the selected taxonomy.

Props GautamGupta, wonderboymusic.
Fixes #16125.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-tag-cloud.php

    r33954 r34273  
    3131        }
    3232
    33         /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
    34         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    35 
    36         echo $args['before_widget'];
    37         if ( $title ) {
    38             echo $args['before_title'] . $title . $args['after_title'];
    39         }
    40         echo '<div class="tagcloud">';
    41 
    4233        /**
    4334         * Filter the taxonomy used in the Tag Cloud widget.
     
    5041         * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'.
    5142         */
    52         wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
    53             'taxonomy' => $current_taxonomy
     43        $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
     44            'taxonomy' => $current_taxonomy,
     45            'echo' => false
    5446        ) ) );
     47
     48        if ( empty( $tag_cloud ) ) {
     49            return;
     50        }
     51
     52        /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
     53        $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
     54
     55        echo $args['before_widget'];
     56        if ( $title ) {
     57            echo $args['before_title'] . $title . $args['after_title'];
     58        }
     59
     60        echo '<div class="tagcloud">';
     61
     62        echo $tag_cloud;
    5563
    5664        echo "</div>\n";
     
    7583    public function form( $instance ) {
    7684        $current_taxonomy = $this->_get_current_taxonomy($instance);
    77         $title = isset( $instance['title'] ) ? $instance['title'] : '';
    78 ?>
    79     <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
    80     <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr( $title ); ?>" /></p>
    81     <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
    82     <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
    83     <?php foreach ( get_taxonomies() as $taxonomy ) :
    84         $tax = get_taxonomy($taxonomy);
    85         if ( !$tax->show_tagcloud || empty($tax->labels->name) )
    86             continue;
    87     ?>
    88         <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo esc_attr( $tax->labels->name ); ?></option>
    89     <?php endforeach; ?>
    90     </select></p><?php
     85        $title_id = $this->get_field_id( 'title' );
     86        $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
     87
     88        echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label>
     89            <input type="text" class="widefat" id="' . $title_id .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" />
     90        </p>';
     91
     92        $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
     93        $id = $this->get_field_id( 'taxonomy' );
     94        $name = $this->get_field_name( 'taxonomy' );
     95        $input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
     96
     97        switch ( count( $taxonomies ) ) {
     98
     99        // No tag cloud supporting taxonomies found, display error message
     100        case 0:
     101            echo '<p>' . __( 'The tag cloud will not be displayed since their are no taxonomies that support the tag cloud widget.' ) . '</p>';
     102            printf( $input, '' );
     103            break;
     104
     105        // Just a single tag cloud supporting taxonomy found, no need to display options
     106        case 1:
     107            $keys = array_keys( $taxonomies );
     108            $taxonomy = reset( $keys );
     109            printf( $input, esc_attr( $taxonomy ) );
     110            break;
     111
     112        // More than one tag cloud supporting taxonomy found, display options
     113        default:
     114            printf(
     115                '<p><label for="%1$s">%2$s</label>' .
     116                '<select class="widefat" id="%1$s" name="%3$s">',
     117                $id,
     118                __( 'Taxonomy:' ),
     119                $name
     120            );
     121
     122            foreach ( $taxonomies as $taxonomy => $tax ) {
     123                printf(
     124                    '<option value="%s"%s>%s</option>',
     125                    esc_attr( $taxonomy ),
     126                    selected( $taxonomy, $current_taxonomy, false ),
     127                    $tax->labels->name
     128                );
     129            }
     130
     131            echo '</select></p>';
     132        }
    91133    }
    92134
Note: See TracChangeset for help on using the changeset viewer.