PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.4.1
WP Popular Posts v5.4.1
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 / Block / Widget / Widget.php
wordpress-popular-posts / src / Block / Widget Last commit date
Widget.php 4 years ago edit.js 4 years ago editor.css 4 years ago widget.js 4 years ago
Widget.php
522 lines
1 <?php
2 namespace WordPressPopularPosts\Block\Widget;
3
4 use WordPressPopularPosts\Helper;
5 use WordPressPopularPosts\Query;
6 use WordPressPopularPosts\Output;
7 use WordPressPopularPosts\Block\Block;
8
9 class Widget extends Block
10 {
11
12 /**
13 * Administrative settings.
14 *
15 * @since 5.4.0
16 * @var array
17 * @access private
18 */
19 private $admin_options = [];
20
21 /**
22 * Image object.
23 *
24 * @since 5.4.0
25 * @var WordPressPopularPosts\Image
26 * @access private
27 */
28 private $thumbnail;
29
30 /**
31 * Output object.
32 *
33 * @since 5.4.0
34 * @var \WordPressPopularPosts\Output
35 * @access private
36 */
37 private $output;
38
39 /**
40 * Translate object.
41 *
42 * @since 5.4.0
43 * @var \WordPressPopularPosts\Translate $translate
44 * @access private
45 */
46 private $translate;
47
48 /**
49 * Themer object.
50 *
51 * @since 5.4.0
52 * @var \WordPressPopularPosts\Themer $themer
53 * @access private
54 */
55 private $themer;
56
57 /**
58 * Default attributes.
59 *
60 * @since 5.4.0
61 * @var array $defaults
62 * @access private
63 */
64 private $defaults;
65
66 /**
67 * Construct.
68 *
69 * @since 5.4.0
70 * @param array $config
71 * @param \WordPressPopularPosts\Output $output
72 * @param \WordPressPopularPosts\Image $image
73 * @param \WordPressPopularPosts\Translate $translate
74 * @param \WordPressPopularPosts\Themer $themer
75 */
76 public function __construct(array $config, \WordPressPopularPosts\Output $output, \WordPressPopularPosts\Image $thumbnail, \WordPressPopularPosts\Translate $translate, \WordPressPopularPosts\Themer $themer)
77 {
78 $this->admin_options = $config;
79 $this->output = $output;
80 $this->thumbnail = $thumbnail;
81 $this->translate = $translate;
82 $this->themer = $themer;
83
84 $this->defaults = [
85 'title' => '',
86 'limit' => 10,
87 'offset' => 0,
88 'range' => 'daily',
89 'time_unit' => 'hour',
90 'time_quantity' => 24,
91 'freshness' => false,
92 'order_by' => 'views',
93 'post_type' => 'post',
94 'pid' => '',
95 'cat' => '',
96 'taxonomy' => 'category',
97 'tax' => '',
98 'term_id' => '',
99 'author' => '',
100 'title_length' => 0,
101 'title_by_words' => 0,
102 'excerpt_length' => 0,
103 'excerpt_format' => 0,
104 'excerpt_by_words' => 0,
105 'thumbnail_width' => 0,
106 'thumbnail_height' => 0,
107 'thumbnail_build' => 'manual',
108 'thumbnail_size' => '',
109 'rating' => false,
110 'stats_comments' => false,
111 'stats_views' => true,
112 'stats_author' => false,
113 'stats_date' => false,
114 'stats_date_format' => 'F j, Y',
115 'stats_category' => false,
116 'stats_taxonomy' => false,
117 'custom_html' => false,
118 'wpp_start' => '<ul class="wpp-list">',
119 'wpp_end' => '</ul>',
120 'header_start' => '<h2>',
121 'header_end' => '</h2>',
122 'post_html' => '',
123 'theme' => ''
124 ];
125 }
126
127 /**
128 * Registers the block.
129 *
130 * @since 5.4.0
131 */
132 public function register()
133 {
134 // Block editor is not available, bail.
135 if ( ! function_exists('register_block_type') ) {
136 return;
137 }
138
139 $block_editor_support = apply_filters('wpp_block_editor_support', true);
140
141 if ( ! $block_editor_support ) {
142 return;
143 }
144
145 wp_register_script(
146 'block-wpp-widget-js',
147 plugin_dir_url(dirname(dirname(dirname(__FILE__)))) . 'assets/js/blocks/block-wpp-widget.js',
148 ['wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-server-side-render'],
149 filemtime(plugin_dir_path(dirname(dirname(dirname(__FILE__)))) . 'assets/js/blocks/block-wpp-widget.js')
150 );
151
152 wp_register_style(
153 'block-wpp-editor-css',
154 plugins_url('editor.css', __FILE__),
155 [],
156 filemtime(plugin_dir_path(__FILE__) . 'editor.css')
157 );
158
159 register_block_type(
160 'wordpress-popular-posts/widget',
161 [
162 'editor_style' => 'block-wpp-editor-css',
163 'editor_script' => 'block-wpp-widget-js',
164 'render_callback' => [$this, 'render'],
165 'attributes' => [
166 '_editMode' => [
167 'type' => 'boolean',
168 'default' => true
169 ],
170 '_isSelected' => [
171 'type' => 'boolean',
172 'default' => false
173 ],
174 'title' => [
175 'type' => 'string',
176 'default' => ''
177 ],
178 'limit' => [
179 'type' =>'number',
180 'default' => 10
181 ],
182 'offset' => [
183 'type' => 'number',
184 'default' => 0
185 ],
186 'order_by' => [
187 'type' => 'string',
188 'default' => 'views'
189 ],
190 'range' => [
191 'type' => 'string',
192 'default' => 'last24hours'
193 ],
194 'time_quantity' => [
195 'type' => 'number',
196 'default' => 24
197 ],
198 'time_unit' => [
199 'type' => 'string',
200 'default' => 'hour'
201 ],
202 'freshness' => [
203 'type' => 'boolean',
204 'default' => false
205 ],
206 /* filters */
207 'post_type' => [
208 'type' => 'string',
209 'default' => 'post'
210 ],
211 'pid' => [
212 'type' => 'string',
213 'default' => ''
214 ],
215 'author' => [
216 'type' => 'string',
217 'default' => ''
218 ],
219 'tax' => [
220 'type' => 'string',
221 'default' => ''
222 ],
223 'term_id' => [
224 'type' => 'string',
225 'default' => ''
226 ],
227 /* post settings */
228 'shorten_title' => [
229 'type' => 'boolean',
230 'default' => false
231 ],
232 'title_length' => [
233 'type' =>'number',
234 'default' => 0
235 ],
236 'title_by_words' => [
237 'type' =>'number',
238 'default' => 0
239 ],
240 'display_post_excerpt' => [
241 'type' => 'boolean',
242 'default' => false
243 ],
244 'excerpt_format' => [
245 'type' => 'boolean',
246 'default' => false
247 ],
248 'excerpt_length' => [
249 'type' =>'number',
250 'default' => 0
251 ],
252 'excerpt_by_words' => [
253 'type' =>'number',
254 'default' => 0
255 ],
256 'display_post_thumbnail' => [
257 'type' => 'boolean',
258 'default' => false
259 ],
260 'thumbnail_width' => [
261 'type' =>'number',
262 'default' => 0
263 ],
264 'thumbnail_height' => [
265 'type' =>'number',
266 'default' => 0
267 ],
268 'thumbnail_build' => [
269 'type' => 'string',
270 'default' => 'manual'
271 ],
272 'thumbnail_size' => [
273 'type' => 'string',
274 'default' => ''
275 ],
276 /* stats tag settings */
277 'stats_comments' => [
278 'type' => 'boolean',
279 'default' => false
280 ],
281 'stats_views' => [
282 'type' => 'boolean',
283 'default' => true
284 ],
285 'stats_author' => [
286 'type' => 'boolean',
287 'default' => false
288 ],
289 'stats_date' => [
290 'type' => 'boolean',
291 'default' => false
292 ],
293 'stats_date_format' => [
294 'type' => 'string',
295 'default' => 'F j, Y'
296 ],
297 'stats_taxonomy' => [
298 'type' => 'boolean',
299 'default' => false
300 ],
301 'taxonomy' => [
302 'type' => 'string',
303 'default' => 'category'
304 ],
305 /* HTML markup settings */
306 'custom_html' => [
307 'type' => 'boolean',
308 'default' => false
309 ],
310 'header_start' => [
311 'type' => 'string',
312 'default' => '<h2>'
313 ],
314 'header_end' => [
315 'type' => 'string',
316 'default' => '</h2>'
317 ],
318 'wpp_start' => [
319 'type' => 'string',
320 'default' => '<ul class="wpp-list">'
321 ],
322 'wpp_end' => [
323 'type' => 'string',
324 'default' => '</ul>'
325 ],
326 'post_html' => [
327 'type' => 'string',
328 'default' => '<li>{thumb} {title} <span class="wpp-meta post-stats">{stats}</span></li>'
329 ],
330 'theme' => [
331 'type' => 'string',
332 'default' => ''
333 ],
334 ]
335 ]
336 );
337 }
338
339 /**
340 * Renders the block.
341 *
342 * @since 5.4.0
343 * @param array
344 * @return string
345 */
346 public function render(array $attributes)
347 {
348 extract($this->parse_attributes($attributes));
349
350 $html = '<div class="widget popular-posts' . (( isset($attributes['className']) && $attributes['className'] ) ? ' '. esc_attr($attributes['className']) : '') . '">';
351
352 // possible values for "Time Range" and "Order by"
353 $time_units = ["minute", "hour", "day", "week", "month"];
354 $range_values = ["daily", "last24hours", "weekly", "last7days", "monthly", "last30days", "all", "custom"];
355 $order_by_values = ["comments", "views", "avg"];
356
357 $theme_data = $this->themer->get_theme($theme);
358
359 if ( ! isset($theme_data['json']) ) {
360 $theme = '';
361 }
362
363 $query_args = [
364 'title' => strip_tags($title),
365 'limit' => ( ! empty($limit) && Helper::is_number($limit) && $limit > 0 ) ? $limit : 10,
366 'offset' => ( ! empty($offset) && Helper::is_number($offset) && $offset >= 0 ) ? $offset : 0,
367 'range' => ( in_array($range, $range_values) ) ? $range : 'daily',
368 'time_quantity' => ( ! empty($time_quantity) && Helper::is_number($time_quantity) && $time_quantity > 0 ) ? $time_quantity : 24,
369 'time_unit' => ( in_array($time_unit, $time_units) ) ? $time_unit : 'hour',
370 'freshness' => empty($freshness) ? false : $freshness,
371 'order_by' => ( in_array($order_by, $order_by_values) ) ? $order_by : 'views',
372 'post_type' => empty($post_type) ? 'post' : $post_type,
373 'pid' => rtrim(preg_replace('|[^0-9,]|', '', $pid), ","),
374 'cat' => rtrim(preg_replace('|[^0-9,-]|', '', $cat), ","),
375 'taxonomy' => empty($tax) ? 'category' : $tax,
376 'term_id' => rtrim(preg_replace('|[^0-9,;-]|', '', $term_id), ","),
377 'author' => rtrim(preg_replace('|[^0-9,]|', '', $author), ","),
378 'shorten_title' => [
379 'active' => ( ! empty($title_length) && Helper::is_number($title_length) && $title_length > 0 ),
380 'length' => ( ! empty($title_length) && Helper::is_number($title_length) ) ? $title_length : 0,
381 'words' => (( ! empty($title_by_words) && Helper::is_number($title_by_words) && $title_by_words > 0 )),
382 ],
383 'post-excerpt' => [
384 'active' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) && $excerpt_length > 0 ),
385 'length' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) ) ? $excerpt_length : 0,
386 'keep_format' => ( ! empty($excerpt_format) && Helper::is_number($excerpt_format) && $excerpt_format > 0 ),
387 'words' => ( ! empty($excerpt_by_words) && Helper::is_number($excerpt_by_words) && $excerpt_by_words > 0 ),
388 ],
389 'thumbnail' => [
390 'active' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ),
391 'width' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ) ? $thumbnail_width : 0,
392 'height' => ( ! empty($thumbnail_height) && Helper::is_number($thumbnail_height) && $thumbnail_height > 0 ) ? $thumbnail_height : 0,
393 'build' => 'predefined' == $thumbnail_build ? 'predefined' : 'manual',
394 'size' => empty($thumbnail_size) ? '' : $thumbnail_size,
395 ],
396 'rating' => empty($rating) ? false : $rating,
397 'stats_tag' => [
398 'comment_count' => empty($stats_comments) ? false : $stats_comments,
399 'views' => empty($stats_views) ? false : $stats_views,
400 'author' => empty($stats_author) ? false : $stats_author,
401 'date' => [
402 'active' => empty($stats_date) ? false : $stats_date,
403 'format' => empty($stats_date_format) ? 'F j, Y' : $stats_date_format
404 ],
405 'category' => empty($stats_category) ? false : $stats_category,
406 'taxonomy' => [
407 'active' => empty($stats_taxonomy) ? false : $stats_taxonomy,
408 'name' => empty($taxonomy) ? 'category' : $taxonomy,
409 ]
410 ],
411 'markup' => [
412 'custom_html' => empty($custom_html) ? false : $custom_html,
413 'wpp-start' => empty($wpp_start) ? '' : $wpp_start,
414 'wpp-end' => empty($wpp_end) ? '' : $wpp_end,
415 'title-start' => empty($header_start) ? '' : $header_start,
416 'title-end' => empty($header_end) ? '' : $header_end,
417 'post-html' => empty($post_html) ? '<li>{thumb} {title} <span class="wpp-meta post-stats">{stats}</span></li>' : $post_html
418 ],
419 'theme' => [
420 'name' => empty($theme) ? '' : $theme
421 ]
422 ];
423
424 // Post / Page / CTP filter
425 $ids = array_filter(explode(",", $query_args['pid']), 'is_numeric');
426 // Got no valid IDs, clear
427 if ( empty($ids) ) {
428 $query_args['pid'] = '';
429 }
430
431 // Category filter
432 $ids = array_filter(explode(",", $query_args['cat']), 'is_numeric');
433 // Got no valid IDs, clear
434 if ( empty($ids) ) {
435 $query_args['cat'] = '';
436 }
437
438 // Author filter
439 $ids = array_filter(explode(",", $query_args['author']), 'is_numeric');
440 // Got no valid IDs, clear
441 if ( empty($ids) ) {
442 $query_args['author'] = '';
443 }
444
445 // Has user set a title?
446 if (
447 ! empty($query_args['title'])
448 && ! empty($query_args['markup']['title-start'])
449 && ! empty($query_args['markup']['title-end'])
450 ) {
451 $html .= htmlspecialchars_decode($query_args['markup']['title-start'], ENT_QUOTES) . $query_args['title'] . htmlspecialchars_decode($query_args['markup']['title-end'], ENT_QUOTES);
452 }
453
454 $isAdmin = isset($_GET['isSelected']) ? $_GET['isSelected'] : false;
455
456 if ( $this->admin_options['tools']['ajax'] && ! is_customize_preview() && ! $isAdmin ) {
457 $html .= '<script type="application/json">' . json_encode($query_args) . '</script>';
458 $html .= '<div class="wpp-widget-block-placeholder"></div>';
459
460 return $html;
461 }
462
463 // Return cached results
464 if ( $this->admin_options['tools']['cache']['active'] ) {
465
466 $key = md5(json_encode($query_args));
467 $popular_posts = \WordPressPopularPosts\Cache::get($key);
468
469 if ( false === $popular_posts ) {
470 $popular_posts = new Query($query_args);
471
472 $time_value = $this->admin_options['tools']['cache']['interval']['value']; // eg. 5
473 $time_unit = $this->admin_options['tools']['cache']['interval']['time']; // eg. 'minute'
474
475 // No popular posts found, check again in 1 minute
476 if ( ! $popular_posts->get_posts() ) {
477 $time_value = 1;
478 $time_unit = 'minute';
479 }
480
481 \WordPressPopularPosts\Cache::set(
482 $key,
483 $popular_posts,
484 $time_value,
485 $time_unit
486 );
487 }
488
489 } // Get popular posts
490 else {
491 $popular_posts = new Query($query_args);
492 }
493
494 $this->output->set_data($popular_posts->get_posts());
495 $this->output->set_public_options($query_args);
496 $this->output->build_output();
497
498 $html .= $this->output->get_output();
499
500 $html .= '</div>';
501
502 return $html;
503 }
504
505 /**
506 * Parses attributes.
507 *
508 * @since 5.4.0
509 * @param array
510 * @return array
511 */
512 private function parse_attributes($atts = [])
513 {
514 $out = array();
515
516 foreach ( $this->defaults as $name => $default ) {
517 $out[$name] = array_key_exists($name, $atts) ? trim($atts[$name]) : $default;
518 }
519
520 return $out;
521 }
522 }