Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório wordpress-ai-plugin, anteriormente
0
wordpress-ai-plugin/mozout-ai/includes/class-settings.php
2026-12-01 13:25:59 +02:00

142 linhas
4,6 KiB
PHP

<?php
if (!defined('ABSPATH')) {
exit;
}
/**
* All the settings live in one option ("mozout_ai_settings"). The API key lives apart, encrypted ("mozout_ai_api_key"),
* and is never shown again after it is saved.
*/
class MOZOUT_AI_Settings
{
const OPTION = 'mozout_ai_settings';
const KEY_OPTION = 'mozout_ai_api_key';
private static $cache = null;
public static function defaults()
{
return array(
'brand' => 'mozout',
'ui_lang' => 'auto',
// Site assistant.
'ai_enabled' => 0,
'ai_model' => 'deepseek-ai/deepseek-v4-flash',
'ai_instructions' => '',
'ai_title_pt' => '',
'ai_title_en' => '',
'ai_welcome_pt' => '',
'ai_welcome_en' => '',
'ai_max_tokens' => 400,
'ai_per_hour' => 10,
'ai_per_day' => 30,
'ai_site_per_day' => 500,
'ai_color' => '#d60037',
'ai_position' => 'right',
// Writing help in the editor.
'write_enabled' => 1,
'write_model' => 'deepseek-ai/deepseek-v4-flash',
'write_tone' => 'friendly',
'write_max_tokens' => 700,
'write_per_day' => 50,
'write_brief' => '',
'write_post_types' => array('post', 'page', 'product'),
);
}
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 (MOZOUT_AI_API_KEY) and the settings page cannot change it. */
public static function key_from_constant()
{
return defined('MOZOUT_AI_API_KEY') && is_string(MOZOUT_AI_API_KEY) && MOZOUT_AI_API_KEY !== '';
}
public static function api_key()
{
if (self::key_from_constant()) {
return trim(MOZOUT_AI_API_KEY);
}
$stored = get_option(self::KEY_OPTION, '');
return $stored ? MOZOUT_AI_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('mozout_ai_bad_key', __('This does not look like an API key. Keys start with sandbox_ or prod_.', 'mozout-ai'));
}
$enc = MOZOUT_AI_Crypto::encrypt($key);
if (is_wp_error($enc)) {
return $enc;
}
update_option(self::KEY_OPTION, $enc, false);
update_option('mozout_ai_api_key_hint', array(
'mode' => strpos($key, 'sandbox_') === 0 ? 'sandbox' : 'prod',
'suffix' => substr($key, -4),
'saved' => time(),
), false);
delete_transient('mozout_ai_token_info');
return true;
}
public static function delete_api_key()
{
delete_option(self::KEY_OPTION);
delete_option('mozout_ai_api_key_hint');
delete_transient('mozout_ai_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_ai_api_key_hint', array());
return is_array($h) ? $h : array();
}
}