PluginProbe
Essential Widgets / 1.8
Essential Widgets v1.8
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 1.8, at includes/widgets/class-ew-posts.php

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