PluginProbe
Media Cloud Sync / 1.2.9
Media Cloud Sync v1.2.9
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / cache.php

cache.php in Media Cloud Sync 1.2.9, at includes/base/cache.php

158 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) {
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 = ($post_id == false ? '' : $post_id) . $meta_name . '_' . $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 $meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true);
78 $data = isset($meta_data[$key]) ? $meta_data[$key] : false;
79
80 if ($data && $cache_enabled) {
81 wp_cache_set($cache_key, $data, Schema::getConstant('CACHE_GROUP'), $cache_expire);
82 }
83
84 return $data;
85 }
86
87 /**
88 * Set Object Cache
89 */
90 public static function set_object_cache($key, $data, $post_id = false, $meta_name = false, $cache_expire = false) {
91 $meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name;
92 $cache_expire = $cache_expire == false ? Schema::getConstant('CACHE_EXPIRE') : $cache_expire;
93
94 $meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true);
95
96 if (is_array($meta_data)) {
97 if(isset($meta_data[$key])){
98 if($meta_data[$key] == $data) {
99 return true;
100 }
101 }
102 $meta_data[$key] = $data;
103 } else {
104 $meta_data = [$key => $data];
105 }
106
107 $update_result = $post_id == false ? update_option($meta_name, $meta_data) : update_post_meta($post_id, $meta_name, $meta_data);
108
109 // Set cache only if data update was successful
110 if (wp_using_ext_object_cache() && $update_result) {
111 $cache_key = ($post_id == false ? '' : $post_id) . $meta_name . '_' . $key;
112 wp_cache_set($cache_key, $data, Schema::getConstant('CACHE_GROUP'), Schema::getConstant('CACHE_EXPIRE'));
113 }
114
115 // Return success if data update was successful
116 return $update_result;
117 }
118
119
120
121 /**
122 * Delete object cache
123 */
124 public static function delete_object_cache($key = false, $post_id = false, $meta_name = false) {
125 $meta_name = $meta_name == false ? Schema::getConstant('META_KEY') : $meta_name;
126 $update_result = false;
127 $meta_data = $post_id == false ? get_option($meta_name, []) : get_post_meta($post_id, $meta_name, true);
128
129 if (is_array($meta_data)) {
130 if($key == false) {
131 $update_result = $post_id == false ? delete_option($meta_name) : delete_post_meta($post_id, $meta_name);
132 } else if (array_key_exists($key, $meta_data)) {
133 unset($meta_data[$key]);
134 if(empty($meta_data)) {
135 $update_result = $post_id == false ? delete_option($meta_name) : delete_post_meta($post_id, $meta_name);
136 } else {
137 $update_result = $post_id == false ? update_option($meta_name, $meta_data) : update_post_meta($post_id, $meta_name, $meta_data);
138 }
139 }
140 }
141
142 // Delete from object cache if caching is enabled
143 if (wp_using_ext_object_cache() && $update_result && $key) {
144 $cache_key = ($post_id == false ? '' : $post_id) . $meta_name . '_' . $key;
145 wp_cache_delete($cache_key, Schema::getConstant('CACHE_GROUP'));
146 }
147
148 // Return success if data delete was successful
149 return $update_result;
150 }
151
152 public static function instance(){
153 if (is_null(self::$instance)) {
154 self::$instance = new self();
155 }
156 return self::$instance;
157 }
158 }