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-table.php

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

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