| @@ -19,12 +19,35 @@ | ||
| 19 | 19 | class ProviderFactory { |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * Per-platform API key settings are stored as ai_api_key_<platform>. |
| 23 | + * | |
| 24 | + * Deliberately private: OpenAI does NOT follow this scheme (see | |
| 25 | + * OPENAI_KEY_FIELD), so hand-concatenating the prefix would silently read | |
| 26 | + * the wrong — always empty — setting for the default platform. Go through | |
| 27 | + * key_field_for() instead; it is the only sanctioned way to name a key field. | |
| 23 | 28 | */ |
| 24 | - const KEY_PREFIX = 'ai_api_key_'; | |
| 29 | + private const KEY_PREFIX = 'ai_api_key_'; | |
| 25 | 30 | |
| 26 | 31 | /** |
| 32 | + * OpenAI keeps the original single-provider field name. Write with AI has | |
| 33 | + * always been OpenAI-only, so the stored key is already the OpenAI key — | |
| 34 | + * reusing the name means existing installs need no migration and no user | |
| 35 | + * has to re-enter anything when multi-platform support lands. | |
| 36 | + */ | |
| 37 | + const OPENAI_KEY_FIELD = 'ai_autowrite_api_key'; | |
| 38 | + | |
| 39 | + /** | |
| 40 | + * Settings key that holds the API key for a platform. | |
| 41 | + * | |
| 42 | + * @param string $platform | |
| 43 | + * @return string | |
| 44 | + */ | |
| 45 | + public static function key_field_for( $platform ) { | |
| 46 | + return 'openai' === $platform ? self::OPENAI_KEY_FIELD : self::KEY_PREFIX . $platform; | |
| 47 | + } | |
| 48 | + | |
| 49 | + /** | |
| 27 | 50 | * @var Settings |
| 28 | 51 | */ |
| 29 | 52 | private $settings; |
| 30 | 53 | |
| @@ -69,20 +92,15 @@ | ||
| 69 | 92 | return $this->is_supported( $platform ) ? $platform : 'openai'; |
| 70 | 93 | } |
| 71 | 94 | |
| 72 | 95 | /** |
| 73 | - * Stored API key for a platform. OpenAI falls back to the pre-multi-platform | |
| 74 | - * `ai_autowrite_api_key` so existing installs keep working before migration. | |
| 96 | + * Stored API key for a platform. | |
| 75 | 97 | * |
| 76 | 98 | * @param string $platform |
| 77 | 99 | * @return string |
| 78 | 100 | */ |
| 79 | 101 | public function api_key_for( $platform ) { |
| 80 | - $key = (string) $this->settings->get( self::KEY_PREFIX . $platform, '' ); | |
| 81 | - if ( '' === $key && 'openai' === $platform ) { | |
| 82 | - $key = (string) $this->settings->get( 'ai_autowrite_api_key', '' ); | |
| 83 | - } | |
| 84 | - return $key; | |
| 102 | + return (string) $this->settings->get( self::key_field_for( $platform ), '' ); | |
| 85 | 103 | } |
| 86 | 104 | |
| 87 | 105 | /** |
| 88 | 106 | * Resolved global model for the active platform. Falls back through the |