config = is_array( $config ) ? $config : array(); } public function get_id() { return isset( $this->config['id'] ) ? sanitize_key( (string) $this->config['id'] ) : ''; } public function get_name() { return isset( $this->config['name'] ) ? (string) $this->config['name'] : $this->get_id(); } public function get_credential_fields() { return isset( $this->config['credential_fields'] ) && is_array( $this->config['credential_fields'] ) ? $this->config['credential_fields'] : array(); } public function get_available_models() { return isset( $this->config['models'] ) && is_array( $this->config['models'] ) ? $this->config['models'] : array(); } public function get_default_model() { return isset( $this->config['default_model'] ) ? (string) $this->config['default_model'] : ''; } public function supports_tone() { return ! empty( $this->config['supports_tone'] ); } public function get_timeout() { return max( 5, isset( $this->config['timeout'] ) ? absint( $this->config['timeout'] ) : 60 ); } public function get_retry_limit() { return max( 0, isset( $this->config['retry_limit'] ) ? absint( $this->config['retry_limit'] ) : 3 ); } public function get_max_batch_size() { return max( 1, isset( $this->config['max_batch_size'] ) ? absint( $this->config['max_batch_size'] ) : 40 ); } public function test_connection( $source_language, $target_language ) { return $this->translate_batch( array( 'aitmt-provider-test' => 'Hello' ), $source_language, $target_language ); } public function translate_batch( $segments, $source_language, $target_language ) { $callback = isset( $this->config['callback'] ) ? $this->config['callback'] : ''; if ( ! is_callable( $callback ) ) { return new WP_Error( 'aitmt_provider_unavailable', __( 'The selected translation provider is not available.', 'ait-multilingual-translate' ) ); } return call_user_func( $callback, $segments, $source_language, $target_language ); } public function get_endpoint() { return isset( $this->config['endpoint'] ) ? esc_url_raw( (string) $this->config['endpoint'] ) : ''; } public function get_badges() { return isset( $this->config['badges'] ) && is_array( $this->config['badges'] ) ? $this->config['badges'] : array(); } } final class AITMT_Provider_Registry { private static $providers; public static function all() { if ( null === self::$providers ) { self::$providers = self::build(); } return self::$providers; } public static function get( $provider_id ) { $provider_id = sanitize_key( (string) $provider_id ); $providers = self::all(); return isset( $providers[ $provider_id ] ) ? $providers[ $provider_id ] : null; } public static function ids() { return array_keys( self::all() ); } private static function build() { $configs = array( 'openai' => array( 'name' => 'OpenAI', 'endpoint' => 'https://api.openai.com/v1/responses', 'default_model' => 'gpt-4o-mini', 'models' => array( 'gpt-4o-mini', 'gpt-4o' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_openai_translate_batch', ), 'gemini' => array( 'name' => 'Google Gemini', 'endpoint' => 'https://generativelanguage.googleapis.com/v1beta/models/', 'default_model' => 'gemini-2.5-flash', 'models' => array( 'gemini-2.5-flash', 'gemini-2.5-pro' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_gemini_translate_batch', ), 'grok' => array( 'name' => 'Grok / xAI', 'endpoint' => 'https://api.x.ai/v1/chat/completions', 'default_model' => 'grok-3-mini', 'models' => array( 'grok-3-mini', 'grok-4' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_grok_translate_batch', ), 'google_translate' => array( 'name' => 'Google Translate', 'endpoint' => 'https://translation.googleapis.com/language/translate/v2', 'default_model' => 'Cloud Translation Basic v2', 'models' => array( 'Cloud Translation Basic v2' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => false, 'callback' => 'aitmt_fallback_google_translate_batch', ), 'deepl' => array( 'name' => 'DeepL', 'endpoint' => 'https://api-free.deepl.com/v2/translate', 'default_model' => 'DeepL API', 'models' => array( 'DeepL Free API', 'DeepL Pro API' ), 'credential_fields' => array( 'api_key', 'plan' ), 'supports_tone' => false, 'callback' => 'aitmt_fallback_deepl_translate_batch', ), 'claude' => array( 'name' => 'Claude', 'endpoint' => 'https://api.anthropic.com/v1/messages', 'default_model' => 'claude-haiku-4-5-20251001', 'models' => array( 'claude-haiku-4-5-20251001' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_claude_translate_batch', ), 'yandex_translate' => array( 'name' => 'Yandex Translate', 'endpoint' => 'https://translate.api.cloud.yandex.net/translate/v2/translate', 'default_model' => 'Yandex Cloud Translate v2', 'models' => array( 'Yandex Cloud Translate v2' ), 'credential_fields' => array( 'api_key', 'folder_id' ), 'supports_tone' => false, 'callback' => 'aitmt_fallback_yandex_translate_batch', ), 'yandexgpt' => array( 'name' => 'YandexGPT', 'endpoint' => 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion', 'default_model' => 'yandexgpt-lite/latest', 'models' => array( 'yandexgpt-lite/latest', 'yandexgpt/latest' ), 'credential_fields' => array( 'api_key', 'folder_id' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_yandexgpt_translate_batch', ), 'mistral' => array( 'name' => 'Mistral AI', 'endpoint' => 'https://api.mistral.ai/v1/chat/completions', 'default_model' => 'mistral-small-latest', 'models' => array( 'mistral-small-latest' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_mistral_translate_batch', ), 'deepseek' => array( 'name' => 'DeepSeek', 'endpoint' => 'https://api.deepseek.com/chat/completions', 'default_model' => 'deepseek-v4-flash', 'models' => array( 'deepseek-v4-flash', 'deepseek-v4-pro' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_deepseek_translate_batch', ), 'qwen' => array( 'name' => 'Qwen / DashScope', 'endpoint' => 'https://dashscope-us.aliyuncs.com/compatible-mode/v1/chat/completions', 'default_model' => 'qwen3.7-plus', 'models' => array( 'qwen3.7-plus', 'qwen3.7-max', 'qwen3.6-flash' ), 'credential_fields' => array( 'api_key', 'region' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_qwen_translate_batch', ), 'baidu' => array( 'name' => 'Baidu Qianfan', 'endpoint' => 'https://api.baiduqianfan.ai/v1/chat/completions', 'default_model' => 'ernie-5.0', 'models' => array( 'ernie-5.0' ), 'credential_fields' => array( 'api_key' ), 'supports_tone' => true, 'callback' => 'aitmt_fallback_baidu_translate_batch', ), ); $providers = array(); foreach ( $configs as $provider_id => $config ) { $config['id'] = $provider_id; $config['timeout'] = isset( $config['timeout'] ) ? $config['timeout'] : 60; $config['retry_limit'] = isset( $config['retry_limit'] ) ? $config['retry_limit'] : 3; $config['max_batch_size'] = isset( $config['max_batch_size'] ) ? $config['max_batch_size'] : 40; $config['badges'] = ! empty( $config['supports_tone'] ) ? array( 'Tone of Voice', 'HTML Aware', 'Batch Support' ) : array( 'Fast', 'Batch Support' ); $providers[ $provider_id ] = new AITMT_Function_Provider_Adapter( $config ); } return $providers; } }