text_processor = $text_processor; $this->config_manager = $config_manager; } /** * Test API connection. * * @return array Test result with success status and details. */ public function test_api_connection() { try { if ( ! $this->config_manager || ! $this->config_manager->get_api_key() ) { return array( 'success' => false, 'error' => 'API key is required', ); } if ( ! $this->text_processor ) { return array( 'success' => false, 'error' => 'Text processor not available', ); } // Run the processing test. $result = $this->text_processor->test_processing(); // Handle the test result. if ( $result['success'] ) { return array( 'success' => true, 'message' => 'API connection successful', 'test_result' => $result['test_result'] ?? '', 'optimization_status' => 'Text optimization successful', 'tokens_used' => $result['tokens_used'] ?? null, ); } else { return array( 'success' => false, 'error' => $result['error'] ?? 'API test failed', ); } } catch ( \Exception $e ) { // For quota exceeded errors, add a special prefix to indicate yellow color. if ( $e->getMessage() === 'CHATGPT_QUOTA_EXCEEDED' ) { return array( 'success' => false, 'error' => '[YELLOW]' . __( 'Przekroczono limit API ChatGPT', 'accessibility-audio-tts-text-to-speech-for-articles' ), ); } return array( 'success' => false, 'error' => __( 'API test failed: ', 'accessibility-audio-tts-text-to-speech-for-articles' ) . $e->getMessage(), ); } } /** * Test API with custom text. * * @param string $test_text Custom text to test with. * @return array Test result. */ public function test_with_custom_text( $test_text ) { try { if ( ! $this->config_manager || ! $this->config_manager->get_api_key() ) { return array( 'success' => false, 'error' => 'API key is required', ); } if ( ! $this->text_processor ) { return array( 'success' => false, 'error' => 'Text processor not available', ); } // Process the custom text. $result = $this->text_processor->fine_tune_text( $test_text ); if ( $result['success'] && $result['optimized'] ) { return array( 'success' => true, 'message' => 'Custom text processing successful', 'original_text' => $test_text, 'optimized_text' => $result['text'], 'tokens_used' => $result['tokens'] ?? null, ); } else { return array( 'success' => false, 'error' => 'Custom text processing failed', ); } } catch ( \Exception $e ) { return array( 'success' => false, 'error' => 'Custom test failed: ' . $e->getMessage(), ); } } /** * Validate API configuration. * * @return array Validation result. */ public function validate_configuration() { if ( ! $this->config_manager ) { return array( 'valid' => false, 'error' => 'Configuration manager not available', ); } $status = $this->config_manager->get_status(); return array( 'valid' => $status['api_available'], 'configured' => $status['configured'], 'valid_format' => $status['valid_format'], 'key_type' => $status['key_type'], 'error' => ! $status['api_available'] ? 'API configuration invalid' : null, ); } /** * Get comprehensive test report. * * @return array Complete test report. */ public function get_test_report() { $config_validation = $this->validate_configuration(); if ( ! $config_validation['valid'] ) { return array( 'overall_status' => 'failed', 'config_status' => $config_validation, 'connection_test' => null, 'error' => $config_validation['error'], ); } $connection_test = $this->test_api_connection(); return array( 'overall_status' => $connection_test['success'] ? 'passed' : 'failed', 'config_status' => $config_validation, 'connection_test' => $connection_test, 'error' => $connection_test['success'] ? null : $connection_test['error'], ); } /** * Quick health check. * * @return bool True if API is healthy. */ public function is_healthy() { try { $config_validation = $this->validate_configuration(); if ( ! $config_validation['valid'] ) { return false; } $connection_test = $this->test_api_connection(); return $connection_test['success']; } catch ( \Exception $e ) { return false; } } }