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-option-controller.php
237 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Data\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use SuperbAddons\Config\Config; |
| 8 | use SuperbAddons\Data\Utils\KeyType; |
| 9 | use SuperbAddons\Data\Utils\OptionException; |
| 10 | |
| 11 | class OptionController |
| 12 | { |
| 13 | private $current_keydomain_option = false; |
| 14 | |
| 15 | public function __construct() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | /* Domains */ |
| 20 | public function GetPreferredDomain($refresh = false) |
| 21 | { |
| 22 | $current_option = self::GetKeyDomainOption($refresh); |
| 23 | return Config::API_DOMAINS[$current_option[KeyDomainOptionKey::DOMAIN]]; |
| 24 | } |
| 25 | |
| 26 | public function UpdateAPIDomain($domain) |
| 27 | { |
| 28 | $domain = absint($domain); |
| 29 | if ($domain < 0 || $domain >= count(Config::API_DOMAINS)) { |
| 30 | throw new OptionException(__("Invalid Domain Key. Option could not be updated.", "superb-blocks")); |
| 31 | } |
| 32 | $current_option = self::GetKeyDomainOption(true); |
| 33 | $current_option[KeyDomainOptionKey::DOMAIN] = $domain; |
| 34 | return update_option(Option::KEY_DOMAIN, $current_option); |
| 35 | } |
| 36 | /* */ |
| 37 | |
| 38 | /* Keys */ |
| 39 | public function GetKey() |
| 40 | { |
| 41 | $current_option = self::GetKeyDomainOption(); |
| 42 | return $current_option[KeyDomainOptionKey::KEY]; |
| 43 | } |
| 44 | |
| 45 | public function GetStamp() |
| 46 | { |
| 47 | $current_option = self::GetKeyDomainOption(); |
| 48 | return $current_option[KeyDomainOptionKey::STAMP]; |
| 49 | } |
| 50 | |
| 51 | public function GetKeyType() |
| 52 | { |
| 53 | $current_option = self::GetKeyDomainOption(); |
| 54 | return $current_option[KeyDomainOptionKey::KEYTYPE]; |
| 55 | } |
| 56 | |
| 57 | public function KeyIsActive() |
| 58 | { |
| 59 | $current_option = self::GetKeyDomainOption(); |
| 60 | return $current_option[KeyDomainOptionKey::KEYACTIVE]; |
| 61 | } |
| 62 | |
| 63 | public function KeyIsExpired() |
| 64 | { |
| 65 | $current_option = self::GetKeyDomainOption(); |
| 66 | return $current_option[KeyDomainOptionKey::KEYEXPIRED]; |
| 67 | } |
| 68 | |
| 69 | public function KeyIsExceeded() |
| 70 | { |
| 71 | $current_option = self::GetKeyDomainOption(); |
| 72 | return isset($current_option[KeyDomainOptionKey::KEYEXCEEDED]) ? $current_option[KeyDomainOptionKey::KEYEXCEEDED] : false; |
| 73 | } |
| 74 | |
| 75 | public function KeyIsVerified() |
| 76 | { |
| 77 | $current_option = self::GetKeyDomainOption(); |
| 78 | if (!isset($current_option[KeyDomainOptionKey::STAMP]) || !isset($current_option[KeyDomainOptionKey::VERIFIED])) { |
| 79 | return false; |
| 80 | } |
| 81 | return !!$current_option[KeyDomainOptionKey::STAMP] && $current_option[KeyDomainOptionKey::VERIFIED]; |
| 82 | } |
| 83 | |
| 84 | public function SetKeyVerificationFailed() |
| 85 | { |
| 86 | $current_option = self::GetKeyDomainOption(); |
| 87 | $current_option[KeyDomainOptionKey::VERIFIED] = false; |
| 88 | return update_option(Option::KEY_DOMAIN, $current_option); |
| 89 | } |
| 90 | |
| 91 | public function HasRegisteredKey() |
| 92 | { |
| 93 | $current_option = self::GetKeyDomainOption(); |
| 94 | return !!$current_option[KeyDomainOptionKey::KEY]; |
| 95 | } |
| 96 | |
| 97 | public function HasPremiumKey() |
| 98 | { |
| 99 | $current_option = self::GetKeyDomainOption(); |
| 100 | return !!$current_option[KeyDomainOptionKey::KEY] && $current_option[KeyDomainOptionKey::KEYTYPE] === KeyType::PREMIUM; |
| 101 | } |
| 102 | |
| 103 | public function HasStandardKey() |
| 104 | { |
| 105 | $current_option = self::GetKeyDomainOption(); |
| 106 | return !!$current_option[KeyDomainOptionKey::KEY] && $current_option[KeyDomainOptionKey::KEYTYPE] === KeyType::STANDARD; |
| 107 | } |
| 108 | |
| 109 | public function UpdateKey($key, $stamp) |
| 110 | { |
| 111 | if (strlen($key) !== 23) { |
| 112 | throw new OptionException(__("Invalid License Key. Option could not be updated.", "superb-blocks")); |
| 113 | } |
| 114 | $current_option = self::GetKeyDomainOption(true); |
| 115 | $current_option[KeyDomainOptionKey::KEY] = $key; |
| 116 | if ($stamp > 0) { |
| 117 | $current_option[KeyDomainOptionKey::STAMP] = $stamp; |
| 118 | } |
| 119 | $current_option[KeyDomainOptionKey::VERIFIED] = true; |
| 120 | return update_option(Option::KEY_DOMAIN, $current_option); |
| 121 | } |
| 122 | |
| 123 | public function UpdateKeyType($keytype, $active, $expired, $exceeded) |
| 124 | { |
| 125 | $current_option = self::GetKeyDomainOption(true); |
| 126 | $current_option[KeyDomainOptionKey::KEYTYPE] = $keytype; |
| 127 | $current_option[KeyDomainOptionKey::KEYACTIVE] = !!$active; |
| 128 | $current_option[KeyDomainOptionKey::KEYEXPIRED] = !!$expired; |
| 129 | $current_option[KeyDomainOptionKey::KEYEXCEEDED] = !!$exceeded; |
| 130 | return update_option(Option::KEY_DOMAIN, $current_option); |
| 131 | } |
| 132 | |
| 133 | public function RemoveKey() |
| 134 | { |
| 135 | $current_option = self::GetKeyDomainOption(true); |
| 136 | $current_option[KeyDomainOptionKey::KEY] = false; |
| 137 | $current_option[KeyDomainOptionKey::KEYTYPE] = KeyType::FREE; |
| 138 | return update_option(Option::KEY_DOMAIN, $current_option); |
| 139 | } |
| 140 | |
| 141 | private function GetKeyDomainOption($refresh = false) |
| 142 | { |
| 143 | if (!$this->current_keydomain_option || $refresh) { |
| 144 | $this->current_keydomain_option = get_option(Option::KEY_DOMAIN, array(KeyDomainOptionKey::DOMAIN => 0, KeyDomainOptionKey::KEY => false, KeyDomainOptionKey::KEYTYPE => KeyType::FREE, KeyDomainOptionKey::KEYEXPIRED => false, KeyDomainOptionKey::KEYACTIVE => true, KeyDomainOptionKey::STAMP => false, KeyDomainOptionKey::VERIFIED => false, KeyDomainOptionKey::KEYEXCEEDED => false)); |
| 145 | } |
| 146 | return $this->current_keydomain_option; |
| 147 | } |
| 148 | |
| 149 | /* */ |
| 150 | |
| 151 | /* Cache */ |
| 152 | public function GetCache($cache_option) |
| 153 | { |
| 154 | return get_option($cache_option, false); |
| 155 | } |
| 156 | |
| 157 | public function SetCache($cache_option, $data) |
| 158 | { |
| 159 | if (!$cache_option) { |
| 160 | return false; |
| 161 | } |
| 162 | return update_option( |
| 163 | $cache_option, |
| 164 | array( |
| 165 | 'last_update' => time(), |
| 166 | 'data' => $data |
| 167 | ), |
| 168 | false |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | public function ClearCache($cache_option) |
| 173 | { |
| 174 | if (!$cache_option) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // Return true if cache is already cleared |
| 179 | if (!$this->GetCache($cache_option)) return true; |
| 180 | |
| 181 | return delete_option($cache_option); |
| 182 | } |
| 183 | |
| 184 | public static function GetSettings() |
| 185 | { |
| 186 | return get_option(Option::SETTINGS, array(SettingsOptionKey::LOGS_ENABLED => true, SettingsOptionKey::LOG_SHARE_ENABLED => false)); |
| 187 | } |
| 188 | |
| 189 | public function SaveSettings($settings) |
| 190 | { |
| 191 | return update_option(Option::SETTINGS, $settings); |
| 192 | } |
| 193 | |
| 194 | public static function GetCompatibilitySettings() |
| 195 | { |
| 196 | return get_option(Option::COMPATIBILITY_SETTINGS, array(CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING => true)); |
| 197 | } |
| 198 | |
| 199 | public function SaveCompatibilitySettings($compatibility_settings) |
| 200 | { |
| 201 | return update_option(Option::COMPATIBILITY_SETTINGS, $compatibility_settings); |
| 202 | } |
| 203 | |
| 204 | /* */ |
| 205 | } |
| 206 | |
| 207 | class Option |
| 208 | { |
| 209 | const PREFIX = 'superbaddonslibrary_'; |
| 210 | const KEY_DOMAIN = self::PREFIX . 'keydomain'; |
| 211 | const SETTINGS = self::PREFIX . 'settings'; |
| 212 | const COMPATIBILITY_SETTINGS = self::PREFIX . 'compatibilitysettings'; |
| 213 | } |
| 214 | |
| 215 | class KeyDomainOptionKey |
| 216 | { |
| 217 | const DOMAIN = 'spbdomain'; |
| 218 | const KEY = 'spbkey'; |
| 219 | const KEYTYPE = 'spbkeytype'; |
| 220 | const KEYACTIVE = 'spbkeyactive'; |
| 221 | const KEYEXPIRED = 'spbkeyexpired'; |
| 222 | const KEYEXCEEDED = 'spbkeyexceeded'; |
| 223 | const STAMP = 'spbkeystamp'; |
| 224 | const VERIFIED = 'spbkeyverified'; |
| 225 | } |
| 226 | |
| 227 | class SettingsOptionKey |
| 228 | { |
| 229 | const LOGS_ENABLED = 'spblogsenabled'; |
| 230 | const LOG_SHARE_ENABLED = 'spblogshareenabled'; |
| 231 | } |
| 232 | |
| 233 | class CompatibilitySettingsOptionKey |
| 234 | { |
| 235 | const SPECTRA_BLOCK_SPACING = 'spbspectrablockspacing'; |
| 236 | } |
| 237 |