PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / 4.5.0
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets v4.5.0
4.5.6 4.5.5 4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 4.1.2 All 148 releases
ultimate-post-kit / traits / global-widget-functions.php

global-widget-functions.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets 4.5.0, at traits/global-widget-functions.php

468 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace UltimatePostKit\Traits;
4
5 use UltimatePostKit\Utils;
6
7 defined('ABSPATH') || die();
8
9 trait Global_Widget_Functions {
10
11 /**
12 * Render Ajax Qery Posts
13 */
14 function mapGroupControlQuery($term_ids = []) {
15 $terms = get_terms(
16 [
17 'term_taxonomy_id' => $term_ids,
18 'hide_empty' => false,
19 ]
20 );
21
22 $tax_terms_map = [];
23
24 foreach ($terms as $term) {
25 $taxonomy = $term->taxonomy;
26 $tax_terms_map[$taxonomy][] = $term->term_id;
27 }
28
29 return $tax_terms_map;
30 }
31 function query_args() {
32 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified in the load-more AJAX handler (check_ajax_referer 'upk-site') before this runs.
33 if ( isset( $_POST['settings'] ) && is_array( $_POST['settings'] ) ) {
34 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified in the load-more AJAX handler (check_ajax_referer 'upk-site') before this runs.
35 extract( map_deep( wp_unslash( $_POST['settings'] ), 'sanitize_text_field' ) );
36 }
37
38 // This handler is reachable unauthenticated (wp_ajax_nopriv_*). Clamp the
39 // page size to a sane positive maximum so a request cannot ask for -1
40 // ("all posts") or a huge value and turn load-more into a DoS amplifier.
41 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified in the load-more AJAX handler (check_ajax_referer 'upk-site') before this runs.
42 $per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 0;
43 $per_page = max( 1, min( 100, $per_page ) );
44 // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified in the load-more AJAX handler (check_ajax_referer 'upk-site') before this runs.
45 $offset = isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) : 0;
46
47 // setmeta args
48 $args = [
49 'posts_per_page' => $per_page,
50 'post_status' => 'publish',
51 'suppress_filters' => false,
52 'orderby' => $posts_orderby,
53 'order' => $posts_order,
54 'offset' => $offset,
55 ];
56 /**
57 * set feature image
58 *
59 */
60 if (isset($posts_only_with_featured_image) && $posts_only_with_featured_image === 'yes') {
61 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Elementor widget query built from user-configured controls; expected behaviour.
62 $args['meta_query'] = [
63 [
64 'key' => '_thumbnail_id',
65 'compare' => 'EXISTS',
66 ],
67 ];
68 }
69
70 /**
71 * set taxonomy query
72 */
73
74 /**
75 * set date query
76 */
77
78 $selected_date = $posts_select_date;
79
80 if (!empty($selected_date)) {
81 $date_query = [];
82
83 switch ($selected_date) {
84 case 'today':
85 $date_query['after'] = '-1 day';
86 break;
87
88 case 'week':
89 $date_query['after'] = '-1 week';
90 break;
91
92 case 'month':
93 $date_query['after'] = '-1 month';
94 break;
95
96 case 'quarter':
97 $date_query['after'] = '-3 month';
98 break;
99
100 case 'year':
101 $date_query['after'] = '-1 year';
102 break;
103
104 case 'exact':
105 $after_date = $posts_date_after;
106 if (!empty($after_date)) {
107 $date_query['after'] = $after_date;
108 }
109
110 $before_date = $posts_date_before;
111 if (!empty($before_date)) {
112 $date_query['before'] = $before_date;
113 }
114 $date_query['inclusive'] = true;
115 break;
116 }
117
118 if (!empty($date_query)) {
119 $args['date_query'] = $date_query;
120 }
121 }
122
123 $exclude_by = isset($posts_exclude_by) ? $posts_exclude_by : [];
124 $include_by = isset($posts_include_by) ? $posts_include_by : [];
125 $include_users = [];
126 $exclude_users = [];
127 // print_r($exclude_by);
128 /**
129 * ignore sticky post
130 */
131 if (!empty($exclude_by) && $posts_source === 'post' && $posts_ignore_sticky_posts === 'yes') {
132 $args['ignore_sticky_posts'] = true;
133 if (in_array('current_post', $exclude_by)) {
134 // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour.
135 $args['post__not_in'] = [get_the_ID()];
136 }
137 }
138
139 /**
140 * set post type
141 */
142
143 if ($posts_source === 'manual_selection') {
144 /**
145 * Set Including Manually
146 */
147 $selected_ids = $posts_selected_ids;
148 $selected_ids = wp_parse_id_list($selected_ids);
149 $args['post_type'] = 'any';
150 if (!empty($selected_ids)) {
151 $args['post__in'] = $selected_ids;
152 }
153 $args['ignore_sticky_posts'] = 1;
154 } elseif ('current_query' === $posts_source) {
155 /**
156 * Make Current Query
157 */
158 $args = $GLOBALS['wp_query']->query_vars;
159 $args = apply_filters('ultimate_post_kit/query/get_query_args/current_query', $args);
160 } elseif ('_related_post_type' === $posts_source) {
161 /**
162 * Set Related Query
163 */
164 $post_id = get_queried_object_id();
165 $related_post_id = is_singular() && (0 !== $post_id) ? $post_id : null;
166 $args['post_type'] = get_post_type($related_post_id);
167
168 // $include_by = $this->getGroupControlQueryParamBy('include');
169 if (in_array('authors', $include_by)) {
170 $args['author__in'] = wp_parse_id_list($settings['posts_include_author_ids']);
171 } else {
172 $args['author__in'] = get_post_field('post_author', $related_post_id);
173 }
174
175 // $exclude_by = $this->getGroupControlQueryParamBy('exclude');
176 if (in_array('authors', $exclude_by)) {
177 $args['author__not_in'] = wp_parse_id_list($posts_exclude_author_ids);
178 }
179
180 if (in_array('current_post', $exclude_by)) {
181 // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour.
182 $args['post__not_in'] = [get_the_ID()];
183 }
184
185 $args['ignore_sticky_posts'] = 1;
186 $args = apply_filters('ultimate_post_kit/query/get_query_args/related_query', $args);
187 } else {
188 $args['post_type'] = $posts_source;
189 $current_post = [];
190
191 /**
192 * Set Taxonomy && Set Authors
193 */
194 $include_terms = [];
195 $exclude_terms = [];
196 $terms_query = [];
197
198 /**
199 * Set exclude Terms
200 */
201 if (!empty($exclude_by)) {
202 if (in_array('authors', $exclude_by)) {
203 $exclude_users = wp_parse_id_list($posts_exclude_author_ids);
204 $include_users = array_diff($include_users, $exclude_users);
205 }
206 if (!empty($exclude_users)) {
207 $args['author__not_in'] = $exclude_users;
208 }
209 if (in_array('current_post', $exclude_by) && is_singular()) {
210 $current_post[] = get_the_ID();
211 }
212 if (in_array('manual_selection', $exclude_by)) {
213 $exclude_ids = $posts_exclude_ids;
214 // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour.
215 $args['post__not_in'] = array_merge($current_post, wp_parse_id_list($exclude_ids));
216 }
217 if (in_array('terms', $exclude_by)) {
218 $exclude_terms = wp_parse_id_list($posts_exclude_term_ids);
219 $include_terms = array_diff($include_terms, $exclude_terms);
220 }
221 if (!empty($exclude_terms)) {
222 $tax_terms_map = $this->mapGroupControlQuery($exclude_terms);
223
224 foreach ($tax_terms_map as $tax => $terms) {
225 $terms_query[] = [
226 'taxonomy' => $tax,
227 'field' => 'term_id',
228 'terms' => $terms,
229 'operator' => 'NOT IN',
230 ];
231 }
232 }
233 }
234
235 /**
236 * Set include Terms
237 */
238 if (!empty($include_by)) {
239
240 if (in_array('authors', $include_by)) {
241 $include_users = wp_parse_id_list($posts_include_author_ids);
242 }
243 if (!empty($include_users)) {
244 $args['author__in'] = $include_users;
245 }
246 if (in_array('terms', $include_by)) {
247 $include_terms = wp_parse_id_list($posts_include_term_ids);
248 }
249 $tax_terms_map = $this->mapGroupControlQuery($include_terms);
250 foreach ($tax_terms_map as $tax => $terms) {
251 $terms_query[] = [
252 'taxonomy' => $tax,
253 'field' => 'term_id',
254 'terms' => $terms,
255 'operator' => 'IN',
256 ];
257 }
258 }
259
260
261 if (!empty($terms_query)) {
262 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Elementor widget query built from user-configured controls; expected behaviour.
263 $args['tax_query'] = $terms_query;
264 $args['tax_query']['relation'] = 'AND';
265 }
266 }
267
268 $ajaxposts = new \WP_Query($args);
269 return $ajaxposts;
270 }
271
272 /**
273 * Render Ajax LoadMore
274 */
275 function render_ajax_loadmore() {
276 $settings = $this->get_settings_for_display();
277 if (!$this->get_settings('ajax_loadmore_enable')) {
278 return;
279 }
280 ?>
281 <div class="upk-loadmore-container">
282 <?php if ($settings['ajax_loadmore_btn'] == 'yes') : ?>
283 <span class="upk-loadmore-btn"><?php esc_html_e('Load More', 'ultimate-post-kit'); ?></span>
284 <?php elseif ($settings['ajax_loadmore_infinite_scroll'] == 'yes') : ?>
285 <div class="upk-ajax-loading" style="display: none;"></div>
286 <?php endif; ?>
287 </div>
288 <?php
289 }
290
291 /**
292 * New Image Render Method With Lazy Load Support
293 *
294 * @return void
295 */
296
297 function render_image($image_id, $size) {
298 $placeholder_image_src = Utils::get_placeholder_image_src();
299 $image_src = wp_get_attachment_image_src($image_id, $size);
300
301 if (!$image_src) {
302 printf('<img class="upk-img" src="%1$s" alt="%2$s">', esc_url($placeholder_image_src), esc_attr(get_the_title()));
303 } else {
304 print(wp_get_attachment_image(
305 $image_id,
306 $size,
307 false,
308 [
309 'class' => 'upk-img',
310 'alt' => esc_attr(get_the_title())
311 ]
312 ));
313 }
314 }
315
316 /**
317 * Old Image Render Method
318 * Not Using Anymore
319 * @return void
320 */
321 function __render_image($image_id, $size) {
322 $placeholder_image_src = Utils::get_placeholder_image_src();
323 $image_src = wp_get_attachment_image_src($image_id, $size);
324 if (!$image_src) {
325 $image_src = $placeholder_image_src;
326 } else {
327 $image_src = $image_src[0];
328 }
329 ?>
330 <img class="upk-img" src="<?php echo esc_url($image_src); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
331 <?php
332 }
333
334 function render_title($widget_name) {
335 $settings = $this->get_settings_for_display();
336 if (!$this->get_settings('show_title')) {
337 return;
338 }
339 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- established hook name relied on across the plugin family; renaming would break integration.
340 apply_filters('upk/' . $widget_name . '/before/title', '');
341 $title = get_the_title();
342 printf(
343 '<%1$s class="upk-title"><a href="%2$s" title="%5$s" class="title-animation-%4$s" aria-label="%5$s">%3$s</a></%1$s>',
344 esc_attr(Utils::get_valid_html_tag($settings['title_tags'])),
345 esc_url( get_permalink() ),
346 esc_html( $title ),
347 esc_attr($settings['title_style']),
348 esc_attr( $title )
349 );
350 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- established hook name relied on across the plugin family; renaming would break integration.
351 apply_filters('upk/' . $widget_name . '/after/title', '');
352 }
353
354
355
356 function render_category() {
357 if (!$this->get_settings('show_category')) {
358 return;
359 }
360 ?>
361 <div class="upk-category">
362 <?php echo wp_kses_post( upk_get_category($this->get_settings('posts_source')) ); ?>
363 </div>
364 <?php
365 }
366
367 function render_date() {
368 $settings = $this->get_settings_for_display();
369 if (!$this->get_settings('show_date')) {
370 return;
371 }
372 $meta_separator = isset( $settings['meta_separator'] ) ? $settings['meta_separator'] : '.';
373 ?>
374 <div class="upk-date">
375 <?php if ($settings['human_diff_time'] == 'yes') {
376 echo esc_html( ultimate_post_kit_post_time_diff( ($settings['human_diff_time_short'] == 'yes') ? 'short' : '' ) );
377 } else {
378 echo get_the_date();
379 } ?>
380 </div>
381
382 <?php if ($settings['show_time']) : ?>
383 <div class="upk-post-time" data-separator="<?php echo esc_attr( $meta_separator ); ?>">
384 <i class="upk-icon-clock" aria-hidden="true"></i>
385 <?php echo esc_html( get_the_time() ); ?>
386 </div>
387 <?php endif; ?>
388 <?php
389 }
390
391 function render_excerpt($excerpt_length) {
392 $settings = $this->get_settings_for_display();
393
394 if (!$this->get_settings('show_excerpt')) {
395 return;
396 }
397 $strip_shortcode = $this->get_settings_for_display('strip_shortcode');
398
399 $ellipsis = isset($settings['ellipsis']) ? $settings['ellipsis'] : '' ;
400 ?>
401 <div class="upk-text">
402 <?php
403 if (has_excerpt()) {
404 the_excerpt();
405 } else {
406 echo wp_kses_post( ultimate_post_kit_custom_excerpt($excerpt_length, $strip_shortcode, $ellipsis) );
407 }
408 ?>
409 </div>
410 <?php
411 }
412
413 /**
414 * Localized comment count with label. Echo with esc_html().
415 *
416 * @param int $post_id Post ID, or 0 for current post in The Loop.
417 * @return string
418 */
419 protected function upk_get_formatted_comments_count( $post_id = 0 ) {
420 return Utils::get_formatted_comments_count( $post_id );
421 }
422
423 /**
424 * Localized comment count number only. Echo with esc_html().
425 *
426 * @param int $post_id Post ID, or 0 for current post in The Loop.
427 * @return string
428 */
429 protected function upk_get_localized_comment_count( $post_id = 0 ) {
430 return Utils::get_localized_comment_count( $post_id );
431 }
432
433 function render_post_format() {
434 $settings = $this->get_settings_for_display();
435
436 if (!$settings['show_post_format']) {
437 return;
438 }
439 ?>
440 <div class="upk-post-format">
441 <a href="<?php echo esc_url(get_permalink()) ?>">
442 <?php if (has_post_format('aside')) : ?>
443 <i class="upk-icon-aside" aria-hidden="true"></i>
444 <?php elseif (has_post_format('gallery')) : ?>
445 <i class="upk-icon-gallery" aria-hidden="true"></i>
446 <?php elseif (has_post_format('link')) : ?>
447 <i class="upk-icon-link" aria-hidden="true"></i>
448 <?php elseif (has_post_format('image')) : ?>
449 <i class="upk-icon-image" aria-hidden="true"></i>
450 <?php elseif (has_post_format('quote')) : ?>
451 <i class="upk-icon-quote" aria-hidden="true"></i>
452 <?php elseif (has_post_format('status')) : ?>
453 <i class="upk-icon-status" aria-hidden="true"></i>
454 <?php elseif (has_post_format('video')) : ?>
455 <i class="upk-icon-video" aria-hidden="true"></i>
456 <?php elseif (has_post_format('audio')) : ?>
457 <i class="upk-icon-music" aria-hidden="true"></i>
458 <?php elseif (has_post_format('chat')) : ?>
459 <i class="upk-icon-chat" aria-hidden="true"></i>
460 <?php else : ?>
461 <i class="upk-icon-post" aria-hidden="true"></i>
462 <?php endif; ?>
463 </a>
464 </div>
465 <?php
466 }
467 }
468