| 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_enabled = wp_using_ext_object_cache(); |
| 70 |
$cache_key = ($is_user_meta ? 'user_' : '').($post_id == false ? '' : $post_id) . $meta_name . !empty($key) ? '_'.$key : ''; |
| 71 |
|
| 72 |
if ($cache_enabled) { |
| 73 |
$data = wp_cache_get($cache_key, Schema::getConstant('CACHE_GROUP')); |
| 74 |
if ($data !== false) return $data; |
| 75 |
} |
| 76 |
|
| 77 |
if ($is_user_meta) { |
| 78 |
$meta_data = get_user_meta($post_id, $meta_name, true); |
| 79 |
} else { |
| 80 |
$meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true); |
| 81 |
} |
| 82 |
|
| 83 |
if( !Utils::is_empty($key) && is_array($meta_data) && array_key_exists($key, $meta_data) ) { |
| 84 |
// If key exists in meta data, return it |
| 85 |
$data = $meta_data[$key]; |
| 86 |
} elseif (is_array($meta_data) && Utils::is_empty($key)) { |
| 87 |
// If no key is provided, return the entire meta data |
| 88 |
$data = $meta_data; |
| 89 |
} else { |
| 90 |
// If key does not exist, return false |
| 91 |
$data = false; |
| 92 |
} |
| 93 |
|
| 94 |
if ($cache_enabled) { |
| 95 |
wp_cache_set($cache_key, $data, Schema::getConstant('CACHE_GROUP'), $cache_expire); |
| 96 |
} |
| 97 |
|
| 98 |
return $data; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Set Object Cache |
| 103 |
*/ |
| 104 |
public static function set_object_cache($key, $data, $post_id = false, $meta_name = false, $cache_expire = false, $is_user_meta = false) { |
| 105 |
$meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name; |
| 106 |
$cache_expire = $cache_expire == false ? Schema::getConstant('CACHE_EXPIRE') : $cache_expire; |
| 107 |
|
| 108 |
if( $is_user_meta) { |
| 109 |
$meta_data = get_user_meta($post_id, $meta_name, true); |
| 110 |
} else { |
| 111 |
$meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true); |
| 112 |
} |
| 113 |
|
| 114 |
// If data is not an array, convert it to an array |
| 115 |
if (!is_array($meta_data)) { |
| 116 |
$meta_data = []; |
| 117 |
} |
| 118 |
|
| 119 |
if(Utils::is_empty($key)) { |
| 120 |
// If key is empty, set the entire meta data |
| 121 |
$meta_data = $data; |
| 122 |
} elseif (is_array($meta_data) && array_key_exists($key, $meta_data)) { |
| 123 |
if($meta_data[$key] == $data) { |
| 124 |
return true; |
| 125 |
} |
| 126 |
// If key exists, update the value |
| 127 |
$meta_data[$key] = $data; |
| 128 |
} else { |
| 129 |
$meta_data[$key] = $data; |
| 130 |
} |
| 131 |
|
| 132 |
// Update the meta data |
| 133 |
if ($is_user_meta) { |
| 134 |
$update_result = update_user_meta($post_id, $meta_name, $meta_data); |
| 135 |
} else { |
| 136 |
$update_result = $post_id == false ? update_option($meta_name, $meta_data) : update_post_meta($post_id, $meta_name, $meta_data); |
| 137 |
} |
| 138 |
// If update failed, return false |
| 139 |
if (!$update_result) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
// If object cache is enabled, set the cache |
| 144 |
// and delete the old cache entry if it exists |
| 145 |
if (wp_using_ext_object_cache()) { |
| 146 |
wp_cache_delete($meta_name, Schema::getConstant('CACHE_GROUP')); |
| 147 |
$cache_key = ($is_user_meta ? 'user_' : '') . ($post_id == false ? '' : $post_id) . $meta_name . '_' . $key; |
| 148 |
wp_cache_set($cache_key, $data, Schema::getConstant('CACHE_GROUP'), $cache_expire); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
// Return success if data update was successful |
| 153 |
return $update_result; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Delete object cache |
| 160 |
*/ |
| 161 |
public static function delete_object_cache($key = false, $post_id = false, $meta_name = false, $is_user_meta = false) { |
| 162 |
$meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name; |
| 163 |
$update_result = false; |
| 164 |
if ($is_user_meta) { |
| 165 |
$meta_data = get_user_meta($post_id, $meta_name, true); |
| 166 |
if (is_array($meta_data) && array_key_exists($key, $meta_data)) { |
| 167 |
unset($meta_data[$key]); |
| 168 |
if (empty($meta_data)) { |
| 169 |
$update_result = delete_user_meta($post_id, $meta_name); |
| 170 |
} else { |
| 171 |
$update_result = update_user_meta($post_id, $meta_name, $meta_data); |
| 172 |
} |
| 173 |
} |
| 174 |
} else { |
| 175 |
$meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true); |
| 176 |
if (is_array($meta_data) && array_key_exists($key, $meta_data)) { |
| 177 |
unset($meta_data[$key]); |
| 178 |
if (empty($meta_data)) { |
| 179 |
$update_result = $post_id == false ? delete_option($meta_name) : delete_post_meta($post_id, $meta_name); |
| 180 |
} else { |
| 181 |
$update_result = $post_id == false ? update_option($meta_name, $meta_data) : update_post_meta($post_id, $meta_name, $meta_data); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
// Delete from object cache if caching is enabled |
| 187 |
if (wp_using_ext_object_cache() && $update_result && $key) { |
| 188 |
$cache_key = ($is_user_meta ? 'user_' : '') . ($post_id == false ? '' : $post_id) . $meta_name . '_' . $key; |
| 189 |
wp_cache_delete($cache_key, Schema::getConstant('CACHE_GROUP')); |
| 190 |
} |
| 191 |
|
| 192 |
// Return success if data delete was successful |
| 193 |
return $update_result; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* Gets the instance of this class. |
| 199 |
* |
| 200 |
* @return Cache |
| 201 |
*/ |
| 202 |
public static function instance(){ |
| 203 |
if (is_null(self::$instance)) { |
| 204 |
self::$instance = new self(); |
| 205 |
} |
| 206 |
return self::$instance; |
| 207 |
} |
| 208 |
} |