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