PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services / 3.2.9
WPBot – AI ChatBot for Live Support, Lead Generation, AI Services v3.2.9
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 8.5.0 8.4.9 All 531 releases
chatbot / includes / class-wpwbot-cache.php

class-wpwbot-cache.php in WPBot – AI ChatBot for Live Support, Lead Generation, AI Services 3.2.9, at includes/class-wpwbot-cache.php

137 lines 4.4 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_Cache' ) ) :
6 /**
7 * Class for plugin search
8 */
9 class wpwBot_Cache {
10 /**
11 * @var AWS_Cache Cache table name
12 */
13 private $cache_table_name;
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 $instance->setup();
24 }
25 return $instance;
26 }
27 /**
28 * Constructor
29 */
30 public function __construct() {}
31 /**
32 * Setup actions and filters for all things settings
33 */
34 public function setup() {
35 global $wpdb;
36 $this->cache_table_name = $wpdb->prefix . QCLD_wpCHATBOT_CACHE_TABLE;
37 add_action( 'aws_cache_clear', array( $this, 'clear_cache' ) );
38 add_action( 'wp_ajax_aws-clear-cache', array( $this, 'clear_cache' ) );
39 }
40 /**
41 * Get caching option name
42 */
43 public function get_cache_name( $s ) {
44 $cache_option_name = 'aws_search_term_' . $s;
45 if ( has_filter('wpml_current_language') ) {
46 $current_lang = apply_filters('wpml_current_language', NULL);
47 if ( $current_lang ) {
48 $cache_option_name = $cache_option_name . '_' . $current_lang;
49 }
50 }
51 return $cache_option_name;
52 }
53 /*
54 * Check if cache table exist
55 */
56 private function is_cache_table_not_exist() {
57 global $wpdb;
58 return ( $wpdb->get_var( "SHOW TABLES LIKE '{$this->cache_table_name}'" ) != $this->cache_table_name );
59 }
60 /*
61 * Create cache table
62 */
63 private function create_cache_table() {
64 global $wpdb;
65 $charset_collate = $wpdb->get_charset_collate();
66 $sql = "CREATE TABLE {$this->cache_table_name} (
67 name VARCHAR(50) NOT NULL,
68 value LONGTEXT NOT NULL
69 ) $charset_collate;";
70 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
71 dbDelta( $sql );
72 }
73 /*
74 * Insert data into cache table
75 */
76 public function insert_into_cache_table( $cache_option_name, $result_array ) {
77 global $wpdb;
78 $values = $wpdb->prepare(
79 "(%s, %s)",
80 $cache_option_name, json_encode( $result_array )
81 );
82 $query = "INSERT IGNORE INTO {$this->cache_table_name}
83 (`name`, `value`)
84 VALUES $values
85 ";
86 $wpdb->query( $query );
87 if ( $wpdb->last_error ) {
88 if ( $this->is_cache_table_not_exist() ) {
89 $this->create_cache_table();
90 }
91 }
92 }
93 /*
94 * Get data from cache table
95 */
96 public function get_from_cache_table( $cache_option_name ) {
97 global $wpdb;
98 $result = '';
99 $where = $wpdb->prepare( " name LIKE %s", $cache_option_name );
100 $sql = "SELECT *
101 FROM
102 {$this->cache_table_name}
103 WHERE
104 {$where}
105 ";
106 $cache_content = $wpdb->get_results( $sql, ARRAY_A );
107 if ( ! $wpdb->last_error ) {
108 if (!empty($cache_content) && !is_wp_error($cache_content) && is_array($cache_content)) {
109 $result = $cache_content[0]['value'];
110 }
111 } else {
112 if ( $this->is_cache_table_not_exist() ) {
113 $this->create_cache_table();
114 }
115 }
116 return $result;
117 }
118 /*
119 * Clear cached terms
120 */
121 public function clear_cache() {
122 global $wpdb;
123 if ( ! $this->is_cache_table_not_exist() ) {
124
125 $terms = "aws_search_term_%";
126 $where = $wpdb->prepare( " name LIKE %s", $terms );
127 $sql = "DELETE FROM {$this->cache_table_name}
128 WHERE {$where}
129 ";
130 $wpdb->query( $sql );
131
132 }
133
134 }
135 }
136 endif;
137 wpwBot_Cache::factory();