PluginProbe
Essential Widgets / 1.3
Essential Widgets v1.3
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 1.3, at includes/widgets/class-ew-pages.php

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