PluginProbe
Powerkit – Supercharge your WordPress Site / 2.9.0
Powerkit – Supercharge your WordPress Site v2.9.0
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / posts / public / class-powerkit-posts-widget.php

class-powerkit-posts-widget.php in Powerkit – Supercharge your WordPress Site 2.9.0, at modules/posts/public/class-powerkit-posts-widget.php

376 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widget Featured Posts
4 *
5 * @link https://codesupply.co
6 * @since 1.0.0
7 *
8 * @package Powerkit
9 * @subpackage Powerkit/widgets
10 */
11
12 /**
13 * Widget Featured Posts
14 */
15 class Powerkit_Featured_Posts_Widget extends WP_Widget {
16
17 /**
18 * The default settings.
19 *
20 * @var array $default_settings The default settings.
21 */
22 public $default_settings = array();
23
24 /**
25 * Sets up a new widget instance.
26 */
27 public function __construct() {
28
29 $this->default_settings = apply_filters(
30 'powerkit_widget_posts_settings', array(
31 'title' => '',
32 'template' => 'list',
33 'post_type' => 'post',
34 'posts_per_page' => 5,
35 'orderby' => 'date',
36 'order' => 'desc',
37 'time_frame' => '',
38 'category' => false,
39 'post_meta' => array( 'date' ),
40 'post_meta_compact' => false,
41 'avoid_duplicate' => false,
42 )
43 );
44
45 $widget_details = array(
46 'classname' => 'powerkit_widget_posts',
47 'description' => esc_html__( 'Display a list of your featured posts.', 'powerkit' ),
48 );
49 parent::__construct( 'powerkit_widget_posts', esc_html__( 'Featured Posts', 'powerkit' ), $widget_details );
50 }
51
52 /**
53 * Outputs the content for the current widget instance.
54 *
55 * @param array $args Display arguments including 'before_title', 'after_title',
56 * 'before_widget', and 'after_widget'.
57 * @param array $instance Settings for the current widget instance.
58 */
59 public function widget( $args, $instance ) {
60 global $pk_posts;
61 global $wp_query;
62
63 if ( ! $pk_posts ) {
64 $pk_posts = array();
65 }
66
67 $params = array_merge( $this->default_settings, $instance );
68
69 if ( in_array( 'category', $params['post_meta'], true ) ) {
70 $params['post_meta_category'] = true;
71 } else {
72 $params['post_meta_category'] = false;
73 }
74
75 $posts_args = array(
76 'post_type' => $params['post_type'],
77 'posts_per_page' => $params['posts_per_page'],
78 'order' => $params['order'],
79 'no_found_rows' => true,
80 'post_status' => 'publish',
81 'ignore_sticky_posts' => true,
82 );
83
84 if ( $params['category'] ) {
85 $category = $params['category'];
86 $posts_args['cat'] = $category;
87 }
88
89 // Avoid Duplicate.
90 if ( $params['avoid_duplicate'] ) {
91 $main_posts = array();
92
93 if ( isset( $wp_query->posts ) && $wp_query->posts ) {
94 $main_posts = wp_list_pluck( $wp_query->posts, 'ID' );
95 }
96
97 if ( $main_posts ) {
98 $posts_args['post__not_in'] = array_merge( $main_posts, $pk_posts );
99 } else {
100 $posts_args['post__not_in'] = $pk_posts;
101 }
102 }
103
104 // Post order.
105 $posts_args['orderby'] = $params['orderby'];
106
107 $type_post_views = powerkit_post_views_enabled();
108
109 if ( $type_post_views && ( 'views' === $params['orderby'] || 'post_views' === $params['orderby'] ) ) {
110 $posts_args['orderby'] = $type_post_views;
111 // Don't hide posts without views.
112 $posts_args['views_query']['hide_empty'] = false;
113 }
114
115 if ( $params['time_frame'] ) {
116 $posts_args['date_query'] = array(
117 array(
118 'column' => 'post_date_gmt',
119 'after' => $params['time_frame'] . ' ago',
120 ),
121 );
122 }
123
124 $posts = new WP_Query( apply_filters_ref_array( 'powerkit_widget_featured_posts_args', array( $posts_args, & $params ) ) );
125
126 if ( $posts->have_posts() ) {
127
128 // Before Widget.
129 echo $args['before_widget']; // XSS.
130
131 // Title.
132 if ( $params['title'] ) {
133 echo $args['before_title'] . apply_filters( 'widget_title', wp_kses( $params['title'], 'pk-title' ), $instance, $this->id_base ) . $args['after_title']; // XSS.
134 }
135
136 // Template.
137 if ( in_array( $params['template'], array( 'list', 'numbered', 'large' ), true ) ) {
138 $class = sprintf( 'pk-widget-posts-template-default pk-widget-posts-template-%s', $params['template'] );
139 } else {
140 $class = sprintf( 'pk-widget-posts-template-%s', $params['template'] );
141 }
142
143 // Number of Posts.
144 $class .= sprintf( ' posts-per-page-%s', (int) $params['posts_per_page'] );
145 ?>
146
147 <div class="widget-body pk-widget-posts <?php echo esc_html( $class ); ?>">
148 <ul>
149 <?php
150 while ( $posts->have_posts() ) :
151 $posts->the_post();
152
153 $pk_posts[] = get_the_ID();
154 ?>
155 <li class="pk-post-item">
156 <?php powerkit_widget_featured_posts_handler( $params['template'], $posts, $params, $instance ); ?>
157 </li>
158 <?php endwhile; ?>
159 </ul>
160 </div>
161
162 <?php
163
164 // After Widget.
165 echo $args['after_widget']; // XSS.
166 }
167
168 wp_reset_postdata();
169 }
170
171 /**
172 * Handles updating settings for the current widget instance.
173 *
174 * @param array $new_instance New settings for this instance as input by the user via
175 * WP_Widget::form().
176 * @param array $old_instance Old settings for this instance.
177 * @return array Settings to save or bool false to cancel saving.
178 */
179 public function update( $new_instance, $old_instance ) {
180 $instance = $new_instance;
181
182 // Post Meta.
183 if ( ! isset( $instance['post_meta'] ) ) {
184 $instance['post_meta'] = array();
185 }
186
187 // Compact Post Meta.
188 if ( ! isset( $instance['post_meta_compact'] ) ) {
189 $instance['post_meta_compact'] = false;
190 }
191
192 // Avoid duplicate posts.
193 if ( ! isset( $instance['avoid_duplicate'] ) ) {
194 $instance['avoid_duplicate'] = false;
195 }
196
197 return apply_filters( 'powerkit_widget_posts_update', $instance );
198 }
199
200 /**
201 * Outputs the widget settings form.
202 *
203 * @param array $instance Current settings.
204 */
205 public function form( $instance ) {
206 $params = array_merge( $this->default_settings, $instance );
207
208 $templates = apply_filters( 'powerkit_featured_posts_templates', array() );
209 ?>
210 <!-- Title -->
211 <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'powerkit' ); ?></label>
212 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $params['title'] ); ?>" /></p>
213
214 <?php do_action( 'powerkit_widget_posts_form_before', $this, $params, $instance ); ?>
215
216 <!-- Template -->
217 <?php if ( $templates ) { ?>
218 <p>
219 <label for="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>"><?php esc_html_e( 'Template:', 'powerkit' ); ?></label>
220 <select name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>" class="widefat">
221 <?php
222 foreach ( $templates as $slug => $template ) {
223 $name = isset( $template['name'] ) ? $template['name'] : $slug;
224 ?>
225 <option value="<?php echo esc_attr( $slug ); ?>" <?php selected( $params['template'], $slug ); ?>><?php echo esc_html( $name ); ?></option>
226 <?php } ?>
227 </select>
228 </p>
229 <?php } ?>
230
231 <!-- Post Type -->
232 <p>
233 <label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>"><?php esc_html_e( 'Post Type', 'powerkit' ); ?>:</label>
234 <select name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>" class="widefat">
235 <?php
236 $post_types = get_post_types( array(
237 'publicly_queryable' => 1,
238 ), 'objects' );
239
240 foreach ( $post_types as $name => $post_type ) {
241 ?>
242 <option value="<?php echo esc_attr( $name ); ?>" <?php selected( $params['post_type'], $name ); ?>><?php echo esc_html( $post_type->labels->name ); ?></option>
243 <?php } ?>
244 </select>
245 </p>
246
247 <!-- Number of Posts -->
248 <p><label for="<?php echo esc_attr( $this->get_field_id( 'posts_per_page' ) ); ?>"><?php esc_html_e( 'Number of posts:', 'powerkit' ); ?></label>
249 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'posts_per_page' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'posts_per_page' ) ); ?>" type="number" value="<?php echo esc_attr( $params['posts_per_page'] ); ?>" /></p>
250
251 <!-- Order by -->
252 <p>
253 <label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php esc_html_e( 'Order by:', 'powerkit' ); ?></label>
254 <select name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" class="widefat">
255 <option value="date" <?php selected( $params['orderby'], 'date' ); ?>><?php esc_html_e( 'Date', 'powerkit' ); ?></option>
256 <option value="comment_count" <?php selected( $params['orderby'], 'comment_count' ); ?>><?php esc_html_e( 'Comments', 'powerkit' ); ?></option>
257 <option value="rand" <?php selected( $params['orderby'], 'rand' ); ?>><?php esc_html_e( 'Random', 'powerkit' ); ?></option>
258
259 <?php if ( powerkit_post_views_enabled() ) { ?>
260 <option value="post_views" <?php selected( $params['orderby'], 'post_views' ); ?>><?php esc_html_e( 'Views', 'powerkit' ); ?></option>
261 <?php } ?>
262 </select>
263 </p>
264
265 <!-- Order -->
266 <p>
267 <label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php esc_html_e( 'Order', 'powerkit' ); ?>:</label>
268 <select name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" class="widefat">
269 <option value="desc" <?php selected( $params['order'], 'desc' ); ?>><?php esc_html_e( 'Descending', 'powerkit' ); ?></option>
270 <option value="asc" <?php selected( $params['order'], 'asc' ); ?>><?php esc_html_e( 'Ascending', 'powerkit' ); ?></option>
271 </select>
272 </p>
273
274 <!-- Time Frame -->
275 <p><label for="<?php echo esc_attr( $this->get_field_id( 'time_frame' ) ); ?>"><?php esc_html_e( 'Time frame:', 'powerkit' ); ?></label>
276 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'time_frame' ) ); ?>" placeholder="<?php esc_html_e( '3 months', 'powerkit' ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'time_frame' ) ); ?>" type="text" value="<?php echo esc_attr( $params['time_frame'] ); ?>" /></p>
277
278 <!-- Category -->
279 <p>
280 <label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php esc_html_e( 'Category:', 'powerkit' ); ?></label>
281 <select name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>[]" id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" class="widefat" style="height: auto !important;" multiple="multiple" size="8">
282 <?php
283 $cat_args = array(
284 'hide_empty' => 0,
285 'hierarchical' => 1,
286 'selected' => (array) $params['category'],
287 'walker' => new Powerkit_Add_Posts_Categories_Tree_Walker(),
288 );
289
290 $allowed_html = array(
291 'option' => array(
292 'class' => true,
293 'value' => true,
294 'selected' => true,
295 ),
296 );
297
298 echo wp_kses( walk_category_dropdown_tree( get_categories( $cat_args ), 0, $cat_args ), $allowed_html );
299 ?>
300 </select>
301 </p>
302
303 <!-- Post meta -->
304 <h4><?php esc_html_e( 'Post meta:', 'powerkit' ); ?></h4>
305
306 <?php
307 $allowed_post_meta = powerkit_allowed_post_meta();
308
309 foreach ( $allowed_post_meta as $key => $caption ) {
310 ?>
311 <p><input id="<?php echo esc_attr( $this->get_field_id( 'post_meta' ) ); ?>-<?php echo esc_attr( $key ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_meta' ) ); ?>[]" type="checkbox" value="<?php echo esc_attr( $key ); ?>" <?php checked( in_array( $key, (array) $params['post_meta'], true ) ? true : false ); ?> />
312 <label for="<?php echo esc_attr( $this->get_field_id( 'post_meta' ) ); ?>-<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $caption ); ?></label></p>
313 <?php
314 }
315 ?>
316
317 <!-- Compact Post Meta -->
318 <h4><?php esc_html_e( 'Compact post meta:', 'powerkit' ); ?></h4>
319 <p><input id="<?php echo esc_attr( $this->get_field_id( 'post_meta_compact' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'post_meta_compact' ) ); ?>" type="checkbox" <?php checked( (bool) $params['post_meta_compact'] ); ?> />
320 <label for="<?php echo esc_attr( $this->get_field_id( 'post_meta_compact' ) ); ?>"><?php esc_html_e( 'Display compact post meta', 'powerkit' ); ?></label></p>
321
322 <!-- Avoid duplicate posts -->
323 <h4><?php esc_html_e( 'Avoid duplicate posts:', 'powerkit' ); ?></h4>
324 <p><input id="<?php echo esc_attr( $this->get_field_id( 'avoid_duplicate' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'avoid_duplicate' ) ); ?>" type="checkbox" <?php checked( (bool) $params['avoid_duplicate'] ); ?> />
325 <label for="<?php echo esc_attr( $this->get_field_id( 'avoid_duplicate' ) ); ?>"><?php esc_html_e( 'Exclude duplicate posts', 'powerkit' ); ?></label></p>
326
327 <?php do_action( 'powerkit_widget_posts_form_after', $this, $params, $instance ); ?>
328
329 <?php
330 }
331 }
332
333 /**
334 * Create HTML dropdown list of Categories.
335 */
336 class Powerkit_Add_Posts_Categories_Tree_Walker extends Walker_CategoryDropdown {
337
338 /**
339 * Starts the element output.
340 *
341 * @since 2.1.0
342 * @access public
343 *
344 * @see Walker::start_el()
345 *
346 * @param string $output Passed by reference. Used to append additional content.
347 * @param object $category Category data object.
348 * @param int $depth Depth of category. Used for padding.
349 * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
350 * See wp_dropdown_categories().
351 * @param int $id Optional. ID of the current category. Default 0 (unused).
352 */
353 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
354 $pad = $depth > 0 ? '- ' . str_repeat( '&nbsp;', $depth * 3 ) : '';
355 $selected = array_map( 'intval', $args['selected'] );
356 $cat_name = apply_filters( 'list_cats', $category->name, $category );
357
358 $output .= sprintf(
359 '<option class="level-%1$s" value="%2$s" %4$s>%3$s</option>',
360 esc_attr( $depth ),
361 esc_attr( $category->term_id ),
362 esc_html( $pad . $cat_name ),
363 selected( in_array( $category->term_id, $selected, true ), true )
364 );
365 $output .= "\n";
366 }
367 }
368
369 /**
370 * Register Widget
371 */
372 function powerkit_widget_init_featured_posts() {
373 register_widget( 'Powerkit_Featured_Posts_Widget' );
374 }
375 add_action( 'widgets_init', 'powerkit_widget_init_featured_posts' );
376