false ]); } if (strpos($key, '.') !== false) { $needle = explode('.', $key); if (is_string($options[$needle[0]])) { $cleanJson = stripslashes($options[$needle[0]]); $options[$needle[0]] = json_decode($cleanJson, true); } if (isset($options[$needle[0]][$needle[1]])) { return $options[$needle[0]][$needle[1]]; } else { return $default; } } if (isset($options[$key])) { if (is_string($options[$key])) { $cleanJson = stripslashes($options[$key]); if (is_array(json_decode($cleanJson, true))) { $options[$key] = json_decode($cleanJson, true); } } if (self::$toJson) { return json_encode($options[$key]); } if ($key === self::$apiKey && !empty($options[$key])) { return self::handleApiKeyRetrieval($options[$key]); } return $options[$key]; } return $default; } catch(Exception $e) { return null; } } public static function update($key, $value) { if ($key === self::$apiKey && !empty($value) && !self::isEncryptedData($value)) { $encrypted = WooMailerLiteEncryption::instance()->encrypt($value); if ($encrypted !== false) { $value = $encrypted; } else { WooMailerLiteLog()->error('WooMailerLite: Encryption failed, storing as plain text temporarily.'); } } $options = get_option(self::$key, []); $options[$key] = $value; return update_option(self::$key, $options); } public static function updateMultiple($data) { if (isset($data[self::$apiKey]) && !empty($data[self::$apiKey])) { $data[self::$apiKey] = WooMailerLiteEncryption::instance()->encrypt($data[self::$apiKey]); } $options = get_option(self::$key, []); $options = array_merge($options, $data); return update_option(self::$key, $options); } public static function delete($key) { $options = get_option(self::$key, []); if (isset($options[$key])) { unset($options[$key]); } return update_option(self::$key, $options); } public static function deleteAll() { return delete_option(self::$key); } public static function toJson() { self::$toJson = true; return new self; } public static function allSupportedOrderStatuses() { return array_merge(self::COMPLETE_ORDER_STATUSES, self::PENDING_ORDER_STATUSES); } public static function isCompleteOrderStatus($status) { return in_array($status, self::COMPLETE_ORDER_STATUSES); } public static function isPendingOrderStatus($status) { return in_array($status, self::PENDING_ORDER_STATUSES); } private static function isEncryptedData($value) { return is_string($value) && strlen($value) > 50 && preg_match('/^[A-Za-z0-9+\/]+=*$/', $value) && base64_decode($value, true) !== false; } private static function handleApiKeyRetrieval($value) { if (self::isEncryptedData($value)) { $decrypted = WooMailerLiteEncryption::instance()->decrypt($value); if ($decrypted !== false) { return $decrypted; } else { WooMailerLiteLog()->error('WooMailerLite: API key decryption failed. Re-enter API key.'); return null; } } else { self::update(self::$apiKey, $value); return $value; } } }