PluginProbe
Essential Widgets / 2.0
Essential Widgets v2.0
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 2.0, at includes/widgets/class-ew-categories.php

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