validate_debug_mode_request() ) { return; } try { $this->configure_debug_mode(); $this->log_debug_mode_info(); $this->send_debug_mode_response(); } catch ( \Exception $e ) { accessibility_audio_tts_error_log( 'Enable debug mode exception: ' . $e->getMessage() ); wp_send_json_error( array( 'message' => 'Error toggling debug mode: ' . $e->getMessage(), ) ); } } /** * Validates debug mode request permissions and security * * @return bool */ private function validate_debug_mode_request() { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Permission denied', 'accessibility-audio-tts-text-to-speech-for-articles' ) ); return false; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ), 'accessibility_audio_tts_nonce' ) ) { wp_send_json_error( __( 'Invalid security token', 'accessibility-audio-tts-text-to-speech-for-articles' ) ); return false; } return true; } /** * Configures debug mode settings */ private function configure_debug_mode() { // Force disable production mode. if ( defined( 'ACCESSIBILITY_AUDIO_TTS_PRODUCTION' ) ) { // Can't change constant, but we can log that it's set. accessibility_audio_tts_debug_log( 'ACCESSIBILITY_AUDIO_TTS_PRODUCTION is defined and set to: ' . ( ACCESSIBILITY_AUDIO_TTS_PRODUCTION ? 'true' : 'false' ), true ); } // Set transient to force debug mode for next requests. set_transient( 'accessibility_audio_tts_force_debug', true, 3600 ); // 1 hour. } /** * Logs debug mode information */ private function log_debug_mode_info() { accessibility_audio_tts_debug_log( '=== DEBUG MODE ENABLED ===', true ); accessibility_audio_tts_debug_log( 'Time: ' . current_time( 'mysql' ), true ); accessibility_audio_tts_debug_log( 'User: ' . get_current_user_id(), true ); $this->log_wp_debug_status(); } /** * Logs WordPress debug status */ private function log_wp_debug_status() { $wp_debug_status = defined( 'WP_DEBUG' ) ? ( WP_DEBUG ? 'true' : 'false' ) : 'undefined'; accessibility_audio_tts_debug_log( 'WP_DEBUG: ' . $wp_debug_status, true ); $wp_debug_log_status = defined( 'WP_DEBUG_LOG' ) ? ( WP_DEBUG_LOG ? 'true' : 'false' ) : 'undefined'; accessibility_audio_tts_debug_log( 'WP_DEBUG_LOG: ' . $wp_debug_log_status, true ); } /** * Sends success response for debug mode enabling */ private function send_debug_mode_response() { wp_send_json_success( array( 'message' => 'Debug mode enabled for 1 hour', 'debug_enabled' => true, 'transient_set' => get_transient( 'accessibility_audio_tts_force_debug' ), 'timestamp' => current_time( 'mysql' ), ) ); } /** * Checks if debug mode is currently active * * @return bool */ public function is_debug_mode_active() { return get_transient( 'accessibility_audio_tts_force_debug' ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( ! defined( 'ACCESSIBILITY_AUDIO_TTS_PRODUCTION' ) || ! ACCESSIBILITY_AUDIO_TTS_PRODUCTION ); } /** * Disables debug mode by clearing the transient */ public function disable_debug_mode() { delete_transient( 'accessibility_audio_tts_force_debug' ); accessibility_audio_tts_debug_log( '=== DEBUG MODE DISABLED ===', true ); } /** * Gets current debug mode configuration * * @return array */ public function get_debug_mode_config() { return array( 'transient_active' => (bool) get_transient( 'accessibility_audio_tts_force_debug' ), 'wp_debug' => defined( 'WP_DEBUG' ) ? WP_DEBUG : false, 'wp_debug_log' => defined( 'WP_DEBUG_LOG' ) ? WP_DEBUG_LOG : false, 'production_mode' => defined( 'ACCESSIBILITY_AUDIO_TTS_PRODUCTION' ) ? ACCESSIBILITY_AUDIO_TTS_PRODUCTION : false, 'debug_active' => $this->is_debug_mode_active(), 'error_reporting' => 0, 'display_errors' => '0', ); } }