24
24
public function __construct() {
25
25
$widget_ops = array(
26
'description' => __( 'A cloud of your most used tags.' ),
26
'description' => __( 'A cloud of your most used tags.' ),
27
27
'customize_selective_refresh' => true,
29
29
parent::__construct( 'tag_cloud', __( 'Tag Cloud' ), $widget_ops );
66
66
* @param array $args Args used for the tag cloud widget.
67
67
* @param array $instance Array of settings for the current widget.
69
$tag_cloud = wp_tag_cloud( apply_filters( 'widget_tag_cloud_args', array(
70
'taxonomy' => $current_taxonomy,
72
'show_count' => $show_count,
69
$tag_cloud = wp_tag_cloud(
71
'widget_tag_cloud_args',
73
'taxonomy' => $current_taxonomy,
75
'show_count' => $show_count,
75
81
if ( empty( $tag_cloud ) ) {
103
109
* @return array Settings to save or bool false to cancel saving.
105
111
public function update( $new_instance, $old_instance ) {
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']);
113
$instance['title'] = sanitize_text_field( $new_instance['title'] );
114
$instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
115
$instance['taxonomy'] = stripslashes( $new_instance['taxonomy'] );
110
116
return $instance;
118
124
* @param array $instance Current settings.
120
126
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;
127
$current_taxonomy = $this->_get_current_taxonomy( $instance );
128
$title_id = $this->get_field_id( 'title' );
129
$count = isset( $instance['count'] ) ? (bool) $instance['count'] : false;
124
130
$instance['title'] = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
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'] .'" />
132
echo '<p><label for="' . $title_id . '">' . __( 'Title:' ) . '</label>
133
<input type="text" class="widefat" id="' . $title_id . '" name="' . $this->get_field_name( 'title' ) . '" value="' . $instance['title'] . '" />
130
136
$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" />';
137
$id = $this->get_field_id( 'taxonomy' );
138
$name = $this->get_field_name( 'taxonomy' );
139
$input = '<input type="hidden" id="' . $id . '" name="' . $name . '" value="%s" />';
135
141
$count_checkbox = sprintf(
136
142
'<p><input type="checkbox" class="checkbox" id="%1$s" name="%2$s"%3$s /> <label for="%1$s">%4$s</label></p>',
143
149
switch ( count( $taxonomies ) ) {
145
// No tag cloud supporting taxonomies found, display error message
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, '' );
151
// Just a single tag cloud supporting taxonomy found, no need to display a select.
153
$keys = array_keys( $taxonomies );
154
$taxonomy = reset( $keys );
155
printf( $input, esc_attr( $taxonomy ) );
156
echo $count_checkbox;
159
// More than one tag cloud supporting taxonomy found, display a select.
162
'<p><label for="%1$s">%2$s</label>' .
163
'<select class="widefat" id="%1$s" name="%3$s">',
169
foreach ( $taxonomies as $taxonomy => $tax ) {
151
// No tag cloud supporting taxonomies found, display error message
153
echo '<p>' . __( 'The tag cloud will not be displayed since there are no taxonomies that support the tag cloud widget.' ) . '</p>';
154
printf( $input, '' );
157
// Just a single tag cloud supporting taxonomy found, no need to display a select.
159
$keys = array_keys( $taxonomies );
160
$taxonomy = reset( $keys );
161
printf( $input, esc_attr( $taxonomy ) );
162
echo $count_checkbox;
165
// More than one tag cloud supporting taxonomy found, display a select.
171
'<option value="%s"%s>%s</option>',
172
esc_attr( $taxonomy ),
173
selected( $taxonomy, $current_taxonomy, false ),
168
'<p><label for="%1$s">%2$s</label>' .
169
'<select class="widefat" id="%1$s" name="%3$s">',
178
echo '</select></p>' . $count_checkbox;
175
foreach ( $taxonomies as $taxonomy => $tax ) {
177
'<option value="%s"%s>%s</option>',
178
esc_attr( $taxonomy ),
179
selected( $taxonomy, $current_taxonomy, false ),
184
echo '</select></p>' . $count_checkbox;
187
193
* @param array $instance Current settings.
188
194
* @return string Name of the current taxonomy if set, otherwise 'post_tag'.
190
public function _get_current_taxonomy($instance) {
191
if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
196
public function _get_current_taxonomy( $instance ) {
197
if ( ! empty( $instance['taxonomy'] ) && taxonomy_exists( $instance['taxonomy'] ) ) {
192
198
return $instance['taxonomy'];
194
201
return 'post_tag';