PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.0.8
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.0.8
4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / data / controllers / class-cache-controller.php
superb-blocks / src / data / controllers Last commit date
class-cache-controller.php 2 years ago class-domainshift-controller.php 2 years ago class-key-controller.php 2 years ago class-log-controller.php 2 years ago class-option-controller.php 2 years ago class-rest-controller.php 2 years ago
class-cache-controller.php
118 lines
1 <?php
2
3 namespace SuperbAddons\Data\Controllers;
4
5 use SuperbAddons\Data\Utils\CacheException;
6
7 use SuperbAddons\Data\Utils\CacheTypes;
8 use SuperbAddons\Data\Utils\CacheOptions;
9 use SuperbAddons\Data\Utils\ElementorCache;
10 use SuperbAddons\Data\Utils\GutenbergCache;
11
12 defined('ABSPATH') || exit();
13
14 class CacheController
15 {
16 public static function GetCache($cache_option, $cache_type)
17 {
18 if (self::IsDataCacheOutdated($cache_type)) {
19 // Return false to indicate that the data cache is outdated and should not be fetched locally
20 return false;
21 }
22
23 return self::GetDataCache($cache_option);
24 }
25
26 private static function IsDataCacheOutdated($cache_type)
27 {
28 // Check cache version and update if needed
29 $option_string = self::GetCacheOptionString(CacheOptions::SERVICE_VERSION);
30
31 $option_controller = new OptionController();
32 $service_info_cache = $option_controller->GetCache($option_string);
33 $cache_invalid = !$service_info_cache || !isset($service_info_cache['last_update']) || !isset($service_info_cache['data']);
34
35 if ($cache_invalid || time() - $service_info_cache['last_update'] > 86400) {
36 // Cache expired or invalid -> Update service info cache
37 $service_status = DomainShiftController::GetServiceStatus();
38 if (!$service_status['online']) {
39 throw new CacheException($service_status['message']);
40 }
41
42 self::SetCache(CacheOptions::SERVICE_VERSION, $service_status);
43
44 if ($cache_invalid) {
45 // Cache invalid -> Data cache should be updated
46 return true;
47 }
48
49 switch ($cache_type) {
50 case CacheTypes::ELEMENTOR:
51 case CacheTypes::GUTENBERG:
52 if ($service_status[$cache_type] !== $service_info_cache['data'][$cache_type]) {
53 // Cache outdated -> Data cache should be updated
54 return true;
55 }
56 break;
57 default:
58 throw new CacheException(__("Invalid cache type:", "superbaddons") . " " . $cache_type);
59 }
60 }
61
62 // Local data cache accepted or is up to date
63 return false;
64 }
65
66 private static function GetDataCache($cache_option)
67 {
68 $option_string = self::GetCacheOptionString($cache_option);
69
70 $option_controller = new OptionController();
71 $cache = $option_controller->GetCache($option_string);
72 if (!$cache || !isset($cache['last_update']) || !isset($cache['data']) || time() - $cache['last_update'] > 86400) {
73 // Cache expired
74 return false;
75 }
76
77 return $cache['data'];
78 }
79
80 public static function SetCache($cache_option, $data)
81 {
82 $option_string = self::GetCacheOptionString($cache_option);
83
84 $option_controller = new OptionController();
85 return $option_controller->SetCache($option_string, $data);
86 }
87
88 public static function ClearCache($cache_option)
89 {
90 $option_string = self::GetCacheOptionString($cache_option);
91
92 $option_controller = new OptionController();
93 return $option_controller->ClearCache($option_string);
94 }
95
96 public static function ClearCacheAll()
97 {
98 $option_controller = new OptionController();
99 return $option_controller->ClearCache(self::GetCacheOptionString(CacheOptions::SERVICE_VERSION))
100 && $option_controller->ClearCache(self::GetCacheOptionString(ElementorCache::SECTIONS))
101 && $option_controller->ClearCache(self::GetCacheOptionString(GutenbergCache::PATTERNS));
102 }
103
104 private static function GetCacheOptionString($cache_option)
105 {
106 switch ($cache_option) {
107 case CacheOptions::SERVICE_VERSION:
108 return Option::PREFIX . CacheOptions::SERVICE_VERSION;
109 case ElementorCache::SECTIONS:
110 return Option::PREFIX . ElementorCache::SECTIONS;
111 case GutenbergCache::PATTERNS:
112 return Option::PREFIX . GutenbergCache::PATTERNS;
113 default:
114 throw new CacheException(__("Invalid cache option:", "superbaddons") . " " . $cache_option);
115 }
116 }
117 }
118