class-cache-controller.php
1 month ago
class-css-controller.php
1 month ago
class-domainshift-controller.php
1 month ago
class-key-controller.php
1 month ago
class-log-controller.php
1 month ago
class-option-controller.php
1 month ago
class-rest-controller.php
1 month ago
class-cache-controller.php
159 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\ChunkLoading; |
| 11 | use SuperbAddons\Data\Utils\GutenbergCache; |
| 12 | |
| 13 | defined('ABSPATH') || exit(); |
| 14 | |
| 15 | class CacheController |
| 16 | { |
| 17 | public static function GetCache($cache_option, $cache_type) |
| 18 | { |
| 19 | if (self::IsDataCacheOutdated($cache_type)) { |
| 20 | // Return false to indicate that the data cache is outdated and should not be fetched locally |
| 21 | return false; |
| 22 | } |
| 23 | |
| 24 | return self::GetDataCache($cache_option); |
| 25 | } |
| 26 | |
| 27 | private static function IsDataCacheOutdated($cache_type) |
| 28 | { |
| 29 | // Check cache version and update if needed |
| 30 | $option_string = self::GetCacheOptionString(CacheOptions::SERVICE_VERSION); |
| 31 | |
| 32 | $option_controller = new OptionController(); |
| 33 | $service_info_cache = $option_controller->GetCache($option_string); |
| 34 | $cache_invalid = !$service_info_cache || !isset($service_info_cache['last_update']) || !isset($service_info_cache['data']); |
| 35 | |
| 36 | if ($cache_invalid || time() - $service_info_cache['last_update'] > 86400) { |
| 37 | // Cache expired or invalid -> Update service info cache |
| 38 | $service_status = DomainShiftController::GetServiceStatus(); |
| 39 | if (!$service_status['online']) { |
| 40 | throw new CacheException(esc_html($service_status['message'])); |
| 41 | } |
| 42 | |
| 43 | self::SetCache(CacheOptions::SERVICE_VERSION, $service_status); |
| 44 | |
| 45 | if ($cache_invalid) { |
| 46 | // Cache invalid -> Data cache should be updated |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | switch ($cache_type) { |
| 51 | case CacheTypes::ELEMENTOR: |
| 52 | case CacheTypes::GUTENBERG: |
| 53 | if ($service_status[$cache_type] !== $service_info_cache['data'][$cache_type]) { |
| 54 | // Cache outdated -> Data cache should be updated |
| 55 | return true; |
| 56 | } |
| 57 | break; |
| 58 | default: |
| 59 | throw new CacheException(esc_html(__("Invalid cache type:", "superb-blocks") . " " . $cache_type)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Local data cache accepted or is up to date |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | private static function GetDataCache($cache_option) |
| 68 | { |
| 69 | $option_string = self::GetCacheOptionString($cache_option); |
| 70 | |
| 71 | $option_controller = new OptionController(); |
| 72 | $cache = $option_controller->GetCache($option_string); |
| 73 | if (!$cache || !isset($cache['last_update']) || !isset($cache['data']) || time() - $cache['last_update'] > 7 * 86400) { |
| 74 | // Cache expired |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return $cache['data']; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Read cache data directly (with 24h TTL check) without the service-version outdated check. |
| 83 | * Used for partial cache reads where we don't want to trigger a service version refresh. |
| 84 | */ |
| 85 | public static function GetDataCacheDirect($cache_option) |
| 86 | { |
| 87 | return self::GetDataCache($cache_option); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Read the raw cache entry (including last_update timestamp). |
| 92 | * Returns the full array with 'last_update' and 'data' keys, or false. |
| 93 | */ |
| 94 | public static function GetDataCacheRaw($cache_option) |
| 95 | { |
| 96 | $option_string = self::GetCacheOptionString($cache_option); |
| 97 | |
| 98 | $option_controller = new OptionController(); |
| 99 | $cache = $option_controller->GetCache($option_string); |
| 100 | if (!$cache || !isset($cache['last_update']) || !isset($cache['data'])) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return $cache; |
| 105 | } |
| 106 | |
| 107 | public static function SetCache($cache_option, $data) |
| 108 | { |
| 109 | $option_string = self::GetCacheOptionString($cache_option); |
| 110 | |
| 111 | $option_controller = new OptionController(); |
| 112 | return $option_controller->SetCache($option_string, $data); |
| 113 | } |
| 114 | |
| 115 | public static function ClearCache($cache_option) |
| 116 | { |
| 117 | $option_string = self::GetCacheOptionString($cache_option); |
| 118 | |
| 119 | $option_controller = new OptionController(); |
| 120 | return $option_controller->ClearCache($option_string); |
| 121 | } |
| 122 | |
| 123 | public static function ClearCacheAll() |
| 124 | { |
| 125 | // Clear the loading lock transient so a stuck lock is always cleared on cache reset |
| 126 | delete_transient(ChunkLoading::LOADING_TRANSIENT); |
| 127 | |
| 128 | $option_controller = new OptionController(); |
| 129 | return $option_controller->ClearCache(self::GetCacheOptionString(CacheOptions::SERVICE_VERSION)) |
| 130 | && $option_controller->ClearCache(self::GetCacheOptionString(ElementorCache::SECTIONS)) |
| 131 | && $option_controller->ClearCache(self::GetCacheOptionString(GutenbergCache::LIBRARY)) |
| 132 | && $option_controller->ClearCache(self::GetCacheOptionString(GutenbergCache::LIBRARY_PARTIAL)) |
| 133 | // Legacy v1 keys - clean up leftover data from users updating from older versions |
| 134 | && $option_controller->ClearCache(self::GetCacheOptionString(GutenbergCache::PATTERNS)) |
| 135 | && $option_controller->ClearCache(self::GetCacheOptionString(GutenbergCache::PAGES)); |
| 136 | } |
| 137 | |
| 138 | private static function GetCacheOptionString($cache_option) |
| 139 | { |
| 140 | switch ($cache_option) { |
| 141 | case CacheOptions::SERVICE_VERSION: |
| 142 | return Option::PREFIX . CacheOptions::SERVICE_VERSION; |
| 143 | case ElementorCache::SECTIONS: |
| 144 | return Option::PREFIX . ElementorCache::SECTIONS; |
| 145 | case GutenbergCache::LIBRARY: |
| 146 | return Option::PREFIX . GutenbergCache::LIBRARY; |
| 147 | case GutenbergCache::LIBRARY_PARTIAL: |
| 148 | return Option::PREFIX . GutenbergCache::LIBRARY_PARTIAL; |
| 149 | // Legacy v1 keys - kept for cleanup on update |
| 150 | case GutenbergCache::PATTERNS: |
| 151 | return Option::PREFIX . GutenbergCache::PATTERNS; |
| 152 | case GutenbergCache::PAGES: |
| 153 | return Option::PREFIX . GutenbergCache::PAGES; |
| 154 | default: |
| 155 | throw new CacheException(esc_html(__("Invalid cache option:", "superb-blocks") . " " . $cache_option)); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 |