| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteOptions |
| 4 |
{ |
| 5 |
private static $key = 'woo_mailerlite_options'; |
| 6 |
private static $apiKey = 'apiKey'; |
| 7 |
protected static $toJson = false; |
| 8 |
|
| 9 |
public const COMPLETE_ORDER_STATUSES = [ |
| 10 |
'wc-completed', |
| 11 |
'wc-processing', |
| 12 |
'completed', |
| 13 |
'processing', |
| 14 |
'wc-iscrizione-ok', |
| 15 |
'iscrizione-ok', |
| 16 |
]; |
| 17 |
|
| 18 |
public const PENDING_ORDER_STATUSES = [ |
| 19 |
'wc-pending', |
| 20 |
]; |
| 21 |
|
| 22 |
public static function all() |
| 23 |
{ |
| 24 |
return get_option(self::$key, []); |
| 25 |
} |
| 26 |
|
| 27 |
public static function get($key, $default = false) |
| 28 |
{ |
| 29 |
try { |
| 30 |
$options = get_option(self::$key, []); |
| 31 |
if (!isset($options['settings'])) { |
| 32 |
$options = self::update('settings', [ |
| 33 |
'subscribeOnCheckout' => false |
| 34 |
]); |
| 35 |
} |
| 36 |
if (strpos($key, '.') !== false) { |
| 37 |
$needle = explode('.', $key); |
| 38 |
|
| 39 |
if (is_string($options[$needle[0]])) { |
| 40 |
$cleanJson = stripslashes($options[$needle[0]]); |
| 41 |
$options[$needle[0]] = json_decode($cleanJson, true); |
| 42 |
} |
| 43 |
if (isset($options[$needle[0]][$needle[1]])) { |
| 44 |
return $options[$needle[0]][$needle[1]]; |
| 45 |
} else { |
| 46 |
return $default; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if (isset($options[$key])) { |
| 51 |
if (is_string($options[$key])) { |
| 52 |
$cleanJson = stripslashes($options[$key]); |
| 53 |
if (is_array(json_decode($cleanJson, true))) { |
| 54 |
$options[$key] = json_decode($cleanJson, true); |
| 55 |
} |
| 56 |
} |
| 57 |
if (self::$toJson) { |
| 58 |
return json_encode($options[$key]); |
| 59 |
} |
| 60 |
|
| 61 |
if ($key === self::$apiKey && !empty($options[$key])) { |
| 62 |
return self::handleApiKeyRetrieval($options[$key]); |
| 63 |
} |
| 64 |
return $options[$key]; |
| 65 |
} |
| 66 |
return $default; |
| 67 |
} catch(Exception $e) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public static function update($key, $value) |
| 73 |
{ |
| 74 |
if ($key === self::$apiKey && !empty($value) && !self::isEncryptedData($value)) { |
| 75 |
$encrypted = WooMailerLiteEncryption::instance()->encrypt($value); |
| 76 |
if ($encrypted !== false) { |
| 77 |
$value = $encrypted; |
| 78 |
} else { |
| 79 |
WooMailerLiteLog()->error('WooMailerLite: Encryption failed, storing as plain text temporarily.'); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$options = get_option(self::$key, []); |
| 84 |
$options[$key] = $value; |
| 85 |
return update_option(self::$key, $options); |
| 86 |
} |
| 87 |
|
| 88 |
public static function updateMultiple($data) |
| 89 |
{ |
| 90 |
if (isset($data[self::$apiKey]) && !empty($data[self::$apiKey])) { |
| 91 |
$data[self::$apiKey] = WooMailerLiteEncryption::instance()->encrypt($data[self::$apiKey]); |
| 92 |
} |
| 93 |
$options = get_option(self::$key, []); |
| 94 |
$options = array_merge($options, $data); |
| 95 |
return update_option(self::$key, $options); |
| 96 |
} |
| 97 |
|
| 98 |
public static function delete($key) |
| 99 |
{ |
| 100 |
$options = get_option(self::$key, []); |
| 101 |
if (isset($options[$key])) { |
| 102 |
unset($options[$key]); |
| 103 |
} |
| 104 |
return update_option(self::$key, $options); |
| 105 |
} |
| 106 |
|
| 107 |
public static function deleteAll() |
| 108 |
{ |
| 109 |
return delete_option(self::$key); |
| 110 |
} |
| 111 |
|
| 112 |
public static function toJson() |
| 113 |
{ |
| 114 |
self::$toJson = true; |
| 115 |
return new self; |
| 116 |
} |
| 117 |
|
| 118 |
public static function allSupportedOrderStatuses() |
| 119 |
{ |
| 120 |
return array_merge(self::COMPLETE_ORDER_STATUSES, self::PENDING_ORDER_STATUSES); |
| 121 |
} |
| 122 |
|
| 123 |
public static function isCompleteOrderStatus($status) |
| 124 |
{ |
| 125 |
return in_array($status, self::COMPLETE_ORDER_STATUSES); |
| 126 |
} |
| 127 |
|
| 128 |
public static function isPendingOrderStatus($status) |
| 129 |
{ |
| 130 |
return in_array($status, self::PENDING_ORDER_STATUSES); |
| 131 |
} |
| 132 |
|
| 133 |
private static function isEncryptedData($value) { |
| 134 |
return is_string($value) |
| 135 |
&& strlen($value) > 50 |
| 136 |
&& preg_match('/^[A-Za-z0-9+\/]+=*$/', $value) |
| 137 |
&& base64_decode($value, true) !== false; |
| 138 |
} |
| 139 |
|
| 140 |
private static function handleApiKeyRetrieval($value) |
| 141 |
{ |
| 142 |
if (self::isEncryptedData($value)) { |
| 143 |
$decrypted = WooMailerLiteEncryption::instance()->decrypt($value); |
| 144 |
if ($decrypted !== false) { |
| 145 |
return $decrypted; |
| 146 |
} else { |
| 147 |
WooMailerLiteLog()->error('WooMailerLite: API key decryption failed. Re-enter API key.'); |
| 148 |
return null; |
| 149 |
} |
| 150 |
} else { |
| 151 |
self::update(self::$apiKey, $value); |
| 152 |
return $value; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|