[ 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          $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  94  
  95          /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  96          $format = apply_filters( 'navigation_widgets_format', $format );
  97  
  98          if ( 'html5' === $format ) {
  99              // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
 100              $title      = trim( strip_tags( $title ) );
 101              $aria_label = $title ? $title : $default_title;
 102              echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
 103          }
 104  
 105          echo '<div class="tagcloud">';
 106  
 107          echo $tag_cloud;
 108  
 109          echo "</div>\n";
 110  
 111          if ( 'html5' === $format ) {
 112              echo '</nav>';
 113          }
 114  
 115          echo $args['after_widget'];
 116      }
 117  
 118      /**
 119       * Handles updating settings for the current Tag Cloud widget instance.
 120       *
 121       * @since 2.8.0
 122       *
 123       * @param array $new_instance New settings for this instance as input by the user via
 124       *                            WP_Widget::form().
 125       * @param array $old_instance Old settings for this instance.
 126       * @return array Settings to save or bool false to cancel saving.
 127       */
 128  	public function update( $new_instance, $old_instance ) {
 129          $instance             = array();
 130          $instance['title']    = sanitize_text_field( $new_instance['title'] );
 131          $instance['count']    = ! empty( $new_instance['count'] ) ? 1 : 0;
 132          $instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
 133          return $instance;
 134      }
 135  
 136      /**
 137       * Outputs the Tag Cloud widget settings form.
 138       *
 139       * @since 2.8.0
 140       *
 141       * @param array $instance Current settings.
 142       */
 143  	public function form( $instance ) {
 144          $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
 145          $count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
 146          ?>
 147          <p>
 148              <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
 149              <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 ); ?>" />
 150          </p>
 151          <?php
 152          $taxonomies       = get_taxonomies( array( 'show_tagcloud' => true ), 'object' );
 153          $current_taxonomy = $this->_get_current_taxonomy( $instance );
 154  
 155          switch ( count( $taxonomies ) ) {
 156  
 157              // No tag cloud supporting taxonomies found, display error message.
 158              case 0:
 159                  ?>
 160                  <input type="hidden" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="" />
 161                  <p>
 162                      <?php _e( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ); ?>
 163                  </p>
 164                  <?php
 165                  break;
 166  
 167              // Just a single tag cloud supporting taxonomy found, no need to display a select.
 168              case 1:
 169                  $keys     = array_keys( $taxonomies );
 170                  $taxonomy = reset( $keys );
 171                  ?>
 172                  <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 ); ?>" />
 173                  <?php
 174                  break;
 175  
 176              // More than one tag cloud supporting taxonomy found, display a select.
 177              default:
 178                  ?>
 179                  <p>
 180                      <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy:' ); ?></label>
 181                      <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>">
 182                      <?php foreach ( $taxonomies as $taxonomy => $tax ) : ?>
 183                          <option value="<?php echo esc_attr( $taxonomy ); ?>" <?php selected( $taxonomy, $current_taxonomy ); ?>>
 184                              <?php echo esc_html( $tax->labels->name ); ?>
 185                          </option>
 186                      <?php endforeach; ?>
 187                      </select>
 188                  </p>
 189                  <?php
 190          }
 191  
 192          if ( count( $taxonomies ) > 0 ) {
 193              ?>
 194              <p>
 195                  <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 ); ?> />
 196                  <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php _e( 'Show tag counts' ); ?></label>
 197              </p>
 198              <?php
 199          }
 200      }
 201  
 202      /**
 203       * Retrieves the taxonomy for the current Tag cloud widget instance.
 204       *
 205       * @since 4.4.0
 206       *
 207       * @param array $instance Current settings.
 208       * @return string Name of the current taxonomy if set, otherwise 'post_tag'.
 209       */
 210  	public function _get_current_taxonomy( $instance ) {
 211          if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
 212              return $instance['taxonomy'];
 213          }
 214  
 215          return 'post_tag';
 216      }
 217  }


Generated: Sat Jan 23 01:00:05 2021 Cross-referenced by PHPXref 0.7.1