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

class-ew-posts.php in Essential Widgets 3.2, at includes/widgets/class-ew-posts.php

336 lines 10.5 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 * Posts Widget
9 *
10 * @package Essential_Widgets
11 */
12
13
14 if ( ! class_exists( 'EW_Posts' ) ) :
15 /**
16 * Makes a custom Widget for Displaying About
17 *
18 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
19 *
20 * @package Essential_Widgets
21 */
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
25
26 /**
27 * Holds widget settings defaults, populated in constructor.
28 *
29 * @var array
30 */
31 protected $defaults;
32
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 );
43
44 $widget_ops = array(
45 'classname' => 'essential-widgets ew-posts ewposts',
46 'description' => esc_html__( 'Displays a list of posts.', 'essential-widgets' ),
47 );
48
49 $control_ops = array(
50 'id_base' => 'ew-posts',
51 );
52
53 parent::__construct(
54 'ew-posts', // Base ID
55 esc_html__( 'EW: Posts', 'essential-widgets' ), // Name
56 $widget_ops,
57 $control_ops
58 );
59 }
60
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 );
72
73 // <select> element options.
74 $types = get_post_types( array( 'public' => true ), 'objects' );
75
76 $order = array(
77 'ASC' => esc_attr__( 'Ascending', 'essential-widgets' ),
78 'DESC' => esc_attr__( 'Descending', 'essential-widgets' ),
79 );
80
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 ); ?>
94
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>
101
102 <?php esc_html_e( 'Post Type:', 'essential-widgets' ); ?>
103
104 <div class="wp-tab-panel">
105 <ul>
106 <?php foreach ( $types as $type ) : ?>
107
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>
114
115 <?php endforeach; ?>
116 </ul>
117 </div>
118
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>
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( '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>
147
148 <p>
149 <label>
150 <?php esc_html_e( 'Order By:', 'essential-widgets' ); ?>
151
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>
161
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>
168
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 }
177
178
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;
192
193 // Sanitize title.
194 $instance['title'] = isset( $new_instance['title'] )
195 ? sanitize_text_field( $new_instance['title'] )
196 : '';
197
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' );
202
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';
208
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';
214
215 // Sanitize number input.
216 $instance['number'] = isset( $new_instance['number'] )
217 ? intval( $new_instance['number'] )
218 : 10;
219
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;
223
224 return $instance;
225 }
226
227
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 );
237
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 );
249
250 if ( $loop->have_posts() ) :
251
252 // Escape widget wrapper attributes.
253 echo wp_kses_post( $args['before_widget'] );
254
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'] );
258
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 );
262
263 echo wp_kses_post( $args['after_title'] );
264 }
265
266 // Output the main content of the widget (shortcode output)
267 echo wp_kses_post( $this->shortcode( $instance ) );
268
269 // Close the widget wrapper safely.
270 echo wp_kses_post( $args['after_widget'] );
271
272 wp_reset_postdata();
273
274 endif;
275 }
276
277 public function shortcode( $atts ) {
278 $atts = wp_parse_args( $atts, $this->defaults );
279
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 );
291
292 $output = '';
293
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 }
298
299 // Output the page list.
300 $output .= '<ul>';
301 while ( $loop->have_posts() ) :
302 $loop->the_post();
303
304 $output .= '<li>';
305 $output .= '<a href="' . esc_url( get_the_permalink() ) . '">' . esc_html( get_the_title() ) . '</a>';
306
307 if ( $atts['show_author'] ) :
308 $output .= '<span class="post-author"> by ' . esc_html( get_the_author() ) . '</span>';
309 endif;
310
311 if ( $atts['show_date'] ) :
312 $output .= ' <span class="post-date">' . esc_html( get_the_date() ) . '</span>';
313 endif;
314 $output .= '</li>';
315
316 endwhile;
317
318 $output .= '</ul>';
319 return $output;
320 }
321 }
322 endif;
323
324
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' );
333 }
334 add_action( 'widgets_init', 'ew_posts_register' );
335 endif;
336