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 / Compatibility / Elementor / widgets / widget.php
wordpress-popular-posts / src / Compatibility / Elementor / widgets Last commit date
widget-controls.php 1 year ago widget.php 1 year ago
widget.php
408 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly.
4 }
5
6 //use WordPressPopularPosts\{Image, Themer};
7
8 /**
9 * Elementor WPP Widget.
10 *
11 * Elementor widget that inserts a WordPress Popular Posts list on your site.
12 *
13 * @since 7.3.0
14 */
15 class Elementor_WPP_Widget extends \Elementor\Widget_Base {
16
17 /**
18 * Image object.
19 *
20 * @since 7.3.0
21 * @var \WordPressPopularPosts\Image
22 * @access private
23 */
24 private $thumbnail;
25
26 /**
27 * Available image sizes.
28 *
29 * @since 7.3.0
30 * @var array
31 */
32 private $available_sizes = [];
33
34 /**
35 * Themer object.
36 *
37 * @since 7.3.0
38 * @var \WordPressPopularPosts\Themer $themer
39 * @access private
40 */
41 private $themer;
42
43 /**
44 * Construct.
45 *
46 * @param array $data Widget data. Default is an empty array.
47 * @param array|null $args Optional. Widget default arguments. Default is null.
48 * @param WordPressPopularPosts\Themer $themer
49 */
50 public function __construct($data = [], $args = null) {
51 parent::__construct($data, $args);
52
53 $this->thumbnail = $args['image'];
54 $this->themer = $args['themer'];
55 $this->available_sizes = $this->thumbnail->get_sizes(null);
56 }
57
58 /**
59 * Get widget name.
60 *
61 * Retrieve WPP widget name.
62 *
63 * @since 7.3.0
64 * @access public
65 * @return string Widget name.
66 */
67 public function get_name(): string {
68 return 'wordpress-popular-posts-ewidget';
69 }
70
71 /**
72 * Get widget title.
73 *
74 * Retrieve WPP widget title.
75 *
76 * @since 7.3.0
77 * @access public
78 * @return string Widget title.
79 */
80 public function get_title(): string {
81 return 'WordPress Popular Posts';
82 }
83
84 /**
85 * Get widget icon.
86 *
87 * Retrieve WPP widget icon.
88 *
89 * @since 7.3.0
90 * @access public
91 * @return string Widget icon.
92 */
93 public function get_icon(): string {
94 return 'wpp-eicon';
95 }
96
97 /**
98 * Get widget categories.
99 *
100 * Retrieve the list of categories the WPP widget belongs to.
101 *
102 * @since 7.3.0
103 * @access public
104 * @return array Widget categories.
105 */
106 public function get_categories(): array {
107 return ['general'];
108 }
109
110 /**
111 * Get widget keywords.
112 *
113 * Retrieve the list of keywords the WPP widget belongs to.
114 *
115 * @since 7.3.0
116 * @access public
117 * @return array Widget keywords.
118 */
119 public function get_keywords(): array {
120 return ['popular', 'posts', 'popularity', 'top'];
121 }
122
123 /**
124 * Get custom help URL.
125 *
126 * Retrieve a URL where the user can get more information about the widget.
127 *
128 * @since 7.3.0
129 * @access public
130 * @return string Widget help URL.
131 */
132 public function get_custom_help_url(): string {
133 return 'https://wordpress.org/support/plugin/wordpress-popular-posts/';
134 }
135
136 /**
137 * Displays a custom CTA to offer upsales and whatnot.
138 *
139 * @since 7.3.0
140 * @see https://developers.elementor.com/docs/widgets/widget-promotions/
141 * @return array
142 */
143 protected function get_upsale_data(): array {
144 return [];
145 }
146
147 /**
148 * Get stack.
149 *
150 * Retrieve the widget stack of controls.
151 *
152 * @since 7.3.0
153 * @param bool $with_common_controls Optional. Whether to include the common controls. Default is true.
154 * @return array Widget stack of controls.
155 */
156 public function get_stack( $with_common_controls = true ) {
157 return parent::get_stack(false);
158 }
159
160 /**
161 * Whether the widget requires inner wrapper.
162 *
163 * Determine whether to optimize the DOM size.
164 *
165 * @since 7.3.0
166 * @access protected
167 * @return bool Whether to optimize the DOM size.
168 */
169 public function has_widget_inner_wrapper(): bool {
170 return false;
171 }
172
173 /**
174 * Whether the element returns dynamic content.
175 *
176 * Determine whether to cache the element output or not.
177 *
178 * @since 7.3.0
179 * @access protected
180 * @return bool Whether to cache the element output.
181 */
182 protected function is_dynamic_content(): bool {
183 return true;
184 }
185
186 /**
187 * Register WPP widget controls.
188 *
189 * Add input fields to allow the user to customize the widget settings.
190 *
191 * @since 7.3.0
192 * @access protected
193 */
194 protected function register_controls(): void {
195 require 'widget-controls.php';
196 }
197
198 /**
199 * Render WPP widget output on the frontend.
200 *
201 * Written in PHP and used to generate the final HTML.
202 *
203 * @since 7.3.0
204 * @access protected
205 */
206 protected function render(): void {
207 $is_edit_mode = \Elementor\Plugin::$instance->editor->is_edit_mode();
208 $widget_id = $this->get_id();
209 $settings = $this->parse_settings();
210
211 /**
212 * Allows to modify settings passed to wpp_get_mostpopular()
213 *
214 * @param array $settings WPP settings
215 * @param string $widget_id Elementor Widget ID
216 */
217 $settings = apply_filters('wpp_elementor_widget_settings', $settings, $widget_id);
218
219 /** Display widget ID above the list when in edit mode */
220 if ( $is_edit_mode && defined('WP_DEBUG') && WP_DEBUG ) {
221 echo '<p style="margin: 0 0 1em; font-size: 10px; font-weight: 600;">[Widget ID:' . esc_html($widget_id) . ']</p>';
222 }
223
224 wpp_get_mostpopular($settings);
225 }
226
227 /**
228 * Parses and cleans up settings data from Elementor
229 * before using it.
230 *
231 * @since 7.3.0
232 * @access private
233 */
234 private function parse_settings() {
235 $settings = $this->get_settings_for_display();
236
237 $allowed_keys = [
238 'header', 'limit', 'offset', 'range', 'time_unit', 'time_quantity', 'freshness', 'order_by', 'post_type', 'exclude', 'cat', 'taxonomy', 'term_id', 'author', 'title_length', 'title_by_words', 'excerpt_length', 'excerpt_format', 'excerpt_by_words',
239 'thumbnail_width', 'thumbnail_height', 'thumbnail_build', 'rating', 'stats_comments', 'stats_views', 'stats_author', 'stats_date', 'stats_date_format', 'stats_category', 'stats_taxonomy', 'wpp_start', 'wpp_end', 'header_start', 'header_end', 'post_html',
240 'theme', 'ajaxify'
241 ];
242
243 /** Handle taxonomies */
244 $wpp_taxonomy_fields = array_filter($settings, function($key) {
245 return str_starts_with($key, 'wpp_taxonomy_'); // str_starts_with() requires either WP 5.9+ or PHP 8+
246 }, ARRAY_FILTER_USE_KEY);
247 $wpp_taxonomies = [];
248
249 if ( $wpp_taxonomy_fields ) {
250 $slugs_arr = array_filter($settings, function($key) {
251 return str_starts_with($key, 'wpp_taxonomy_slug_'); // str_starts_with() requires either WP 5.9+ or PHP 8+
252 }, ARRAY_FILTER_USE_KEY);
253
254 if ( $slugs_arr ) {
255 $slugs = array_filter($slugs_arr, function($slug) {
256 return 0 !== $slug;
257 });
258
259 if ( $slugs ) {
260 $slugs = array_values($slugs);
261
262 foreach( $slugs as $slug ) {
263 if ( ! isset($wpp_taxonomy_fields['wpp_taxonomy_' . $slug . '_terms']) ) {
264 continue;
265 }
266
267 $terms = array_filter(
268 explode(
269 ',',
270 trim(preg_replace('|[^0-9,-]|', '', $wpp_taxonomy_fields['wpp_taxonomy_' . $slug . '_terms']), ', ')
271 ),
272 'is_numeric'
273 );
274
275 if ( ! empty($terms) ) {
276 $wpp_taxonomies[$slug] = implode(',', $terms);
277 }
278 }
279 }
280 }
281 }
282
283 if ( $wpp_taxonomies ) {
284 $settings['taxonomy'] = implode(';', array_keys($wpp_taxonomies));
285 $settings['term_id'] = implode(';', array_values($wpp_taxonomies));
286 }
287
288 /** Handle thumbnail selection */
289 if ( isset($settings['thumbnail_size']) && isset($this->available_sizes[$settings['thumbnail_size']]) ) {
290 $settings['thumbnail_width'] = $this->available_sizes[$settings['thumbnail_size']]['width'];
291 $settings['thumbnail_height'] = $this->available_sizes[$settings['thumbnail_size']]['height'];
292 }
293
294 /** Let's remove all Elementor related settings */
295 $settings = array_filter($settings, function($key) use ($allowed_keys) {
296 return in_array($key, $allowed_keys);
297 }, ARRAY_FILTER_USE_KEY);
298
299 /** Handle toggles that are enabled by default */
300 if ( ! $settings['stats_views'] ) {
301 $settings['stats_views'] = '0';
302 }
303
304 /** Handle themes */
305 $registered_themes = $this->themer->get_themes();
306
307 if ( $settings['theme'] && isset($registered_themes[$settings['theme']]) ) {
308 $theme_config = $registered_themes[$settings['theme']]['json']['config'];
309
310 if (
311 isset($theme_config['shorten_title'])
312 && $theme_config['shorten_title']['active']
313 && is_numeric($theme_config['shorten_title']['length'])
314 && $theme_config['shorten_title']['length'] > 0
315 ) {
316 $settings['title_length'] = $theme_config['shorten_title']['length'];
317
318 if ( $theme_config['shorten_title']['length'] ) {
319 $settings['title_by_words'] = 1;
320 }
321 }
322
323 if (
324 isset($theme_config['post-excerpt'])
325 && $theme_config['post-excerpt']['active']
326 && is_numeric($theme_config['post-excerpt']['length'])
327 && $theme_config['post-excerpt']['length'] > 0
328 ) {
329 $settings['excerpt_length'] = $theme_config['post-excerpt']['length'];
330
331 if ( $theme_config['post-excerpt']['keep_format'] ) {
332 $settings['excerpt_format'] = 1;
333 }
334
335 if ( $theme_config['post-excerpt']['words'] ) {
336 $settings['excerpt_by_words'] = 1;
337 }
338 }
339
340 if (
341 isset($theme_config['thumbnail'])
342 && $theme_config['thumbnail']['active']
343 && is_numeric($theme_config['thumbnail']['width'])
344 && is_numeric($theme_config['thumbnail']['height'])
345 && $theme_config['thumbnail']['width'] > 0
346 && $theme_config['thumbnail']['height'] > 0
347 ) {
348 $settings['thumbnail_width'] = $theme_config['thumbnail']['width'];
349 $settings['thumbnail_height'] = $theme_config['thumbnail']['height'];
350 $settings['thumbnail_build'] = $theme_config['thumbnail']['build'] ?? 'manual';
351
352 if ( $theme_config['post-excerpt']['keep_format'] ) {
353 $settings['excerpt_format'] = 1;
354 }
355
356 if ( $theme_config['post-excerpt']['words'] ) {
357 $settings['excerpt_by_words'] = 1;
358 }
359 }
360
361 if ( isset($theme_config['rating']) && $theme_config['rating'] ) {
362 $settings['rating'] = 1;
363 }
364
365 if ( isset($theme_config['stats_tag']['comment_count']) && $theme_config['stats_tag']['comment_count'] ) {
366 $settings['stats_comments'] = 1;
367 }
368
369 if ( isset($theme_config['stats_tag']['views']) && $theme_config['stats_tag']['views'] ) {
370 $settings['stats_views'] = 1;
371 }
372
373 if ( isset($theme_config['stats_tag']['author']) && $theme_config['stats_tag']['author'] ) {
374 $settings['stats_author'] = 1;
375 }
376
377 if ( isset($theme_config['stats_tag']['date']) && $theme_config['stats_tag']['date']['active'] ) {
378 $settings['stats_date'] = 1;
379
380 if ( isset($theme_config['stats_tag']['date']['format']) && $theme_config['stats_tag']['date']['format'] ) {
381 $settings['stats_date_format'] = $theme_config['stats_tag']['date']['format'];
382 }
383 }
384
385 if ( isset($theme_config['stats_tag']['category']) && $theme_config['stats_tag']['category'] ) {
386 $settings['stats_category'] = 1;
387 } else {
388 if ( isset($theme_config['stats_tag']['taxonomy']) && $theme_config['stats_tag']['taxonomy']['active'] ) {
389 $settings['stats_taxonomy'] = 1;
390 }
391 }
392
393 /**
394 * @TODO
395 * Allow displaying multiple taxonomies
396 */
397
398 $settings['wpp_start'] = $theme_config['markup']['wpp-start'];
399 $settings['post_html'] = $theme_config['markup']['post-html'];
400 $settings['wpp_end'] = $theme_config['markup']['wpp-end'];
401 } else {
402 $settings['theme'] = '';
403 }
404
405 return $settings;
406 }
407 }
408