PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 5.0.2
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v5.0.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-table.php

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

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