validate_ajax_request() ) { return; } try { $debug_log = $this->get_current_debug_log(); wp_send_json_success( array( 'debug_log' => $debug_log, 'message' => __( 'Debug log downloaded', 'accessibility-audio-tts-text-to-speech-for-articles' ), 'timestamp' => current_time( 'mysql' ), ) ); } catch ( \Exception $e ) { $this->handle_ajax_exception( $e, 'Get debug log' ); } } /** * Simple test endpoint */ public function test_simple() { if ( ! $this->validate_ajax_request() ) { return; } try { accessibility_audio_tts_debug_log( '[Debug_Controller] Simple test endpoint called' ); wp_send_json_success( array( 'message' => __( 'Simple test completed successfully', 'accessibility-audio-tts-text-to-speech-for-articles' ), 'timestamp' => current_time( 'mysql' ), 'php_version' => PHP_VERSION, 'wordpress_version' => get_bloginfo( 'version' ), ) ); } catch ( \Exception $e ) { $this->handle_ajax_exception( $e, 'Simple test' ); } } /** * Clear ChatGPT key */ public function clear_chatgpt_key() { if ( ! $this->validate_ajax_request() ) { return; } try { accessibility_audio_tts_debug_log( '[Debug_Controller] Clearing ChatGPT API key' ); delete_option( 'accessibility_audio_tts_chatgpt_api_key' ); // Verify deletion. $remaining_key = get_option( 'accessibility_audio_tts_chatgpt_api_key', '' ); if ( empty( $remaining_key ) ) { wp_send_json_success( array( 'message' => __( 'ChatGPT key deleted successfully', 'accessibility-audio-tts-text-to-speech-for-articles' ), ) ); } else { wp_send_json_error( array( 'message' => __( 'Error deleting ChatGPT key', 'accessibility-audio-tts-text-to-speech-for-articles' ), ) ); } } catch ( \Exception $e ) { $this->handle_ajax_exception( $e, 'Clear ChatGPT key' ); } } /** * Fix voice issue (diagnostic endpoint) */ public function fix_voice_issue() { if ( ! $this->validate_ajax_request() ) { return; } try { accessibility_audio_tts_debug_log( '[Debug_Controller] Voice issue diagnostic started' ); $diagnostic_results = array( 'cached_voices' => $this->diagnose_cached_voices(), 'api_connectivity' => $this->diagnose_api_connectivity(), 'voice_settings' => $this->diagnose_voice_settings(), 'permissions' => $this->diagnose_permissions(), ); $fixes_applied = $this->apply_voice_fixes( $diagnostic_results ); wp_send_json_success( array( 'message' => __( 'Voice diagnostics completed', 'accessibility-audio-tts-text-to-speech-for-articles' ), 'diagnostic_results' => $diagnostic_results, 'fixes_applied' => $fixes_applied, 'timestamp' => current_time( 'mysql' ), ) ); } catch ( \Exception $e ) { $this->handle_ajax_exception( $e, 'Fix voice issue' ); } } /** * Get current debug log content */ private function get_current_debug_log() { if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { $log_file = \Accessibility_Audio_TTS\Core\Privacy_Lifecycle::latest_log_file(); if ( '' !== $log_file ) { return \Accessibility_Audio_TTS\Core\Privacy_Lifecycle::read_log_tail( $log_file, 50 ); } } return __( 'Debug log is not available or empty', 'accessibility-audio-tts-text-to-speech-for-articles' ); } /** * Diagnose cached voices. */ private function diagnose_cached_voices() { $cached_voices = get_option( 'accessibility_audio_tts_voices_list', array() ); return array( 'count' => count( $cached_voices ), 'status' => count( $cached_voices ) > 0 ? 'ok' : 'empty', 'last_updated' => get_option( 'accessibility_audio_tts_voices_last_updated', 'never' ), ); } /** * Diagnose API connectivity. */ private function diagnose_api_connectivity() { $api_key = get_option( 'accessibility_audio_tts_api_key', '' ); return array( 'api_key_set' => ! empty( $api_key ), 'api_key_length' => strlen( $api_key ), 'status' => ! empty( $api_key ) ? 'configured' : 'missing', ); } /** * Diagnose voice settings. */ private function diagnose_voice_settings() { return array( 'voice_id' => get_option( 'accessibility_audio_tts_voice_id', '' ), 'voice_name' => get_option( 'accessibility_audio_tts_voice_name', '' ), 'model' => get_option( 'accessibility_audio_tts_model', 'eleven_multilingual_v2' ), ); } /** * Diagnose permissions. */ private function diagnose_permissions() { $upload_dir = wp_upload_dir(); return array( 'uploads_writable' => File_Validation_Helper::is_directory_writable( $upload_dir['basedir'] ), 'uploads_path' => $upload_dir['basedir'], ); } /** * Apply automatic fixes for voice issues. * * @param array $diagnostic_results Diagnostic results array. */ private function apply_voice_fixes( $diagnostic_results ) { $fixes = array(); // Clear cache if voices are corrupted. if ( 'empty' === $diagnostic_results['cached_voices']['status'] ) { delete_option( 'accessibility_audio_tts_voices_list' ); $fixes[] = 'Cleared corrupted voice cache'; } // Set default model if missing. if ( empty( $diagnostic_results['voice_settings']['model'] ) ) { update_option( 'accessibility_audio_tts_model', 'eleven_multilingual_v2' ); $fixes[] = 'Set default model'; } return $fixes; } }