PluginProbe
WebberZone Top 10 — Popular Posts / 4.3.4
WebberZone Top 10 — Popular Posts v4.3.4
4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 1.6.1 All 117 releases
top-10 / includes / frontend / widgets / class-posts-widget.php

class-posts-widget.php in WebberZone Top 10 — Popular Posts 4.3.4, at includes/frontend/widgets/class-posts-widget.php

383 lines 18.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Posts Widget class.
4 *
5 * @package WebberZone\Top_Ten\Frontend\Widgets
6 */
7
8 namespace WebberZone\Top_Ten\Frontend\Widgets;
9
10 use WebberZone\Top_Ten\Frontend\Display;
11
12 if ( ! defined( 'WPINC' ) ) {
13 die;
14 }
15
16 /**
17 * Top 10 Widget.
18 *
19 * @since 3.3.0
20 */
21 class Posts_Widget extends \WP_Widget {
22
23 /**
24 * Register widget with WordPress.
25 */
26 public function __construct() {
27 parent::__construct(
28 'widget_tptn_pop', // Base ID.
29 __( 'Popular Posts [Top 10]', 'top-10' ), // Name.
30 array(
31 'description' => __( 'Display popular posts', 'where-did-they-go-from-here' ),
32 'classname' => 'tptn_posts_list_widget',
33 'customize_selective_refresh' => true,
34 'show_instance_in_rest' => true,
35 )
36 );
37 }
38
39 /**
40 * Back-end widget form.
41 *
42 * @see WP_Widget::form()
43 *
44 * @param array $instance Previously saved values from database.
45 */
46 public function form( $instance ) {
47 $instance['title'] = isset( $instance['title'] ) ? $instance['title'] : '';
48 $instance['limit'] = isset( $instance['limit'] ) ? $instance['limit'] : '';
49 $instance['offset'] = isset( $instance['offset'] ) ? $instance['offset'] : '';
50 $instance['disp_list_count'] = isset( $instance['disp_list_count'] ) ? $instance['disp_list_count'] : '';
51 $instance['show_excerpt'] = isset( $instance['show_excerpt'] ) ? $instance['show_excerpt'] : '';
52 $instance['show_author'] = isset( $instance['show_author'] ) ? $instance['show_author'] : '';
53 $instance['show_date'] = isset( $instance['show_date'] ) ? $instance['show_date'] : '';
54 $instance['post_thumb_op'] = isset( $instance['post_thumb_op'] ) ? $instance['post_thumb_op'] : 'text_only';
55 $instance['thumb_height'] = isset( $instance['thumb_height'] ) ? $instance['thumb_height'] : '';
56 $instance['thumb_width'] = isset( $instance['thumb_width'] ) ? $instance['thumb_width'] : '';
57 $instance['daily'] = isset( $instance['daily'] ) ? $instance['daily'] : 'overall';
58 $instance['daily_range'] = isset( $instance['daily_range'] ) ? $instance['daily_range'] : '';
59 $instance['hour_range'] = isset( $instance['hour_range'] ) ? $instance['hour_range'] : '';
60 $instance['include_cat_ids'] = isset( $instance['include_cat_ids'] ) ? $instance['include_cat_ids'] : '';
61 $instance['include_post_ids'] = isset( $instance['include_post_ids'] ) ? $instance['include_post_ids'] : '';
62
63 // Parse the Post types.
64 $post_types = array();
65 // If post_types is empty or contains a query string then use parse_str else consider it comma-separated.
66 if ( ! empty( $instance['post_types'] ) && false === strpos( $instance['post_types'], '=' ) ) {
67 $post_types = explode( ',', $instance['post_types'] );
68 } elseif ( ! empty( $instance['post_types'] ) ) {
69 parse_str( $instance['post_types'], $post_types ); // Save post types in $post_types variable.
70 }
71 $wp_post_types = get_post_types(
72 array(
73 'public' => true,
74 )
75 );
76 $posts_types_inc = array_intersect( $wp_post_types, $post_types );
77
78 ?>
79 <p>
80 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'top-10' ); ?></label>
81 <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( $instance['title'] ); ?>" />
82 </p>
83 <p>
84 <label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php esc_html_e( 'No. of posts', 'top-10' ); ?></label>
85 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'limit' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['limit'] ); ?>" />
86 </p>
87 <p>
88 <label for="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>"><?php esc_html_e( 'Offset', 'top-10' ); ?></label>
89 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'offset' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'offset' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['offset'] ); ?>" />
90 </p>
91 <p>
92 <label for="<?php echo esc_attr( $this->get_field_id( 'daily' ) ); ?>"><?php esc_html_e( 'Select period', 'top-10' ); ?></label>
93 <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'daily' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'daily' ) ); ?>">
94 <option value="overall" <?php selected( $instance['daily'], 'overall' ); ?>><?php esc_html_e( 'Overall', 'top-10' ); ?></option>
95 <option value="daily" <?php selected( $instance['daily'], 'daily' ); ?>><?php esc_html_e( 'Custom time period (Enter below)', 'top-10' ); ?></option>
96 </select>
97 </p>
98 <p>
99 <label for="<?php echo esc_attr( $this->get_field_id( 'daily_range' ) ); ?>"><?php esc_html_e( 'Days', 'top-10' ); ?></label>
100 <input id="<?php echo esc_attr( $this->get_field_id( 'daily_range' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'daily_range' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['daily_range'] ); ?>" />
101 <br />
102 <label for="<?php echo esc_attr( $this->get_field_id( 'hour_range' ) ); ?>"><?php esc_html_e( 'Hours', 'top-10' ); ?></label>
103 <input id="<?php echo esc_attr( $this->get_field_id( 'hour_range' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hour_range' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['hour_range'] ); ?>" />
104 </p>
105 <p>
106 <input class="checkbox" type="checkbox" <?php checked( $instance['disp_list_count'], true ); ?> id="<?php echo esc_attr( $this->get_field_id( 'disp_list_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'disp_list_count' ) ); ?>" />
107 <label for="<?php echo esc_attr( $this->get_field_id( 'disp_list_count' ) ); ?>"><?php esc_html_e( 'Show count', 'top-10' ); ?></label>
108 <br/>
109
110 <input class="checkbox" type="checkbox" <?php checked( $instance['show_excerpt'], true ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_excerpt' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_excerpt' ) ); ?>" />
111 <label for="<?php echo esc_attr( $this->get_field_id( 'show_excerpt' ) ); ?>"><?php esc_html_e( 'Show excerpt', 'top-10' ); ?></label>
112 <br/>
113
114 <input class="checkbox" type="checkbox" <?php checked( $instance['show_author'], true ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_author' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_author' ) ); ?>" />
115 <label for="<?php echo esc_attr( $this->get_field_id( 'show_author' ) ); ?>"><?php esc_html_e( 'Show author', 'top-10' ); ?></label>
116 <br/>
117
118 <input class="checkbox" type="checkbox" <?php checked( $instance['show_date'], true ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" />
119 <label for="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>"><?php esc_html_e( 'Show date', 'top-10' ); ?></label>
120 </p>
121
122 <p>
123 <label for="<?php echo esc_attr( $this->get_field_id( 'post_thumb_op' ) ); ?>"><?php esc_html_e( 'Thumbnail options', 'top-10' ); ?></label>
124 <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'post_thumb_op' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_thumb_op' ) ); ?>">
125 <option value="inline" <?php selected( $instance['post_thumb_op'], 'inline' ); ?>><?php esc_html_e( 'Thumbnails inline, before title', 'top-10' ); ?></option>
126 <option value="after" <?php selected( $instance['post_thumb_op'], 'after' ); ?>><?php esc_html_e( 'Thumbnails inline, after title', 'top-10' ); ?></option>
127 <option value="thumbs_only" <?php selected( $instance['post_thumb_op'], 'thumbs_only' ); ?>><?php esc_html_e( 'Only thumbnails, no text', 'top-10' ); ?></option>
128 <option value="text_only" <?php selected( $instance['post_thumb_op'], 'text_only' ); ?>><?php esc_html_e( 'No thumbnails, only text.', 'top-10' ); ?></option>
129 </select>
130 </p>
131 <p>
132 <label for="<?php echo esc_attr( $this->get_field_id( 'thumb_height' ) ); ?>"><?php esc_html_e( 'Thumbnail height', 'top-10' ); ?></label>
133 <input id="<?php echo esc_attr( $this->get_field_id( 'thumb_height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_height' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['thumb_height'] ); ?>" />
134 </p>
135 <p>
136 <label for="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>"><?php esc_html_e( 'Thumbnail width', 'top-10' ); ?></label>
137 <input id="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_width' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['thumb_width'] ); ?>" />
138 </p>
139 <p>
140 <label for="<?php echo esc_attr( $this->get_field_id( 'include_cat_ids' ) ); ?>">
141 <?php esc_html_e( 'Only from categories (comma-separated list of term taxonomy IDs)', 'top-10' ); ?>:
142 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'include_cat_ids' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'include_cat_ids' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['include_cat_ids'] ); ?>" />
143 </label>
144 </p>
145 <p>
146 <label for="<?php echo esc_attr( $this->get_field_id( 'include_post_ids' ) ); ?>"><?php esc_html_e( 'Include IDs', 'top-10' ); ?></label>
147 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'include_post_ids' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'include_post_ids' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['include_post_ids'] ); ?>" />
148 </p>
149 <p>
150 <label for="<?php echo esc_attr( $this->get_field_id( 'post_types' ) ); ?>"><?php esc_html_e( 'Post types to include', 'top-10' ); ?></label>
151 <br />
152 <?php foreach ( $wp_post_types as $wp_post_type ) { ?>
153
154 <label>
155 <input id="<?php echo esc_attr( $this->get_field_id( 'post_types' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_types' ) ); ?>[]" type="checkbox" value="<?php echo esc_attr( $wp_post_type ); ?>" <?php checked( true, in_array( $wp_post_type, $posts_types_inc, true ), true ); ?> />
156 <?php echo esc_attr( $wp_post_type ); ?>
157 </label>
158 <br />
159
160 <?php } ?>
161 </p>
162
163 <?php
164 /**
165 * Fires after Top 10 widget options.
166 *
167 * @since 2.0.0
168 *
169 * @param array $instance Widget options array
170 */
171 do_action( 'tptn_widget_options_after', $instance );
172 ?>
173
174 <?php
175 }
176
177 /**
178 * Sanitize widget form values as they are saved.
179 *
180 * @see WP_Widget::update()
181 *
182 * @param array $new_instance Values just sent to be saved.
183 * @param array $old_instance Previously saved values from database.
184 *
185 * @return array Updated safe values to be saved.
186 */
187 public function update( $new_instance, $old_instance ) {
188 $instance = $old_instance;
189 $instance['title'] = wp_strip_all_tags( $new_instance['title'] );
190 $instance['limit'] = $new_instance['limit'];
191 $instance['offset'] = $new_instance['offset'];
192 $instance['disp_list_count'] = isset( $new_instance['disp_list_count'] ) ? (bool) $new_instance['disp_list_count'] : false;
193 $instance['show_excerpt'] = isset( $new_instance['show_excerpt'] ) ? (bool) $new_instance['show_excerpt'] : false;
194 $instance['show_author'] = isset( $new_instance['show_author'] ) ? (bool) $new_instance['show_author'] : false;
195 $instance['show_date'] = isset( $new_instance['show_date'] ) ? (bool) $new_instance['show_date'] : false;
196 $instance['daily'] = $new_instance['daily'];
197 $instance['daily_range'] = $new_instance['daily_range'];
198 $instance['hour_range'] = $new_instance['hour_range'];
199 $instance['thumb_height'] = $new_instance['thumb_height'];
200 $instance['thumb_width'] = $new_instance['thumb_width'];
201
202 $instance['post_thumb_op'] = 'text_only';
203 if ( in_array( $new_instance['post_thumb_op'], array( 'inline', 'after', 'thumbs_only', 'text_only' ), true ) ) {
204 $instance['post_thumb_op'] = $new_instance['post_thumb_op'];
205 }
206
207 $instance['include_post_ids'] = implode( ',', array_filter( array_map( 'absint', explode( ',', $new_instance['include_post_ids'] ) ) ) );
208
209 // Process post types to be selected.
210 $wp_post_types = get_post_types(
211 array(
212 'public' => true,
213 )
214 );
215 $post_types = isset( $new_instance['post_types'] ) ? $new_instance['post_types'] : array();
216 $post_types = array_intersect( $wp_post_types, (array) $post_types );
217 $instance['post_types'] = implode( ',', $post_types );
218
219 // Save include_categories.
220 $include_categories = wp_parse_id_list( $new_instance['include_cat_ids'] );
221
222 foreach ( $include_categories as $cat_name ) {
223 $cat = get_term_by( 'term_taxonomy_id', $cat_name );
224
225 if ( isset( $cat->term_taxonomy_id ) ) {
226 $include_cat_ids[] = $cat->term_taxonomy_id;
227 $include_cat_names[] = $cat->name;
228 }
229 }
230 $instance['include_cat_ids'] = isset( $include_cat_ids ) ? join( ',', $include_cat_ids ) : '';
231 $instance['include_categories'] = isset( $include_cat_names ) ? \WebberZone\Top_Ten\Util\Helpers::str_putcsv( $include_cat_names ) : '';
232
233 /**
234 * Filters Update widget options array.
235 *
236 * @since 2.0.0
237 *
238 * @param array $instance Widget options array
239 */
240 return apply_filters( 'tptn_widget_options_update', $instance );
241 }
242
243 /**
244 * Front-end display of widget.
245 *
246 * @see WP_Widget::widget()
247 *
248 * @param array $args Widget arguments.
249 * @param array $instance Saved values from database.
250 */
251 public function widget( $args, $instance ) {
252 global $post;
253
254 if ( ! isset( $args['widget_id'] ) ) {
255 $args['widget_id'] = (string) $this->id;
256 }
257
258 if ( Display::exclude_on( $post, $args ) ) {
259 return;
260 }
261 $this->front_end_styles( $args, $instance );
262
263 $default_title = wp_strip_all_tags( tptn_get_option( 'title' ) );
264 $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
265
266 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
267 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
268
269 $limit = ! empty( $instance['limit'] ) ? $instance['limit'] : tptn_get_option( 'limit' );
270
271 $offset = isset( $instance['offset'] ) ? $instance['offset'] : 0;
272 $daily_range = ( empty( $instance['daily_range'] ) ) ? tptn_get_option( 'daily_range' ) : $instance['daily_range'];
273 $hour_range = ( empty( $instance['hour_range'] ) ) ? tptn_get_option( 'hour_range' ) : $instance['hour_range'];
274 $daily = ( isset( $instance['daily'] ) && ( 'daily' === $instance['daily'] ) ) ? true : false;
275 $post_thumb_op = isset( $instance['post_thumb_op'] ) ? $instance['post_thumb_op'] : 'text_only';
276 $thumb_height = ! empty( $instance['thumb_height'] ) ? absint( $instance['thumb_height'] ) : tptn_get_option( 'thumb_height' );
277 $thumb_width = ! empty( $instance['thumb_width'] ) ? absint( $instance['thumb_width'] ) : tptn_get_option( 'thumb_width' );
278 $disp_list_count = isset( $instance['disp_list_count'] ) ? $instance['disp_list_count'] : '';
279 $show_excerpt = isset( $instance['show_excerpt'] ) ? $instance['show_excerpt'] : '';
280 $show_author = isset( $instance['show_author'] ) ? $instance['show_author'] : '';
281 $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : '';
282 $post_types = ! empty( $instance['post_types'] ) ? $instance['post_types'] : tptn_get_option( 'post_types' );
283 $include_cat_ids = isset( $instance['include_cat_ids'] ) ? $instance['include_cat_ids'] : '';
284 $include_post_ids = isset( $instance['include_post_ids'] ) ? $instance['include_post_ids'] : '';
285
286 $output = $args['before_widget'];
287 $output .= $args['before_title'] . $title . $args['after_title'];
288
289 $arguments = array(
290 'is_widget' => 1,
291 'instance_id' => $args['widget_id'],
292 'heading' => 0,
293 'limit' => $limit,
294 'offset' => $offset,
295 'daily' => $daily,
296 'daily_range' => $daily_range,
297 'hour_range' => $hour_range,
298 'show_excerpt' => $show_excerpt,
299 'show_author' => $show_author,
300 'show_date' => $show_date,
301 'post_thumb_op' => $post_thumb_op,
302 'thumb_height' => $thumb_height,
303 'thumb_width' => $thumb_width,
304 'thumb_size' => array( $thumb_width, $thumb_height ),
305 'disp_list_count' => $disp_list_count,
306 'post_types' => $post_types,
307 'include_cat_ids' => $include_cat_ids,
308 'include_post_ids' => $include_post_ids,
309 );
310
311 /**
312 * Filters arguments passed to tptn_pop_posts for the widget.
313 *
314 * @since 2.0.0
315 *
316 * @param array $arguments Widget options array
317 */
318 $arguments = apply_filters( 'tptn_widget_options', $arguments );
319
320 $output .= Display::pop_posts( $arguments );
321
322 $output .= $args['after_widget'];
323
324 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
325 }
326
327
328 /**
329 * Add styles to the front end if the widget is active.
330 *
331 * @since 2.3.0
332 *
333 * @param array $args Widget arguments.
334 * @param array $instance Saved values from database.
335 */
336 public function front_end_styles( $args, $instance ) {
337
338 $style_array = \WebberZone\Top_Ten\Frontend\Styles_Handler::get_style();
339
340 if ( ! empty( $style_array['name'] ) ) {
341 $style = $style_array['name'];
342 $extra_css = $style_array['extra_css'];
343
344 wp_register_style(
345 "tptn-style-{$style}-{$args['widget_id']}",
346 false,
347 array( "tptn-style-{$style}" ),
348 TOP_TEN_VERSION
349 );
350
351 $thumb_height = ( ! empty( $instance['thumb_height'] ) ) ? absint( $instance['thumb_height'] ) : \tptn_get_option( 'thumb_height' );
352 $thumb_width = ( ! empty( $instance['thumb_width'] ) ) ? absint( $instance['thumb_width'] ) : \tptn_get_option( 'thumb_width' );
353 $width_value = $thumb_width ? $thumb_width : 1;
354 $height_value = $thumb_height ? $thumb_height : 1;
355 $aspect_ratio = sprintf( '%d / %d', $width_value, $height_value );
356
357 // Scope thumbnail sizing to this widget instance so layout CSS can react to it.
358 $extra_css .= "
359 .tptn_posts_widget-{$args['widget_id']} {
360 --tptn-thumb-width: {$thumb_width}px;
361 --tptn-thumb-height: {$thumb_height}px;
362 --tptn-thumb-aspect-ratio: {$aspect_ratio};
363 }
364 .tptn_posts_widget-{$args['widget_id']} li > a.tptn_link {
365 width: min( var(--tptn-thumb-width), 100% );
366 max-width: 100%;
367 }
368 .tptn_posts_widget-{$args['widget_id']} img.tptn_thumb {
369 width: min( var(--tptn-thumb-width), 100% ) !important;
370 height: var(--tptn-thumb-height) !important;
371 max-width: 100%;
372 max-height: var(--tptn-thumb-height);
373 aspect-ratio: var(--tptn-thumb-aspect-ratio);
374 object-fit: cover;
375 }
376 ";
377
378 wp_enqueue_style( "tptn-style-{$style}-{$args['widget_id']}" );
379 wp_add_inline_style( "tptn-style-{$style}-{$args['widget_id']}", $extra_css );
380 }
381 }
382 }
383