PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 7.4.0
WP Popular Posts v7.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 / Shortcode / Posts.php
wordpress-popular-posts / src / Shortcode Last commit date
Posts.php 1 year ago Shortcode.php 2 years ago ShortcodeLoader.php 2 years ago ViewsCount.php 2 years ago
Posts.php
254 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' => '', /* Deprecated */
94 'exclude' => '',
95 'cat' => '',
96 'taxonomy' => 'category',
97 'term_id' => '',
98 'author' => '',
99 'title_length' => 0,
100 'title_by_words' => 0,
101 'excerpt_length' => 0,
102 'excerpt_format' => 0,
103 'excerpt_by_words' => 0,
104 'thumbnail_width' => 0,
105 'thumbnail_height' => 0,
106 'thumbnail_build' => 'manual',
107 'rating' => false,
108 'stats_comments' => false,
109 'stats_views' => true,
110 'stats_author' => false,
111 'stats_date' => false,
112 'stats_date_format' => 'F j, Y',
113 'stats_category' => false,
114 'stats_taxonomy' => false,
115 'wpp_start' => '<ul class="wpp-list">',
116 'wpp_end' => '</ul>',
117 'header_start' => '<h2>',
118 'header_end' => '</h2>',
119 'post_html' => '',
120 'theme' => '',
121 'ajaxify' => 1
122 ], $attributes, 'wpp'));
123
124 // possible values for "Time Range" and "Order by"
125 $time_units = ['minute', 'hour', 'day', 'week', 'month'];
126 $range_values = ['daily', 'last24hours', 'weekly', 'last7days', 'monthly', 'last30days', 'all', 'custom'];
127 $order_by_values = ['comments', 'views', 'avg'];
128
129 $shortcode_ops = [
130 'title' => strip_tags($header), // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- We want the behavior of strip_tags
131 'limit' => ( ! empty($limit ) && Helper::is_number($limit) && $limit > 0 ) ? $limit : 10,
132 'offset' => ( ! empty($offset) && Helper::is_number($offset) && $offset >= 0 ) ? $offset : 0,
133 'range' => ( in_array($range, $range_values) ) ? $range : 'daily',
134 'time_quantity' => ( ! empty($time_quantity ) && Helper::is_number($time_quantity) && $time_quantity > 0 ) ? $time_quantity : 24,
135 'time_unit' => ( in_array($time_unit, $time_units) ) ? $time_unit : 'hour',
136 'freshness' => empty($freshness) ? false : $freshness,
137 'order_by' => ( in_array($order_by, $order_by_values) ) ? $order_by : 'views',
138 'post_type' => empty($post_type) ? 'post' : $post_type,
139 'pid' => rtrim(preg_replace('|[^0-9,]|', '', $pid), ','), /* Deprecated */
140 'exclude' => rtrim(preg_replace('|[^0-9,]|', '', $exclude), ','),
141 'cat' => rtrim(preg_replace('|[^0-9,-]|', '', $cat), ','),
142 'taxonomy' => empty($taxonomy) ? 'category' : $taxonomy,
143 'term_id' => rtrim(preg_replace('|[^0-9,;-]|', '', $term_id), ','),
144 'author' => rtrim(preg_replace('|[^0-9,]|', '', $author), ','),
145 'shorten_title' => [
146 'active' => ( ! empty($title_length) && Helper::is_number($title_length) && $title_length > 0 ),
147 'length' => ( ! empty($title_length) && Helper::is_number($title_length) ) ? $title_length : 0,
148 'words' => ( ! empty($title_by_words) && Helper::is_number($title_by_words) && $title_by_words > 0 ),
149 ],
150 'post-excerpt' => [
151 'active' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) && $excerpt_length > 0 ),
152 'length' => ( ! empty($excerpt_length) && Helper::is_number($excerpt_length) ) ? $excerpt_length : 0,
153 'keep_format' => ( ! empty($excerpt_format) && Helper::is_number($excerpt_format) && $excerpt_format > 0 ),
154 'words' => ( ! empty($excerpt_by_words) && Helper::is_number($excerpt_by_words) && $excerpt_by_words > 0 ),
155 ],
156 'thumbnail' => [
157 'active' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ),
158 'build' => 'predefined' === $thumbnail_build ? 'predefined' : 'manual',
159 'width' => ( ! empty($thumbnail_width) && Helper::is_number($thumbnail_width) && $thumbnail_width > 0 ) ? $thumbnail_width : 0,
160 'height' => ( ! empty($thumbnail_height) && Helper::is_number($thumbnail_height) && $thumbnail_height > 0 ) ? $thumbnail_height : 0,
161 ],
162 'rating' => empty($rating) ? false : $rating,
163 'stats_tag' => [
164 'comment_count' => empty($stats_comments) ? false : $stats_comments,
165 'views' => empty($stats_views) ? false : $stats_views,
166 'author' => empty($stats_author) ? false : $stats_author,
167 'date' => [
168 'active' => empty($stats_date) ? false : $stats_date,
169 'format' => empty($stats_date_format) ? 'F j, Y' : $stats_date_format
170 ],
171 'category' => empty($stats_category) ? false : $stats_category,
172 'taxonomy' => [
173 'active' => empty($stats_taxonomy) ? false : $stats_taxonomy,
174 'name' => empty($taxonomy) ? 'category' : $taxonomy,
175 ]
176 ],
177 'markup' => [
178 'custom_html' => true,
179 'wpp-start' => empty($wpp_start) ? '' : $wpp_start,
180 'wpp-end' => empty($wpp_end) ? '' : $wpp_end,
181 'title-start' => empty($header_start) ? '' : $header_start,
182 'title-end' => empty($header_end) ? '' : $header_end,
183 'post-html' => empty($post_html) ? '<li class="{current_class}">{thumb} {title} <span class="wpp-meta post-stats">{stats}</span><p class="wpp-excerpt">{excerpt}</p></li>' : $post_html
184 ],
185 'theme' => [
186 'name' => trim($theme)
187 ]
188 ];
189
190 // Post / Page / CTP filter
191 $shortcode_ops['exclude'] = ( ! empty($shortcode_ops['pid']) ) ? $shortcode_ops['pid'] : $shortcode_ops['exclude'];
192
193 $ids = array_filter(explode(',', $shortcode_ops['exclude']), 'is_numeric');
194 // Got no valid IDs, clear
195 if ( empty($ids) ) {
196 $shortcode_ops['exclude'] = '';
197 }
198
199 // Category filter
200 $ids = array_filter(explode(',', $shortcode_ops['cat']), 'is_numeric');
201 // Got no valid IDs, clear
202 if ( empty($ids) ) {
203 $shortcode_ops['cat'] = '';
204 }
205
206 // Author filter
207 $ids = array_filter(explode( ',', $shortcode_ops['author']), 'is_numeric');
208 // Got no valid IDs, clear
209 if ( empty($ids) ) {
210 $shortcode_ops['author'] = '';
211 }
212
213 $shortcode_content = '';
214 $cached = false;
215
216 // Has the user set a title?
217 if (
218 ! empty($header)
219 && ! empty($header_start)
220 && ! empty($header_end)
221 ) {
222 $header_html = htmlspecialchars_decode($header_start, ENT_QUOTES) . $header . htmlspecialchars_decode($header_end, ENT_QUOTES);
223 $header_html = apply_filters('wpp_custom_header_html', $header_html, $shortcode_ops);
224 $header_html = Helper::sanitize_html($header_html, $shortcode_ops);
225 $shortcode_content .= $header_html;
226 }
227
228 $isAdmin = isset($_GET['isSelected']) ? $_GET['isSelected'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- isSelected is a boolean from wp-admin
229
230 $load_via_ajax = $this->config['tools']['ajax'];
231
232 if ( isset($attributes['ajaxify']) && is_numeric($attributes['ajaxify']) ) {
233 $load_via_ajax = (bool) absint($ajaxify);
234 }
235
236 if ( $load_via_ajax && ! is_customize_preview() && ! $isAdmin ) {
237 $shortcode_content .= '<div class="wpp-shortcode">';
238 $shortcode_content .= '<script type="application/json" data-id="wpp-shortcode-inline-js">' . wp_json_encode($shortcode_ops) . '</script>';
239 $shortcode_content .= '<div class="wpp-shortcode-placeholder"></div>';
240 $shortcode_content .= '</div>';
241 } else {
242 $popular_posts = $this->maybe_query($shortcode_ops);
243
244 $this->output->set_data($popular_posts->get_posts());
245 $this->output->set_public_options($shortcode_ops);
246 $this->output->build_output();
247
248 $shortcode_content .= $this->output->get_output();
249 }
250
251 return $shortcode_content;
252 }
253 }
254