'core', 'openai_api_key' => '', 'openai_model' => 'gpt-4o-mini', 'anthropic_api_key' => '', 'anthropic_model' => 'claude-sonnet-5', 'gemini_api_key' => '', 'gemini_model' => 'gemini-2.0-flash', 'groq_api_key' => '', 'groq_model' => 'meta-llama/llama-4-scout-17b-16e-instruct', 'alt_max_length' => 125, 'content_scan_limit' => 200, 'include_alt_prefix' => '', ); public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } private function __construct() { add_action( 'admin_init', array( $this, 'register_settings' ) ); } public static function get_all() { $saved = get_option( AIATG_OPTION_KEY, array() ); return wp_parse_args( $saved, self::DEFAULTS ); } public static function get( $key ) { $all = self::get_all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } public function register_settings() { register_setting( 'aiatg_settings_group', AIATG_OPTION_KEY, array( $this, 'sanitize' ) ); } public function sanitize( $input ) { $existing = self::get_all(); $sanitized = $existing; $sanitized['provider'] = in_array( $input['provider'] ?? '', array( 'core', 'openai', 'anthropic', 'gemini', 'groq' ), true ) ? $input['provider'] : $existing['provider']; foreach ( array( 'openai_api_key', 'anthropic_api_key', 'gemini_api_key', 'groq_api_key' ) as $key_field ) { if ( isset( $input[ $key_field ] ) ) { $sanitized[ $key_field ] = sanitize_text_field( trim( $input[ $key_field ] ) ); } } foreach ( array( 'openai_model', 'anthropic_model', 'gemini_model', 'groq_model' ) as $model_field ) { if ( isset( $input[ $model_field ] ) && '' !== trim( $input[ $model_field ] ) ) { $sanitized[ $model_field ] = sanitize_text_field( trim( $input[ $model_field ] ) ); } } if ( isset( $input['alt_max_length'] ) ) { $len = absint( $input['alt_max_length'] ); $sanitized['alt_max_length'] = $len > 0 ? min( $len, 300 ) : self::DEFAULTS['alt_max_length']; } if ( isset( $input['content_scan_limit'] ) ) { $limit = absint( $input['content_scan_limit'] ); $sanitized['content_scan_limit'] = $limit > 0 ? min( $limit, 2000 ) : self::DEFAULTS['content_scan_limit']; } $sanitized['include_alt_prefix'] = isset( $input['include_alt_prefix'] ) ? sanitize_text_field( $input['include_alt_prefix'] ) : $existing['include_alt_prefix']; return $sanitized; } /** * Returns the API key + model configured for the currently active provider. * For the 'core' provider (WordPress AI Client) there is no API key to * manage here — readiness is reported via a synthetic 'core' value so * callers can still use a simple `! empty( $config['api_key'] )` check. */ public static function get_active_provider_config() { $all = self::get_all(); $provider = $all['provider']; if ( 'core' === $provider ) { return array( 'provider' => 'core', 'api_key' => AIATG_Provider_CoreAI::is_available() ? 'core' : '', 'model' => '', ); } return array( 'provider' => $provider, 'api_key' => $all[ $provider . '_api_key' ] ?? '', 'model' => $all[ $provider . '_model' ] ?? '', ); } }