| 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 |
} |
| 38 |
/** |
| 39 |
* Get caching option name |
| 40 |
*/ |
| 41 |
public function get_cache_name( $s ) { |
| 42 |
$cache_option_name = 'aws_search_term_' . $s; |
| 43 |
if ( has_filter('wpml_current_language') ) { |
| 44 |
$current_lang = apply_filters('wpml_current_language', NULL); |
| 45 |
if ( $current_lang ) { |
| 46 |
$cache_option_name = $cache_option_name . '_' . $current_lang; |
| 47 |
} |
| 48 |
} |
| 49 |
return $cache_option_name; |
| 50 |
} |
| 51 |
/* |
| 52 |
* Check if cache table exist |
| 53 |
*/ |
| 54 |
private function is_cache_table_not_exist() { |
| 55 |
global $wpdb; |
| 56 |
return ( $wpdb->get_var( "SHOW TABLES LIKE '{$this->cache_table_name}'" ) != $this->cache_table_name ); //DB Call OK, No Caching OK |
| 57 |
} |
| 58 |
/* |
| 59 |
* Create cache table |
| 60 |
*/ |
| 61 |
private function create_cache_table() { |
| 62 |
global $wpdb; |
| 63 |
$charset_collate = $wpdb->get_charset_collate(); |
| 64 |
$sql = "CREATE TABLE {$this->cache_table_name} ( |
| 65 |
name VARCHAR(50) NOT NULL, |
| 66 |
value LONGTEXT NOT NULL |
| 67 |
) $charset_collate;"; |
| 68 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 69 |
dbDelta( $sql ); |
| 70 |
} |
| 71 |
/* |
| 72 |
* Insert data into cache table |
| 73 |
*/ |
| 74 |
public function insert_into_cache_table( $cache_option_name, $result_array ) { |
| 75 |
|
| 76 |
global $wpdb; |
| 77 |
|
| 78 |
$query = $wpdb->prepare("INSERT IGNORE INTO {$this->cache_table_name} |
| 79 |
(`name`, `value`) |
| 80 |
VALUES (%s, %s)", $cache_option_name, json_encode( $result_array )); |
| 81 |
|
| 82 |
$wpdb->query( $query ); //DB Call OK, No Caching OK |
| 83 |
|
| 84 |
if ( $wpdb->last_error ) { |
| 85 |
if ( $this->is_cache_table_not_exist() ) { |
| 86 |
$this->create_cache_table(); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
/* |
| 92 |
* Get data from cache table |
| 93 |
*/ |
| 94 |
public function get_from_cache_table( $cache_option_name ) { |
| 95 |
|
| 96 |
global $wpdb; |
| 97 |
|
| 98 |
$result = ''; |
| 99 |
|
| 100 |
$safe_sql = $wpdb->prepare("SELECT * FROM {$this->cache_table_name} WHERE `name` LIKE %s", $cache_option_name); |
| 101 |
|
| 102 |
$cache_content = $wpdb->get_results( $safe_sql, ARRAY_A ); //DB Call OK, No Caching OK |
| 103 |
|
| 104 |
if ( ! $wpdb->last_error ) { |
| 105 |
if (!empty($cache_content) && !is_wp_error($cache_content) && is_array($cache_content)) { |
| 106 |
$result = $cache_content[0]['value']; |
| 107 |
} |
| 108 |
} else { |
| 109 |
if ( $this->is_cache_table_not_exist() ) { |
| 110 |
$this->create_cache_table(); |
| 111 |
} |
| 112 |
} |
| 113 |
return $result; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
endif; |
| 118 |
wpwBot_Cache::factory(); |