PluginProbe
Essential Widgets / 3.2
Essential Widgets v3.2
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 3.2, at includes/widgets/class-ew-categories.php

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