PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.3.2
WP Popular Posts v5.3.2
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Widget / Widget.php
wordpress-popular-posts / src / Widget Last commit date
Widget.php 5 years ago form.php 5 years ago
Widget.php
451 lines
1 <?php
2
3 namespace WordPressPopularPosts\Widget;
4
5 use WordPressPopularPosts\Helper;
6 use WordPressPopularPosts\Query;
7
8 class Widget extends \WP_Widget {
9
10 /**
11 * Default options.
12 *
13 * @since 5.0.0
14 * @var array
15 */
16 private $defaults = [];
17
18 /**
19 * Administrative settings.
20 *
21 * @since 2.3.3
22 * @var array
23 */
24 private $admin_options = [];
25
26 /**
27 * Image object.
28 *
29 * @since 5.0.0
30 * @var WordPressPopularPosts\Image
31 */
32 private $thumbnail;
33
34 /**
35 * Output object.
36 *
37 * @var \WordPressPopularPosts\Output
38 * @access private
39 */
40 private $output;
41
42 /**
43 * Translate object.
44 *
45 * @var \WordPressPopularPosts\Translate $translate
46 * @access private
47 */
48 private $translate;
49
50 /**
51 * Themer object.
52 *
53 * @var \WordPressPopularPosts\Themer $themer
54 * @access private
55 */
56 private $themer;
57
58 /**
59 * Construct.
60 *
61 * @since 1.0.0
62 * @param array $options
63 * @param array $config
64 * @param \WordPressPopularPosts\Output $output
65 * @param \WordPressPopularPosts\Image $image
66 * @param \WordPressPopularPosts\Translate $translate
67 * @param \WordPressPopularPosts\Themer $themer
68 */
69 public function __construct(array $options, array $config, \WordPressPopularPosts\Output $output, \WordPressPopularPosts\Image $thumbnail, \WordPressPopularPosts\Translate $translate, \WordPressPopularPosts\Themer $themer)
70 {
71 // Create the widget
72 parent::__construct(
73 'wpp',
74 'WordPress Popular Posts',
75 [
76 'classname' => 'popular-posts',
77 'description' => __('The most Popular Posts on your blog.', 'wordpress-popular-posts')
78 ]
79 );
80
81 $this->defaults = $options;
82 $this->admin_options = $config;
83 $this->output = $output;
84 $this->thumbnail = $thumbnail;
85 $this->translate = $translate;
86 $this->themer = $themer;
87 }
88
89 /**
90 * Widget hooks.
91 *
92 * @since 5.0.0
93 */
94 public function hooks()
95 {
96 // Register the widget
97 add_action('widgets_init', [$this, 'register']);
98 }
99
100 /**
101 * Registers the widget.
102 *
103 * @since 5.0.0
104 */
105 public function register()
106 {
107 register_widget($this);
108 }
109
110 /**
111 * Outputs the content of the widget.
112 *
113 * @since 1.0.0
114 * @param array $args The array of form elements.
115 * @param array $instance The current instance of the widget.
116 */
117 public function widget($args, $instance)
118 {
119 /**
120 * @var string $name
121 * @var string $id
122 * @var string $description
123 * @var string $class
124 * @var string $before_widget
125 * @var string $after_widget
126 * @var string $before_title
127 * @var string $after_title
128 * @var string $widget_id
129 * @var string $widget_name
130 */
131 extract($args, EXTR_SKIP);
132
133 $instance = Helper::merge_array_r(
134 $this->defaults,
135 (array) $instance
136 );
137
138 $markup = ( $instance['markup']['custom_html'] || has_filter('wpp_custom_html') || has_filter('wpp_post') )
139 ? 'custom'
140 : 'regular';
141
142 echo "\n" . $before_widget . "\n";
143
144 // Has user set a title?
145 if ( '' != $instance['title'] ) {
146 $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
147
148 if (
149 $instance['markup']['custom_html']
150 && $instance['markup']['title-start'] != ""
151 && $instance['markup']['title-end'] != ""
152 ) {
153 echo htmlspecialchars_decode($instance['markup']['title-start'], ENT_QUOTES) . $title . htmlspecialchars_decode($instance['markup']['title-end'], ENT_QUOTES);
154 } else {
155 echo $before_title . $title . $after_title;
156 }
157 }
158
159 // Expose Widget ID for customization
160 $instance['widget_id'] = $widget_id;
161
162 // Get posts
163 if ( $this->admin_options['tools']['ajax'] && ! is_customize_preview() ) {
164 ?>
165 <div class="wpp-widget-placeholder" data-widget-id="<?php echo esc_attr($widget_id); ?>"></div>
166 <?php
167 } else {
168 $this->get_popular($instance);
169 }
170
171 echo "\n" . $after_widget . "\n";
172 }
173
174 /**
175 * Generates the administration form for the widget.
176 *
177 * @since 1.0.0
178 * @param array $instance The array of keys and values for the widget.
179 */
180 public function form($instance)
181 {
182 $instance = Helper::merge_array_r(
183 $this->defaults,
184 (array) $instance
185 );
186 require plugin_dir_path(__FILE__) . '/form.php';
187 }
188
189 /**
190 * Processes the widget's options to be saved.
191 *
192 * @since 1.0.0
193 * @param array $new_instance The previous instance of values before the update.
194 * @param array $old_instance The new instance of values to be generated via the update.
195 * @return array $instance Updated instance.
196 */
197 public function update($new_instance, $old_instance)
198 {
199 if ( empty($old_instance) ) {
200 $old_instance = $this->defaults;
201 } else {
202 $old_instance = Helper::merge_array_r(
203 $this->defaults,
204 (array) $old_instance
205 );
206 }
207
208 $instance = $old_instance;
209
210 $instance['title'] = htmlspecialchars(stripslashes_deep(strip_tags($new_instance['title'])), ENT_QUOTES);
211 $instance['limit'] = ( Helper::is_number($new_instance['limit']) && $new_instance['limit'] > 0 )
212 ? $new_instance['limit']
213 : 10;
214 $instance['range'] = $new_instance['range'];
215 $instance['time_quantity'] = ( Helper::is_number($new_instance['time_quantity']) && $new_instance['time_quantity'] > 0 )
216 ? $new_instance['time_quantity']
217 : 24;
218 $instance['time_unit'] = $new_instance['time_unit'];
219 $instance['order_by'] = $new_instance['order_by'];
220
221 // FILTERS
222 // user did not set a post type name, so we fall back to default
223 $instance['post_type'] = ( '' == $new_instance['post_type'] )
224 ? 'post,page'
225 : $new_instance['post_type'];
226
227 $instance['freshness'] = isset($new_instance['freshness']);
228
229 // Post / Page / CTP filter
230 $ids = array_filter(explode(",", rtrim(preg_replace('|[^0-9,]|', '', $new_instance['pid']), ",")), 'is_numeric');
231 // Got no valid IDs, clear
232 if ( empty($ids) ) {
233 $instance['pid'] = '';
234 }
235 else {
236 $instance['pid'] = implode(",", $ids);
237 }
238
239 // Taxonomy filter
240 $taxonomies = $new_instance['taxonomy'];
241
242 if ( isset($taxonomies['names']) ) {
243 // Remove taxonomies that don't have any valid term IDs
244 foreach( $taxonomies['terms'] as $taxonomy => $terms ) {
245 $taxonomies['terms'][$taxonomy] = array_filter(
246 explode(",", trim(preg_replace('|[^0-9,-]|', '', $taxonomies['terms'][$taxonomy]), ", ")),
247 'is_numeric'
248 );
249
250 if (
251 empty($taxonomies['terms'][$taxonomy])
252 || ! in_array($taxonomy, $taxonomies['names'])
253 ) {
254 unset($taxonomies['terms'][$taxonomy]);
255 } else {
256 $taxonomies['terms'][$taxonomy] = implode(',', $taxonomies['terms'][$taxonomy]);
257 }
258 }
259
260 if ( ! empty($taxonomies['terms']) ) {
261 $instance['taxonomy'] = implode(';', array_keys($taxonomies['terms']));
262 $instance['term_id'] = implode(';', array_values($taxonomies['terms']));
263 }
264 } // Discard everything
265 else {
266 $instance['taxonomy'] = '';
267 $instance['term_id'] = '';
268 }
269
270 // Author filter
271 $ids = array_filter(explode(",", rtrim(preg_replace('|[^0-9,]|', '', $new_instance['uid']), ",")), 'is_numeric');
272 // Got no valid IDs, clear
273 if ( empty($ids) ) {
274 $instance['author'] = '';
275 }
276 else {
277 $instance['author'] = implode( ",", $ids );
278 }
279
280 $instance['shorten_title']['words'] = $new_instance['shorten_title-words'];
281 $instance['shorten_title']['active'] = isset($new_instance['shorten_title-active']);
282 $instance['shorten_title']['length'] = ( Helper::is_number($new_instance['shorten_title-length']) && $new_instance['shorten_title-length'] > 0 )
283 ? $new_instance['shorten_title-length']
284 : 25;
285
286 $instance['post-excerpt']['keep_format'] = isset($new_instance['post-excerpt-format']);
287 $instance['post-excerpt']['words'] = $new_instance['post-excerpt-words'];
288 $instance['post-excerpt']['active'] = isset($new_instance['post-excerpt-active']);
289 $instance['post-excerpt']['length'] = ( Helper::is_number($new_instance['post-excerpt-length']) && $new_instance['post-excerpt-length'] > 0 )
290 ? $new_instance['post-excerpt-length']
291 : 55;
292
293 $instance['thumbnail']['active'] = isset($new_instance['thumbnail-active']);
294 $instance['thumbnail']['build'] = $new_instance['thumbnail-size-source'];
295 $instance['thumbnail']['width'] = 75;
296 $instance['thumbnail']['height'] = 75;
297
298 // Use predefined thumbnail sizes
299 if ( 'predefined' == $new_instance['thumbnail-size-source'] ) {
300 $default_thumbnail_sizes = $this->thumbnail->get_sizes();
301 $size = $default_thumbnail_sizes[$new_instance['thumbnail-size']];
302
303 $instance['thumbnail']['width'] = $size['width'];
304 $instance['thumbnail']['height'] = $size['height'];
305 $instance['thumbnail']['crop'] = $size['crop'];
306 } // Set thumbnail size manually
307 else {
308 if ( Helper::is_number($new_instance['thumbnail-width']) && Helper::is_number($new_instance['thumbnail-height']) ) {
309 $instance['thumbnail']['width'] = $new_instance['thumbnail-width'];
310 $instance['thumbnail']['height'] = $new_instance['thumbnail-height'];
311 $instance['thumbnail']['crop'] = true;
312 }
313 }
314
315 $instance['rating'] = isset($new_instance['rating']);
316 $instance['stats_tag']['comment_count'] = isset($new_instance['comment_count']);
317 $instance['stats_tag']['views'] = isset($new_instance['views']);
318 $instance['stats_tag']['author'] = isset($new_instance['author']);
319 $instance['stats_tag']['date']['active'] = isset($new_instance['date']);
320 $instance['stats_tag']['date']['format'] = empty($new_instance['date_format'])
321 ? 'F j, Y'
322 : $new_instance['date_format'];
323
324 $instance['stats_tag']['taxonomy']['active'] = isset($new_instance['stats_taxonomy']);
325 $instance['stats_tag']['taxonomy']['name'] = isset($new_instance['stats_taxonomy_name']) ? $new_instance['stats_taxonomy_name'] : 'category';
326 $instance['stats_tag']['category'] = isset($new_instance['stats_taxonomy'] ); // Deprecated in 4.0.0!
327
328 $instance['markup']['custom_html'] = isset($new_instance['custom_html']);
329 $instance['markup']['wpp-start'] = empty($new_instance['wpp-start'])
330 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? htmlspecialchars('<ul class="wpp-list">', ENT_QUOTES) : ''
331 : htmlspecialchars($new_instance['wpp-start'], ENT_QUOTES);
332
333 $instance['markup']['wpp-end'] = empty($new_instance['wpp-end'])
334 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? htmlspecialchars('</ul>', ENT_QUOTES) : ''
335 : htmlspecialchars($new_instance['wpp-end'], ENT_QUOTES);
336
337 $instance['markup']['post-html'] = empty($new_instance['post-html'])
338 ? htmlspecialchars('<li>{thumb} {title} {stats}</li>', ENT_QUOTES)
339 : htmlspecialchars($new_instance['post-html'], ENT_QUOTES);
340
341 $instance['markup']['title-start'] = empty($new_instance['title-start'])
342 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? '<h2>' : ''
343 : htmlspecialchars($new_instance['title-start'], ENT_QUOTES);
344
345 $instance['markup']['title-end'] = empty($new_instance['title-end'])
346 ? ! $old_instance['markup']['custom_html'] && $instance['markup']['custom_html'] ? '</h2>' : '' :
347 htmlspecialchars($new_instance['title-end'], ENT_QUOTES);
348
349 $instance['theme'] = [
350 'name' => isset($new_instance['theme']) ? $new_instance['theme'] : '',
351 'applied' => isset($new_instance['theme']) ? (bool) $new_instance['theme-applied'] : false
352 ];
353
354 if ( ! isset($new_instance['theme']) || $old_instance['theme']['name'] != $new_instance['theme'] ) {
355 $instance['theme']['applied'] = false;
356 }
357
358 $theme = $instance['theme']['name'] ? $this->themer->get_theme($instance['theme']['name']) : null;
359
360 if (
361 is_array($theme)
362 && isset($theme['json'])
363 && isset($theme['json']['config'])
364 && is_array($theme['json']['config'])
365 && ! $instance['theme']['applied']
366 ) {
367 $instance = Helper::merge_array_r(
368 $instance,
369 $theme['json']['config']
370 );
371 $instance['markup']['custom_html'] = true;
372 $instance['theme']['applied'] = true;
373
374 $current_sidebar_data = $this->get_sidebar_data();
375
376 if ( $current_sidebar_data ) {
377 $instance['markup']['title-start'] = htmlspecialchars($current_sidebar_data['before_title'], ENT_QUOTES);
378 $instance['markup']['title-end'] = htmlspecialchars($current_sidebar_data['after_title'], ENT_QUOTES);
379 }
380 }
381
382 return $instance;
383 }
384
385 /**
386 * Returns HTML list.
387 *
388 * @since 2.3.3
389 */
390 public function get_popular($instance = null)
391 {
392 if ( is_array($instance) && ! empty($instance) ) {
393 // Return cached results
394 if ( $this->admin_options['tools']['cache']['active'] ) {
395
396 $key = md5(json_encode($instance));
397 $popular_posts = \WordPressPopularPosts\Cache::get($key);
398
399 if ( false === $popular_posts ) {
400 $popular_posts = new Query($instance);
401
402 $time_value = $this->admin_options['tools']['cache']['interval']['value']; // eg. 5
403 $time_unit = $this->admin_options['tools']['cache']['interval']['time']; // eg. 'minute'
404
405 // No popular posts found, check again in 1 minute
406 if ( ! $popular_posts->get_posts() ) {
407 $time_value = 1;
408 $time_unit = 'minute';
409 }
410
411 \WordPressPopularPosts\Cache::set(
412 $key,
413 $popular_posts,
414 $time_value,
415 $time_unit
416 );
417 }
418
419 } // Get popular posts
420 else {
421 $popular_posts = new Query($instance);
422 }
423
424 $this->output->set_data($popular_posts->get_posts());
425 $this->output->set_public_options($instance);
426 $this->output->build_output();
427 $this->output->output();
428 }
429 }
430
431 /**
432 * Returns data on the current sidebar.
433 *
434 * @since 5.0.0
435 * @access private
436 * @return array|null
437 */
438 private function get_sidebar_data()
439 {
440 global $wp_registered_sidebars;
441 $sidebars = wp_get_sidebars_widgets();
442
443 foreach ( (array) $sidebars as $sidebar_id => $sidebar ) {
444 if ( in_array($this->id, (array) $sidebar, true ) )
445 return $wp_registered_sidebars[$sidebar_id];
446 }
447
448 return null;
449 }
450 }
451