PluginProbe
Essential Widgets / 1.1
Essential Widgets v1.1
3.2.1 3.3 3.2 trunk 1.0.0 1.0.1 1.1 1.2 1.2.1 1.2.2 1.3 1.4 1.4.1 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 1.9 2.0 2.1 2.2 All 31 releases
essential-widgets / includes / widgets / class-ew-categories.php

class-ew-categories.php in Essential Widgets 1.1, at includes/widgets/class-ew-categories.php

391 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Custom Category Widget
4 *
5 * @package Essential_Widgets
6 */
7
8
9 /**
10 * Custom Category Widget
11 */
12 class EW_Categories extends WP_Widget {
13
14 /**
15 * Holds widget settings defaults, populated in constructor.
16 *
17 * @var array
18 */
19 protected $defaults;
20
21 function __construct() {
22
23 $this->defaults = array(
24 'title' => esc_attr__( 'Categories', 'essential-widgets' ),
25 'taxonomy' => 'category',
26 'style' => 'list',
27 'include' => '',
28 'exclude' => '',
29 'exclude_tree' => '',
30 'child_of' => '',
31 'current_category' => '',
32 'search' => '',
33 'hierarchical' => true,
34 'hide_empty' => true,
35 'order' => 'ASC',
36 'orderby' => 'name',
37 'depth' => 0,
38 'number' => '',
39 'feed' => '',
40 'feed_type' => '',
41 'feed_image' => '',
42 'use_desc_for_title' => false,
43 'show_count' => false,
44 );
45
46 $widget_ops = array(
47 'classname' => 'essential-widgets widget_categories ew-category ewcategory',
48 'description' => esc_html__( 'Displays a list of categories', 'essential-widgets' ),
49 );
50
51 $control_ops = array(
52 'id_base' => 'ew-category'
53 );
54
55 parent::__construct(
56 'ew-category', // Base ID
57 __( 'EW: Categories', 'essential-widgets' ), // Name
58 $widget_ops,
59 $control_ops
60 );
61 }
62
63 function form($instance) {
64 // Merge the user-selected arguments with the defaults.
65 $instance = wp_parse_args( (array) $instance, $this->defaults );
66
67 // <select> element options.
68 $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'objects' );
69 $terms = get_terms( $instance['taxonomy'] );
70
71 $style = array(
72 'list' => esc_attr__( 'List', 'essential-widgets' ),
73 'none' => esc_attr__( 'None', 'essential-widgets' )
74 );
75
76 $order = array(
77 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
78 'DESC' => esc_attr__( 'Descending', 'essential-widgets' )
79 );
80
81 $orderby = array(
82 'count' => esc_attr__( 'Count', 'essential-widgets' ),
83 'ID' => esc_attr__( 'ID', 'essential-widgets' ),
84 'name' => esc_attr__( 'Name', 'essential-widgets' ),
85 'slug' => esc_attr__( 'Slug', 'essential-widgets' ),
86 'term_group' => esc_attr__( 'Term Group', 'essential-widgets' )
87 );
88
89 $feed_type = array(
90 '' => '',
91 'atom' => esc_attr__( 'Atom', 'essential-widgets' ),
92 'rdf' => esc_attr__( 'RDF', 'essential-widgets' ),
93 'rss' => esc_attr__( 'RSS', 'essential-widgets' ),
94 'rss2' => esc_attr__( 'RSS 2.0', 'essential-widgets' )
95 ); ?>
96
97 <p>
98 <label>
99 <?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
100 <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( $instance['title'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['title'] ); ?>" />
101 </label>
102 </p>
103
104 <p>
105 <label>
106 <?php esc_html_e( 'Taxonomy:', 'essential-widgets' ); ?>
107
108 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'taxonomy' ) ); ?>">
109
110 <?php foreach ( $taxonomies as $taxonomy ) : ?>
111
112 <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $instance['taxonomy'], $taxonomy->name ); ?>><?php echo esc_html( $taxonomy->labels->singular_name ); ?></option>
113
114 <?php endforeach; ?>
115
116 </select>
117 </label>
118 </p>
119
120 <p>
121 <label>
122 <?php esc_html_e( 'Style:', 'essential-widgets' ); ?>
123
124 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'style' ) ); ?>">
125
126 <?php foreach ( $style as $option_value => $option_label ) : ?>
127
128 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['style'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
129
130 <?php endforeach; ?>
131
132 </select>
133 </label>
134 </p>
135
136 <p>
137 <label>
138 <?php esc_html_e( 'Order:', 'essential-widgets' ); ?>
139
140 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
141
142 <?php foreach ( $order as $option_value => $option_label ) : ?>
143
144 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
145
146 <?php endforeach; ?>
147
148 </select>
149 </label>
150 </p>
151
152 <p>
153 <label>
154 <?php esc_html_e( 'Order By:', 'essential-widgets' ); ?>
155
156 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
157
158 <?php foreach ( $orderby as $option_value => $option_label ) : ?>
159
160 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
161
162 <?php endforeach; ?>
163
164 </select>
165 </label>
166 </p>
167
168 <p>
169 <label>
170 <?php esc_html_e( 'Depth:', 'essential-widgets' ); ?>
171 <input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>" value="<?php echo esc_attr( $instance['depth'] ); ?>" placeholder="0" />
172 </label>
173 </p>
174
175 <p>
176 <label>
177 <?php esc_html_e( 'Number:', 'essential-widgets' ); ?>
178 <input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" placeholder="0" />
179 </label>
180 </p>
181
182 <p>
183 <label>
184 <?php esc_html_e( 'Include:', 'essential-widgets' ); ?>
185 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'include' ) ); ?>" value="<?php echo esc_attr( $instance['include'] ); ?>" placeholder="1,2,3&hellip;" />
186 </label>
187 </p>
188
189 <p>
190 <label>
191 <?php esc_html_e( 'Exclude:', 'essential-widgets' ); ?>
192 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php echo esc_attr( $instance['exclude'] ); ?>" placeholder="1,2,3&hellip;" />
193 </label>
194 </p>
195
196 <p class="button-primary more"><?php esc_html_e( 'More Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-down"></i></p>
197
198 <div class="advanced-section">
199
200 <p>
201 <label>
202 <?php esc_html_e( 'Exclude Tree:', 'essential-widgets' ); ?>
203 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'exclude_tree' ) ); ?>" value="<?php echo esc_attr( $instance['exclude_tree'] ); ?>" placeholder="1,2,3&hellip;" />
204 </label>
205 </p>
206
207 <p>
208 <label>
209 <?php esc_html_e( 'Child Of:', 'essential-widgets' ); ?>
210 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'child_of' ) ); ?>" value="<?php echo esc_attr( $instance['child_of'] ); ?>" placeholder="0" />
211 </label>
212 </p>
213
214 <p>
215 <label>
216 <?php esc_html_e( 'Current Category:', 'essential-widgets' ); ?>
217 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'current_category' ) ); ?>" value="<?php echo esc_attr( $instance['current_category'] ); ?>" placeholder="0" />
218 </label>
219 </p>
220
221 <p>
222 <label>
223 <?php esc_html_e( 'Search:', 'essential-widgets' ); ?>
224 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'search' ) ); ?>" value="<?php echo esc_attr( $instance['search'] ); ?>" />
225 </label>
226 </p>
227
228 <p>
229 <label>
230 <?php esc_html_e( 'Feed Type:', 'essential-widgets' ); ?>
231
232 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'feed_type' ) ); ?>">
233
234 <?php foreach ( $feed_type as $option_value => $option_label ) : ?>
235
236 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['feed_type'], $option_value ); ?>><?php echo esc_html($option_label ); ?></option>
237
238 <?php endforeach; ?>
239
240 </select>
241 </label>
242 </p>
243
244 <p>
245 <label>
246 <?php esc_html_e( 'Feed Text:', 'essential-widgets' ); ?>
247 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'feed' ) ); ?>" value="<?php echo esc_attr( $instance['feed'] ); ?>" />
248 </label>
249 </p>
250
251 <p>
252 <label>
253 <?php esc_html_e( 'Feed Image:', 'essential-widgets' ); ?>
254 <input type="url" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'feed_image' ) ); ?>" value="<?php echo esc_attr( $instance['feed_image'] ); ?>" placeholder="<?php echo esc_attr( home_url( 'images/example.png' ) ); ?>" />
255 </label>
256 </p>
257
258 <p>
259 <label>
260 <input type="checkbox" <?php checked( $instance['hierarchical'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'hierarchical' ) ); ?>" />
261 <?php esc_html_e( 'Hierarchical?', 'essential-widgets' ); ?>
262 </label>
263 </p>
264
265 <p>
266 <label>
267 <input type="checkbox" <?php checked( $instance['use_desc_for_title'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'use_desc_for_title' ) ); ?>" />
268 <?php esc_html_e( 'Show description on hover?', 'essential-widgets' ); ?>
269 </label>
270 </p>
271
272 <p>
273 <label>
274 <input type="checkbox" <?php checked( $instance['show_count'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" />
275 <?php esc_html_e( 'Show count?', 'essential-widgets' ); ?>
276 </label>
277 </p>
278
279 <p>
280 <label>
281 <input type="checkbox" <?php checked( $instance['hide_empty'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'hide_empty' ) ); ?>" />
282 <?php esc_html_e( 'Hide empty?', 'essential-widgets' ); ?>
283 </label>
284 </p>
285
286 </div><!-- .advanced-section -->
287
288 <div style="clear:both;">&nbsp;</div>
289
290
291 <?php
292 }
293
294 function update( $new_instance, $old_instance ) {
295
296 // If new taxonomy is chosen, reset includes and excludes.
297 if ( $new_instance['taxonomy'] !== $old_instance['taxonomy'] )
298 $new_instance['include'] = $new_instance['exclude'] = '';
299
300 // Sanitize key.
301 $instance['taxonomy'] = sanitize_key( $new_instance['taxonomy'] );
302
303 // Sanitize title.
304 $instance['title'] = sanitize_text_field( $new_instance['title'] );
305
306 // Strip tags.
307 $instance['search'] = strip_tags( $new_instance['search'] );
308 $instance['feed'] = strip_tags( $new_instance['feed'] );
309
310 // Whitelist options.
311 $order = array( 'ASC', 'DESC' );
312 $orderby = array( 'count', 'ID', 'name', 'slug', 'term_group' );
313 $style = array( 'list', 'none' );
314 $feed_type = array( '', 'atom', 'rdf', 'rss', 'rss2' );
315
316 $instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'ASC';
317 $instance['orderby'] = in_array( $new_instance['orderby'], $orderby ) ? $new_instance['orderby'] : 'name';
318 $instance['style'] = in_array( $new_instance['style'], $style ) ? $new_instance['style'] : 'list';
319 $instance['feed_type'] = in_array( $new_instance['feed_type'], $feed_type ) ? $new_instance['feed_type'] : '';
320
321 // Integers.
322 $instance['number'] = intval( $new_instance['number'] );
323 $instance['depth'] = absint( $new_instance['depth'] );
324 $instance['child_of'] = absint( $new_instance['child_of'] );
325 $instance['current_category'] = absint( $new_instance['current_category'] );
326
327 // Only allow integers and commas.
328 $instance['include'] = preg_replace( '/[^0-9,]/', '', $new_instance['include'] );
329 $instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] );
330 $instance['exclude_tree'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude_tree'] );
331
332 // URLs.
333 $instance['feed_image'] = esc_url_raw( $new_instance['feed_image'] );
334
335 // Checkboxes.
336 $instance['hierarchical'] = isset( $new_instance['hierarchical'] ) ? 1 : 0;
337 $instance['use_desc_for_title'] = isset( $new_instance['use_desc_for_title'] ) ? 1 : 0;
338 $instance['show_count'] = isset( $new_instance['show_count'] ) ? 1 : 0;
339 $instance['hide_empty'] = isset( $new_instance['hide_empty'] ) ? 1 : 0;
340
341 // Return sanitized options.
342 return $instance;
343 }
344
345 function widget( $args, $instance ) {
346 // Set the $args for wp_list_categories() to the $instance array.
347 $instance = wp_parse_args( $instance, $this->defaults );
348
349 // Set the $title_li and $echo arguments to false.
350 $instance['title_li'] = false;
351 $instance['echo'] = false;
352
353 // Output the args's $before_widget wrapper.
354 echo $args['before_widget'];
355
356 // If a title was input by the user, display it.
357 if ( ! empty( $instance['title'] ) )
358 echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
359
360 // Get the categories list.
361 $categories = str_replace( array( "\r", "\n", "\t" ), '', wp_list_categories( $instance ) );
362
363 $categories = str_replace('</a> (', '</a> <span>(', $categories);
364 $categories = str_replace(')', ')</span>', $categories);
365
366 // If 'list' is the user-selected style, wrap the categories in an unordered list.
367 if ( 'list' == $instance['style'] )
368 $categories = '<ul class="categories">' . $categories . '</ul><!-- .categories -->';
369
370 // If no style is given, wrap in a <p> tag for formatting.
371 else
372 $categories = '<p class="categories style-none">' . $categories . '</p>';
373
374 // Output the categories list.
375 echo $categories;
376
377 // Close the args's widget wrapper.
378 echo $args['after_widget'];
379 }
380 }// end Category_Widget class
381
382 /**
383 * Intiate Category_Widget Class.
384 *
385 * @since 1.0.0
386 */
387 function ew_categories_register() {
388 register_widget( 'EW_Categories' );
389 }
390 add_action( 'widgets_init', 'ew_categories_register' );
391