[ 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          $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
 128          $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 129          ?>
 130          <p>
 131              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 132              <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 ); ?>" />
 133          </p>
 134          <?php
 135          $taxonomies       = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
 136          $current_taxonomy = $this->_get_current_taxonomy( $instance );
 137  
 138          switch ( count( $taxonomies ) ) {
 139  
 140              // No tag cloud supporting taxonomies found, display error message.
 141              case 0:
 142                  ?>
 143                  <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="" />
 144                  <p>
 145                      <?php _e( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ); ?>
 146                  </p>
 147                  <?php
 148                  break;
 149  
 150              // Just a single tag cloud supporting taxonomy found, no need to display a select.
 151              case 1:
 152                  $keys     = array_keys( $taxonomies );
 153                  $taxonomy = reset( $keys );
 154                  ?>
 155                  <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 ); ?>" />
 156                  <?php
 157                  break;
 158  
 159              // More than one tag cloud supporting taxonomy found, display a select.
 160              default:
 161                  ?>
 162                  <p>
 163                      <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:' ); ?></label>
 164                      <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>">
 165                      <?php foreach ( $taxonomies as $taxonomy => $tax ) : ?>
 166                          <option value="<?php echo esc_attr( $taxonomy ); ?>" <?php selected( $taxonomy, $current_taxonomy ); ?>>
 167                              <?php echo esc_html( $tax->labels->name ); ?>
 168                          </option>
 169                      <?php endforeach; ?>
 170                      </select>
 171                  </p>
 172                  <?php
 173          }
 174  
 175          if ( count( $taxonomies ) > 0 ) {
 176              ?>
 177              <p>
 178                  <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 ); ?> />
 179                  <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show tag counts' ); ?></label>
 180              </p>
 181              <?php
 182          }
 183      }
 184  
 185      /**
 186       * Retrieves the taxonomy for the current Tag cloud widget instance.
 187       *
 188       * @since 4.4.0
 189       *
 190       * @param array $instance Current settings.
 191       * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
 192       */
 193  	public function _get_current_taxonomy( $instance ) {
 194          if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
 195              return $instance['taxonomy'];
 196          }
 197  
 198          return 'post_tag';
 199      }
 200  }


Generated: Thu Jul 2 01:00:04 2020 Cross-referenced by PHPXref 0.7.1