option_name; } public function get_fields() { return [ 'token' => [ 'label' => __('OpenAI Token', 'ai-consigliere'), 'type' => 'password', ], 'lang' => [ 'label' => __('Language', 'ai-consigliere'), 'type' => 'select', 'options' => self::get_language_map(), ], 'model' => [ 'label' => __('Model', 'ai-consigliere'), 'type' => 'datalist', 'options' => self::get_model_options(), 'description' => __('Pick one of the suggestions or type any model name your account can use.', 'ai-consigliere'), ], 'temp' => [ 'label' => __('Temperature', 'ai-consigliere'), 'type' => 'select', 'options' => self::get_temperature_options(), ] ]; } public static function get_language_map() { return [ 'en' => __('English', 'ai-consigliere'), 'it' => __('Italian', 'ai-consigliere'), 'fr' => __('French', 'ai-consigliere'), 'es' => __('Spanish', 'ai-consigliere'), 'de' => __('German', 'ai-consigliere'), ]; } public static function get_temperature_options() { return [ '0.2' => __('0.2 - Precise, predictable answers', 'ai-consigliere'), '0.5' => __('0.5 - Good mix of reliability and creativity', 'ai-consigliere'), '0.7' => __('0.7 - More variety, still coherent', 'ai-consigliere'), '1.0' => __('1.0 - Brainstorming, storytelling', 'ai-consigliere'), '1.3' => __('1.3 - Risk of incoherence, good for wild ideas', 'ai-consigliere'), ]; } /** * Modelli da suggerire nel campo. * * Erano scritti a mano e sono rimasti fermi al 2024: chi installava il plugin * si trovava bloccato su tre nomi e senza modo di indicarne uno piu recente. * Ora l'elenco arriva da OpenAI quando c'e una chiave configurata, e i nomi * qui sotto restano solo come ripiego. * * @return array */ public static function get_model_options() { $remote = self::fetch_remote_models(); if (!empty($remote)) { return $remote; } return self::get_fallback_models(); } /** * @return array */ public static function get_fallback_models() { return [ 'gpt-4o' => 'gpt-4o', 'gpt-4o-mini' => 'gpt-4o-mini', 'gpt-4-turbo' => 'gpt-4-turbo', ]; } /** * Chiede a OpenAI quali modelli puo usare questo account. * * Il risultato sta in cache mezza giornata: il campo impostazioni non deve * aspettare una chiamata di rete a ogni apertura, e la lista cambia di rado. * * @return array */ private static function fetch_remote_models() { $settings = get_option('aiconsigliere_settings'); $token = is_array($settings) && !empty($settings['token']) ? $settings['token'] : ''; if ('' === $token) { return []; } $cached = get_transient('aiconsigliere_models'); if (is_array($cached)) { return $cached; } $response = wp_remote_get('https://api.openai.com/v1/models', [ 'timeout' => 8, 'headers' => ['Authorization' => 'Bearer ' . $token], ]); if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { // Anche il fallimento si ricorda per poco, cosi una chiave sbagliata // non fa ritentare a ogni apertura della pagina. set_transient('aiconsigliere_models', [], 5 * MINUTE_IN_SECONDS); return []; } $body = json_decode(wp_remote_retrieve_body($response), true); $models = []; if (isset($body['data']) && is_array($body['data'])) { foreach ($body['data'] as $model) { if (empty($model['id']) || 0 !== strpos($model['id'], 'gpt')) { continue; } $models[$model['id']] = $model['id']; } } ksort($models); set_transient('aiconsigliere_models', $models, 12 * HOUR_IN_SECONDS); return $models; } public function get_settings() { return get_option($this->option_name); } public function sanitize($input) { $clean = []; $fields = $this->get_fields(); $stored = get_option($this->option_name); $input['__stored'] = is_array($stored) ? $stored : []; foreach ($fields as $key => $field) { $value = $input[$key] ?? ''; if ($field['type'] === 'text' || $field['type'] === 'datalist') { // 'datalist' accetta qualunque nome: i suggerimenti sono tali, non // un elenco chiuso. Riportarlo al primo valore valido cancellerebbe // in silenzio il modello scelto dall'utente. $clean[$key] = sanitize_text_field($value); } elseif ($field['type'] === 'password') { // Il campo non rimanda al browser il valore salvato, quindi arriva // vuoto a ogni salvataggio: vuoto vuol dire "non cambiare". $clean[$key] = '' !== $value ? sanitize_text_field($value) : (isset($input['__stored'][$key]) ? $input['__stored'][$key] : ''); } elseif ($field['type'] === 'select') { $valid_options = array_keys($field['options']); $clean[$key] = in_array($value, $valid_options, true) ? $value : $valid_options[0]; } } return $clean; } public function register_settings() { register_setting('aiconsigliere_settings_group', $this->option_name, [$this, 'sanitize']); } public function render_form() { $options = get_option($this->option_name); $fields = $this->get_fields(); ?>
$field): ?>

$label): ?>