'', 'label' => '', 'model' => '' ); } return array( 'provider' => $credentials['provider'], 'label' => self::get_provider_label( $credentials['provider'] ), 'model' => (string) $credentials['model'], ); } public static function get_connector_models() { // When the custom ChatGPT API key is configured AND toggled on, model // selection should reflect what that key actually has access to — // not the separate WordPress AI Connectors, even if those are also // configured. Short-circuit here rather than merging the two. $use_custom_key = (bool) get_option( 'wtofe_use_custom_api_key', false ); $custom_key = trim( (string) get_option( 'wtofe_chatgpt_api_key', '' ) ); if ( $use_custom_key && '' !== $custom_key ) { return self::get_custom_api_key_models( $custom_key ); } $connector_keys = array( 'openai' => 'connectors_ai_openai_api_key', 'google' => 'connectors_ai_google_api_key', 'anthropic' => 'connectors_ai_anthropic_api_key', ); $static = array( 'text_generation' => array( 'openai' => array( 'label' => 'OpenAI', 'models' => array( array( 'id' => 'gpt-6-luna', 'label' => 'GPT-6 Luna' ), array( 'id' => 'gpt-5.6-luna', 'label' => 'GPT-5.6 Luna' ), array( 'id' => 'gpt-5.5-pro', 'label' => 'GPT-5.5 Pro' ), array( 'id' => 'gpt-5.5', 'label' => 'GPT-5.5' ), array( 'id' => 'gpt-5.4', 'label' => 'GPT-5.4' ), array( 'id' => 'gpt-5.4-mini', 'label' => 'GPT-5.4 Mini' ), ), ), 'google' => array( 'label' => 'Google', 'models' => array( array( 'id' => 'gemini-3.8-flash', 'label' => 'Gemini 3.8 Flash' ), array( 'id' => 'gemini-3.6-flash', 'label' => 'Gemini 3.6 Flash' ), array( 'id' => 'gemini-3.1-pro', 'label' => 'Gemini 3.1 Pro' ), array( 'id' => 'gemini-3-flash', 'label' => 'Gemini 3 Flash' ), array( 'id' => 'gemini-2.5-pro', 'label' => 'Gemini 2.5 Pro' ), array( 'id' => 'gemini-2.5-flash', 'label' => 'Gemini 2.5 Flash' ), ), ), 'anthropic' => array( 'label' => 'Anthropic', 'models' => array( array( 'id' => 'claude-sonnet-5', 'label' => 'Claude Sonnet 5' ), array( 'id' => 'claude-opus-4-6', 'label' => 'Claude Opus 4.6' ), array( 'id' => 'claude-sonnet-4-6', 'label' => 'Claude Sonnet 4.6' ), array( 'id' => 'claude-haiku-4-5', 'label' => 'Claude Haiku 4.5' ), ), ), ), 'image_generation' => array( 'openai' => array( 'label' => 'OpenAI', 'models' => array( array( 'id' => 'gpt-image-2.5-sunburst', 'label' => 'GPT Image 2.5 Sunburst' ), array( 'id' => 'gpt-image-2', 'label' => 'GPT Image 2' ), array( 'id' => 'gpt-image-1.5', 'label' => 'GPT Image 1.5' ), ), ), 'google' => array( 'label' => 'Google', 'models' => array( array( 'id' => 'gemini-3.1-flash-image-preview', 'label' => 'Gemini 3.1 Flash Image' ), array( 'id' => 'gemini-3-pro-image', 'label' => 'Gemini 3 Pro Image' ), ), ), // Anthropic has no image generation — intentionally omitted. ), ); // Try AiClient first — gets live models from all configured providers. if ( class_exists( \WordPress\AiClient\AiClient::class ) ) { try { $registry = \WordPress\AiClient\AiClient::defaultRegistry(); $live = array( 'text_generation' => array(), 'image_generation' => array() ); $found_any = false; foreach ( $registry->getRegisteredProviderIds() as $provider_id ) { if ( ! $registry->isProviderConfigured( $provider_id ) ) { continue; } $class_name = $registry->getProviderClassName( $provider_id ); $provider_meta = $class_name::metadata(); $provider_label = (string) $provider_meta->getName(); $pid = (string) $provider_id; foreach ( $class_name::modelMetadataDirectory()->listModelMetadata() as $model_meta ) { $capabilities = array_map( 'strval', $model_meta->getSupportedCapabilities() ); foreach ( array( 'text_generation', 'image_generation' ) as $cap ) { if ( ! in_array( $cap, $capabilities, true ) ) { continue; } if ( ! isset( $live[ $cap ][ $pid ] ) ) { $live[ $cap ][ $pid ] = array( 'label' => $provider_label, 'models' => array() ); } $entry = array( 'id' => (string) $model_meta->getId(), 'label' => (string) $model_meta->getName() ); if ( ! in_array( $entry, $live[ $cap ][ $pid ]['models'], true ) ) { $live[ $cap ][ $pid ]['models'][] = $entry; $found_any = true; } } } } if ( $found_any ) { // For any capability group that came back empty from AiClient, // fall back to the static list for configured providers. foreach ( array( 'text_generation', 'image_generation' ) as $cap ) { if ( empty( $live[ $cap ] ) ) { $live[ $cap ] = self::static_models_for_configured_providers( $static[ $cap ], $connector_keys, $cap ); } } return $live; } } catch ( \Throwable $e ) { // fall through to static list } } // Fallback: static lists for every provider that has a key configured. return array( 'text_generation' => self::static_models_for_configured_providers( $static['text_generation'], $connector_keys, 'text_generation' ), 'image_generation' => self::static_models_for_configured_providers( $static['image_generation'], $connector_keys, 'image_generation' ), ); } /** * Model list sourced entirely from the custom ChatGPT API key — used instead * of get_connector_models()'s AiClient/connector path when that key is * configured and toggled on. Fetches the account's live model list; falls * back to a known-good static OpenAI model set if the live call fails. * * @param string $api_key * @return array */ private static function get_custom_api_key_models( $api_key ) { $static_openai_text = array( array( 'id' => 'gpt-6-luna', 'label' => 'GPT-6 Luna' ), array( 'id' => 'gpt-5.6-luna', 'label' => 'GPT-5.6 Luna' ), array( 'id' => 'gpt-5.5', 'label' => 'GPT-5.5' ), array( 'id' => 'gpt-5.4', 'label' => 'GPT-5.4' ), array( 'id' => 'gpt-5.4-mini', 'label' => 'GPT-5.4 Mini' ), ); $static_openai_image = array( array( 'id' => 'gpt-image-2.5-sunburst', 'label' => 'GPT Image 2.5 Sunburst' ), array( 'id' => 'gpt-image-2', 'label' => 'GPT Image 2' ), array( 'id' => 'gpt-image-1.5', 'label' => 'GPT Image 1.5' ), ); $live = self::fetch_openai_models_for_custom_key( $api_key ); $text_models = ( null !== $live && ! empty( $live['text_generation'] ) ) ? $live['text_generation'] : $static_openai_text; $image_models = ( null !== $live && ! empty( $live['image_generation'] ) ) ? $live['image_generation'] : $static_openai_image; return array( 'text_generation' => array( 'openai' => array( 'label' => 'OpenAI', 'models' => $text_models ) ), 'image_generation' => array( 'openai' => array( 'label' => 'OpenAI', 'models' => $image_models ) ), ); } /** * Filters a static provider→models map to only those providers with a configured key. * * @param array $static_cap Provider map for one capability. * @param array $connector_keys Map of provider_id => wp_option_name. * @return array */ private static function static_models_for_configured_providers( array $static_cap, array $connector_keys, $capability = 'text_generation' ) { $result = array(); foreach ( $connector_keys as $provider => $option ) { if ( '' !== trim( (string) get_option( $option, '' ) ) && isset( $static_cap[ $provider ] ) ) { $result[ $provider ] = $static_cap[ $provider ]; } } return $result; } /** * Fetches the list of models available to the given OpenAI API key from * OpenAI's /v1/models endpoint, split into text- and image-generation * capable models. Cached in a transient (keyed by key hash) for a few * hours so we're not hitting the API on every admin page load. * * @param string $api_key * @return array{text_generation: array, image_generation: array}|null Null on failure (caller should fall back to the static list). */ private static function fetch_openai_models_for_custom_key( $api_key ) { $api_key = trim( (string) $api_key ); if ( '' === $api_key ) { return null; } // v2: bumped when the filter/sort below changed, so stale cached lists are dropped. $cache_key = 'wtofe_openai_models_v2_' . md5( $api_key ); $cached = get_transient( $cache_key ); if ( false !== $cached ) { return $cached; } $response = wp_remote_get( 'https://api.openai.com/v1/models', array( //phpcs:ignore PluginCheck.CodeAnalysis.AIProvider.DirectIntegration 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, ), 'timeout' => 10, ) ); if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { // Cache the failure briefly too, so an invalid/expired key doesn't // trigger a live request on every single page load. set_transient( $cache_key, null, 5 * MINUTE_IN_SECONDS ); return null; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['data'] ) || ! is_array( $body['data'] ) ) { set_transient( $cache_key, null, 5 * MINUTE_IN_SECONDS ); return null; } $text_models = array(); $image_models = array(); foreach ( $body['data'] as $model ) { $id = isset( $model['id'] ) ? (string) $model['id'] : ''; if ( '' === $id ) { continue; } $created = isset( $model['created'] ) ? (int) $model['created'] : 0; // Image generation models. if ( 0 === strpos( $id, 'gpt-image-' ) || 0 === strpos( $id, 'dall-e-' ) ) { $image_models[] = array( 'id' => $id, 'label' => self::humanize_openai_model_id( $id ), 'created' => $created ); continue; } // Skip models /v1/chat/completions doesn't accept: embeddings, audio, realtime/live, // moderation, legacy completion (instruct/base), and Responses-API-only (codex, pro). if ( preg_match( '/embedding|whisper|tts|moderation|audio|realtime|live|transcribe|davinci|babbage|curie|search|similarity|edit|instruct|codex|-pro\b/i', $id ) ) { continue; } // Keep chat-capable GPT not o-series / ChatGPT models. if ( 0 === strpos( $id, 'gpt-' ) ) { // || 0 === strpos( $id, 'chatgpt-' ) || preg_match( '/^o[0-9]/', $id ) $text_models[] = array( 'id' => $id, 'label' => self::humanize_openai_model_id( $id ), 'created' => $created ); } } // Newest first by release timestamp — sorting by ID string is unreliable // (e.g. "gpt-live-1" sorts above "gpt-6-sol"). Ties fall back to ID so an // undated alias ("gpt-5.5") lands above its dated snapshot. $newest_first = function( $a, $b ) { if ( $a['created'] !== $b['created'] ) { return $b['created'] - $a['created']; } return strlen( $a['id'] ) - strlen( $b['id'] ); }; usort( $text_models, $newest_first ); usort( $image_models, $newest_first ); $strip_created = function( $m ) { return array( 'id' => $m['id'], 'label' => $m['label'] ); }; $text_models = array_map( $strip_created, $text_models ); $image_models = array_map( $strip_created, $image_models ); $result = array( 'text_generation' => $text_models, 'image_generation' => $image_models, ); set_transient( $cache_key, $result, 6 * HOUR_IN_SECONDS ); return $result; } /** * Turns an OpenAI model ID like "gpt-5.4-mini" into a display label like "GPT 5.4 Mini". * * @param string $id * @return string */ private static function humanize_openai_model_id( $id ) { $label = str_replace( array( '-', '_' ), ' ', $id ); $label = ucwords( $label ); $label = preg_replace( '/\bGpt\b/', 'GPT', $label ); $label = preg_replace( '/\bTts\b/', 'TTS', $label ); return $label; } /** * Returns the correct model ID for the active provider and feature. * * Validates the saved option against known models for the provider. * Falls back to the provider's default if the saved value belongs to a * different provider or is empty. * * @param string $provider 'openai', 'google', or 'anthropic'. * @param string $feature Feature key e.g. 'description', 'tags', 'image_generation'. * @return string */ private static function resolve_model_for_provider( $provider, $feature ) { // Determine which provider the saved model actually belongs to (by ID prefix / // live provider registry), rather than checking against a fixed whitelist — // a whitelist goes stale the moment a new model is released or fetched live // from OpenAI for a custom API key. $option_key = '' !== $feature ? 'wtofe_ai_model_' . $feature : ''; $saved_model = '' !== $option_key ? trim( (string) get_option( $option_key, '' ) ) : ''; // If saved model is set and belongs to the active provider, use it. if ( '' !== $saved_model && self::get_provider_for_model( $saved_model ) === $provider ) { return $saved_model; } // Saved model belongs to a different provider or is empty ("Provider Default") — // use the provider's hardcoded default model for this task. return self::get_default_model_for_provider( $provider, $feature ); } /** * Returns the hardcoded default model used when "Provider Default" is selected. * Defaults are comparable tiers across providers — fast, cost-efficient models * suited to product content generation. Falls back to the provider's newest * model only for a provider/capability with no default defined here. * * @param string $provider 'openai', 'google', or 'anthropic'. * @param string $feature Feature key e.g. 'description', 'tags', 'image_generation'. * @return string */ public static function get_default_model_for_provider( $provider, $feature = '' ) { $capability = ( 'image_generation' === $feature ) ? 'image_generation' : 'text_generation'; $defaults = array( 'text_generation' => array( 'openai' => 'gpt-6-luna', 'anthropic' => 'claude-sonnet-5', 'google' => 'gemini-3.8-flash', ), 'image_generation' => array( 'openai' => 'gpt-image-2.5-sunburst', 'google' => 'gemini-3.1-flash-image-preview', ), ); if ( isset( $defaults[ $capability ][ $provider ] ) ) { return $defaults[ $capability ][ $provider ]; } return self::get_newest_model_for_provider( $provider, $feature ); } /** * Returns the newest available model for a provider/feature, i.e. whatever * currently sits at the top of that provider's dropdown in get_connector_models(). * That list is already ordered newest-first everywhere it's built (live AiClient * registry order, the static fallback tables, and the custom-API-key live fetch, * which explicitly sorts newest IDs first) — so "first item" IS "provider default" * in a way that tracks reality instead of a hardcoded model string going stale. * * @param string $provider 'openai', 'google', or 'anthropic'. * @param string $feature Feature key e.g. 'description', 'tags', 'image_generation'. * @return string Model ID, or '' if no models are available for this provider/feature. */ public static function get_newest_model_for_provider( $provider, $feature = '' ) { $capability = ( 'image_generation' === $feature ) ? 'image_generation' : 'text_generation'; $all_models = self::get_connector_models(); $models = $all_models[ $capability ][ $provider ]['models'] ?? array(); if ( ! empty( $models ) && isset( $models[0]['id'] ) ) { return (string) $models[0]['id']; } // Last-resort hardcoded fallback, only reached if the provider has no // models at all in either the live registry or the static tables above // (e.g. a brand-new provider ID that hasn't been added to $static yet). $last_resort = array( 'default' => array( 'openai' => 'gpt-4.1-mini', 'google' => 'gemini-2.5-flash', 'anthropic' => 'claude-3-5-sonnet-latest', ), 'image_generation' => array( 'openai' => 'gpt-image-2.5-sunburst', 'google' => 'gemini-2.0-flash-preview-image-generation', ), ); $group = $last_resort[ $feature ] ?? $last_resort['default']; return $group[ $provider ] ?? ''; } /** * Returns whether the WordPress AI Connectors screen (Settings > Connectors) * is available on this install — this ships in WordPress 7.0 and later. * Used to hide connector-dependent UI (the provider overview, links to * options-connectors.php) on older WordPress versions where that screen * simply doesn't exist. * * @return bool */ public static function has_ai_connectors_screen() { global $wp_version; if ( ! empty( $wp_version ) && version_compare( $wp_version, '7.0', '>=' ) ) { return true; } // Fallback: the Connectors API's own function/class, in case it was // backported via a feature plugin ahead of core. return function_exists( 'wp_ai_client_prompt' ) || class_exists( \WordPress\AiClient\AiClient::class ); } /** * Returns whether any AI Connector key is configured. * * @return bool */ public static function has_active_connector() { $use_custom = (bool) get_option( 'wtofe_use_custom_api_key', false ); if ( $use_custom && '' !== trim( (string) get_option( 'wtofe_chatgpt_api_key', '' ) ) ) { return true; } return self::has_connector_keys(); } /** * Returns whether any WordPress AI Connector key is configured * (does not count the plugin's own ChatGPT API key). * * @return bool */ public static function has_connector_keys() { foreach ( array( 'connectors_ai_openai_api_key', 'connectors_ai_google_api_key', 'connectors_ai_anthropic_api_key' ) as $option ) { if ( '' !== trim( (string) get_option( $option, '' ) ) ) { return true; } } return false; } /** * Returns the provider ID ('openai', 'google', 'anthropic') that owns the given model ID. * Returns empty string if the model is unknown. * * @param string $model_id * @return string */ private static function get_provider_for_model( $model_id ) { if ( empty( $model_id ) ) { return ''; } // Prefer WordPress AI Client registry (dynamic models) if ( class_exists( \WordPress\AiClient\AiClient::class ) ) { try { $registry = \WordPress\AiClient\AiClient::defaultRegistry(); foreach ( $registry->getRegisteredProviderIds() as $provider_id ) { if ( ! $registry->isProviderConfigured( $provider_id ) ) { continue; } $class = $registry->getProviderClassName( $provider_id ); foreach ( $class::modelMetadataDirectory()->listModelMetadata() as $meta ) { if ( (string) $meta->getId() === $model_id ) { return (string) $provider_id; } } } } catch ( \Throwable $e ) { // Silent fallback } } // Fallback to heuristic return self::guess_provider_from_prefix( $model_id ); } /** * Returns the provider ID ('openai', 'google', 'anthropic') that owns the given model ID. * Returns empty string if the model is unknown. * * @param string $model_id * @return string */ private static function guess_provider_from_prefix( $model_id ) { $m = strtolower( trim( $model_id ) ); // OpenAI if ( strpos( $m, 'gpt-' ) === 0 || strpos( $m, 'o' ) === 0 || strpos( $m, 'text-embedding-' ) === 0 || strpos( $m, 'dall-e-' ) === 0 || strpos( $m, 'gpt-image-' ) === 0 ) { return 'openai'; } // Google if ( strpos( $m, 'gemini-' ) === 0 || strpos( $m, 'imagen-' ) === 0 ) { return 'google'; } // Anthropic if ( strpos( $m, 'claude-' ) === 0 ) { return 'anthropic'; } return ''; } /** * Returns the wp_options key for a provider's connector API key. * * @param string $provider * @return string */ private static function get_connector_option_for_provider( $provider ) { $map = array( 'openai' => 'connectors_ai_openai_api_key', 'google' => 'connectors_ai_google_api_key', 'anthropic' => 'connectors_ai_anthropic_api_key', ); return $map[ $provider ] ?? ''; } }