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
chatbot / includes / class-wpwbot-table.php

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

455 lines 16.8 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_Table' ) ) :
6 /**
7 * Class for plugin index table
8 */
9 class wpwBot_Table {
10 /**
11 * @var wpwBot_Table Index table name
12 */
13 private $table_name;
14 /**
15 * Constructor
16 */
17 public function __construct() {
18 global $wpdb;
19 $this->table_name = $wpdb->prefix . QCLD_wpCHATBOT_INDEX_TABLE;
20 add_action( 'wp_insert_post', array( $this, 'update_table' ), 10, 3 );
21
22 if ( defined('wpCOMMERCE_VERSION') ) {
23 if ( version_compare( wpCOMMERCE_VERSION, '3.0', ">=" ) ) {
24 add_action( 'wpcommerce_variable_product_sync_data', array( $this, 'variable_product_changed' ) );
25 } else {
26 add_action( 'wpcommerce_variable_product_sync', array( $this, 'variable_product_changed' ), 10, 2 );
27 }
28 }
29
30 add_action( 'wp_ajax_qcld-wp-chabot-reindex', array( $this, 'reindex_table' ) );
31 add_action( 'wp_ajax_qcld-wp-chabot-cancel-index', array( $this, 'cancel_reindex' ) );
32 }
33 /*
34 * Reindex plugin table
35 */
36 public function reindex_table( $return) {
37 $nonce = sanitize_text_field(wp_unslash($_POST['nonce']));
38 if (! wp_verify_nonce($nonce,'wp_chatbot') || ! current_user_can('manage_options')) {
39 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'chatbot')));
40 wp_die();
41
42 }else{
43 global $wpdb;
44 $index_meta = get_option( 'wp_chatbot_index_meta', false );
45 $status = false;
46 // No current index going on. Let's start over
47 if ( false === $index_meta ) {
48 $status = 'start';
49 $index_meta = array(
50 'offset' => 0,
51 'start' => true,
52 );
53
54 // phpcs:ignore
55 $wpdb->query("DROP TABLE IF EXISTS {$this->table_name}"); //DB Call OK
56
57 $this->create_table();
58
59 $index_meta['found_posts'] = $this->get_number_of_products();
60 } else if ( ! empty( $index_meta['site_stack'] ) && $index_meta['offset'] >= $index_meta['found_posts'] ) {
61 $status = 'start';
62 $index_meta['start'] = true;
63 $index_meta['offset'] = 0;
64 $index_meta['current_site'] = array_shift( $index_meta['site_stack'] );
65 } else {
66 $index_meta['start'] = false;
67 }
68 $index_meta = apply_filters( 'wp_chatbot_index_meta', $index_meta );
69 $posts_per_page = apply_filters( 'wp_chatbot_index_posts_per_page', 30 );
70 $args = array(
71 'posts_per_page' => $posts_per_page,
72 'fields' => 'ids',
73 'post_type' => 'product',
74 'post_status' => 'publish',
75 'offset' => $index_meta['offset'],
76 'ignore_sticky_posts' => true,
77 'suppress_filters' => true,
78 'no_found_rows' => 1,
79 'orderby' => 'ID',
80 'order' => 'DESC',
81 );
82 $posts = get_posts( $args );
83 if ( $status !== 'start' ) {
84 if ( $posts && count( $posts ) > 0 ) {
85 $queued_posts = array();
86 foreach( $posts as $post_id ) {
87 $queued_posts[] = absint( $post_id );
88 }
89 $this->fill_table( $queued_posts );
90 $index_meta['offset'] = absint( $index_meta['offset'] + $posts_per_page );
91 if ( $index_meta['offset'] >= $index_meta['found_posts'] ) {
92 $index_meta['offset'] = $index_meta['found_posts'];
93 }
94 update_option( 'wp_chatbot_index_meta', $index_meta );
95 } else {
96 // We are done (with this site)
97 $index_meta['offset'] = (int) count( $posts );
98 delete_option( 'wp_chatbot_index_meta' );
99 update_option( 'wp_chatbot_index_count', 1 );
100 }
101 } else {
102 update_option( 'wp_chatbot_index_meta', $index_meta );
103 }
104 if ( $return ) {
105 return $index_meta;
106 } else {
107 wp_send_json_success( $index_meta );
108 }
109 }
110 }
111 /*
112 * Get total number of products
113 */
114 private function get_number_of_products() {
115 $args = array(
116 'posts_per_page' => -1,
117 'fields' => 'ids',
118 'post_type' => 'product',
119 'post_status' => 'publish',
120 'ignore_sticky_posts' => true,
121 'suppress_filters' => true,
122 'no_found_rows' => 1,
123 'orderby' => 'ID',
124 'order' => 'DESC',
125 );
126 $posts = get_posts( $args );
127 if ( $posts && count( $posts ) > 0 ) {
128 $count = count( $posts );
129 } else {
130 $count = 0;
131 }
132 return $count;
133 }
134 /*
135 * Check if index table exist
136 */
137 private function is_table_not_exist() {
138 global $wpdb;
139 return ( $wpdb->get_var( "SHOW TABLES LIKE '{$this->table_name}'" ) != $this->table_name ); //DB Call OK, No Caching OK
140 }
141 /*
142 * Create index table
143 */
144 private function create_table() {
145 global $wpdb;
146 $charset_collate = $wpdb->get_charset_collate();
147 $sql = "CREATE TABLE {$this->table_name} (
148 id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
149 term VARCHAR(50) NOT NULL DEFAULT 0,
150 term_source VARCHAR(20) NOT NULL DEFAULT 0,
151 type VARCHAR(50) NOT NULL DEFAULT 0,
152 count BIGINT(20) UNSIGNED NOT NULL DEFAULT 0,
153 in_stock INT(11) NOT NULL DEFAULT 0,
154 visibility VARCHAR(20) NOT NULL DEFAULT 0,
155 lang VARCHAR(20) NOT NULL DEFAULT 0
156 ) $charset_collate;";
157 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
158 dbDelta( $sql );
159
160 $table_report = $wpdb->prefix . 'wpbot_chat_report';
161
162 if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_report'" ) != $table_report ) {
163 $sql_report = "
164 CREATE TABLE `$table_report` (
165 `id` int(11) NOT NULL AUTO_INCREMENT,
166 `user_id` int(11) NOT NULL,
167 `conversation_id` int(11) DEFAULT NULL,
168 `message` longtext NOT NULL,
169 `feedback` varchar(20) DEFAULT NULL,
170 `meta_info` text DEFAULT NULL,
171 `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
172 PRIMARY KEY (`id`)
173 ) $charset_collate AUTO_INCREMENT=1;
174 ";
175
176 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
177 dbDelta( $sql_report );
178 }
179 }
180 /*
181 * Insert data into the index table
182 */
183 private function fill_table( $posts ) {
184
185 }
186 /*
187 * Scrap all product data and insert to table
188 */
189 private function insert_into_table( $data ) {
190 global $wpdb;
191 $values = array();
192 foreach( $data['terms'] as $source => $all_terms ) {
193 foreach ( $all_terms as $term => $count ) {
194 if ( ! $term ) {
195 continue;
196 }
197 $value = $wpdb->prepare(
198 "(%d, %s, %s, %s, %d, %d, %s, %s)",
199 $data['id'], $term, $source, 'product', $count, $data['in_stock'], $data['visibility'], $data['lang']
200 );
201 $values[] = $value;
202 }
203 }
204 if ( count( $values ) > 0 ) {
205 $values = implode( ', ', $values );
206 $query = "INSERT IGNORE INTO {$this->table_name}
207 (`id`, `term`, `term_source`, `type`, `count`, `in_stock`, `visibility`, `lang`)
208 VALUES $values
209 ";
210 $wpdb->query( $query ); //DB Call OK, No Caching OK
211 }
212 }
213 /*
214 * Update index table
215 */
216 public function update_table( $post_id, $post, $update ) {
217 global $wpdb;
218 if ( $this->is_table_not_exist() ) {
219 $this->create_table();
220 }
221 $slug = 'product';
222 if ( $slug != $post->post_type ) {
223 return;
224 }
225 $wpdb->delete( $this->table_name, array( 'id' => $post_id ) ); //DB Call OK, No Caching OK
226 $posts = get_posts( array(
227 'posts_per_page' => -1,
228 'fields' => 'ids',
229 'post_type' => 'product',
230 'post_status' => 'publish',
231 'suppress_filters' => false,
232 'no_found_rows' => 1,
233 'include' => $post_id
234 ) );
235 $this->fill_table( $posts );
236 }
237 /*
238 * Fires when products terms are changed
239 */
240 public function term_changed( $term_id, $tt_id, $taxonomy ) {
241 if ( $taxonomy === 'product_cat' || $taxonomy === 'product_tag' ) {
242 }
243 }
244 /*
245 * Fires when products variations are changed
246 */
247 public function variable_product_changed( $product, $children = null ) {
248 global $wpdb;
249 $product_id = '';
250
251 if ( $this->is_table_not_exist() ) {
252 $this->create_table();
253 }
254
255 if ( is_object( $product ) ) {
256 $product_id = $product->get_id();
257 } else {
258 $product_id = $product;
259 }
260
261 $wpdb->delete( $this->table_name, array( 'id' => $product_id ) ); //DB Call OK, No Caching OK
262
263 $posts = get_posts( array(
264 'posts_per_page' => -1,
265 'fields' => 'ids',
266 'post_type' => 'product',
267 'post_status' => 'publish',
268 'suppress_filters' => false,
269 'no_found_rows' => 1,
270 'include' => $product_id
271 ) );
272 $this->fill_table( $posts );
273 }
274 /*
275 * Cancel index
276 */
277 public function cancel_reindex() {
278 $nonce = sanitize_text_field(wp_unslash($_POST['nonce']));
279 if (! wp_verify_nonce($nonce,'wp_chatbot') || ! current_user_can('manage_options')) {
280 wp_send_json(array('success' => false, 'msg' => esc_html__('Failed in Security check', 'chatbot')));
281 wp_die();
282
283 }else{
284 delete_option( 'wp_chatbot_index_meta' );
285 wp_send_json_success( 'Deleted!' );
286 }
287 }
288 /*
289 * Strip shortcodes
290 */
291 private function strip_shortcodes( $content ) {
292 $content = preg_replace( '#\[[^\]]+\]#', '', $content );
293 return $content;
294 }
295 /*
296 * Extract terms from content
297 */
298 private function extract_terms( $str ) {
299 $stopwords = str_replace('\\', '', get_option('qlcd_wp_chatbot_stop_words'));
300 $str = $this->html2txt( $str );
301 // Avoid single A-Z.
302
303 $special_cars = $this->get_special_chars();
304 $str = str_replace( $special_cars, "", $str );
305 $str = str_replace( array(
306 "ˇ",
307 "°",
308 "Ë›",
309 "Ëť",
310 "¸",
311 "§",
312 "=",
313 "¨",
314 "’",
315 "‘",
316 "”",
317 "“",
318 "„",
319 "´",
320 "—",
321 "–",
322 "Ă—",
323 '&#8217;',
324 "&nbsp;",
325 chr( 194 ) . chr( 160 )
326 ), " ", $str );
327 $str = str_replace( 'Ăź', 'ss', $str );
328 $str = preg_replace( '/[[:space:]]+/', ' ', $str );
329 // Most objects except unicode characters
330 $str = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u', '', $str );
331 // Line feeds, carriage returns, tabs
332 $str = preg_replace( '/[\x00-\x1F\x80-\x9F]/u', '', $str );
333 if ( function_exists( 'mb_strtolower' ) ) {
334 $str = mb_strtolower( $str );
335 } else {
336 $str = strtolower( $str );
337 }
338 $str = preg_replace( '/^[a-z]$/i', "", $str );
339 $str = trim( preg_replace( '/\s+/', ' ', $str ) );
340 $str_array = array_count_values( explode( ' ', $str ) );
341 if ( $stopwords && $str_array && ! empty( $str_array ) ) {
342 $stopwords_array = explode( ',', $stopwords );
343 if ( $stopwords_array && ! empty( $stopwords_array ) ) {
344 $stopwords_array = array_map( 'trim', $stopwords_array );
345 foreach ( $str_array as $str_word => $str_count ) {
346 if ( in_array( $str_word, $stopwords_array ) ) {
347 unset( $str_array[$str_word] );
348 }
349 }
350 }
351 }
352 return $str_array;
353 }
354 /*
355 * Removes scripts, styles, html tags
356 */
357 public function html2txt( $str ) {
358 $search = array(
359 '@<script[^>]*?>.*?</script>@si',
360 '@<[\/\!]*?[^<>]*?>@si',
361 '@<style[^>]*?>.*?</style>@siU',
362 '@<![\s\S]*?--[ \t\n\r]*>@'
363 );
364 $str = preg_replace( $search, '', $str );
365 $str = esc_attr( $str );
366 $str = stripslashes( $str );
367 $str = str_replace( array( "\r", "\n" ), ' ', $str );
368 $str = str_replace( array(
369 "·",
370 "…",
371 "€",
372 "&shy;"
373 ), "", $str );
374 return $str;
375 }
376 /*
377 * Get special characters that must be striped
378 */
379 public function get_special_chars() {
380 $chars = array(
381 '-',
382 '_',
383 '|',
384 '+',
385 '`',
386 '~',
387 '!',
388 '@',
389 '#',
390 '$',
391 '%',
392 '^',
393 '&',
394 '*',
395 '(',
396 ')',
397 '\\',
398 '?',
399 ';',
400 ':',
401 "'",
402 '"',
403 ".",
404 ",",
405 "<",
406 ">",
407 "{",
408 "}",
409 "/",
410 "[",
411 "]",
412 );
413 return apply_filters( 'wp_chatbot_special_chars', $chars );
414 }
415 /*
416 * Get string with current product terms ids
417 *
418 * @return string List of terms ids
419 */
420 private function get_terms_list( $id, $taxonomy ) {
421 $terms = get_the_terms( $id, $taxonomy );
422 if ( is_wp_error( $terms ) ) {
423 return '';
424 }
425 if ( empty( $terms ) ) {
426 return '';
427 }
428 $cats_array_temp = array();
429 foreach ( $terms as $term ) {
430 $cats_array_temp[] = $term->term_id;
431 }
432 return implode( ', ', $cats_array_temp );
433 }
434 /*
435 * Get string with current product terms names
436 *
437 * @return string List of terms names
438 */
439 private function get_terms_names_list( $id, $taxonomy ) {
440 $terms = get_the_terms( $id, $taxonomy );
441 if ( is_wp_error( $terms ) ) {
442 return '';
443 }
444 if ( empty( $terms ) ) {
445 return '';
446 }
447 $cats_array_temp = array();
448 foreach ( $terms as $term ) {
449 $cats_array_temp[] = $term->name;
450 }
451 return implode( ', ', $cats_array_temp );
452 }
453 }
454 endif;
455 new wpwBot_Table();