WP_Widget_Tag_Cloud

Core class used to implement a Tag cloud widget.

Defined (1)

The class is defined in the following location(s).

/wp-includes/widgets/class-wp-widget-tag-cloud.php  
  1. class WP_Widget_Tag_Cloud extends WP_Widget { 
  2.  
  3. /** 
  4. * Sets up a new Tag Cloud widget instance. 
  5. * @since 2.8.0 
  6. * @access public 
  7. */ 
  8. public function __construct() { 
  9. $widget_ops = array( 
  10. 'description' => __( 'A cloud of your most used tags.' ),  
  11. 'customize_selective_refresh' => true,  
  12. ); 
  13. parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops ); 
  14.  
  15. /** 
  16. * Outputs the content for the current Tag Cloud widget instance. 
  17. * @since 2.8.0 
  18. * @access public 
  19. * @param array $args Display arguments including 'before_title', 'after_title',  
  20. * 'before_widget', and 'after_widget'. 
  21. * @param array $instance Settings for the current Tag Cloud widget instance. 
  22. */ 
  23. public function widget( $args, $instance ) { 
  24. $current_taxonomy = $this->_get_current_taxonomy($instance); 
  25. if ( !empty($instance['title']) ) { 
  26. $title = $instance['title']; 
  27. } else { 
  28. if ( 'post_tag' == $current_taxonomy ) { 
  29. $title = __('Tags'); 
  30. } else { 
  31. $tax = get_taxonomy($current_taxonomy); 
  32. $title = $tax->labels->name; 
  33.  
  34. /** 
  35. * Filters the taxonomy used in the Tag Cloud widget. 
  36. * @since 2.8.0 
  37. * @since 3.0.0 Added taxonomy drop-down. 
  38. * @see wp_tag_cloud() 
  39. * @param array $args Args used for the tag cloud widget. 
  40. */ 
  41. $tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array( 
  42. 'taxonomy' => $current_taxonomy,  
  43. 'echo' => false 
  44. ) ) ); 
  45.  
  46. if ( empty( $tag_cloud ) ) { 
  47. return; 
  48.  
  49. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ 
  50. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 
  51.  
  52. echo $args['before_widget']; 
  53. if ( $title ) { 
  54. echo $args['before_title'] . $title . $args['after_title']; 
  55.  
  56. echo '<div class="tagcloud">'; 
  57.  
  58. echo $tag_cloud; 
  59.  
  60. echo "</div>\n"; 
  61. echo $args['after_widget']; 
  62.  
  63. /** 
  64. * Handles updating settings for the current Tag Cloud widget instance. 
  65. * @since 2.8.0 
  66. * @access public 
  67. * @param array $new_instance New settings for this instance as input by the user via 
  68. * WP_Widget::form(). 
  69. * @param array $old_instance Old settings for this instance. 
  70. * @return array Settings to save or bool false to cancel saving. 
  71. */ 
  72. public function update( $new_instance, $old_instance ) { 
  73. $instance = array(); 
  74. $instance['title'] = sanitize_text_field( $new_instance['title'] ); 
  75. $instance['taxonomy'] = stripslashes($new_instance['taxonomy']); 
  76. return $instance; 
  77.  
  78. /** 
  79. * Outputs the Tag Cloud widget settings form. 
  80. * @since 2.8.0 
  81. * @access public 
  82. * @param array $instance Current settings. 
  83. */ 
  84. public function form( $instance ) { 
  85. $current_taxonomy = $this->_get_current_taxonomy($instance); 
  86. $title_id = $this->get_field_id( 'title' ); 
  87. $instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; 
  88.  
  89. echo '<p><label for="' . $title_id .'">' . __( 'Title:' ) . '</label> 
  90. <input type="text" class="widefat" id="' . $title_id .'" name="' . $this->get_field_name( 'title' ) .'" value="' . $instance['title'] .'" /> 
  91. </p>'; 
  92.  
  93. $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'object' ); 
  94. $id = $this->get_field_id( 'taxonomy' ); 
  95. $name = $this->get_field_name( 'taxonomy' ); 
  96. $input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />'; 
  97.  
  98. switch ( count( $taxonomies ) ) { 
  99.  
  100. // No tag cloud supporting taxonomies found, display error message 
  101. case 0: 
  102. echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>'; 
  103. printf( $input, '' ); 
  104. break; 
  105.  
  106. // Just a single tag cloud supporting taxonomy found, no need to display options 
  107. case 1: 
  108. $keys = array_keys( $taxonomies ); 
  109. $taxonomy = reset( $keys ); 
  110. printf( $input, esc_attr( $taxonomy ) ); 
  111. break; 
  112.  
  113. // More than one tag cloud supporting taxonomy found, display options 
  114. default: 
  115. printf( 
  116. '<p><label for="%1$s">%2$s</label>' . 
  117. '<select class="widefat" id="%1$s" name="%3$s">',  
  118. $id,  
  119. __( 'Taxonomy:' ),  
  120. $name 
  121. ); 
  122.  
  123. foreach ( $taxonomies as $taxonomy => $tax ) { 
  124. printf( 
  125. '<option value="%s"%s>%s</option>',  
  126. esc_attr( $taxonomy ),  
  127. selected( $taxonomy, $current_taxonomy, false ),  
  128. $tax->labels->name 
  129. ); 
  130.  
  131. echo '</select></p>'; 
  132.  
  133. /** 
  134. * Retrieves the taxonomy for the current Tag cloud widget instance. 
  135. * @since 4.4.0 
  136. * @access public 
  137. * @param array $instance Current settings. 
  138. * @return string Name of the current taxonomy if set, otherwise 'post_tag'. 
  139. */ 
  140. public function _get_current_taxonomy($instance) { 
  141. if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) ) 
  142. return $instance['taxonomy']; 
  143.  
  144. return 'post_tag';