class-cache-controller.php
2 years ago
class-css-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
121 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:", "superb-blocks") . " " . $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 | && $option_controller->ClearCache(self::GetCacheOptionString(GutenbergCache::PAGES)); |
| 103 | } |
| 104 | |
| 105 | private static function GetCacheOptionString($cache_option) |
| 106 | { |
| 107 | switch ($cache_option) { |
| 108 | case CacheOptions::SERVICE_VERSION: |
| 109 | return Option::PREFIX . CacheOptions::SERVICE_VERSION; |
| 110 | case ElementorCache::SECTIONS: |
| 111 | return Option::PREFIX . ElementorCache::SECTIONS; |
| 112 | case GutenbergCache::PATTERNS: |
| 113 | return Option::PREFIX . GutenbergCache::PATTERNS; |
| 114 | case GutenbergCache::PAGES: |
| 115 | return Option::PREFIX . GutenbergCache::PAGES; |
| 116 | default: |
| 117 | throw new CacheException(__("Invalid cache option:", "superb-blocks") . " " . $cache_option); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 |