| @@ -1,118 +1 @@ | ||
| 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( $wpdb->prepare( "SHOW TABLES LIKE %s", $wpdb->esc_like( $this->cache_table_name ) ) ) != $this->cache_table_name ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 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 ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 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 ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter | |
| 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(); | |
| 1 | +<?php if ( ! defined( 'ABSPATH' ) ) { exit; } if ( ! class_exists( 'wpwBot_Cache' ) ) : /** * Class for plugin search */ class wpwBot_Cache { /** * @var AWS_Cache Cache table name */ private $cache_table_name; /** * Return a singleton instance of the current class * * @return object */ public static function factory() { static $instance = false; if ( ! $instance ) { $instance = new self(); $instance->setup(); } return $instance; } /** * Constructor */ public function __construct() {} /** * Setup actions and filters for all things settings */ public function setup() { global $wpdb; $this->cache_table_name = $wpdb->prefix . QCLD_wpCHATBOT_CACHE_TABLE; add_action( 'aws_cache_clear', array( $this, 'clear_cache' ) ); add_action( 'wp_ajax_aws-clear-cache', array( $this, 'clear_cache' ) ); } /** * Get caching option name */ public function get_cache_name( $s ) { $cache_option_name = 'aws_search_term_' . $s; if ( has_filter('wpml_current_language') ) { $current_lang = apply_filters('wpml_current_language', NULL); if ( $current_lang ) { $cache_option_name = $cache_option_name . '_' . $current_lang; } } return $cache_option_name; } /* * Check if cache table exist */ private function is_cache_table_not_exist() { global $wpdb; return ( $wpdb->get_var( "SHOW TABLES LIKE '{$this->cache_table_name}'" ) != $this->cache_table_name ); } /* * Create cache table */ private function create_cache_table() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$this->cache_table_name} ( name VARCHAR(50) NOT NULL, value LONGTEXT NOT NULL ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } /* * Insert data into cache table */ public function insert_into_cache_table( $cache_option_name, $result_array ) { global $wpdb; $values = $wpdb->prepare( "(%s, %s)", $cache_option_name, json_encode( $result_array ) ); $query = "INSERT IGNORE INTO {$this->cache_table_name} (`name`, `value`) VALUES $values "; $wpdb->query( $query ); if ( $wpdb->last_error ) { if ( $this->is_cache_table_not_exist() ) { $this->create_cache_table(); } } } /* * Get data from cache table */ public function get_from_cache_table( $cache_option_name ) { global $wpdb; $result = ''; $where = $wpdb->prepare( " name LIKE %s", $cache_option_name ); $sql = "SELECT * FROM {$this->cache_table_name} WHERE {$where} "; $cache_content = $wpdb->get_results( $sql, ARRAY_A ); if ( ! $wpdb->last_error ) { if (!empty($cache_content) && !is_wp_error($cache_content) && is_array($cache_content)) { $result = $cache_content[0]['value']; } } else { if ( $this->is_cache_table_not_exist() ) { $this->create_cache_table(); } } return $result; } /* * Clear cached terms */ public function clear_cache() { global $wpdb; if ( ! $this->is_cache_table_not_exist() ) { $terms = "aws_search_term_%"; $where = $wpdb->prepare( " name LIKE %s", $terms ); $sql = "DELETE FROM {$this->cache_table_name} WHERE {$where} "; $wpdb->query( $sql ); } } } endif; wpwBot_Cache::factory(); | |