class-wp-widget-tag-cloud.php (6768B)
1 <?php 2 /** 3 * Widget API: WP_Widget_Tag_Cloud class 4 * 5 * @package WordPress 6 * @subpackage Widgets 7 * @since 4.4.0 8 */ 9 10 /** 11 * Core class used to implement a Tag cloud widget. 12 * 13 * @since 2.8.0 14 * 15 * @see WP_Widget 16 */ 17 class WP_Widget_Tag_Cloud extends WP_Widget { 18 19 /** 20 * Sets up a new Tag Cloud widget instance. 21 * 22 * @since 2.8.0 23 */ 24 public function __construct() { 25 $widget_ops = array( 26 'description' => __( 'A cloud of your most used tags.' ), 27 'customize_selective_refresh' => true, 28 'show_instance_in_rest' => true, 29 ); 30 parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops ); 31 } 32 33 /** 34 * Outputs the content for the current Tag Cloud widget instance. 35 * 36 * @since 2.8.0 37 * 38 * @param array $args Display arguments including 'before_title', 'after_title', 39 * 'before_widget', and 'after_widget'. 40 * @param array $instance Settings for the current Tag Cloud widget instance. 41 */ 42 public function widget( $args, $instance ) { 43 $current_taxonomy = $this->_get_current_taxonomy( $instance ); 44 45 if ( ! empty( $instance['title'] ) ) { 46 $title = $instance['title']; 47 } else { 48 if ( 'post_tag' === $current_taxonomy ) { 49 $title = __( 'Tags' ); 50 } else { 51 $tax = get_taxonomy( $current_taxonomy ); 52 $title = $tax->labels->name; 53 } 54 } 55 56 $show_count = ! empty( $instance['count'] ); 57 58 $tag_cloud = wp_tag_cloud( 59 /** 60 * Filters the taxonomy used in the Tag Cloud widget. 61 * 62 * @since 2.8.0 63 * @since 3.0.0 Added taxonomy drop-down. 64 * @since 4.9.0 Added the `$instance` parameter. 65 * 66 * @see wp_tag_cloud() 67 * 68 * @param array $args Args used for the tag cloud widget. 69 * @param array $instance Array of settings for the current widget. 70 */ 71 apply_filters( 72 'widget_tag_cloud_args', 73 array( 74 'taxonomy' => $current_taxonomy, 75 'echo' => false, 76 'show_count' => $show_count, 77 ), 78 $instance 79 ) 80 ); 81 82 if ( empty( $tag_cloud ) ) { 83 return; 84 } 85 86 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 87 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 88 89 echo $args['before_widget']; 90 if ( $title ) { 91 echo $args['before_title'] . $title . $args['after_title']; 92 } 93 94 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 95 96 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 97 $format = apply_filters( 'navigation_widgets_format', $format ); 98 99 if ( 'html5' === $format ) { 100 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 101 $title = trim( strip_tags( $title ) ); 102 $aria_label = $title ? $title : $default_title; 103 echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">'; 104 } 105 106 echo '<div class="tagcloud">'; 107 108 echo $tag_cloud; 109 110 echo "</div>\n"; 111 112 if ( 'html5' === $format ) { 113 echo '</nav>'; 114 } 115 116 echo $args['after_widget']; 117 } 118 119 /** 120 * Handles updating settings for the current Tag Cloud widget instance. 121 * 122 * @since 2.8.0 123 * 124 * @param array $new_instance New settings for this instance as input by the user via 125 * WP_Widget::form(). 126 * @param array $old_instance Old settings for this instance. 127 * @return array Settings to save or bool false to cancel saving. 128 */ 129 public function update( $new_instance, $old_instance ) { 130 $instance = array(); 131 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 132 $instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0; 133 $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] ); 134 return $instance; 135 } 136 137 /** 138 * Outputs the Tag Cloud widget settings form. 139 * 140 * @since 2.8.0 141 * 142 * @param array $instance Current settings. 143 */ 144 public function form( $instance ) { 145 $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; 146 $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false; 147 ?> 148 <p> 149 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 150 <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 ); ?>" /> 151 </p> 152 <?php 153 $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' ); 154 $current_taxonomy = $this->_get_current_taxonomy( $instance ); 155 156 switch ( count( $taxonomies ) ) { 157 158 // No tag cloud supporting taxonomies found, display error message. 159 case 0: 160 ?> 161 <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="" /> 162 <p> 163 <?php _e( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ); ?> 164 </p> 165 <?php 166 break; 167 168 // Just a single tag cloud supporting taxonomy found, no need to display a select. 169 case 1: 170 $keys = array_keys( $taxonomies ); 171 $taxonomy = reset( $keys ); 172 ?> 173 <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="<?php echo esc_attr( $taxonomy ); ?>" /> 174 <?php 175 break; 176 177 // More than one tag cloud supporting taxonomy found, display a select. 178 default: 179 ?> 180 <p> 181 <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:' ); ?></label> 182 <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>"> 183 <?php foreach ( $taxonomies as $taxonomy => $tax ) : ?> 184 <option value="<?php echo esc_attr( $taxonomy ); ?>" <?php selected( $taxonomy, $current_taxonomy ); ?>> 185 <?php echo esc_html( $tax->labels->name ); ?> 186 </option> 187 <?php endforeach; ?> 188 </select> 189 </p> 190 <?php 191 } 192 193 if ( count( $taxonomies ) > 0 ) { 194 ?> 195 <p> 196 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" <?php checked( $count, true ); ?> /> 197 <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show tag counts' ); ?></label> 198 </p> 199 <?php 200 } 201 } 202 203 /** 204 * Retrieves the taxonomy for the current Tag cloud widget instance. 205 * 206 * @since 4.4.0 207 * 208 * @param array $instance Current settings. 209 * @return string Name of the current taxonomy if set, otherwise 'post_tag'. 210 */ 211 public function _get_current_taxonomy( $instance ) { 212 if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) { 213 return $instance['taxonomy']; 214 } 215 216 return 'post_tag'; 217 } 218 }