class-wp-widget-tag-cloud.php 5.7 KB

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