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

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