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