PluginProbe
Essential Widgets / 3.0
Essential Widgets v3.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-posts.php

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

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