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