129 linhas
4,3 KiB
PHP
129 linhas
4,3 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* All the settings live in one option ("interweb_mz_sms_center_settings"). The API key lives apart, encrypted ("interweb_mz_sms_center_api_key"),
|
|
* and is never shown again after it is saved.
|
|
*/
|
|
class INTERWEB_MZ_SMS_CENTER_Settings
|
|
{
|
|
const OPTION = 'interweb_mz_sms_center_settings';
|
|
const KEY_OPTION = 'interweb_mz_sms_center_api_key';
|
|
|
|
private static $cache = null;
|
|
|
|
public static function defaults()
|
|
{
|
|
return array(
|
|
'brand' => 'interweb-mz',
|
|
'ui_lang' => 'auto',
|
|
'sms_sender' => '',
|
|
|
|
// WooCommerce order alerts.
|
|
'woo_enabled' => 0,
|
|
'woo_owner_phone' => '',
|
|
'woo_shipped_status' => 1,
|
|
'woo_shipped_key' => '',
|
|
'woo_lang' => 'auto',
|
|
'woo_events' => array(),
|
|
'woo_templates' => array(),
|
|
'woo_order_box' => 1,
|
|
);
|
|
}
|
|
|
|
public static function all()
|
|
{
|
|
if (self::$cache === null) {
|
|
$saved = get_option(self::OPTION, array());
|
|
self::$cache = array_merge(self::defaults(), is_array($saved) ? $saved : array());
|
|
}
|
|
return self::$cache;
|
|
}
|
|
|
|
public static function get($key)
|
|
{
|
|
$all = self::all();
|
|
return isset($all[$key]) ? $all[$key] : null;
|
|
}
|
|
|
|
public static function update(array $values)
|
|
{
|
|
$all = array_merge(self::all(), $values);
|
|
$allowed = array_keys(self::defaults());
|
|
$clean = array();
|
|
foreach ($allowed as $k) {
|
|
$clean[$k] = $all[$k];
|
|
}
|
|
update_option(self::OPTION, $clean, true);
|
|
self::$cache = null;
|
|
}
|
|
|
|
/* ---- API key ---- */
|
|
|
|
/** True when the key comes from wp-config.php (INTERWEB_MZ_SMS_CENTER_API_KEY) and the settings page cannot change it. */
|
|
public static function key_from_constant()
|
|
{
|
|
return defined('INTERWEB_MZ_SMS_CENTER_API_KEY') && is_string(INTERWEB_MZ_SMS_CENTER_API_KEY) && INTERWEB_MZ_SMS_CENTER_API_KEY !== '';
|
|
}
|
|
|
|
public static function api_key()
|
|
{
|
|
if (self::key_from_constant()) {
|
|
return trim(INTERWEB_MZ_SMS_CENTER_API_KEY);
|
|
}
|
|
$stored = get_option(self::KEY_OPTION, '');
|
|
return $stored ? INTERWEB_MZ_SMS_CENTER_Crypto::decrypt($stored) : '';
|
|
}
|
|
|
|
public static function has_stored_key()
|
|
{
|
|
return (bool) get_option(self::KEY_OPTION, '');
|
|
}
|
|
|
|
/** The saved key exists but cannot be decrypted (for example, the salts changed). */
|
|
public static function key_unreadable()
|
|
{
|
|
return !self::key_from_constant() && self::has_stored_key() && self::api_key() === '';
|
|
}
|
|
|
|
/** @return true|WP_Error */
|
|
public static function save_api_key($key)
|
|
{
|
|
$key = trim((string) $key);
|
|
if (!preg_match('/^(sandbox|prod)_[A-Za-z0-9_\-]{20,}$/', $key)) {
|
|
return new WP_Error('interweb_mz_sms_center_bad_key', __('This does not look like an API key. Keys start with sandbox_ or prod_.', 'interweb-mz-sms-center'));
|
|
}
|
|
$enc = INTERWEB_MZ_SMS_CENTER_Crypto::encrypt($key);
|
|
if (is_wp_error($enc)) {
|
|
return $enc;
|
|
}
|
|
update_option(self::KEY_OPTION, $enc, false);
|
|
update_option('interweb_mz_sms_center_api_key_hint', array(
|
|
'mode' => strpos($key, 'sandbox_') === 0 ? 'sandbox' : 'prod',
|
|
'suffix' => substr($key, -4),
|
|
'saved' => time(),
|
|
), false);
|
|
delete_transient('interweb_mz_sms_center_token_info');
|
|
return true;
|
|
}
|
|
|
|
public static function delete_api_key()
|
|
{
|
|
delete_option(self::KEY_OPTION);
|
|
delete_option('interweb_mz_sms_center_api_key_hint');
|
|
delete_transient('interweb_mz_sms_center_token_info');
|
|
}
|
|
|
|
/** What can be shown about the key: mode and the last 4 characters. Never the key. */
|
|
public static function key_hint()
|
|
{
|
|
if (self::key_from_constant()) {
|
|
$k = self::api_key();
|
|
return array('mode' => strpos($k, 'sandbox_') === 0 ? 'sandbox' : 'prod', 'suffix' => substr($k, -4), 'constant' => true);
|
|
}
|
|
$h = get_option('interweb_mz_sms_center_api_key_hint', array());
|
|
return is_array($h) ? $h : array();
|
|
}
|
|
}
|