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