PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.3.8
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.3.8
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
← All changes | includes/class-wpwbot-search.php +279 -279 8.6.28.3.8 View file →
@@ -1,280 +1,280 @@
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(wp_unslash($_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 -
198 - $sql = $wpdb->prepare("SELECT
199 - distinct ID,
200 - {$query['relevance']} as relevance
201 - FROM
202 - {$table_name}
203 - WHERE
204 - type = 'product'
205 - {$query['source']}
206 - {$query['search']}
207 - {$query['stock']}
208 - {$query['visibility']}
209 - {$query['lang']}
210 - GROUP BY ID
211 - ORDER BY
212 - relevance DESC
213 - LIMIT 0, %d
214 - ", $results_num);
215 -
216 - //SQL was processed through $wpdb->prepare() in many steps.
217 - $posts_ids = $this->get_posts_ids( $sql ); //DB Call OK, No Caching OK
218 -
219 - return $posts_ids;
220 -
221 - }
222 - /*
223 - * Get array of included to search result posts ids
224 - */
225 - private function get_posts_ids( $sql ) {
226 -
227 - global $wpdb;
228 -
229 - $posts_ids = array();
230 -
231 - /**Passed SQL was processed using $wpdb->prepare() in line 198 and above*/
232 - // phpcs:ignore
233 - $search_results = $wpdb->get_results( $sql ); //DB Call OK, No Caching OK
234 -
235 - if ( !empty( $search_results ) && !is_wp_error( $search_results ) && is_array( $search_results ) ) {
236 - foreach ( $search_results as $search_result ) {
237 - $post_id = intval( $search_result->ID );
238 - if ( ! in_array( $post_id, $posts_ids ) ) {
239 - $posts_ids[] = $post_id;
240 - }
241 - }
242 - }
243 -
244 - unset( $search_results );
245 -
246 - return $posts_ids;
247 -
248 - }
249 - /**
250 - * @param $posts_ids
251 - * @return array
252 - */
253 - public function get_load_more_products($posts_ids){
254 - return $this->get_products($posts_ids);
255 - }
256 - /*
257 - * Get products info
258 - */
259 - private function get_products( $posts_ids ) {
260 - $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
261 - $products_array = array();
262 - $more_product_ids=array();
263 - $products_num =count( $posts_ids );
264 - if ( $products_num > 0 ) {
265 - //if result products ids are more than per_page then keep remaing ids as load more option.
266 - if($products_num >$product_per_page){
267 - $more_product_ids=array_slice($posts_ids,$product_per_page);
268 - $current_product_ids=array_slice($posts_ids,0,$product_per_page);
269 - }else{
270 - $current_product_ids=$posts_ids;
271 - }
272 - foreach ( $current_product_ids as $post_id ) {
273 - $product = wc_get_product( $post_id );
274 - $products_array[] = $product;
275 - }
276 - }
277 - return array('products'=>$products_array,'more_ids'=>$more_product_ids,'total_products'=>$products_num);
278 - }
279 - }
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(wp_unslash($_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 +
198 + $sql = $wpdb->prepare("SELECT
199 + distinct ID,
200 + {$query['relevance']} as relevance
201 + FROM
202 + {$table_name}
203 + WHERE
204 + type = 'product'
205 + {$query['source']}
206 + {$query['search']}
207 + {$query['stock']}
208 + {$query['visibility']}
209 + {$query['lang']}
210 + GROUP BY ID
211 + ORDER BY
212 + relevance DESC
213 + LIMIT 0, %d
214 + ", $results_num);
215 +
216 + //SQL was processed through $wpdb->prepare() in many steps.
217 + $posts_ids = $this->get_posts_ids( $sql ); //DB Call OK, No Caching OK
218 +
219 + return $posts_ids;
220 +
221 + }
222 + /*
223 + * Get array of included to search result posts ids
224 + */
225 + private function get_posts_ids( $sql ) {
226 +
227 + global $wpdb;
228 +
229 + $posts_ids = array();
230 +
231 + /**Passed SQL was processed using $wpdb->prepare() in line 198 and above*/
232 + // phpcs:ignore
233 + $search_results = $wpdb->get_results( $sql ); //DB Call OK, No Caching OK
234 +
235 + if ( !empty( $search_results ) && !is_wp_error( $search_results ) && is_array( $search_results ) ) {
236 + foreach ( $search_results as $search_result ) {
237 + $post_id = intval( $search_result->ID );
238 + if ( ! in_array( $post_id, $posts_ids ) ) {
239 + $posts_ids[] = $post_id;
240 + }
241 + }
242 + }
243 +
244 + unset( $search_results );
245 +
246 + return $posts_ids;
247 +
248 + }
249 + /**
250 + * @param $posts_ids
251 + * @return array
252 + */
253 + public function get_load_more_products($posts_ids){
254 + return $this->get_products($posts_ids);
255 + }
256 + /*
257 + * Get products info
258 + */
259 + private function get_products( $posts_ids ) {
260 + $product_per_page = get_option('qlcd_wp_chatbot_ppp') != '' ? get_option('qlcd_wp_chatbot_ppp') : 10;
261 + $products_array = array();
262 + $more_product_ids=array();
263 + $products_num =count( $posts_ids );
264 + if ( $products_num > 0 ) {
265 + //if result products ids are more than per_page then keep remaing ids as load more option.
266 + if($products_num >$product_per_page){
267 + $more_product_ids=array_slice($posts_ids,$product_per_page);
268 + $current_product_ids=array_slice($posts_ids,0,$product_per_page);
269 + }else{
270 + $current_product_ids=$posts_ids;
271 + }
272 + foreach ( $current_product_ids as $post_id ) {
273 + $product = wc_get_product( $post_id );
274 + $products_array[] = $product;
275 + }
276 + }
277 + return array('products'=>$products_array,'more_ids'=>$more_product_ids,'total_products'=>$products_num);
278 + }
279 + }
280 280 endif;