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
← All changes | includes/widgets/class-ew-posts.php +268 -207 1.63.1 View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +
3 +if ( ! defined( 'ABSPATH' ) ) {
4 + exit; // Exit if accessed directly.
5 +}
6 +
2 7 /**
3 8 * Posts Widget
4 9 *
5 10 * @package Essential_Widgets
@@ -5,270 +10,326 @@
5 10 * @package Essential_Widgets
6 11 */
7 12
8 13
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_Posts extends WP_Widget {
17 -
14 +if ( ! class_exists( 'EW_Posts' ) ) :
18 15 /**
19 - * Holds widget settings defaults, populated in constructor.
16 + * Makes a custom Widget for Displaying About
20 17 *
21 - * @var array
18 + * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
19 + *
20 + * @package Essential_Widgets
22 21 */
23 - protected $defaults;
22 + class EW_Posts extends WP_Widget // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- EW_ is this plugin's abbreviated prefix; wrapped in class_exists() guard.
23 + {
24 24
25 - function __construct() {
26 25
27 - $this->defaults = array(
28 - 'title' => esc_attr__( 'Posts', 'essential-widgets' ),
29 - 'post_type' => array( 'post' ),
30 - 'order' => 'DESC',
31 - 'orderby' => 'date',
32 - 'number' => 10,
33 - 'show_date' => false,
34 - 'show_author' => false
35 - );
26 + /**
27 + * Holds widget settings defaults, populated in constructor.
28 + *
29 + * @var array
30 + */
31 + protected $defaults;
36 32
37 - $widget_ops = array(
38 - 'classname' => 'essential-widgets ew-posts ewposts',
39 - 'description' => esc_html__( 'Displays a list of posts.', 'essential-widgets' ),
40 - );
33 + public function __construct() {
34 + $this->defaults = array(
35 + 'title' => esc_attr__( 'Posts', 'essential-widgets' ),
36 + 'post_type' => array( 'post' ),
37 + 'order' => 'DESC',
38 + 'orderby' => 'date',
39 + 'number' => 10,
40 + 'show_date' => false,
41 + 'show_author' => false,
42 + );
41 43
42 - $control_ops = array(
43 - 'id_base' => 'ew-posts',
44 - );
44 + $widget_ops = array(
45 + 'classname' => 'essential-widgets ew-posts ewposts',
46 + 'description' => esc_html__( 'Displays a list of posts.', 'essential-widgets' ),
47 + );
45 48
46 - parent::__construct(
47 - 'ew-posts', // Base ID
48 - esc_html__( 'EW: Posts', 'essential-widgets' ), // Name
49 - $widget_ops,
50 - $control_ops
51 - );
52 - }
49 + $control_ops = array(
50 + 'id_base' => 'ew-posts',
51 + );
53 52
54 - /**
55 - * Displays the widget control options in the Widgets admin screen.
56 - *
57 - * @since 1.0.0
58 - * @access public
59 - * @param array $instance
60 - * @param void
61 - */
62 - public function form( $instance ) {
53 + parent::__construct(
54 + 'ew-posts', // Base ID
55 + esc_html__( 'EW: Posts', 'essential-widgets' ), // Name
56 + $widget_ops,
57 + $control_ops
58 + );
59 + }
63 60
64 - // Merge the user-selected arguments with the defaults.
65 - $instance = wp_parse_args( (array) $instance, $this->defaults );
61 + /**
62 + * Displays the widget control options in the Widgets admin screen.
63 + *
64 + * @since 1.0.0
65 + * @access public
66 + * @param array $instance
67 + * @param void
68 + */
69 + public function form( $instance ) {
70 + // Merge the user-selected arguments with the defaults.
71 + $instance = wp_parse_args( (array) $instance, $this->defaults );
66 72
67 - // <select> element options.
68 - $types = get_post_types( array( 'public' => true ), 'objects' );
73 + // <select> element options.
74 + $types = get_post_types( array( 'public' => true ), 'objects' );
69 75
70 - $order = array(
71 - 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
72 - 'DESC' => esc_attr__( 'Descending', 'essential-widgets' )
73 - );
76 + $order = array(
77 + 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
78 + 'DESC' => esc_attr__( 'Descending', 'essential-widgets' ),
79 + );
74 80
75 - $orderby = array(
76 - 'author' => esc_attr__( 'Author', 'essential-widgets' ),
77 - 'name' => esc_attr__( 'Slug', 'essential-widgets' ),
78 - 'none' => esc_attr__( 'None', 'essential-widgets' ),
79 - 'type' => esc_attr__( 'Type', 'essential-widgets' ),
80 - 'date' => esc_attr__( 'Date', 'essential-widgets' ),
81 - 'ID' => esc_attr__( 'ID', 'essential-widgets' ),
82 - 'modified' => esc_attr__( 'Date (Modified)', 'essential-widgets' ),
83 - 'parent' => esc_attr__( 'Parent', 'essential-widgets' ),
84 - 'comment_count' => esc_attr__( 'Comment Count', 'essential-widgets' ),
85 - 'menu_order' => esc_attr__( 'Menu Order', 'essential-widgets' ),
86 - 'title' => esc_attr__( 'Title', 'essential-widgets' )
87 - ); ?>
81 + $orderby = array(
82 + 'author' => esc_attr__( 'Author', 'essential-widgets' ),
83 + 'name' => esc_attr__( 'Slug', 'essential-widgets' ),
84 + 'none' => esc_attr__( 'None', 'essential-widgets' ),
85 + 'type' => esc_attr__( 'Type', 'essential-widgets' ),
86 + 'date' => esc_attr__( 'Date', 'essential-widgets' ),
87 + 'ID' => esc_attr__( 'ID', 'essential-widgets' ),
88 + 'modified' => esc_attr__( 'Date (Modified)', 'essential-widgets' ),
89 + 'parent' => esc_attr__( 'Parent', 'essential-widgets' ),
90 + 'comment_count' => esc_attr__( 'Comment Count', 'essential-widgets' ),
91 + 'menu_order' => esc_attr__( 'Menu Order', 'essential-widgets' ),
92 + 'title' => esc_attr__( 'Title', 'essential-widgets' ),
93 + ); ?>
88 94
89 - <p>
90 - <label>
91 - <?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
92 - <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'] ); ?>" />
93 - </label>
94 - </p>
95 + <p>
96 + <label>
97 + <?php esc_html_e( 'Title:', 'essential-widgets' ); ?>
98 + <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'] ); ?>" />
99 + </label>
100 + </p>
95 101
96 - <?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?>
102 + <?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?>
97 103
98 - <div class="wp-tab-panel">
99 - <ul>
100 - <?php foreach ( $types as $type ) : ?>
104 + <div class="wp-tab-panel">
105 + <ul>
106 + <?php foreach ( $types as $type ) : ?>
101 107
102 - <li>
103 - <label>
104 - <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>[]" value="<?php echo esc_attr( $type->name ); ?>" <?php checked( in_array( $type->name, (array)$instance['post_type'] ) ); ?> />
105 - <?php echo esc_html( $type->labels->singular_name ); ?>
106 - </label>
107 - </li>
108 + <li>
109 + <label>
110 + <input type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>[]" value="<?php echo esc_attr( $type->name ); ?>" <?php checked( in_array( $type->name, (array) $instance['post_type'], true ) ); ?> />
111 + <?php echo esc_html( $type->labels->singular_name ); ?>
112 + </label>
113 + </li>
108 114
109 - <?php endforeach; ?>
110 - </ul>
111 - </div>
115 + <?php endforeach; ?>
116 + </ul>
117 + </div>
112 118
113 - <p>
114 - <label>
115 - <?php esc_html_e( 'Number:', 'essential-widgets' ); ?>
116 - <input type="number" min="1" size="3" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" placeholder="<?php echo esc_attr( $this->defaults['number'] ); ?>" />
117 - </label>
118 - </p>
119 + <p>
120 + <label>
121 + <?php esc_html_e( 'Number:', 'essential-widgets' ); ?>
122 + <input
123 + type="number"
124 + min="1"
125 + size="3"
126 + class="widefat"
127 + name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>"
128 + value="<?php echo esc_attr( $instance['number'] ); ?>"
129 + placeholder="<?php echo esc_attr( $this->defaults['number'] ); ?>"
130 + />
131 + </label>
132 + </p>
119 133
120 - <p>
121 - <label>
122 - <?php esc_html_e( 'Order:', 'essential-widgets' ); ?>
134 + <p>
135 + <label>
136 + <?php esc_html_e( 'Order:', 'essential-widgets' ); ?>
123 137
124 - <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
138 + <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
139 + <?php foreach ( $order as $option_value => $option_label ) : ?>
140 + <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>>
141 + <?php echo esc_html( $option_label ); ?>
142 + </option>
143 + <?php endforeach; ?>
144 + </select>
145 + </label>
146 + </p>
125 147
126 - <?php foreach ( $order as $option_value => $option_label ) : ?>
148 + <p>
149 + <label>
150 + <?php esc_html_e( 'Order By:', 'essential-widgets' ); ?>
127 151
128 - <option value="<?php echo $option_value; ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo $option_label; ?></option>
152 + <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
153 + <?php foreach ( $orderby as $option_value => $option_label ) : ?>
154 + <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['orderby'], $option_value ); ?>>
155 + <?php echo esc_html( $option_label ); ?>
156 + </option>
157 + <?php endforeach; ?>
158 + </select>
159 + </label>
160 + </p>
129 161
130 - <?php endforeach; ?>
162 + <p>
163 + <label>
164 + <input type="checkbox" <?php checked( $instance['show_date'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" />
165 + <?php esc_html_e( 'Display post date?', 'essential-widgets' ); ?>
166 + </label>
167 + </p>
131 168
132 - </select>
133 - </label>
134 - </p>
169 + <p>
170 + <label>
171 + <input type="checkbox" <?php checked( $instance['show_author'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_author' ) ); ?>" />
172 + <?php esc_html_e( 'Display post author?', 'essential-widgets' ); ?>
173 + </label>
174 + </p>
175 + <?php
176 + }
135 177
136 - <p>
137 - <label>
138 - <?php esc_html_e( 'Order By:', 'essential-widgets' ); ?>
139 178
140 - <select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
179 + /**
180 + * update the particular instant
181 + *
182 + * This function should check that $new_instance is set correctly.
183 + * The newly calculated value of $instance should be returned.
184 + * If "false" is returned, the instance won't be saved/updated.
185 + *
186 + * $new_instance New settings for this instance as input by the user via form()
187 + * $old_instance Old settings for this instance
188 + * Settings to save or bool false to cancel saving
189 + */
190 + public function update( $new_instance, $old_instance ) {
191 + $instance = $old_instance;
141 192
142 - <?php foreach ( $orderby as $option_value => $option_label ) : ?>
193 + // Sanitize title.
194 + $instance['title'] = isset( $new_instance['title'] )
195 + ? sanitize_text_field( $new_instance['title'] )
196 + : '';
143 197
144 - <option value="<?php echo $option_value; ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo $option_label; ?></option>
198 + // Sanitize post_type array.
199 + $instance['post_type'] = isset( $new_instance['post_type'] )
200 + ? array_map( 'sanitize_key', (array) $new_instance['post_type'] )
201 + : array( 'post' );
145 202
146 - <?php endforeach; ?>
203 + // Whitelist order options.
204 + $order = array( 'ASC', 'DESC' );
205 + $instance['order'] = ( isset( $new_instance['order'] ) && in_array( $new_instance['order'], $order, true ) )
206 + ? $new_instance['order']
207 + : 'DESC';
147 208
148 - </select>
149 - </label>
150 - </p>
209 + // Whitelist orderby options.
210 + $orderby = array( 'author', 'name', 'none', 'type', 'date', 'ID', 'modified', 'parent', 'comment_count', 'menu_order', 'title' );
211 + $instance['orderby'] = ( isset( $new_instance['orderby'] ) && in_array( $new_instance['orderby'], $orderby, true ) )
212 + ? $new_instance['orderby']
213 + : 'date';
151 214
152 - <p>
153 - <label>
154 - <input type="checkbox" <?php checked( $instance['show_date'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" />
155 - <?php esc_html_e( 'Display post date?', 'essential-widgets' ); ?>
156 - </label>
157 - </p>
215 + // Sanitize number input.
216 + $instance['number'] = isset( $new_instance['number'] )
217 + ? intval( $new_instance['number'] )
218 + : 10;
158 219
159 - <p>
160 - <label>
161 - <input type="checkbox" <?php checked( $instance['show_author'], true ); ?> name="<?php echo esc_attr( $this->get_field_name( 'show_author' ) ); ?>" />
162 - <?php esc_html_e( 'Display post author?', 'essential-widgets' ); ?>
163 - </label>
164 - </p>
165 - <?php }
220 + // Checkboxes: show_date and show_author.
221 + $instance['show_date'] = !empty( $new_instance['show_date'] ) ? 1 : 0;
222 + $instance['show_author'] = !empty( $new_instance['show_author'] ) ? 1 : 0;
166 223
224 + return $instance;
225 + }
167 226
168 - /**
169 - * update the particular instant
170 - *
171 - * This function should check that $new_instance is set correctly.
172 - * The newly calculated value of $instance should be returned.
173 - * If "false" is returned, the instance won't be saved/updated.
174 - *
175 - * $new_instance New settings for this instance as input by the user via form()
176 - * $old_instance Old settings for this instance
177 - * Settings to save or bool false to cancel saving
178 - */
179 - function update( $new_instance, $old_instance ) {
180 227
181 - $instance = $old_instance;
228 + /**
229 + * Displays the Widget in the front-end.
230 + *
231 + * $args Display arguments including before_title, after_title, before_widget, and after_widget.
232 + * $instance The settings for the particular instance of the widget
233 + */
234 + public function widget( $args, $instance ) {
235 + // Merge instance with defaults.
236 + $instance = wp_parse_args( $instance, $this->defaults );
182 237
183 - // Sanitize title.
184 - $instance['title'] = sanitize_text_field( $new_instance['title'] );
238 + $loop = new \WP_Query(
239 + array(
240 + 'posts_per_page' => $instance['number'],
241 + 'post_type' => $instance['post_type'],
242 + 'order' => $instance['order'],
243 + 'orderby' => $instance['orderby'],
244 + 'no_found_rows' => true,
245 + 'post_status' => 'publish',
246 + 'ignore_sticky_posts' => true,
247 + )
248 + );
185 249
186 - // Sanitize key.
187 - $instance['post_type'] = array_map( 'sanitize_key', $new_instance['post_type'] );
250 + if ( $loop->have_posts() ) :
188 251
189 - // Whitelist options.
190 - $order = array( 'ASC', 'DESC' );
191 - $orderby = array( 'author', 'name', 'none', 'type', 'date', 'ID', 'modified', 'parent', 'comment_count', 'menu_order', 'title' );
252 + // Escape widget wrapper attributes.
253 + echo wp_kses_post( $args['before_widget'] );
192 254
193 - $instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'DESC';
194 - $instance['orderby'] = in_array( $new_instance['orderby'], $orderby ) ? $new_instance['orderby'] : 'date';
255 + // If a title was input by the user, display it safely.
256 + if ( ! empty( $instance['title'] ) ) {
257 + echo wp_kses_post( $args['before_title'] );
195 258
196 - // Integers.
197 - $instance['number'] = intval( $new_instance['number'] );
259 + // Apply filters to title, then escape it for output
260 + $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
261 + echo esc_html( $title );
198 262
199 - // Checkboxes.
200 - $instance['show_date'] = isset( $new_instance['show_date'] ) ? 1 : 0;
201 - $instance['show_author'] = isset( $new_instance['show_author'] ) ? 1 : 0;
263 + echo wp_kses_post( $args['after_title'] );
264 + }
202 265
203 - // Return sanitized options.
204 - return $instance;
205 - }
266 + // Output the main content of the widget (shortcode output)
267 + echo wp_kses_post( $this->shortcode( $instance ) );
206 268
207 - /**
208 - * Displays the Widget in the front-end.
209 - *
210 - * $args Display arguments including before_title, after_title, before_widget, and after_widget.
211 - * $instance The settings for the particular instance of the widget
212 - */
213 - function widget( $args, $instance ) {
269 + // Close the widget wrapper safely.
270 + echo wp_kses_post( $args['after_widget'] );
214 271
215 - // Set the $args for wp_get_archives() to the $instance array.
216 - $instance = wp_parse_args( $instance, $this->defaults );
272 + wp_reset_postdata();
217 273
218 - $loop = new \WP_Query(
219 - array(
220 - 'posts_per_page' => $instance['number'],
221 - 'post_type' => $instance['post_type'],
222 - 'order' => $instance['order'],
223 - 'orderby' => $instance['orderby'],
224 - 'no_found_rows' => true,
225 - 'post_status' => 'publish',
226 - 'ignore_sticky_posts' => true,
227 - )
228 - );
274 + endif;
275 + }
229 276
230 - if ( $loop->have_posts() ) : ?>
231 - <?php echo $args['before_widget']; ?>
277 + public function shortcode( $atts ) {
278 + $atts = wp_parse_args( $atts, $this->defaults );
232 279
233 - <?php
234 - if ( ! empty( $instance['title'] ) )
235 - echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
236 - ?>
280 + $loop = new \WP_Query(
281 + array(
282 + 'posts_per_page' => $atts['number'],
283 + 'post_type' => $atts['post_type'],
284 + 'order' => $atts['order'],
285 + 'orderby' => $atts['orderby'],
286 + 'no_found_rows' => true,
287 + 'post_status' => 'publish',
288 + 'ignore_sticky_posts' => true,
289 + )
290 + );
237 291
238 - <ul>
292 + $output = '';
239 293
240 - <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
294 + // Only show this title in block element and not on widget.
295 + if ( isset( $atts['is_block'] ) && true === $atts['is_block'] && !empty( $atts['title'] ) ) {
296 + $output .= '<h2 class="ew-post-block-title">' . esc_html( $atts['title'] ) . '</h2>';
297 + }
241 298
242 - <li>
243 - <a href="<?php the_permalink(); ?>"><?php get_the_title() ? the_title() : the_ID(); ?></a>
299 + // Output the page list.
300 + $output .= '<ul>';
301 + while ( $loop->have_posts() ) :
302 + $loop->the_post();
244 303
245 - <?php if ( $instance['show_author'] ) : ?>
246 - <span class="post-author"><?php echo ' by ' . get_the_author(); ?></span>
247 - <?php endif; ?>
304 + $output .= '<li>';
305 + $output .= '<a href="' . esc_url( get_the_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
248 306
249 - <?php if ( $instance['show_date'] ) : ?>
250 - <span class="post-date"><?php echo get_the_date(); ?></span>
251 - <?php endif; ?>
252 - </li>
307 + if ( $atts['show_author'] ) :
308 + $output .= '<span class="post-author"> by ' . esc_html( get_the_author() ) . '</span>';
309 + endif;
253 310
254 - <?php endwhile; ?>
311 + if ( $atts['show_date'] ) :
312 + $output .= ' <span class="post-date">' . esc_html( get_the_date() ) . '</span>';
313 + endif;
314 + $output .= '</li>';
255 315
256 - </ul>
316 + endwhile;
257 317
258 - <?php echo $args['after_widget']; ?>
318 + $output .= '</ul>';
319 + return $output;
320 + }
321 + }
322 +endif;
259 323
260 - <?php wp_reset_postdata();
261 324
262 - endif;
325 +if ( ! function_exists( 'ew_posts_register' ) ) :
326 + /**
327 + * Intiate About_Widget Class.
328 + *
329 + * @since 1.0.0
330 + */
331 + function ew_posts_register() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ew_ is this plugin's abbreviated prefix.
332 + register_widget( 'EW_Posts' );
263 333 }
264 -}
265 -
266 -/**
267 - * Intiate About_Widget Class.
268 - *
269 - * @since 1.0.0
270 - */
271 -function ew_posts_register() {
272 - register_widget( 'EW_Posts' );
273 -}
274 -add_action( 'widgets_init', 'ew_posts_register' );
334 + add_action( 'widgets_init', 'ew_posts_register' );
335 +endif;