PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.4.0
WP Popular Posts v5.4.0
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
516 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 wp_enqueue_script(
140 'block-wpp-widget-js',
141 plugin_dir_url(dirname(dirname(dirname(__FILE__)))) . 'assets/js/blocks/block-wpp-widget.js',
142 ['wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-server-side-render'],
143 filemtime(plugin_dir_path(dirname(dirname(dirname(__FILE__)))) . 'assets/js/blocks/block-wpp-widget.js')
144 );
145
146 wp_register_style(
147 'block-wpp-editor-css',
148 plugins_url('editor.css', __FILE__),
149 [],
150 filemtime(plugin_dir_path(__FILE__) . 'editor.css')
151 );
152
153 register_block_type(
154 'wordpress-popular-posts/widget',
155 [
156 'editor_style' => 'block-wpp-editor-css',
157 'editor_script' => 'block-wpp-widget-js',
158 'render_callback' => [$this, 'render'],
159 'attributes' => [
160 '_editMode' => [
161 'type' => 'boolean',
162 'default' => true
163 ],
164 '_isSelected' => [
165 'type' => 'boolean',
166 'default' => false
167 ],
168 'title' => [
169 'type' => 'string',
170 'default' => ''
171 ],
172 'limit' => [
173 'type' =>'number',
174 'default' => 10
175 ],
176 'offset' => [
177 'type' => 'number',
178 'default' => 0
179 ],
180 'order_by' => [
181 'type' => 'string',
182 'default' => 'views'
183 ],
184 'range' => [
185 'type' => 'string',
186 'default' => 'last24hours'
187 ],
188 'time_quantity' => [
189 'type' => 'number',
190 'default' => 24
191 ],
192 'time_unit' => [
193 'type' => 'string',
194 'default' => 'hour'
195 ],
196 'freshness' => [
197 'type' => 'boolean',
198 'default' => false
199 ],
200 /* filters */
201 'post_type' => [
202 'type' => 'string',
203 'default' => 'post'
204 ],
205 'pid' => [
206 'type' => 'string',
207 'default' => ''
208 ],
209 'author' => [
210 'type' => 'string',
211 'default' => ''
212 ],
213 'tax' => [
214 'type' => 'string',
215 'default' => ''
216 ],
217 'term_id' => [
218 'type' => 'string',
219 'default' => ''
220 ],
221 /* post settings */
222 'shorten_title' => [
223 'type' => 'boolean',
224 'default' => false
225 ],
226 'title_length' => [
227 'type' =>'number',
228 'default' => 0
229 ],
230 'title_by_words' => [
231 'type' =>'number',
232 'default' => 0
233 ],
234 'display_post_excerpt' => [
235 'type' => 'boolean',
236 'default' => false
237 ],
238 'excerpt_format' => [
239 'type' => 'boolean',
240 'default' => false
241 ],
242 'excerpt_length' => [
243 'type' =>'number',
244 'default' => 0
245 ],
246 'excerpt_by_words' => [
247 'type' =>'number',
248 'default' => 0
249 ],
250 'display_post_thumbnail' => [
251 'type' => 'boolean',
252 'default' => false
253 ],
254 'thumbnail_width' => [
255 'type' =>'number',
256 'default' => 0
257 ],
258 'thumbnail_height' => [
259 'type' =>'number',
260 'default' => 0
261 ],
262 'thumbnail_build' => [
263 'type' => 'string',
264 'default' => 'manual'
265 ],
266 'thumbnail_size' => [
267 'type' => 'string',
268 'default' => ''
269 ],
270 /* stats tag settings */
271 'stats_comments' => [
272 'type' => 'boolean',
273 'default' => false
274 ],
275 'stats_views' => [
276 'type' => 'boolean',
277 'default' => true
278 ],
279 'stats_author' => [
280 'type' => 'boolean',
281 'default' => false
282 ],
283 'stats_date' => [
284 'type' => 'boolean',
285 'default' => false
286 ],
287 'stats_date_format' => [
288 'type' => 'string',
289 'default' => 'F j, Y'
290 ],
291 'stats_taxonomy' => [
292 'type' => 'boolean',
293 'default' => false
294 ],
295 'taxonomy' => [
296 'type' => 'string',
297 'default' => 'category'
298 ],
299 /* HTML markup settings */
300 'custom_html' => [
301 'type' => 'boolean',
302 'default' => false
303 ],
304 'header_start' => [
305 'type' => 'string',
306 'default' => '<h2>'
307 ],
308 'header_end' => [
309 'type' => 'string',
310 'default' => '</h2>'
311 ],
312 'wpp_start' => [
313 'type' => 'string',
314 'default' => '<ul class="wpp-list">'
315 ],
316 'wpp_end' => [
317 'type' => 'string',
318 'default' => '</ul>'
319 ],
320 'post_html' => [
321 'type' => 'string',
322 'default' => '<li>{thumb} {title} <span class="wpp-meta post-stats">{stats}</span></li>'
323 ],
324 'theme' => [
325 'type' => 'string',
326 'default' => ''
327 ],
328 ]
329 ]
330 );
331 }
332
333 /**
334 * Renders the block.
335 *
336 * @since 5.4.0
337 * @param array
338 * @return string
339 */
340 public function render(array $attributes)
341 {
342 extract($this->parse_attributes($attributes));
343
344 $html = '<div class="widget popular-posts' . (( isset($attributes['className']) && $attributes['className'] ) ? ' '. esc_attr($attributes['className']) : '') . '">';
345
346 // possible values for "Time Range" and "Order by"
347 $time_units = ["minute", "hour", "day", "week", "month"];
348 $range_values = ["daily", "last24hours", "weekly", "last7days", "monthly", "last30days", "all", "custom"];
349 $order_by_values = ["comments", "views", "avg"];
350
351 $theme_data = $this->themer->get_theme($theme);
352
353 if ( ! isset($theme_data['json']) ) {
354 $theme = '';
355 }
356
357 $query_args = [
358 'title' => strip_tags($title),
359 'limit' => ( ! empty($limit) && Helper::is_number($limit) && $limit > 0 ) ? $limit : 10,
360 'offset' => ( ! empty($offset) && Helper::is_number($offset) && $offset >= 0 ) ? $offset : 0,
361 'range' => ( in_array($range, $range_values) ) ? $range : 'daily',
362 'time_quantity' => ( ! empty($time_quantity) && Helper::is_number($time_quantity) && $time_quantity > 0 ) ? $time_quantity : 24,
363 'time_unit' => ( in_array($time_unit, $time_units) ) ? $time_unit : 'hour',
364 'freshness' => empty($freshness) ? false : $freshness,
365 'order_by' => ( in_array($order_by, $order_by_values) ) ? $order_by : 'views',
366 'post_type' => empty($post_type) ? 'post' : $post_type,
367 'pid' => rtrim(preg_replace('|[^0-9,]|', '', $pid), ","),
368 'cat' => rtrim(preg_replace('|[^0-9,-]|', '', $cat), ","),
369 'taxonomy' => empty($tax) ? 'category' : $tax,
370 'term_id' => rtrim(preg_replace('|[^0-9,;-]|', '', $term_id), ","),
371 'author' => rtrim(preg_replace('|[^0-9,]|', '', $author), ","),
372 'shorten_title' => [
373 'active' => ( ! empty($title_length) && Helper::is_number($title_length) && $title_length > 0 ),
374 'length' => ( ! empty($title_length) && Helper::is_number($title_length) ) ? $title_length : 0,
375 'words' => (( ! empty($title_by_words) && Helper::is_number($title_by_words) && $title_by_words > 0 )),
376 ],
377 'post-excerpt' => [
378 'active' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) && $excerpt_length > 0 ),
379 'length' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) ) ? $excerpt_length : 0,
380 'keep_format' => ( ! empty($excerpt_format) && Helper::is_number($excerpt_format) && $excerpt_format > 0 ),
381 'words' => ( ! empty($excerpt_by_words) && Helper::is_number($excerpt_by_words) && $excerpt_by_words > 0 ),
382 ],
383 'thumbnail' => [
384 'active' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ),
385 'width' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ) ? $thumbnail_width : 0,
386 'height' => ( ! empty($thumbnail_height) && Helper::is_number($thumbnail_height) && $thumbnail_height > 0 ) ? $thumbnail_height : 0,
387 'build' => 'predefined' == $thumbnail_build ? 'predefined' : 'manual',
388 'size' => empty($thumbnail_size) ? '' : $thumbnail_size,
389 ],
390 'rating' => empty($rating) ? false : $rating,
391 'stats_tag' => [
392 'comment_count' => empty($stats_comments) ? false : $stats_comments,
393 'views' => empty($stats_views) ? false : $stats_views,
394 'author' => empty($stats_author) ? false : $stats_author,
395 'date' => [
396 'active' => empty($stats_date) ? false : $stats_date,
397 'format' => empty($stats_date_format) ? 'F j, Y' : $stats_date_format
398 ],
399 'category' => empty($stats_category) ? false : $stats_category,
400 'taxonomy' => [
401 'active' => empty($stats_taxonomy) ? false : $stats_taxonomy,
402 'name' => empty($taxonomy) ? 'category' : $taxonomy,
403 ]
404 ],
405 'markup' => [
406 'custom_html' => empty($custom_html) ? false : $custom_html,
407 'wpp-start' => empty($wpp_start) ? '' : $wpp_start,
408 'wpp-end' => empty($wpp_end) ? '' : $wpp_end,
409 'title-start' => empty($header_start) ? '' : $header_start,
410 'title-end' => empty($header_end) ? '' : $header_end,
411 'post-html' => empty($post_html) ? '<li>{thumb} {title} <span class="wpp-meta post-stats">{stats}</span></li>' : $post_html
412 ],
413 'theme' => [
414 'name' => empty($theme) ? '' : $theme
415 ]
416 ];
417
418 // Post / Page / CTP filter
419 $ids = array_filter(explode(",", $query_args['pid']), 'is_numeric');
420 // Got no valid IDs, clear
421 if ( empty($ids) ) {
422 $query_args['pid'] = '';
423 }
424
425 // Category filter
426 $ids = array_filter(explode(",", $query_args['cat']), 'is_numeric');
427 // Got no valid IDs, clear
428 if ( empty($ids) ) {
429 $query_args['cat'] = '';
430 }
431
432 // Author filter
433 $ids = array_filter(explode(",", $query_args['author']), 'is_numeric');
434 // Got no valid IDs, clear
435 if ( empty($ids) ) {
436 $query_args['author'] = '';
437 }
438
439 // Has user set a title?
440 if (
441 ! empty($query_args['title'])
442 && ! empty($query_args['markup']['title-start'])
443 && ! empty($query_args['markup']['title-end'])
444 ) {
445 $html .= htmlspecialchars_decode($query_args['markup']['title-start'], ENT_QUOTES) . $query_args['title'] . htmlspecialchars_decode($query_args['markup']['title-end'], ENT_QUOTES);
446 }
447
448 $isAdmin = isset($_GET['isSelected']) ? $_GET['isSelected'] : false;
449
450 if ( $this->admin_options['tools']['ajax'] && ! is_customize_preview() && ! $isAdmin ) {
451 $html .= '<script type="application/json">' . json_encode($query_args) . '</script>';
452 $html .= '<div class="wpp-widget-block-placeholder"></div>';
453
454 return $html;
455 }
456
457 // Return cached results
458 if ( $this->admin_options['tools']['cache']['active'] ) {
459
460 $key = md5(json_encode($query_args));
461 $popular_posts = \WordPressPopularPosts\Cache::get($key);
462
463 if ( false === $popular_posts ) {
464 $popular_posts = new Query($query_args);
465
466 $time_value = $this->admin_options['tools']['cache']['interval']['value']; // eg. 5
467 $time_unit = $this->admin_options['tools']['cache']['interval']['time']; // eg. 'minute'
468
469 // No popular posts found, check again in 1 minute
470 if ( ! $popular_posts->get_posts() ) {
471 $time_value = 1;
472 $time_unit = 'minute';
473 }
474
475 \WordPressPopularPosts\Cache::set(
476 $key,
477 $popular_posts,
478 $time_value,
479 $time_unit
480 );
481 }
482
483 } // Get popular posts
484 else {
485 $popular_posts = new Query($query_args);
486 }
487
488 $this->output->set_data($popular_posts->get_posts());
489 $this->output->set_public_options($query_args);
490 $this->output->build_output();
491
492 $html .= $this->output->get_output();
493
494 $html .= '</div>';
495
496 return $html;
497 }
498
499 /**
500 * Parses attributes.
501 *
502 * @since 5.4.0
503 * @param array
504 * @return array
505 */
506 private function parse_attributes($atts = [])
507 {
508 $out = array();
509
510 foreach ( $this->defaults as $name => $default ) {
511 $out[$name] = array_key_exists($name, $atts) ? trim($atts[$name]) : $default;
512 }
513
514 return $out;
515 }
516 }