| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons; |
| 4 |
|
| 5 |
use Elementor\Utils; |
| 6 |
use WP_Query; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
class Search_Ajax |
| 13 |
{ |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
add_action('wp_ajax_king_addons_data_fetch', [$this, 'data_fetch']); |
| 18 |
add_action('wp_ajax_nopriv_king_addons_data_fetch', [$this, 'data_fetch']); |
| 19 |
} |
| 20 |
|
| 21 |
public function data_fetch() |
| 22 |
{ |
| 23 |
|
| 24 |
$nonce = $_POST['nonce']; |
| 25 |
|
| 26 |
if (!wp_verify_nonce($nonce, 'king_addons_search_nonce')) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
$all_post_types = []; |
| 31 |
foreach (Core::getCustomTypes('post', false) as $key => $value) { |
| 32 |
$all_post_types[] = $key; |
| 33 |
} |
| 34 |
|
| 35 |
$tax_query = ''; |
| 36 |
|
| 37 |
if (isset($_POST['king_addons_category']) && $_POST['king_addons_category'] != '') { |
| 38 |
$tax_query = array( |
| 39 |
array( |
| 40 |
'taxonomy' => sanitize_text_field($_POST['king_addons_option_post_type'] ?? ''), |
| 41 |
'field' => 'term_id', |
| 42 |
'terms' => sanitize_text_field($_POST['king_addons_category']), |
| 43 |
), |
| 44 |
); |
| 45 |
} else if (isset($_POST['king_addons_category']) && $_POST['king_addons_category'] == 0 && isset($_POST['king_addons_query_type']) && $_POST['king_addons_query_type'] != 'all') { |
| 46 |
if (!empty($_POST['king_addons_option_post_type'])) { |
| 47 |
$tax_query = array( |
| 48 |
array( |
| 49 |
'taxonomy' => sanitize_text_field($_POST['king_addons_option_post_type']), |
| 50 |
'field' => 'term_id', |
| 51 |
'terms' => sanitize_text_field($_POST['king_addons_category']), |
| 52 |
), |
| 53 |
); |
| 54 |
} else { |
| 55 |
$taxonomy_type_string = sanitize_text_field($_POST['king_addons_taxonomy_type'] ?? ''); |
| 56 |
|
| 57 |
if (strpos($taxonomy_type_string, ' ') !== false) { |
| 58 |
$taxonomy_types = explode(' ', $taxonomy_type_string); |
| 59 |
$tax_query = [ |
| 60 |
'relation' => 'OR' |
| 61 |
]; |
| 62 |
|
| 63 |
foreach ($taxonomy_types as $taxonomy_type) { |
| 64 |
$tax_query[] = [ |
| 65 |
'taxonomy' => $taxonomy_type, |
| 66 |
'operator' => 'EXISTS' |
| 67 |
]; |
| 68 |
} |
| 69 |
} else { |
| 70 |
$tax_query = array( |
| 71 |
array( |
| 72 |
'taxonomy' => sanitize_text_field($_POST['king_addons_taxonomy_type'] ?? ''), |
| 73 |
'operator' => 'EXISTS', |
| 74 |
), |
| 75 |
); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ((isset($_POST['king_addons_category']) && $_POST['king_addons_category'] == 0) || (isset($_POST['king_addons_query_type']) && $_POST['king_addons_query_type'] === 'all')) { |
| 81 |
$tax_query = []; |
| 82 |
} |
| 83 |
|
| 84 |
$can_view_protected_posts = current_user_can('read_private_posts'); |
| 85 |
|
| 86 |
$query_args = [ |
| 87 |
'posts_per_page' => sanitize_text_field($_POST['king_addons_number_of_results']), |
| 88 |
's' => sanitize_text_field($_POST['king_addons_keyword']), |
| 89 |
'post_type' => $_POST['king_addons_query_type'] === 'all' || !king_addons_freemius()->can_use_premium_code__premium_only() ? $all_post_types : [sanitize_text_field($_POST['king_addons_query_type'])], |
| 90 |
'offset' => sanitize_text_field($_POST['king_addons_search_results_offset']), |
| 91 |
'meta_query' => 'yes' === sanitize_text_field($_POST['king_addons_exclude_without_thumb']) ? [['key' => '_thumbnail_id']] : '', |
| 92 |
'tax_query' => $tax_query, |
| 93 |
'post_status' => 'publish', |
| 94 |
]; |
| 95 |
|
| 96 |
if (!$can_view_protected_posts || 'yes' !== sanitize_text_field($_POST['king_addons_show_ps_pt'])) { |
| 97 |
$query_args['post_password'] = ''; |
| 98 |
} |
| 99 |
|
| 100 |
$the_query = new WP_Query($query_args); |
| 101 |
|
| 102 |
if ($the_query->have_posts()) : |
| 103 |
while ($the_query->have_posts()) : $the_query->the_post(); |
| 104 |
|
| 105 |
$post_thumb = has_post_thumbnail() ? get_the_post_thumbnail(get_the_ID(), 'medium') : '<img src="' . Utils::get_placeholder_image_src() . '">'; |
| 106 |
$post_url = esc_url(get_the_permalink()); |
| 107 |
$target = esc_attr($_POST['king_addons_ajax_search_link_target']); |
| 108 |
$show_thumbnail = ('yes' === sanitize_text_field($_POST['king_addons_show_ajax_thumbnail'])); |
| 109 |
$can_show_content = (!post_password_required() || $can_view_protected_posts); |
| 110 |
|
| 111 |
?> |
| 112 |
<li data-number-of-results="<?php echo $the_query->found_posts; ?>"> |
| 113 |
<?php if ($show_thumbnail) : ?> |
| 114 |
<a class="king-addons-ajax-img-wrap" target="<?php echo $target; ?>" |
| 115 |
href="<?php echo $post_url; ?>"> |
| 116 |
<?php echo $post_thumb; ?> |
| 117 |
</a> |
| 118 |
<?php endif; ?> |
| 119 |
<div class="king-addons-ajax-search-content"> |
| 120 |
<a target="<?php echo $target; ?>" class="king-addons-ajax-title" |
| 121 |
href="<?php echo $post_url; ?>"><?php the_title(); ?></a> |
| 122 |
<?php if ($can_show_content && 'yes' === sanitize_text_field($_POST['king_addons_show_description'])) : ?> |
| 123 |
<p class="king-addons-ajax-desc"> |
| 124 |
<a target="<?php echo $target; ?>" href="<?php echo $post_url; ?>"> |
| 125 |
<?php echo wp_trim_words(wp_kses_post(get_the_content()), sanitize_text_field($_POST['king_addons_number_of_words'])); ?> |
| 126 |
</a> |
| 127 |
</p> |
| 128 |
<?php endif; ?> |
| 129 |
<?php if ($can_show_content && sanitize_text_field($_POST['king_addons_show_view_result_btn'] ?? '')) : ?> |
| 130 |
<a target="<?php echo $target; ?>" class="king-addons-view-result" |
| 131 |
href="<?php echo $post_url; ?>"> |
| 132 |
<?php echo esc_html(sanitize_text_field($_POST['king_addons_view_result_text'] ?? '')); ?> |
| 133 |
</a> |
| 134 |
<?php endif; ?> |
| 135 |
</div> |
| 136 |
</li> |
| 137 |
<?php |
| 138 |
endwhile; |
| 139 |
|
| 140 |
wp_reset_postdata(); |
| 141 |
|
| 142 |
else : |
| 143 |
if (sanitize_text_field($_POST['king_addons_search_results_offset'] ?? '0') <= 0) { |
| 144 |
echo '<p class="king-addons-no-results">' . esc_html(sanitize_text_field($_POST['king_addons_no_results'] ?? '')) . '</p>'; |
| 145 |
} |
| 146 |
endif; |
| 147 |
|
| 148 |
die(); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
new Search_Ajax(); |