PluginProbe
Powerkit – Supercharge your WordPress Site / 3.1.1
Powerkit – Supercharge your WordPress Site v3.1.1
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 3.1.1, at modules/posts/public/class-powerkit-posts-widget.php

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