From bec1944e44ed3e62e94a1a18213f4f7d6f174519 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 17 Sep 2015 22:10:46 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@34273 602fd350-edb4-49c9-b593-d223f7449a82 --- .../widgets/class-wp-widget-tag-cloud.php | 92 ++++++++++++++----- 1 file changed, 67 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/widgets/class-wp-widget-tag-cloud.php b/src/wp-includes/widgets/class-wp-widget-tag-cloud.php index 6390ace0f5..26b3fd8717 100644 --- a/src/wp-includes/widgets/class-wp-widget-tag-cloud.php +++ b/src/wp-includes/widgets/class-wp-widget-tag-cloud.php @@ -30,15 +30,6 @@ class WP_Widget_Tag_Cloud extends WP_Widget { } } - /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); - - echo $args['before_widget']; - if ( $title ) { - echo $args['before_title'] . $title . $args['after_title']; - } - echo '
'; - /** * Filter the taxonomy used in the Tag Cloud widget. * @@ -49,10 +40,27 @@ class WP_Widget_Tag_Cloud extends WP_Widget { * * @param array $current_taxonomy The taxonomy to use in the tag cloud. Default 'tags'. */ - wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array( - 'taxonomy' => $current_taxonomy + $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array( + 'taxonomy' => $current_taxonomy, + 'echo' => false ) ) ); + if ( empty( $tag_cloud ) ) { + return; + } + + /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ + $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); + + echo $args['before_widget']; + if ( $title ) { + echo $args['before_title'] . $title . $args['after_title']; + } + + echo '
'; + + echo $tag_cloud; + echo "
\n"; echo $args['after_widget']; } @@ -74,20 +82,54 @@ class WP_Widget_Tag_Cloud extends WP_Widget { */ public function form( $instance ) { $current_taxonomy = $this->_get_current_taxonomy($instance); - $title = isset( $instance['title'] ) ? $instance['title'] : ''; -?> -

-

-

-

get_field_id( 'title' ); + $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; + + echo '

+ +

'; + + $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' ); + $id = $this->get_field_id( 'taxonomy' ); + $name = $this->get_field_name( 'taxonomy' ); + $input = ''; + + switch ( count( $taxonomies ) ) { + + // No tag cloud supporting taxonomies found, display error message + case 0: + echo '

' . __( 'The tag cloud will not be displayed since their are no taxonomies that support the tag cloud widget.' ) . '

'; + printf( $input, '' ); + break; + + // Just a single tag cloud supporting taxonomy found, no need to display options + case 1: + $keys = array_keys( $taxonomies ); + $taxonomy = reset( $keys ); + printf( $input, esc_attr( $taxonomy ) ); + break; + + // More than one tag cloud supporting taxonomy found, display options + default: + printf( + '

' . + '

'; + } } /**