Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

185 lignes
4.9 KiB

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