PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 6.4.2
WP Popular Posts v6.4.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 / Shortcode / Posts.php
wordpress-popular-posts / src / Shortcode Last commit date
Posts.php 2 years ago Shortcode.php 2 years ago ShortcodeLoader.php 2 years ago ViewsCount.php 2 years ago
Posts.php
248 lines
1 <?php
2 namespace WordPressPopularPosts\Shortcode;
3
4 use WordPressPopularPosts\{ Helper, Output };
5 use WordPressPopularPosts\Shortcode\Shortcode;
6 use WordPressPopularPosts\Traits\QueriesPosts;
7
8 class Posts extends Shortcode {
9
10 use QueriesPosts;
11
12 /**
13 * Admin settings.
14 *
15 * @since 6.3.0
16 * @var array
17 */
18 private $config = [];
19
20 /**
21 * Output object.
22 *
23 * @since 6.3.0
24 * @var \WordPressPopularPosts\Output $output
25 * @access private
26 */
27 private $output;
28
29 /**
30 * Construct.
31 *
32 * @param array $admin_options
33 * @param \WordPressPopularPosts\Output $output Output class.
34 */
35 public function __construct(array $admin_options, Output $output)
36 {
37 $this->config = $admin_options;
38 $this->output = $output;
39 $this->tag = 'wpp';
40 }
41
42 /**
43 * Handles the HTML output of the shortcode.
44 *
45 * @since 6.3.0
46 * @param mixed $attributes Array of attributes passed to the shortcode, or an empty string if nothing is passed
47 * @return string Views count
48 */
49 public function handle($attributes = []) : string
50 {
51 /**
52 * @var string $header
53 * @var int $limit
54 * @var int $offset
55 * @var string $range
56 * @var bool $freshness
57 * @var string $order_by
58 * @var string $post_type
59 * @var string $pid
60 * @var string $cat
61 * @var string $author
62 * @var int $title_length
63 * @var int $title_by_words
64 * @var int $excerpt_length
65 * @var int $excerpt_format
66 * @var int $excerpt_by_words
67 * @var int $thumbnail_width
68 * @var int $thumbnail_height
69 * @var string $thumbnail_build
70 * @var bool $rating
71 * @var bool $stats_comments
72 * @var bool $stats_views
73 * @var bool $stats_author
74 * @var bool $stats_date
75 * @var string $stats_date_format
76 * @var bool $stats_category
77 * @var string $wpp_start
78 * @var string $wpp_end
79 * @var string $header_start
80 * @var string $header_end
81 * @var string $post_html
82 */
83 extract(shortcode_atts([
84 'header' => '',
85 'limit' => 10,
86 'offset' => 0,
87 'range' => 'daily',
88 'time_unit' => 'hour',
89 'time_quantity' => 24,
90 'freshness' => false,
91 'order_by' => 'views',
92 'post_type' => 'post',
93 'pid' => '',
94 'cat' => '',
95 'taxonomy' => 'category',
96 'term_id' => '',
97 'author' => '',
98 'title_length' => 0,
99 'title_by_words' => 0,
100 'excerpt_length' => 0,
101 'excerpt_format' => 0,
102 'excerpt_by_words' => 0,
103 'thumbnail_width' => 0,
104 'thumbnail_height' => 0,
105 'thumbnail_build' => 'manual',
106 'rating' => false,
107 'stats_comments' => false,
108 'stats_views' => true,
109 'stats_author' => false,
110 'stats_date' => false,
111 'stats_date_format' => 'F j, Y',
112 'stats_category' => false,
113 'stats_taxonomy' => false,
114 'wpp_start' => '<ul class="wpp-list">',
115 'wpp_end' => '</ul>',
116 'header_start' => '<h2>',
117 'header_end' => '</h2>',
118 'post_html' => '',
119 'theme' => '',
120 'ajaxify' => 1
121 ], $attributes, 'wpp'));
122
123 // possible values for "Time Range" and "Order by"
124 $time_units = ['minute', 'hour', 'day', 'week', 'month'];
125 $range_values = ['daily', 'last24hours', 'weekly', 'last7days', 'monthly', 'last30days', 'all', 'custom'];
126 $order_by_values = ['comments', 'views', 'avg'];
127
128 $shortcode_ops = [
129 'title' => strip_tags($header), // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- We want the behavior of strip_tags
130 'limit' => ( ! empty($limit ) && Helper::is_number($limit) && $limit > 0 ) ? $limit : 10,
131 'offset' => ( ! empty($offset) && Helper::is_number($offset) && $offset >= 0 ) ? $offset : 0,
132 'range' => ( in_array($range, $range_values) ) ? $range : 'daily',
133 'time_quantity' => ( ! empty($time_quantity ) && Helper::is_number($time_quantity) && $time_quantity > 0 ) ? $time_quantity : 24,
134 'time_unit' => ( in_array($time_unit, $time_units) ) ? $time_unit : 'hour',
135 'freshness' => empty($freshness) ? false : $freshness,
136 'order_by' => ( in_array($order_by, $order_by_values) ) ? $order_by : 'views',
137 'post_type' => empty($post_type) ? 'post' : $post_type,
138 'pid' => rtrim(preg_replace('|[^0-9,]|', '', $pid), ','),
139 'cat' => rtrim(preg_replace('|[^0-9,-]|', '', $cat), ','),
140 'taxonomy' => empty($taxonomy) ? 'category' : $taxonomy,
141 'term_id' => rtrim(preg_replace('|[^0-9,;-]|', '', $term_id), ','),
142 'author' => rtrim(preg_replace('|[^0-9,]|', '', $author), ','),
143 'shorten_title' => [
144 'active' => ( ! empty($title_length) && Helper::is_number($title_length) && $title_length > 0 ),
145 'length' => ( ! empty($title_length) && Helper::is_number($title_length) ) ? $title_length : 0,
146 'words' => ( ! empty($title_by_words) && Helper::is_number($title_by_words) && $title_by_words > 0 ),
147 ],
148 'post-excerpt' => [
149 'active' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) && $excerpt_length > 0 ),
150 'length' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) ) ? $excerpt_length : 0,
151 'keep_format' => ( ! empty($excerpt_format) && Helper::is_number($excerpt_format) && $excerpt_format > 0 ),
152 'words' => ( ! empty($excerpt_by_words) && Helper::is_number($excerpt_by_words) && $excerpt_by_words > 0 ),
153 ],
154 'thumbnail' => [
155 'active' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ),
156 'build' => 'predefined' === $thumbnail_build ? 'predefined' : 'manual',
157 'width' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ) ? $thumbnail_width : 0,
158 'height' => ( ! empty($thumbnail_height) && Helper::is_number($thumbnail_height) && $thumbnail_height > 0 ) ? $thumbnail_height : 0,
159 ],
160 'rating' => empty($rating) ? false : $rating,
161 'stats_tag' => [
162 'comment_count' => empty($stats_comments) ? false : $stats_comments,
163 'views' => empty($stats_views) ? false : $stats_views,
164 'author' => empty($stats_author) ? false : $stats_author,
165 'date' => [
166 'active' => empty($stats_date) ? false : $stats_date,
167 'format' => empty($stats_date_format) ? 'F j, Y' : $stats_date_format
168 ],
169 'category' => empty($stats_category) ? false : $stats_category,
170 'taxonomy' => [
171 'active' => empty($stats_taxonomy) ? false : $stats_taxonomy,
172 'name' => empty($taxonomy) ? 'category' : $taxonomy,
173 ]
174 ],
175 'markup' => [
176 'custom_html' => true,
177 'wpp-start' => empty($wpp_start) ? '' : $wpp_start,
178 'wpp-end' => empty($wpp_end) ? '' : $wpp_end,
179 'title-start' => empty($header_start) ? '' : $header_start,
180 'title-end' => empty($header_end) ? '' : $header_end,
181 'post-html' => empty($post_html) ? '<li>{thumb} {title} <span class="wpp-meta post-stats">{stats}</span></li>' : $post_html
182 ],
183 'theme' => [
184 'name' => trim($theme)
185 ]
186 ];
187
188 // Post / Page / CTP filter
189 $ids = array_filter(explode(',', $shortcode_ops['pid']), 'is_numeric');
190 // Got no valid IDs, clear
191 if ( empty($ids) ) {
192 $shortcode_ops['pid'] = '';
193 }
194
195 // Category filter
196 $ids = array_filter(explode(',', $shortcode_ops['cat']), 'is_numeric');
197 // Got no valid IDs, clear
198 if ( empty($ids) ) {
199 $shortcode_ops['cat'] = '';
200 }
201
202 // Author filter
203 $ids = array_filter(explode( ',', $shortcode_ops['author']), 'is_numeric');
204 // Got no valid IDs, clear
205 if ( empty($ids) ) {
206 $shortcode_ops['author'] = '';
207 }
208
209 $shortcode_content = '';
210 $cached = false;
211
212 // is there a title defined by user?
213 if (
214 ! empty($header)
215 && ! empty($header_start)
216 && ! empty($header_end)
217 ) {
218 $shortcode_content .= htmlspecialchars_decode($header_start, ENT_QUOTES) . $header . htmlspecialchars_decode($header_end, ENT_QUOTES);
219 $shortcode_content = Helper::sanitize_html($shortcode_content, $shortcode_ops);
220 }
221
222 $isAdmin = isset($_GET['isSelected']) ? $_GET['isSelected'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- isSelected is a boolean from wp-admin
223
224 $load_via_ajax = $this->config['tools']['ajax'];
225
226 if ( isset($attributes['ajaxify']) && is_numeric($attributes['ajaxify']) ) {
227 $load_via_ajax = (bool) absint($attributes['ajaxify']);
228 }
229
230 if ( $load_via_ajax && ! is_customize_preview() && ! $isAdmin ) {
231 $shortcode_content .= '<div class="wpp-shortcode">';
232 $shortcode_content .= '<script type="application/json">' . wp_json_encode($shortcode_ops) . '</script>';
233 $shortcode_content .= '<div class="wpp-shortcode-placeholder"></div>';
234 $shortcode_content .= '</div>';
235 } else {
236 $popular_posts = $this->maybe_query($shortcode_ops);
237
238 $this->output->set_data($popular_posts->get_posts());
239 $this->output->set_public_options($shortcode_ops);
240 $this->output->build_output();
241
242 $shortcode_content .= $this->output->get_output();
243 }
244
245 return $shortcode_content;
246 }
247 }
248