PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 7.3.3
WP Popular Posts v7.3.3
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 / Shortcode / ViewsCount.php
wordpress-popular-posts / src / Shortcode Last commit date
Posts.php 1 year ago Shortcode.php 1 year ago ShortcodeLoader.php 1 year ago ViewsCount.php 1 year ago
ViewsCount.php
91 lines
1 <?php
2 namespace WordPressPopularPosts\Shortcode;
3
4 use WordPressPopularPosts\Helper;
5 use WordPressPopularPosts\Shortcode\Shortcode;
6
7 class ViewsCount extends Shortcode {
8
9 /**
10 * Construct.
11 */
12 public function __construct()
13 {
14 $this->tag = 'wpp_views_count';
15 }
16
17 /**
18 * Handles the HTML output of the shortcode.
19 *
20 * @since 6.3.0
21 * @param mixed $attributes Array of attributes passed to the shortcode, or an empty string if nothing is passed
22 * @return string Views count
23 */
24 public function handle($attributes = []) : string
25 {
26 $views = '';
27
28 if ( function_exists('wpp_get_views') ) {
29 $attributes = shortcode_atts(
30 [
31 'post_id' => null,
32 'range' => 'all',
33 'time_unit' => 'hour',
34 'time_quantity' => 24,
35 'number_format' => 1,
36 'include_views_text' => 1
37 ],
38 $attributes,
39 $this->tag
40 );
41
42 if ( ! $attributes['post_id'] ) {
43 if ( is_singular() ) {
44 $attributes['post_id'] = \get_queried_object_id();
45 }
46 }
47
48 if ( $attributes['post_id'] && is_numeric($attributes['post_id']) ) {
49 $valid_time_ranges = ['last24hours', 'last7days', 'last30days', 'all', 'custom'];
50
51 if (
52 'all' === $attributes['range']
53 || ! in_array($attributes['range'], $valid_time_ranges)
54 ) {
55 $views = wpp_get_views($attributes['post_id'], 'all', false);
56 } elseif ( 'custom' !== $attributes['range'] ) {
57 $views = wpp_get_views($attributes['post_id'], $attributes['range'], false);
58 } else {
59 $views = wpp_get_views(
60 $attributes['post_id'],
61 [
62 'range' => 'custom',
63 'time_unit' => $attributes['time_unit'],
64 'time_quantity' => $attributes['time_quantity']
65 ],
66 false
67 );
68 }
69
70 $views = (int) $views;
71 $views_string = $views;
72
73 if ( $views && $attributes['number_format'] ) {
74 $views_string = ( 'prettify' === $attributes['number_format'] ) ? Helper::prettify_number($views) : number_format_i18n($views);
75 }
76
77 if ( $attributes['include_views_text'] ) {
78 return sprintf(
79 _n('%s view', '%s views', $views, 'wordpress-popular-posts'),
80 $views_string
81 );
82 }
83
84 return $views_string;
85 }
86 }
87
88 return $views;
89 }
90 }
91