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-pages.php

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

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