[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref