141 linhas
4,6 KiB
PHP
141 linhas
4,6 KiB
PHP
<?php
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* All the settings live in one option ("mozout_domains_settings"). The API key lives apart, encrypted ("mozout_domains_api_key"),
|
|
* and is never shown again after it is saved.
|
|
*/
|
|
class MOZOUT_DOMAINS_Settings
|
|
{
|
|
const OPTION = 'mozout_domains_settings';
|
|
const KEY_OPTION = 'mozout_domains_api_key';
|
|
|
|
private static $cache = null;
|
|
|
|
public static function defaults()
|
|
{
|
|
return array_merge(array(
|
|
'brand' => 'mozout',
|
|
'ui_lang' => 'auto',
|
|
), MOZOUT_DOMAINS_Admin::defaults());
|
|
}
|
|
|
|
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);
|
|
$clean = array();
|
|
foreach (array_keys(self::defaults()) 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 (MOZOUT_DOMAINS_API_KEY) and the settings page cannot change it. */
|
|
public static function key_from_constant()
|
|
{
|
|
return defined('MOZOUT_DOMAINS_API_KEY') && is_string(MOZOUT_DOMAINS_API_KEY) && MOZOUT_DOMAINS_API_KEY !== '';
|
|
}
|
|
|
|
public static function api_key()
|
|
{
|
|
if (self::key_from_constant()) {
|
|
return trim(MOZOUT_DOMAINS_API_KEY);
|
|
}
|
|
$stored = get_option(self::KEY_OPTION, '');
|
|
return $stored ? MOZOUT_DOMAINS_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 check_key_format($key)
|
|
{
|
|
if (!preg_match('/^(sandbox|prod)_[A-Za-z0-9_\-]{20,}$/', trim((string) $key))) {
|
|
return new WP_Error('mozout_domains_bad_key', __('This does not look like an API key. Keys start with sandbox_ or prod_.', 'mozout-domains'));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param string $mode Mode reported by the API (sandbox or production). Empty: taken from the key prefix.
|
|
* @return true|WP_Error
|
|
*/
|
|
public static function save_api_key($key, $mode = '')
|
|
{
|
|
$key = trim((string) $key);
|
|
$format = self::check_key_format($key);
|
|
if (is_wp_error($format)) {
|
|
return $format;
|
|
}
|
|
$enc = MOZOUT_DOMAINS_Crypto::encrypt($key);
|
|
if (is_wp_error($enc)) {
|
|
return $enc;
|
|
}
|
|
update_option(self::KEY_OPTION, $enc, false);
|
|
update_option('mozout_domains_api_key_hint', array(
|
|
'mode' => $mode !== '' ? ($mode === 'sandbox' ? 'sandbox' : 'prod') : (strpos($key, 'sandbox_') === 0 ? 'sandbox' : 'prod'),
|
|
'suffix' => substr($key, -4),
|
|
'saved' => time(),
|
|
), false);
|
|
delete_transient('mozout_domains_token_info');
|
|
return true;
|
|
}
|
|
|
|
public static function delete_api_key()
|
|
{
|
|
delete_option(self::KEY_OPTION);
|
|
delete_option('mozout_domains_api_key_hint');
|
|
delete_transient('mozout_domains_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('mozout_domains_api_key_hint', array());
|
|
return is_array($h) ? $h : array();
|
|
}
|
|
|
|
/** True when writes made with the key are only simulated (Sandbox, or a Production key still in test). */
|
|
public static function simulated()
|
|
{
|
|
$info = get_transient('mozout_domains_token_info');
|
|
if (is_array($info)) {
|
|
return $info['mode'] === 'sandbox' || !empty($info['simulated']);
|
|
}
|
|
$hint = self::key_hint();
|
|
return isset($hint['mode']) && $hint['mode'] === 'sandbox';
|
|
}
|
|
}
|