forms
5 days ago
wpr-ajax-search.php
5 days ago
wpr-filter-grid-media.php
5 days ago
wpr-form-handlers.php
5 days ago
wpr-grid-helpers.php
5 days ago
wpr-load-more-instagram-posts.php
5 days ago
wpr-load-more-tweets.php
5 days ago
wpr-post-likes.php
5 days ago
wpr-woo-grid-helpers.php
5 days ago
wpr-ajax-search.php
248 lines
| 1 | <?php |
| 2 | namespace WprAddons\Classes\Modules; |
| 3 | |
| 4 | use Elementor\Utils; |
| 5 | use Elementor\Group_Control_Image_Size; |
| 6 | use WprAddons\Classes\Utilities; |
| 7 | |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * WPR_Ajax_Search setup |
| 15 | * |
| 16 | * @since 3.4.6 |
| 17 | */ |
| 18 | |
| 19 | class WPR_Ajax_Search { |
| 20 | |
| 21 | public function __construct() { |
| 22 | add_action('wp_ajax_wpr_data_fetch' , [$this, 'data_fetch']); |
| 23 | add_action('wp_ajax_nopriv_wpr_data_fetch',[$this, 'data_fetch']); |
| 24 | } |
| 25 | |
| 26 | public function data_fetch() { |
| 27 | |
| 28 | $nonce = $_POST['nonce']; |
| 29 | |
| 30 | if ( !wp_verify_nonce( $nonce, 'wpr-addons-js' ) ) { |
| 31 | return; // Get out of here, the nonce is rotten! |
| 32 | } |
| 33 | |
| 34 | $all_post_types = []; |
| 35 | |
| 36 | if ( sanitize_text_field($_POST['wpr_show_attachments']) == 'yes' ) { |
| 37 | $all_post_types = ['attachment']; |
| 38 | } |
| 39 | |
| 40 | foreach( Utilities::get_custom_types_of( 'post', false ) as $key=>$value ) { |
| 41 | array_push($all_post_types, $key); |
| 42 | } |
| 43 | |
| 44 | $tax_query = ''; |
| 45 | |
| 46 | if ( $_POST['wpr_category'] != false && $_POST['wpr_category'] != '' ) { |
| 47 | $tax_query = array( |
| 48 | array( |
| 49 | 'taxonomy' => $_POST['wpr_option_post_type'], |
| 50 | 'field' => 'term_id', |
| 51 | 'terms' => sanitize_text_field($_POST['wpr_category']), |
| 52 | ), |
| 53 | ); |
| 54 | |
| 55 | // if ( isset($_POST['wpr_option_post_type']) && !empty($_POST['wpr_option_post_type']) ) { |
| 56 | // $tax_query = array( |
| 57 | // array( |
| 58 | // 'taxonomy' => $_POST['wpr_option_post_type'], |
| 59 | // 'field' => 'term_id', |
| 60 | // 'terms' => sanitize_text_field($_POST['wpr_category']), |
| 61 | // ), |
| 62 | // ); |
| 63 | // } else { |
| 64 | // $tax_query = array( |
| 65 | // array( |
| 66 | // 'taxonomy' => $_POST['wpr_query_type'] == 'product' ? $_POST['wpr_query_type'] . '_cat' : 'category', |
| 67 | // 'field' => 'term_id', |
| 68 | // 'terms' => sanitize_text_field($_POST['wpr_category']), |
| 69 | // ), |
| 70 | // ); |
| 71 | // } |
| 72 | } else if ( $_POST['wpr_category'] == 0 && $_POST['wpr_query_type'] != 'all' ) { |
| 73 | if ( !empty($_POST['wpr_option_post_type']) ) { |
| 74 | $tax_query = array( |
| 75 | array( |
| 76 | 'taxonomy' => $_POST['wpr_option_post_type'], |
| 77 | 'field' => 'term_id', |
| 78 | 'terms' => sanitize_text_field($_POST['wpr_category']), |
| 79 | ), |
| 80 | ); |
| 81 | } else { |
| 82 | // Get the string from the POST data |
| 83 | $taxonomy_type_string = $_POST['wpr_taxonomy_type']; |
| 84 | |
| 85 | // Check if the string contains spaces |
| 86 | if (strpos($taxonomy_type_string, ' ') !== false) { |
| 87 | // Split the string into an array based on spaces |
| 88 | $taxonomy_types = explode(' ', $taxonomy_type_string); |
| 89 | |
| 90 | |
| 91 | $tax_query = [ |
| 92 | 'relation' => 'OR' |
| 93 | ]; |
| 94 | |
| 95 | foreach( $taxonomy_types as $taxonomy_type ) { |
| 96 | array_push($tax_query, [ |
| 97 | 'taxonomy' => $taxonomy_type, |
| 98 | 'operator' => 'EXISTS' |
| 99 | ]); |
| 100 | } |
| 101 | } else { |
| 102 | // If there are no spaces, leave it as a single-item array |
| 103 | $taxonomy_types = $taxonomy_type_string; |
| 104 | |
| 105 | $tax_query = array( |
| 106 | array( |
| 107 | 'taxonomy' => $_POST['wpr_taxonomy_type'], |
| 108 | 'operator' => 'EXISTS', |
| 109 | ), |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | $keyword = sanitize_text_field( $_POST['wpr_keyword'] ); |
| 116 | $can_view_protected_posts = current_user_can('read_private_posts'); |
| 117 | $meta_query = []; |
| 118 | |
| 119 | if ( 'yes' === sanitize_text_field( $_POST['wpr_exclude_without_thumb'] ) ) { |
| 120 | $meta_query[] = [ |
| 121 | 'key' => '_thumbnail_id', |
| 122 | ]; |
| 123 | } |
| 124 | |
| 125 | if ( ( 'yes' !== sanitize_text_field($_POST['wpr_show_ps_pt'] ) ) || !$can_view_protected_posts ) { |
| 126 | $args = |
| 127 | [ |
| 128 | 'posts_per_page' => sanitize_text_field($_POST['wpr_number_of_results']), |
| 129 | 's' => sanitize_text_field( $_POST['wpr_keyword'] ), |
| 130 | 'post_type' => $_POST['wpr_query_type'] === 'all' || (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ? $all_post_types : array( sanitize_text_field($_POST['wpr_query_type']) ), |
| 131 | 'offset' => sanitize_text_field($_POST['wpr_search_results_offset']), |
| 132 | 'meta_query' => $meta_query ?: '', |
| 133 | 'tax_query' => $tax_query, |
| 134 | 'post_status' => in_array('attachment', $all_post_types) ? ['publish', 'inherit'] : 'publish', |
| 135 | 'post_password' => '' |
| 136 | ]; |
| 137 | } else { |
| 138 | $args = |
| 139 | [ |
| 140 | 'posts_per_page' => sanitize_text_field($_POST['wpr_number_of_results']), |
| 141 | 's' => sanitize_text_field( $_POST['wpr_keyword'] ), |
| 142 | 'post_type' => $_POST['wpr_query_type'] === 'all' || (!defined('WPR_ADDONS_PRO_VERSION') || !wpr_fs()->can_use_premium_code()) ? $all_post_types : array( sanitize_text_field($_POST['wpr_query_type']) ), |
| 143 | 'offset' => sanitize_text_field($_POST['wpr_search_results_offset']), |
| 144 | 'meta_query' => $meta_query ?: '', |
| 145 | 'tax_query' => $tax_query, |
| 146 | 'post_status' => in_array('attachment', $all_post_types) ? ['publish', 'inherit'] : 'publish', |
| 147 | ]; |
| 148 | } |
| 149 | |
| 150 | $the_query = new \WP_Query( $args ); |
| 151 | |
| 152 | if ( !$the_query->have_posts() && $keyword !== '' && 'yes' === sanitize_text_field( $_POST['wpr_meta_query'] ) ) { |
| 153 | $fallback_args = $args; |
| 154 | $fallback_args['s'] = ''; // disable default search |
| 155 | $fallback_args['meta_query'] = [ |
| 156 | [ |
| 157 | 'value' => $keyword, |
| 158 | 'compare' => 'LIKE' |
| 159 | ] |
| 160 | ]; |
| 161 | |
| 162 | $the_query = new \WP_Query($fallback_args); |
| 163 | } |
| 164 | |
| 165 | if( $the_query->have_posts() ) : |
| 166 | $number_of_queried_posts = $the_query->found_posts; |
| 167 | $post_count = 0; |
| 168 | |
| 169 | while( $the_query->have_posts() ) : $the_query->the_post(); |
| 170 | |
| 171 | // if ( ( !has_post_thumbnail() && 'yes' === sanitize_text_field($_POST['wpr_exclude_without_thumb'])) ) : |
| 172 | // continue; |
| 173 | // endif; |
| 174 | |
| 175 | ob_start(); |
| 176 | // the_post_thumbnail(sanitize_text_field($_POST['ajax_search_img_size'])); |
| 177 | the_post_thumbnail('medium'); |
| 178 | $post_thumb = ob_get_clean(); |
| 179 | ?> |
| 180 | |
| 181 | <li data-number-of-results="<?php echo esc_attr( (string) $the_query->found_posts ); ?>"> |
| 182 | |
| 183 | <?php if ( !post_password_required() || $can_view_protected_posts ) : ?> |
| 184 | |
| 185 | <?php if ( 'yes' === sanitize_text_field($_POST['wpr_show_ajax_thumbnail']) ) : |
| 186 | if ( has_post_thumbnail() ) : |
| 187 | echo '<a class="wpr-ajax-img-wrap" target="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ) . '" href="' . esc_url( get_the_permalink() ) . '">' . wp_kses_post( $post_thumb ) . '</a>'; |
| 188 | // echo '<a class="wpr-ajax-img-wrap" target="'. sanitize_text_field($_POST['ajax_search_link_target']) .'" href="'. esc_url( get_the_permalink() ) .'">'. '<img src="'. Group_Control_Image_Size::get_attachment_image_src( get_post_thumbnail_id(), 'ajax_search_image', [$_POST['ajax_search_image_size']] ) .'"/>' .'</a>'; |
| 189 | else : |
| 190 | echo '<a class="wpr-ajax-img-wrap" target="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ) . '" href="' . esc_url( get_the_permalink() ) . '"><img src="' . esc_url( Utils::get_placeholder_image_src() ) . '" alt=""></a>'; |
| 191 | endif ; |
| 192 | endif ; ?> |
| 193 | |
| 194 | <div class="wpr-ajax-search-content"> |
| 195 | <a target="<?php echo esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ); ?>" class="wpr-ajax-title" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php the_title(); ?></a> |
| 196 | |
| 197 | <?php if ( sanitize_text_field($_POST['wpr_show_description']) == 'yes' ) : ?> |
| 198 | <p class="wpr-ajax-desc"><a target="<?php echo esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ); ?>" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( wp_trim_words( get_the_content(), (int) sanitize_text_field( wp_unslash( $_POST['wpr_number_of_words'] ) ) ) ); ?></a></p> |
| 199 | <?php endif; ?> |
| 200 | |
| 201 | <?php if ( 'yes' === sanitize_text_field($_POST['wpr_show_product_price']) && |
| 202 | get_post_type() === 'product' && |
| 203 | class_exists('WooCommerce') ) : |
| 204 | $product = wc_get_product(get_the_ID()); |
| 205 | if ($product) { |
| 206 | $price_html = '<div class="wpr-search-product-price">'. $product->get_price_html() .'</div>'; |
| 207 | |
| 208 | echo wp_kses_post( $price_html ); |
| 209 | } |
| 210 | endif; ?> |
| 211 | |
| 212 | <?php if ( sanitize_text_field($_POST['wpr_show_view_result_btn']) ) : ?> |
| 213 | <a target="<?php echo esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ); ?>" class="wpr-view-result" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo esc_html( sanitize_text_field( wp_unslash( $_POST['wpr_view_result_text'] ) ) ); ?></a> |
| 214 | <?php endif; ?> |
| 215 | </div> |
| 216 | |
| 217 | <?php else: ?> |
| 218 | |
| 219 | <?php if ( 'yes' === sanitize_text_field($_POST['wpr_show_ajax_thumbnail']) ) : |
| 220 | echo '<a class="wpr-ajax-img-wrap" target="' . esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ) . '" href="' . esc_url( get_the_permalink() ) . '"><img src="' . esc_url( Utils::get_placeholder_image_src() ) . '" alt=""></a>'; |
| 221 | endif ; ?> |
| 222 | |
| 223 | <div class="wpr-ajax-search-content"> |
| 224 | <a target="<?php echo esc_attr( sanitize_text_field( wp_unslash( $_POST['wpr_ajax_search_link_target'] ) ) ); ?>" class="wpr-ajax-title" href="<?php echo esc_url( get_the_permalink() ); ?>"><?php the_title(); ?></a> |
| 225 | </div> |
| 226 | |
| 227 | <?php endif; ?> |
| 228 | |
| 229 | </li> |
| 230 | <?php |
| 231 | $post_count++; |
| 232 | endwhile; |
| 233 | |
| 234 | wp_reset_postdata(); |
| 235 | |
| 236 | else : |
| 237 | if (0 < sanitize_text_field($_POST['wpr_search_results_offset'])) { |
| 238 | } else { |
| 239 | echo '<p class="wpr-no-results">' . esc_html( sanitize_text_field( wp_unslash( $_POST['wpr_no_results'] ) ) ) . '</p>'; |
| 240 | } |
| 241 | |
| 242 | endif; |
| 243 | |
| 244 | die(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | new WPR_Ajax_Search(); |