[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-tag-cloud.php (source)

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


Generated: Tue Dec 10 01:00:04 2019 Cross-referenced by PHPXref 0.7.1