api_key = trim( $raw_api_key ); accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] TRIMMED API key length: ' . strlen( $this->api_key ), true ); } /** * Check if the API is available (has valid API key). * * @return bool True if API is available. */ public function is_available() { return ! empty( $this->api_key ) && $this->validate_api_key( $this->api_key ); } /** * Get current API key. * * @return string Current API key. */ public function get_api_key() { return $this->api_key; } /** * Update API key. * * @param string $api_key New API key. * @return bool Success status. */ public function update_api_key( $api_key ) { $this->api_key = trim( $api_key ); update_option( 'accessibility_audio_tts_chatgpt_api_key', $this->api_key ); return true; } /** * Get all configuration settings. * * @return array Configuration settings. */ public function get_settings() { return array( 'api_key' => $this->api_key, 'has_api_key' => ! empty( $this->api_key ), 'is_valid' => $this->is_available(), ); } /** * Update all settings. * * @param array $settings Settings to update. * @return bool Success status. */ public function update_settings( $settings ) { if ( isset( $settings['api_key'] ) ) { $this->update_api_key( $settings['api_key'] ); } return true; } /** * Validate API key format. * * @param string $api_key API key to validate. * @return bool True if valid format. */ public function validate_api_key( $api_key ) { if ( empty( $api_key ) ) { return false; } // Check format and length based on key type. if ( strpos( $api_key, 'sk-proj-' ) === 0 ) { // New format keys (sk-proj-) are around 164 characters. return strlen( $api_key ) >= 100 && strlen( $api_key ) <= 200; } elseif ( strpos( $api_key, 'sk-' ) === 0 ) { // Legacy format keys (sk-) are around 56 characters. return strlen( $api_key ) >= 40 && strlen( $api_key ) <= 80; } return false; } /** * Clean and reset API key (fix for corrupted keys). * * @param string $new_key New API key to set. * @return bool Success status. */ public function clean_and_reset_api_key( $new_key ) { accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: Input key length: ' . strlen( $new_key ), true ); accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: Input key starts: ' . substr( $new_key, 0, 15 ), true ); // Clean the key - trim and validate format. $cleaned_key = trim( $new_key ); // Validate format. if ( ! $this->validate_api_key( $cleaned_key ) ) { accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: Invalid key format after cleaning', true ); return false; } // Force delete old key first. delete_option( 'accessibility_audio_tts_chatgpt_api_key' ); accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: Old key deleted from database', true ); // Add new key. add_option( 'accessibility_audio_tts_chatgpt_api_key', $cleaned_key, '', 'no' ); accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: New key added to database', true ); // Verify what was actually saved. $saved_key = get_option( 'accessibility_audio_tts_chatgpt_api_key', '' ); accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: Saved key length: ' . strlen( $saved_key ), true ); accessibility_audio_tts_debug_log( '[ChatGPT_Config_Manager] CLEAN_RESET: Keys match: ' . ( $cleaned_key === $saved_key ? 'YES' : 'NO' ), true ); if ( $cleaned_key === $saved_key ) { $this->api_key = $cleaned_key; return true; } return false; } /** * Clear all configuration. * * @return bool Success status. */ public function clear_config() { $this->api_key = ''; delete_option( 'accessibility_audio_tts_chatgpt_api_key' ); return true; } /** * Get masked API key for display. * * @return string Masked API key. */ public function get_masked_api_key() { if ( empty( $this->api_key ) ) { return ''; } $length = strlen( $this->api_key ); if ( $length <= 10 ) { return str_repeat( '*', $length ); } return substr( $this->api_key, 0, 4 ) . str_repeat( '*', $length - 8 ) . substr( $this->api_key, -4 ); } /** * Get configuration status. * * @return array Status information. */ public function get_status() { return array( 'configured' => ! empty( $this->api_key ), 'valid_format' => $this->validate_api_key( $this->api_key ), 'api_available' => $this->is_available(), 'key_type' => $this->get_key_type(), 'masked_key' => $this->get_masked_api_key(), ); } /** * Get API key type. * * @return string Key type (new, legacy, invalid, or empty). */ private function get_key_type() { if ( empty( $this->api_key ) ) { return 'empty'; } if ( strpos( $this->api_key, 'sk-proj-' ) === 0 ) { return 'new'; } elseif ( strpos( $this->api_key, 'sk-' ) === 0 ) { return 'legacy'; } return 'invalid'; } }