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