| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Cache { |
| 7 |
private static $instance = null; |
| 8 |
private $assets_url; |
| 9 |
private $version; |
| 10 |
private $token; |
| 11 |
|
| 12 |
|
| 13 |
private static $cached_data=[]; |
| 14 |
|
| 15 |
/** |
| 16 |
* Admin constructor. |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 21 |
$this->version = WPMCS_VERSION; |
| 22 |
$this->token = WPMCS_TOKEN; |
| 23 |
|
| 24 |
// Initialize setup |
| 25 |
$this->init(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Initialize datas |
| 30 |
*/ |
| 31 |
public function init() { |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Update Static Cache |
| 37 |
*/ |
| 38 |
public static function update_item_cache( $item_id, $data ){ |
| 39 |
self::$cached_data[$item_id] = $data; |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Update Static Cache |
| 45 |
*/ |
| 46 |
public static function get_item_cache( $item_id, $default = false ){ |
| 47 |
return isset(self::$cached_data[$item_id]) ? self::$cached_data[$item_id] : $default; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Delete Static Cache |
| 52 |
*/ |
| 53 |
public static function delete_item_cache( $item_id = false ){ |
| 54 |
if($item_id === false) { |
| 55 |
self::$cached_data = []; |
| 56 |
} else { |
| 57 |
unset(self::$cached_data[$item_id]); |
| 58 |
} |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get Object Cache |
| 64 |
*/ |
| 65 |
public static function get_object_cache($key = '', $post_id = false, $meta_name = false, $cache_expire = false, $is_user_meta = false) { |
| 66 |
$meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name; |
| 67 |
$cache_expire = $cache_expire == false ? Schema::getConstant('CACHE_EXPIRE') : $cache_expire; |
| 68 |
|
| 69 |
$cache_key = ($is_user_meta ? 'user_' : '').($post_id == false ? '' : $post_id) . $meta_name . (!empty($key) ? '_'.$key : ''); |
| 70 |
|
| 71 |
$data = wp_cache_get($cache_key, Schema::getConstant('CACHE_GROUP')); |
| 72 |
if ($data !== false) return $data; |
| 73 |
|
| 74 |
if ($is_user_meta) { |
| 75 |
$meta_data = get_user_meta($post_id, $meta_name, true); |
| 76 |
} else { |
| 77 |
$meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true); |
| 78 |
} |
| 79 |
|
| 80 |
if( !Utils::is_empty($key) && is_array($meta_data) && array_key_exists($key, $meta_data) ) { |
| 81 |
// If key exists in meta data, return it |
| 82 |
$data = $meta_data[$key]; |
| 83 |
} elseif (is_array($meta_data) && Utils::is_empty($key)) { |
| 84 |
// If no key is provided, return the entire meta data |
| 85 |
$data = $meta_data; |
| 86 |
} else { |
| 87 |
// If key does not exist, return false |
| 88 |
$data = false; |
| 89 |
} |
| 90 |
|
| 91 |
// Set the data in object cache |
| 92 |
wp_cache_set($cache_key, $data, Schema::getConstant('CACHE_GROUP'), $cache_expire); |
| 93 |
|
| 94 |
return $data; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set Object Cache |
| 99 |
*/ |
| 100 |
public static function set_object_cache($key, $data, $post_id = false, $meta_name = false, $cache_expire = false, $is_user_meta = false) { |
| 101 |
$meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name; |
| 102 |
$cache_expire = $cache_expire == false ? Schema::getConstant('CACHE_EXPIRE') : $cache_expire; |
| 103 |
|
| 104 |
if( $is_user_meta) { |
| 105 |
$meta_data = get_user_meta($post_id, $meta_name, true); |
| 106 |
} else { |
| 107 |
$meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true); |
| 108 |
} |
| 109 |
|
| 110 |
// If data is not an array, convert it to an array |
| 111 |
if (!is_array($meta_data)) { |
| 112 |
$meta_data = []; |
| 113 |
} |
| 114 |
|
| 115 |
if(Utils::is_empty($key)) { |
| 116 |
// If key is empty, set the entire meta data |
| 117 |
$meta_data = $data; |
| 118 |
} elseif (is_array($meta_data) && array_key_exists($key, $meta_data)) { |
| 119 |
if($meta_data[$key] == $data) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
// If key exists, update the value |
| 123 |
$meta_data[$key] = $data; |
| 124 |
} else { |
| 125 |
$meta_data[$key] = $data; |
| 126 |
} |
| 127 |
|
| 128 |
// Update the meta data |
| 129 |
if ($is_user_meta) { |
| 130 |
$update_result = update_user_meta($post_id, $meta_name, $meta_data); |
| 131 |
} else { |
| 132 |
$update_result = $post_id == false ? update_option($meta_name, $meta_data) : update_post_meta($post_id, $meta_name, $meta_data); |
| 133 |
} |
| 134 |
// If update failed, return false |
| 135 |
if (!$update_result) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
// Update the object cache if caching is enabled |
| 140 |
$cache_key = ($is_user_meta ? 'user_' : '') . ($post_id == false ? '' : $post_id) . $meta_name . '_' . $key; |
| 141 |
wp_cache_delete($cache_key, Schema::getConstant('CACHE_GROUP')); |
| 142 |
wp_cache_set($cache_key, $data, Schema::getConstant('CACHE_GROUP'), $cache_expire); |
| 143 |
|
| 144 |
|
| 145 |
// Return success if data update was successful |
| 146 |
return $update_result; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Delete object cache |
| 153 |
*/ |
| 154 |
public static function delete_object_cache($key = false, $post_id = false, $meta_name = false, $is_user_meta = false) { |
| 155 |
$meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name; |
| 156 |
$update_result = false; |
| 157 |
if ($is_user_meta) { |
| 158 |
$meta_data = get_user_meta($post_id, $meta_name, true); |
| 159 |
if (is_array($meta_data) && array_key_exists($key, $meta_data)) { |
| 160 |
unset($meta_data[$key]); |
| 161 |
if (empty($meta_data)) { |
| 162 |
$update_result = delete_user_meta($post_id, $meta_name); |
| 163 |
} else { |
| 164 |
$update_result = update_user_meta($post_id, $meta_name, $meta_data); |
| 165 |
} |
| 166 |
} |
| 167 |
} else { |
| 168 |
$meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true); |
| 169 |
if (is_array($meta_data) && array_key_exists($key, $meta_data)) { |
| 170 |
unset($meta_data[$key]); |
| 171 |
if (empty($meta_data)) { |
| 172 |
$update_result = $post_id == false ? delete_option($meta_name) : delete_post_meta($post_id, $meta_name); |
| 173 |
} else { |
| 174 |
$update_result = $post_id == false ? update_option($meta_name, $meta_data) : update_post_meta($post_id, $meta_name, $meta_data); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// Delete from object cache |
| 180 |
if ($update_result && $key) { |
| 181 |
$cache_key = ($is_user_meta ? 'user_' : '') . ($post_id == false ? '' : $post_id) . $meta_name . '_' . $key; |
| 182 |
wp_cache_delete($cache_key, Schema::getConstant('CACHE_GROUP')); |
| 183 |
} |
| 184 |
|
| 185 |
// Return success if data delete was successful |
| 186 |
return $update_result; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Flush Cache |
| 192 |
* @since 1.3.2 |
| 193 |
*/ |
| 194 |
public static function flush_object_cache() { |
| 195 |
if(wp_cache_supports('flush_group')) { |
| 196 |
wp_cache_flush_group(Schema::getConstant('CACHE_GROUP')); |
| 197 |
} else { |
| 198 |
wp_cache_flush(); |
| 199 |
} |
| 200 |
self::$cached_data = []; |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* Gets the instance of this class. |
| 207 |
* |
| 208 |
* @return Cache |
| 209 |
*/ |
| 210 |
public static function instance(){ |
| 211 |
if (is_null(self::$instance)) { |
| 212 |
self::$instance = new self(); |
| 213 |
} |
| 214 |
return self::$instance; |
| 215 |
} |
| 216 |
} |