PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 7.3.1
WP Popular Posts v7.3.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 / 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
413 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 /** We're in Edit mode, disable AJAX loading and display widget ID */
212 if ( $is_edit_mode ) {
213 $settings['ajaxify'] = '0';
214 }
215
216 /**
217 * Allows to modify settings passed to wpp_get_mostpopular()
218 *
219 * @param array $settings WPP settings
220 * @param string $widget_id Elementor Widget ID
221 */
222 $settings = apply_filters('wpp_elementor_widget_settings', $settings, $widget_id);
223
224 /** Display widget ID above the list when in edit mode */
225 if ( $is_edit_mode && defined('WP_DEBUG') && WP_DEBUG ) {
226 echo '<p style="margin: 0 0 1em; font-size: 10px; font-weight: 600;">[Widget ID:' . esc_html($widget_id) . ']</p>';
227 }
228
229 wpp_get_mostpopular($settings);
230 }
231
232 /**
233 * Parses and cleans up settings data from Elementor
234 * before using it.
235 *
236 * @since 7.3.0
237 * @access private
238 */
239 private function parse_settings() {
240 $settings = $this->get_settings_for_display();
241
242 $allowed_keys = [
243 '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',
244 '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',
245 'theme', 'ajaxify'
246 ];
247
248 /** Handle taxonomies */
249 $wpp_taxonomy_fields = array_filter($settings, function($key) {
250 return str_starts_with($key, 'wpp_taxonomy_'); // str_starts_with() requires either WP 5.9+ or PHP 8+
251 }, ARRAY_FILTER_USE_KEY);
252 $wpp_taxonomies = [];
253
254 if ( $wpp_taxonomy_fields ) {
255 $slugs_arr = array_filter($settings, function($key) {
256 return str_starts_with($key, 'wpp_taxonomy_slug_'); // str_starts_with() requires either WP 5.9+ or PHP 8+
257 }, ARRAY_FILTER_USE_KEY);
258
259 if ( $slugs_arr ) {
260 $slugs = array_filter($slugs_arr, function($slug) {
261 return 0 !== $slug;
262 });
263
264 if ( $slugs ) {
265 $slugs = array_values($slugs);
266
267 foreach( $slugs as $slug ) {
268 if ( ! isset($wpp_taxonomy_fields['wpp_taxonomy_' . $slug . '_terms']) ) {
269 continue;
270 }
271
272 $terms = array_filter(
273 explode(
274 ',',
275 trim(preg_replace('|[^0-9,-]|', '', $wpp_taxonomy_fields['wpp_taxonomy_' . $slug . '_terms']), ', ')
276 ),
277 'is_numeric'
278 );
279
280 if ( ! empty($terms) ) {
281 $wpp_taxonomies[$slug] = implode(',', $terms);
282 }
283 }
284 }
285 }
286 }
287
288 if ( $wpp_taxonomies ) {
289 $settings['taxonomy'] = implode(';', array_keys($wpp_taxonomies));
290 $settings['term_id'] = implode(';', array_values($wpp_taxonomies));
291 }
292
293 /** Handle thumbnail selection */
294 if ( isset($settings['thumbnail_size']) && isset($this->available_sizes[$settings['thumbnail_size']]) ) {
295 $settings['thumbnail_width'] = $this->available_sizes[$settings['thumbnail_size']]['width'];
296 $settings['thumbnail_height'] = $this->available_sizes[$settings['thumbnail_size']]['height'];
297 }
298
299 /** Let's remove all Elementor related settings */
300 $settings = array_filter($settings, function($key) use ($allowed_keys) {
301 return in_array($key, $allowed_keys);
302 }, ARRAY_FILTER_USE_KEY);
303
304 /** Handle toggles that are enabled by default */
305 if ( ! $settings['stats_views'] ) {
306 $settings['stats_views'] = '0';
307 }
308
309 /** Handle themes */
310 $registered_themes = $this->themer->get_themes();
311
312 if ( $settings['theme'] && isset($registered_themes[$settings['theme']]) ) {
313 $theme_config = $registered_themes[$settings['theme']]['json']['config'];
314
315 if (
316 isset($theme_config['shorten_title'])
317 && $theme_config['shorten_title']['active']
318 && is_numeric($theme_config['shorten_title']['length'])
319 && $theme_config['shorten_title']['length'] > 0
320 ) {
321 $settings['title_length'] = $theme_config['shorten_title']['length'];
322
323 if ( $theme_config['shorten_title']['length'] ) {
324 $settings['title_by_words'] = 1;
325 }
326 }
327
328 if (
329 isset($theme_config['post-excerpt'])
330 && $theme_config['post-excerpt']['active']
331 && is_numeric($theme_config['post-excerpt']['length'])
332 && $theme_config['post-excerpt']['length'] > 0
333 ) {
334 $settings['excerpt_length'] = $theme_config['post-excerpt']['length'];
335
336 if ( $theme_config['post-excerpt']['keep_format'] ) {
337 $settings['excerpt_format'] = 1;
338 }
339
340 if ( $theme_config['post-excerpt']['words'] ) {
341 $settings['excerpt_by_words'] = 1;
342 }
343 }
344
345 if (
346 isset($theme_config['thumbnail'])
347 && $theme_config['thumbnail']['active']
348 && is_numeric($theme_config['thumbnail']['width'])
349 && is_numeric($theme_config['thumbnail']['height'])
350 && $theme_config['thumbnail']['width'] > 0
351 && $theme_config['thumbnail']['height'] > 0
352 ) {
353 $settings['thumbnail_width'] = $theme_config['thumbnail']['width'];
354 $settings['thumbnail_height'] = $theme_config['thumbnail']['height'];
355 $settings['thumbnail_build'] = $theme_config['thumbnail']['build'] ?? 'manual';
356
357 if ( $theme_config['post-excerpt']['keep_format'] ) {
358 $settings['excerpt_format'] = 1;
359 }
360
361 if ( $theme_config['post-excerpt']['words'] ) {
362 $settings['excerpt_by_words'] = 1;
363 }
364 }
365
366 if ( isset($theme_config['rating']) && $theme_config['rating'] ) {
367 $settings['rating'] = 1;
368 }
369
370 if ( isset($theme_config['stats_tag']['comment_count']) && $theme_config['stats_tag']['comment_count'] ) {
371 $settings['stats_comments'] = 1;
372 }
373
374 if ( isset($theme_config['stats_tag']['views']) && $theme_config['stats_tag']['views'] ) {
375 $settings['stats_views'] = 1;
376 }
377
378 if ( isset($theme_config['stats_tag']['author']) && $theme_config['stats_tag']['author'] ) {
379 $settings['stats_author'] = 1;
380 }
381
382 if ( isset($theme_config['stats_tag']['date']) && $theme_config['stats_tag']['date']['active'] ) {
383 $settings['stats_date'] = 1;
384
385 if ( isset($theme_config['stats_tag']['date']['format']) && $theme_config['stats_tag']['date']['format'] ) {
386 $settings['stats_date_format'] = $theme_config['stats_tag']['date']['format'];
387 }
388 }
389
390 if ( isset($theme_config['stats_tag']['category']) && $theme_config['stats_tag']['category'] ) {
391 $settings['stats_category'] = 1;
392 } else {
393 if ( isset($theme_config['stats_tag']['taxonomy']) && $theme_config['stats_tag']['taxonomy']['active'] ) {
394 $settings['stats_taxonomy'] = 1;
395 }
396 }
397
398 /**
399 * @TODO
400 * Allow displaying multiple taxonomies
401 */
402
403 $settings['wpp_start'] = $theme_config['markup']['wpp-start'];
404 $settings['post_html'] = $theme_config['markup']['post-html'];
405 $settings['wpp_end'] = $theme_config['markup']['wpp-end'];
406 } else {
407 $settings['theme'] = '';
408 }
409
410 return $settings;
411 }
412 }
413