PluginProbe
Essential Widgets / 3.1
Essential Widgets v3.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-pages.php

class-ew-pages.php in Essential Widgets 3.1, at includes/widgets/class-ew-pages.php

438 lines 16.0 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 * Pages Widget
9 *
10 * @package Essential_Widgets
11 */
12
13
14 /**
15 * Makes a custom Widget for Displaying About
16 *
17 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
18 *
19 * @package Essential_Widgets
20 */
21 if ( ! class_exists( 'EW_Pages' ) ) :
22 class EW_Pages extends WP_Widget // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- EW_ is this plugin's abbreviated prefix; wrapped in class_exists() guard.
23 {
24
25
26 /**
27 * Holds widget settings defaults, populated in constructor.
28 *
29 * @var array
30 */
31 protected $defaults;
32
33 public function __construct() {
34
35 // Set up the defaults.
36 $this->defaults = array(
37 'title' => esc_attr__( 'Pages', 'essential-widgets' ),
38 'post_type' => 'page',
39 'depth' => 0,
40 'number' => '',
41 'offset' => '',
42 'child_of' => '',
43 'include' => '',
44 'exclude' => '', // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_pages() parameter, not a WP_Query post__not_in clause.
45 'exclude_tree' => '',
46 'meta_key' => '', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required wp_list_pages() parameter.
47 'meta_value' => '', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Required wp_list_pages() parameter.
48 'authors' => '',
49 'link_before' => '',
50 'link_after' => '',
51 'show_date' => '',
52 'hierarchical' => true,
53 'sort_column' => 'post_title',
54 'sort_order' => 'ASC',
55 'date_format' => get_option( 'date_format' ),
56 );
57
58 $widget_ops = array(
59 'classname' => 'essential-widgets ew-pages ewpages',
60 'description' => esc_html__( 'Displays a list of pages.', 'essential-widgets' ),
61 );
62
63 $control_ops = array(
64 'id_base' => 'ew-pages',
65 );
66
67 parent::__construct(
68 'ew-pages', // Base ID
69 esc_html__( 'EW: Pages', 'essential-widgets' ), // Name
70 $widget_ops,
71 $control_ops
72 );
73 }
74
75 /**
76 * Displays the widget control options in the Widgets admin screen.
77 *
78 * @since 1.0.0
79 * @access public
80 * @param array $instance
81 * @param void
82 */
83 public function form( $instance ) {
84 // Merge the user-selected arguments with the defaults.
85 $instance = wp_parse_args( (array) $instance, $this->defaults );
86
87 $post_types = get_post_types(
88 array(
89 'public' => true,
90 'hierarchical' => true,
91 ),
92 'objects'
93 );
94
95 $sort_order = array(
96 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
97 'DESC' => esc_attr__( 'Descending', 'essential-widgets' ),
98 );
99
100 $sort_column = array(
101 'post_author' => esc_attr__( 'Author', 'essential-widgets' ),
102 'post_date' => esc_attr__( 'Date', 'essential-widgets' ),
103 'ID' => esc_attr__( 'ID', 'essential-widgets' ),
104 'menu_order' => esc_attr__( 'Menu Order', 'essential-widgets' ),
105 'post_modified' => esc_attr__( 'Modified', 'essential-widgets' ),
106 'post_name' => esc_attr__( 'Slug', 'essential-widgets' ),
107 'post_title' => esc_attr__( 'Title', 'essential-widgets' ),
108 );
109
110 $show_date = array(
111 '' => esc_attr__( 'Select', 'essential-widgets' ),
112 'created' => esc_attr__( 'Created', 'essential-widgets' ),
113 'modified' => esc_attr__( 'Modified', 'essential-widgets' ),
114 );
115
116 $meta_key = array_merge( array( '' ), (array) get_meta_keys() ); ?>
117
118 <p>
119 <label>
120 <?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
121 <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'] ); ?>" />
122 </label>
123 </p>
124
125 <p>
126 <label>
127 <?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?>
128
129 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>">
130
131 <?php foreach ( $post_types as $post_type ) : ?>
132
133 <option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( $instance['post_type'], $post_type->name ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></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( 'sort_order' ) ); ?>">
146
147 <?php foreach ( $sort_order as $option_value => $option_label ) : ?>
148
149 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['sort_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( 'sort_column' ) ); ?>">
162
163 <?php foreach ( $sort_column as $option_value => $option_label ) : ?>
164
165 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['sort_column'], $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( 'Offset:', 'essential-widgets' ); ?>
190 <input type="number" class="widefat" size="5" min="0" name="<?php echo esc_attr( $this->get_field_name( 'offset' ) ); ?>" value="<?php echo esc_attr( $instance['offset'] ); ?>" placeholder="0" />
191 </label>
192 </p>
193
194 <p>
195 <label>
196 <?php esc_html_e( 'Child Of:', 'essential-widgets' ); ?>
197 <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" />
198 </label>
199 </p>
200
201 <p>
202 <label>
203 <?php esc_html_e( 'Include:', 'essential-widgets' ); ?>
204 <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;" />
205 </label>
206 </p>
207
208 <p>
209 <label>
210 <?php esc_html_e( 'Exclude:', 'essential-widgets' ); ?>
211 <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;" />
212 </label>
213 </p>
214
215 <p class="button-primary ect-toggle-btn more">
216 <span class="ect-more-text"><?php esc_html_e( 'More Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-down"></i></span>
217 <span class="ect-hide-text"><?php esc_html_e( 'Hide Options', 'essential-widgets' ); ?><i class="dashicons dashicons-arrow-up"></i></span>
218
219 <div class="advanced-section">
220
221 <p>
222 <label>
223 <?php esc_html_e( 'Exclude Tree:', 'essential-widgets' ); ?>
224 <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;" />
225 </label>
226 </p>
227
228 <p>
229 <label>
230 <?php esc_html_e( 'Meta Key:', 'essential-widgets' ); ?>
231
232 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'meta_key' ) ); ?>">
233
234 <?php foreach ( $meta_key as $meta ) : ?>
235
236 <option value="<?php echo esc_attr( $meta ); ?>" <?php selected( $instance['meta_key'], $meta ); ?>><?php echo esc_html( $meta ); ?></option>
237
238 <?php endforeach; ?>
239
240 </select>
241 </label>
242 </p>
243
244 <p>
245 <label>
246 <?php esc_html_e( 'Meta Value:', 'essential-widgets' ); ?>
247 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'meta_value' ) ); ?>" value="<?php echo esc_attr( $instance['meta_value'] ); ?>" />
248 </label>
249 </p>
250
251 <p>
252 <label>
253 <?php esc_html_e( 'Authors:', 'essential-widgets' ); ?>
254 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'authors' ) ); ?>" value="<?php echo esc_attr( $instance['authors'] ); ?>" placeholder="1,2,3&hellip;" />
255 </label>
256 </p>
257
258 <p>
259 <label>
260 <?php esc_html_e( 'Link Before:', 'essential-widgets' ); ?>
261 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_before' ) ); ?>" value="<?php echo esc_attr( $instance['link_before'] ); ?>" />
262 </label>
263 </p>
264
265 <p>
266 <label>
267 <?php esc_html_e( 'Link After:', 'essential-widgets' ); ?>
268 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'link_after' ) ); ?>" value="<?php echo esc_attr( $instance['link_after'] ); ?>" />
269 </label>
270 </p>
271
272 <p>
273 <label>
274 <?php esc_html_e( 'Show Date:', 'essential-widgets' ); ?>
275
276 <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>">
277
278 <?php foreach ( $show_date as $option_value => $option_label ) : ?>
279
280 <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['show_date'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
281
282 <?php endforeach; ?>
283
284 </select>
285 </label>
286 </p>
287
288 <p>
289 <label>
290 <?php esc_html_e( 'Date Format:', 'essential-widgets' ); ?>
291 <input type="text" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'date_format' ) ); ?>" value="<?php echo esc_attr( $instance['date_format'] ); ?>" placeholder="<?php echo esc_attr( get_option( 'date_format' ) ); ?>" />
292 </label>
293 </p>
294
295 <p>
296 <label>
297 <input type="checkbox" <?php checked( $instance['hierarchical'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'hierarchical' ) ); ?>" />
298 <?php esc_html_e( 'Hierarchical?', 'essential-widgets' ); ?>
299 </label>
300 </p>
301
302 </div><!-- .advanced-section -->
303
304 <div style="clear:both;">&nbsp;</div>
305 <?php
306 }
307
308 /**
309 * update the particular instant
310 *
311 * This function should check that $new_instance is set correctly.
312 * The newly calculated value of $instance should be returned.
313 * If "false" is returned, the instance won't be saved/updated.
314 *
315 * $new_instance New settings for this instance as input by the user via form()
316 * $old_instance Old settings for this instance
317 * Settings to save or bool false to cancel saving
318 */
319 public function update( $new_instance, $old_instance ) {
320 $instance = $old_instance;
321
322 // Sanitize title.
323 $instance['title'] = sanitize_text_field( $new_instance['title'] );
324
325 // Strip tags.
326 $instance['meta_key'] = wp_strip_all_tags( $new_instance['meta_key'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Required wp_list_pages() parameter.
327 $instance['meta_value'] = wp_strip_all_tags( $new_instance['meta_value'] ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Required wp_list_pages() parameter.
328 $instance['date_format'] = wp_strip_all_tags( $new_instance['date_format'] );
329
330 // Sanitize key.
331 $instance['post_type'] = sanitize_key( $new_instance['post_type'] );
332
333 // Whitelist options.
334 $sort_order = array( 'ASC', 'DESC' );
335 $sort_column = array( 'post_author', 'post_date', 'ID', 'menu_order', 'post_modified', 'post_name', 'post_title' );
336 $show_date = array( '', 'created', 'modified' );
337
338 $instance['sort_column'] = in_array( $new_instance['sort_column'], $sort_column, true ) ? $new_instance['sort_column'] : 'post_title';
339 $instance['sort_order'] = in_array( $new_instance['sort_order'], $sort_order, true ) ? $new_instance['sort_order'] : 'ASC';
340 $instance['show_date'] = in_array( $new_instance['show_date'], $show_date, true ) ? $new_instance['show_date'] : '';
341
342 // Text boxes. Make sure user can use 'unfiltered_html'.
343 $instance['link_before'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_before'] : wp_kses_post( $new_instance['link_before'] );
344 $instance['link_after'] = current_user_can( 'unfiltered_html' ) ? $new_instance['link_after'] : wp_kses_post( $new_instance['link_after'] );
345
346 // Integers.
347 $instance['number'] = absint( $new_instance['number'] );
348 $instance['depth'] = absint( $new_instance['depth'] );
349 $instance['child_of'] = absint( $new_instance['child_of'] );
350 $instance['offset'] = absint( $new_instance['offset'] );
351
352 // Sanitize text field
353 $instance['include'] = sanitize_text_field( $new_instance['include'] );
354 $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_pages() parameter.
355 $instance['exclude_tree'] = sanitize_text_field( $new_instance['exclude_tree'] );
356 $instance['authors'] = sanitize_text_field( $new_instance['authors'] );
357
358 // Then restrict to numbers & commas
359 $instance['include'] = preg_replace( '/[^0-9,]/', '', $new_instance['include'] );
360 $instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- Required wp_list_pages() parameter.
361 $instance['exclude_tree'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude_tree'] );
362 $instance['authors'] = preg_replace( '/[^0-9,]/', '', $new_instance['authors'] );
363
364 // Checkboxes.
365 $instance['hierarchical'] = isset( $new_instance['hierarchical'] ) ? 1 : 0;
366
367 // Return sanitized options.
368 return $instance;
369 }
370
371 /**
372 * Displays the Widget in the front-end.
373 *
374 * $args Display arguments including before_title, after_title, before_widget, and after_widget.
375 * $instance The settings for the particular instance of the widget
376 */
377 public function widget( $args, $instance ) {
378 // Merge instance with defaults.
379 $instance = wp_parse_args( $instance, $this->defaults );
380
381 // Escape widget wrapper attributes.
382 echo wp_kses_post( $args['before_widget'] );
383
384 // If a title was input by the user, display it safely.
385 if ( ! empty( $instance['title'] ) ) {
386 echo wp_kses_post( $args['before_title'] );
387
388 // Apply filters to title, then escape it for output
389 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
390 echo esc_html( $title );
391
392 echo wp_kses_post( $args['after_title'] );
393 }
394
395 // Output the main content of the widget (shortcode output)
396 echo wp_kses_post( $this->shortcode( $instance ) );
397
398 // Close the widget wrapper safely.
399 echo wp_kses_post( $args['after_widget'] );
400 }
401
402 public function shortcode( $atts ) {
403 // Set the $title_li and $echo to false.
404 $atts['title_li'] = false;
405 $atts['echo'] = false;
406
407 $ew_pages = '';
408
409 // Only show this title in block element and not on widget.
410 if ( isset( $atts['is_block'] ) && true === $atts['is_block'] && !empty( $atts['title'] ) ) {
411 $ew_pages .= '<h2 class="ew-page-block-title">' . esc_html( $atts['title'] ) . '</h2>';
412 }
413
414 // Output the page list.
415 $ew_pages .= '<ul class="pages">' . str_replace(
416 array( "\r", "\n", "\t" ),
417 '',
418 wp_list_pages( $atts )
419 ) . '</ul>';
420
421 return wp_kses_post( $ew_pages );
422 }
423 }
424 endif;
425
426
427 if ( ! function_exists( 'ew_pages_register' ) ) :
428 /**
429 * Intiate About_Widget Class.
430 *
431 * @since 1.0.0
432 */
433 function ew_pages_register() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix.
434 register_widget( 'EW_Pages' );
435 }
436 add_action( 'widgets_init', 'ew_pages_register' );
437 endif;
438