| 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 |
// This runs on wp_ajax_nopriv_* and $posts_source comes straight from $_POST, |
| 189 |
// so restrict it to post types this site already exposes to anonymous visitors. |
| 190 |
// Without this a request can enumerate published entries of post types that are |
| 191 |
// deliberately hidden from anonymous access (e.g. Elementor's elementor_library). |
| 192 |
$args['post_type'] = ultimate_post_kit_sanitize_public_post_type( $posts_source ); |
| 193 |
$current_post = []; |
| 194 |
|
| 195 |
/** |
| 196 |
* Set Taxonomy && Set Authors |
| 197 |
*/ |
| 198 |
$include_terms = []; |
| 199 |
$exclude_terms = []; |
| 200 |
$terms_query = []; |
| 201 |
|
| 202 |
/** |
| 203 |
* Set exclude Terms |
| 204 |
*/ |
| 205 |
if (!empty($exclude_by)) { |
| 206 |
if (in_array('authors', $exclude_by)) { |
| 207 |
$exclude_users = wp_parse_id_list($posts_exclude_author_ids); |
| 208 |
$include_users = array_diff($include_users, $exclude_users); |
| 209 |
} |
| 210 |
if (!empty($exclude_users)) { |
| 211 |
$args['author__not_in'] = $exclude_users; |
| 212 |
} |
| 213 |
if (in_array('current_post', $exclude_by) && is_singular()) { |
| 214 |
$current_post[] = get_the_ID(); |
| 215 |
} |
| 216 |
if (in_array('manual_selection', $exclude_by)) { |
| 217 |
$exclude_ids = $posts_exclude_ids; |
| 218 |
// phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- Elementor widget query built from user-configured controls; expected behaviour. |
| 219 |
$args['post__not_in'] = array_merge($current_post, wp_parse_id_list($exclude_ids)); |
| 220 |
} |
| 221 |
if (in_array('terms', $exclude_by)) { |
| 222 |
$exclude_terms = wp_parse_id_list($posts_exclude_term_ids); |
| 223 |
$include_terms = array_diff($include_terms, $exclude_terms); |
| 224 |
} |
| 225 |
if (!empty($exclude_terms)) { |
| 226 |
$tax_terms_map = $this->mapGroupControlQuery($exclude_terms); |
| 227 |
|
| 228 |
foreach ($tax_terms_map as $tax => $terms) { |
| 229 |
$terms_query[] = [ |
| 230 |
'taxonomy' => $tax, |
| 231 |
'field' => 'term_id', |
| 232 |
'terms' => $terms, |
| 233 |
'operator' => 'NOT IN', |
| 234 |
]; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Set include Terms |
| 241 |
*/ |
| 242 |
if (!empty($include_by)) { |
| 243 |
|
| 244 |
if (in_array('authors', $include_by)) { |
| 245 |
$include_users = wp_parse_id_list($posts_include_author_ids); |
| 246 |
} |
| 247 |
if (!empty($include_users)) { |
| 248 |
$args['author__in'] = $include_users; |
| 249 |
} |
| 250 |
if (in_array('terms', $include_by)) { |
| 251 |
$include_terms = wp_parse_id_list($posts_include_term_ids); |
| 252 |
} |
| 253 |
$tax_terms_map = $this->mapGroupControlQuery($include_terms); |
| 254 |
foreach ($tax_terms_map as $tax => $terms) { |
| 255 |
$terms_query[] = [ |
| 256 |
'taxonomy' => $tax, |
| 257 |
'field' => 'term_id', |
| 258 |
'terms' => $terms, |
| 259 |
'operator' => 'IN', |
| 260 |
]; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
if (!empty($terms_query)) { |
| 266 |
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Elementor widget query built from user-configured controls; expected behaviour. |
| 267 |
$args['tax_query'] = $terms_query; |
| 268 |
$args['tax_query']['relation'] = 'AND'; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
$ajaxposts = new \WP_Query($args); |
| 273 |
return $ajaxposts; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Render Ajax LoadMore |
| 278 |
*/ |
| 279 |
function render_ajax_loadmore() { |
| 280 |
$settings = $this->get_settings_for_display(); |
| 281 |
if (!$this->get_settings('ajax_loadmore_enable')) { |
| 282 |
return; |
| 283 |
} |
| 284 |
?> |
| 285 |
<div class="upk-loadmore-container"> |
| 286 |
<?php if ($settings['ajax_loadmore_btn'] == 'yes') : ?> |
| 287 |
<span class="upk-loadmore-btn"><?php esc_html_e('Load More', 'ultimate-post-kit'); ?></span> |
| 288 |
<?php elseif ($settings['ajax_loadmore_infinite_scroll'] == 'yes') : ?> |
| 289 |
<div class="upk-ajax-loading" style="display: none;"></div> |
| 290 |
<?php endif; ?> |
| 291 |
</div> |
| 292 |
<?php |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* New Image Render Method With Lazy Load Support |
| 297 |
* |
| 298 |
* @return void |
| 299 |
*/ |
| 300 |
|
| 301 |
function render_image($image_id, $size) { |
| 302 |
$placeholder_image_src = Utils::get_placeholder_image_src(); |
| 303 |
$image_src = wp_get_attachment_image_src($image_id, $size); |
| 304 |
|
| 305 |
if (!$image_src) { |
| 306 |
printf('<img class="upk-img" src="%1$s" alt="%2$s">', esc_url($placeholder_image_src), esc_attr(get_the_title())); |
| 307 |
} else { |
| 308 |
print(wp_get_attachment_image( |
| 309 |
$image_id, |
| 310 |
$size, |
| 311 |
false, |
| 312 |
[ |
| 313 |
'class' => 'upk-img', |
| 314 |
'alt' => esc_attr(get_the_title()) |
| 315 |
] |
| 316 |
)); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Old Image Render Method |
| 322 |
* Not Using Anymore |
| 323 |
* @return void |
| 324 |
*/ |
| 325 |
function __render_image($image_id, $size) { |
| 326 |
$placeholder_image_src = Utils::get_placeholder_image_src(); |
| 327 |
$image_src = wp_get_attachment_image_src($image_id, $size); |
| 328 |
if (!$image_src) { |
| 329 |
$image_src = $placeholder_image_src; |
| 330 |
} else { |
| 331 |
$image_src = $image_src[0]; |
| 332 |
} |
| 333 |
?> |
| 334 |
<img class="upk-img" src="<?php echo esc_url($image_src); ?>" alt="<?php echo esc_attr(get_the_title()); ?>"> |
| 335 |
<?php |
| 336 |
} |
| 337 |
|
| 338 |
function render_title($widget_name) { |
| 339 |
$settings = $this->get_settings_for_display(); |
| 340 |
if (!$this->get_settings('show_title')) { |
| 341 |
return; |
| 342 |
} |
| 343 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- established hook name relied on across the plugin family; renaming would break integration. |
| 344 |
apply_filters('upk/' . $widget_name . '/before/title', ''); |
| 345 |
$title = get_the_title(); |
| 346 |
printf( |
| 347 |
'<%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>', |
| 348 |
esc_attr(Utils::get_valid_html_tag($settings['title_tags'])), |
| 349 |
esc_url( get_permalink() ), |
| 350 |
esc_html( $title ), |
| 351 |
esc_attr($settings['title_style']), |
| 352 |
esc_attr( $title ) |
| 353 |
); |
| 354 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- established hook name relied on across the plugin family; renaming would break integration. |
| 355 |
apply_filters('upk/' . $widget_name . '/after/title', ''); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
function render_category() { |
| 361 |
if (!$this->get_settings('show_category')) { |
| 362 |
return; |
| 363 |
} |
| 364 |
?> |
| 365 |
<div class="upk-category"> |
| 366 |
<?php echo wp_kses_post( upk_get_category($this->get_settings('posts_source')) ); ?> |
| 367 |
</div> |
| 368 |
<?php |
| 369 |
} |
| 370 |
|
| 371 |
function render_date() { |
| 372 |
$settings = $this->get_settings_for_display(); |
| 373 |
if (!$this->get_settings('show_date')) { |
| 374 |
return; |
| 375 |
} |
| 376 |
$meta_separator = isset( $settings['meta_separator'] ) ? $settings['meta_separator'] : '.'; |
| 377 |
?> |
| 378 |
<div class="upk-date"> |
| 379 |
<?php if ($settings['human_diff_time'] == 'yes') { |
| 380 |
echo esc_html( ultimate_post_kit_post_time_diff( ($settings['human_diff_time_short'] == 'yes') ? 'short' : '' ) ); |
| 381 |
} else { |
| 382 |
echo get_the_date(); |
| 383 |
} ?> |
| 384 |
</div> |
| 385 |
|
| 386 |
<?php if ($settings['show_time']) : ?> |
| 387 |
<div class="upk-post-time" data-separator="<?php echo esc_attr( $meta_separator ); ?>"> |
| 388 |
<i class="upk-icon-clock" aria-hidden="true"></i> |
| 389 |
<?php echo esc_html( get_the_time() ); ?> |
| 390 |
</div> |
| 391 |
<?php endif; ?> |
| 392 |
<?php |
| 393 |
} |
| 394 |
|
| 395 |
function render_excerpt($excerpt_length) { |
| 396 |
$settings = $this->get_settings_for_display(); |
| 397 |
|
| 398 |
if (!$this->get_settings('show_excerpt')) { |
| 399 |
return; |
| 400 |
} |
| 401 |
$strip_shortcode = $this->get_settings_for_display('strip_shortcode'); |
| 402 |
|
| 403 |
$ellipsis = isset($settings['ellipsis']) ? $settings['ellipsis'] : '' ; |
| 404 |
?> |
| 405 |
<div class="upk-text"> |
| 406 |
<?php |
| 407 |
if (has_excerpt()) { |
| 408 |
the_excerpt(); |
| 409 |
} else { |
| 410 |
echo wp_kses_post( ultimate_post_kit_custom_excerpt($excerpt_length, $strip_shortcode, $ellipsis) ); |
| 411 |
} |
| 412 |
?> |
| 413 |
</div> |
| 414 |
<?php |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Localized comment count with label. Echo with esc_html(). |
| 419 |
* |
| 420 |
* @param int $post_id Post ID, or 0 for current post in The Loop. |
| 421 |
* @return string |
| 422 |
*/ |
| 423 |
protected function upk_get_formatted_comments_count( $post_id = 0 ) { |
| 424 |
return Utils::get_formatted_comments_count( $post_id ); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Localized comment count number only. Echo with esc_html(). |
| 429 |
* |
| 430 |
* @param int $post_id Post ID, or 0 for current post in The Loop. |
| 431 |
* @return string |
| 432 |
*/ |
| 433 |
protected function upk_get_localized_comment_count( $post_id = 0 ) { |
| 434 |
return Utils::get_localized_comment_count( $post_id ); |
| 435 |
} |
| 436 |
|
| 437 |
function render_post_format() { |
| 438 |
$settings = $this->get_settings_for_display(); |
| 439 |
|
| 440 |
if (!$settings['show_post_format']) { |
| 441 |
return; |
| 442 |
} |
| 443 |
?> |
| 444 |
<div class="upk-post-format"> |
| 445 |
<a href="<?php echo esc_url(get_permalink()) ?>"> |
| 446 |
<?php if (has_post_format('aside')) : ?> |
| 447 |
<i class="upk-icon-aside" aria-hidden="true"></i> |
| 448 |
<?php elseif (has_post_format('gallery')) : ?> |
| 449 |
<i class="upk-icon-gallery" aria-hidden="true"></i> |
| 450 |
<?php elseif (has_post_format('link')) : ?> |
| 451 |
<i class="upk-icon-link" aria-hidden="true"></i> |
| 452 |
<?php elseif (has_post_format('image')) : ?> |
| 453 |
<i class="upk-icon-image" aria-hidden="true"></i> |
| 454 |
<?php elseif (has_post_format('quote')) : ?> |
| 455 |
<i class="upk-icon-quote" aria-hidden="true"></i> |
| 456 |
<?php elseif (has_post_format('status')) : ?> |
| 457 |
<i class="upk-icon-status" aria-hidden="true"></i> |
| 458 |
<?php elseif (has_post_format('video')) : ?> |
| 459 |
<i class="upk-icon-video" aria-hidden="true"></i> |
| 460 |
<?php elseif (has_post_format('audio')) : ?> |
| 461 |
<i class="upk-icon-music" aria-hidden="true"></i> |
| 462 |
<?php elseif (has_post_format('chat')) : ?> |
| 463 |
<i class="upk-icon-chat" aria-hidden="true"></i> |
| 464 |
<?php else : ?> |
| 465 |
<i class="upk-icon-post" aria-hidden="true"></i> |
| 466 |
<?php endif; ?> |
| 467 |
</a> |
| 468 |
</div> |
| 469 |
<?php |
| 470 |
} |
| 471 |
} |
| 472 |
|