PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 3.4.2
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v3.4.2
8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 8.5.4 8.5.3 8.5.2 All 533 releases
chatbot / includes / class-wpwbot-search.php

class-wpwbot-search.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 3.4.2, at includes/class-wpwbot-search.php

266 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 if ( ! class_exists( 'wpwBot_Search' ) ) :
6 /**
7 * Class for plugin search
8 */
9 class wpwBot_Search {
10 /**
11 * @var AWS_Search Array of all plugin data $data
12 */
13 private $data = array();
14 /**
15 * Return a singleton instance of the current class
16 *
17 * @return object
18 */
19 public static function factory() {
20 static $instance = false;
21 if ( ! $instance ) {
22 $instance = new self();
23 }
24 return $instance;
25 }
26 /**
27 * Constructor
28 */
29 public function __construct() {}
30 /*
31 * Search
32 */
33 public function search( $keyword = '' ) {
34 global $wpdb;
35 $special_chars = $this->get_special_chars();
36 $s = $keyword ? esc_attr( $keyword ) : esc_attr( $_POST['keyword'] );
37 $s = stripslashes( $s );
38 $s = str_replace( array( "\r", "\n" ), '', $s );
39 $s = str_replace( $special_chars, '', $s );
40 $show_cats = 'true';
41 $show_tags = 'true';
42 $search_in = 'true';
43 $outofstock = 'true';
44 $search_in_arr = explode( ',', 'title,content,category,excerpt,tag,sku');
45 // Search in title if all options is disabled
46 if ( ! $search_in ) {
47 $search_in_arr = array( 'title' );
48 }
49 $categories_array = array();
50 $tags_array = array();
51 $this->data['s'] = $s;
52 $this->data['results_num'] = 50;//This static before setting option on wpwbot admin panel.
53 $this->data['search_terms'] = array();
54 $this->data['search_terms'] = array_unique( explode( ' ', $s ) );
55 $this->data['search_in'] = $search_in_arr;
56 $this->data['outofstock'] = $outofstock;
57 $posts_ids = $this->query_index_table();
58 $products_array = $this->get_products( $posts_ids );
59 return $products_array;
60 }
61 /*
62 * Get special characters that must be striped
63 */
64 public function get_special_chars() {
65 $chars = array(
66 '-',
67 '_',
68 '|',
69 '+',
70 '`',
71 '~',
72 '!',
73 '@',
74 '#',
75 '$',
76 '%',
77 '^',
78 '&',
79 '*',
80 '(',
81 ')',
82 '\\',
83 '?',
84 ';',
85 ':',
86 "'",
87 '"',
88 ".",
89 ",",
90 "<",
91 ">",
92 "{",
93 "}",
94 "/",
95 "[",
96 "]",
97 );
98 return apply_filters( 'aws_special_chars', $chars );
99 }
100 /*
101 * Query in index table
102 */
103 private function query_index_table() {
104 global $wpdb;
105 $table_name = $wpdb->prefix . QCLD_wpCHATBOT_INDEX_TABLE;
106 $search_in_arr = $this->data['search_in'];
107 $results_num = $this->data['results_num'];
108 $outofstock = $this->data['outofstock'];
109 $reindex_version = get_option( 'aws_reindex_version' );
110 $query = array();
111 $query['search'] = '';
112 $query['source'] = '';
113 $query['relevance'] = '';
114 $query['stock'] = '';
115 $query['visibility'] = '';
116 $query['lang'] = '';
117 $search_array = array();
118 $source_array = array();
119 $relevance_array = array();
120 $new_relevance_array = array();
121 foreach ( $this->data['search_terms'] as $search_term ) {
122 $search_term_len = strlen( $search_term );
123 $relevance_title = 200 + 20 * $search_term_len;
124 $relevance_content = 35 + 4 * $search_term_len;
125 $relevance_title_like = 40 + 2 * $search_term_len;
126 $relevance_content_like = 35 + 1 * $search_term_len;
127 $search_term_like = preg_replace( '/(s|es|ies)$/i', '', $search_term );
128 $like = '%' . $wpdb->esc_like( $search_term_like ) . '%';
129 if ( $search_term_len > 1 ) {
130 $search_array[] = $wpdb->prepare( '( term LIKE %s )', $like );
131 } else {
132 $search_array[] = $wpdb->prepare( '( term = "%s" )', $search_term );
133 }
134 foreach ( $search_in_arr as $search_in_term ) {
135 switch ( $search_in_term ) {
136 case 'title':
137 $relevance_array['title'][] = $wpdb->prepare( "( case when ( term_source = 'title' AND term = '%s' ) then {$relevance_title} * count else 0 end )", $search_term );
138 $relevance_array['title'][] = $wpdb->prepare( "( case when ( term_source = 'title' AND term LIKE %s ) then {$relevance_title_like} * count else 0 end )", $like );
139 break;
140 case 'content':
141 $relevance_array['content'][] = $wpdb->prepare( "( case when ( term_source = 'content' AND term = '%s' ) then {$relevance_content} * count else 0 end )", $search_term );
142 $relevance_array['content'][] = $wpdb->prepare( "( case when ( term_source = 'content' AND term LIKE %s ) then {$relevance_content_like} * count else 0 end )", $like );
143 break;
144 case 'excerpt':
145 $relevance_array['content'][] = $wpdb->prepare( "( case when ( term_source = 'excerpt' AND term = '%s' ) then {$relevance_content} * count else 0 end )", $search_term );
146 $relevance_array['content'][] = $wpdb->prepare( "( case when ( term_source = 'excerpt' AND term LIKE %s ) then {$relevance_content_like} * count else 0 end )", $like );
147 break;
148 case 'category':
149 $relevance_array['category'][] = $wpdb->prepare( "( case when ( term_source = 'category' AND term = '%s' ) then 35 else 0 end )", $search_term );
150 $relevance_array['category'][] = $wpdb->prepare( "( case when ( term_source = 'category' AND term LIKE %s ) then 5 else 0 end )", $like );
151 break;
152 case 'tag':
153 $relevance_array['tag'][] = $wpdb->prepare( "( case when ( term_source = 'tag' AND term = '%s' ) then 35 else 0 end )", $search_term );
154 $relevance_array['tag'][] = $wpdb->prepare( "( case when ( term_source = 'tag' AND term LIKE %s ) then 5 else 0 end )", $like );
155 break;
156 case 'sku':
157 $relevance_array['sku'][] = $wpdb->prepare( "( case when ( term_source = 'sku' AND term = '%s' ) then 300 else 0 end )", $search_term );
158 $relevance_array['sku'][] = $wpdb->prepare( "( case when ( term_source = 'sku' AND term LIKE %s ) then 50 else 0 end )", $like );
159 break;
160 }
161 }
162 }
163 // Sort 'relevance' queries in the array by search priority
164 foreach ( $search_in_arr as $search_in_item ) {
165 if ( isset( $relevance_array[$search_in_item] ) ) {
166 $new_relevance_array[$search_in_item] = implode( ' + ', $relevance_array[$search_in_item] );
167 }
168 }
169 foreach ( $search_in_arr as $search_in_term ) {
170 $source_array[] = "term_source = '{$search_in_term}'";
171 }
172 $query['relevance'] .= sprintf( ' (SUM( %s )) ', implode( ' + ', $new_relevance_array ) );
173 $query['search'] .= sprintf( ' AND ( %s )', implode( ' OR ', $search_array ) );
174 $query['source'] .= sprintf( ' AND ( %s )', implode( ' OR ', $source_array ) );
175 if ( $reindex_version && version_compare( $reindex_version, '1.16', '>=' ) ) {
176 if ( $outofstock !== 'true' ) {
177 $query['stock'] .= " AND in_stock = 1";
178 }
179 $query['visibility'] .= " AND NOT visibility LIKE '%hidden%'";
180 }
181 if ( ( defined( 'ICL_SITEPRESS_VERSION' ) || function_exists( 'pll_current_language' ) ) && $reindex_version && version_compare( $reindex_version, '1.20', '>=' ) ) {
182 $current_lang = false;
183 if ( has_filter('wpml_current_language') ) {
184 $current_lang = apply_filters( 'wpml_current_language', NULL );
185 } elseif ( function_exists( 'pll_current_language' ) ) {
186 $current_lang = pll_current_language();
187 }
188 if ( $current_lang ) {
189 $query['lang'] .= $wpdb->prepare( " AND ( lang LIKE %s OR lang = '' )", $current_lang );
190 }
191 } elseif( function_exists( 'qtranxf_getLanguage' ) ) {
192 $current_lang = qtranxf_getLanguage();
193 if ( $current_lang ) {
194 $query['lang'] .= $wpdb->prepare( " AND ( lang LIKE %s OR lang = '' )", $current_lang );
195 }
196 }
197 $sql = "SELECT
198 distinct ID,
199 {$query['relevance']} as relevance
200 FROM
201 {$table_name}
202 WHERE
203 type = 'product'
204 {$query['source']}
205 {$query['search']}
206 {$query['stock']}
207 {$query['visibility']}
208 {$query['lang']}
209 GROUP BY ID
210 ORDER BY
211 relevance DESC
212 LIMIT 0, {$results_num}
213 ";
214 $posts_ids = $this->get_posts_ids( $sql );
215 return $posts_ids;
216 }
217 /*
218 * Get array of included to search result posts ids
219 */
220 private function get_posts_ids( $sql ) {
221 global $wpdb;
222 $posts_ids = array();
223 $search_results = $wpdb->get_results( $sql );
224 if ( !empty( $search_results ) && !is_wp_error( $search_results ) && is_array( $search_results ) ) {
225 foreach ( $search_results as $search_result ) {
226 $post_id = intval( $search_result->ID );
227 if ( ! in_array( $post_id, $posts_ids ) ) {
228 $posts_ids[] = $post_id;
229 }
230 }
231 }
232 unset( $search_results );
233 return $posts_ids;
234 }
235 /**
236 * @param $posts_ids
237 * @return array
238 */
239 public function get_load_more_products($posts_ids){
240 return $this->get_products($posts_ids);
241 }
242 /*
243 * Get products info
244 */
245 private function get_products( $posts_ids ) {
246 $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
247 $products_array = array();
248 $more_product_ids=array();
249 $products_num =count( $posts_ids );
250 if ( $products_num > 0 ) {
251 //if result products ids are more than per_page then keep remaing ids as load more option.
252 if($products_num >$product_per_page){
253 $more_product_ids=array_slice($posts_ids,$product_per_page);
254 $current_product_ids=array_slice($posts_ids,0,$product_per_page);
255 }else{
256 $current_product_ids=$posts_ids;
257 }
258 foreach ( $current_product_ids as $post_id ) {
259 $product = wc_get_product( $post_id );
260 $products_array[] = $product;
261 }
262 }
263 return array('products'=>$products_array,'more_ids'=>$more_product_ids,'total_products'=>$products_num);
264 }
265 }
266 endif;