settings = Plugin::get_settings( 'debug' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); add_filter( 'adminease_localize_script', [ $this, 'adminease_localize_script' ] ); add_action( 'adminease_settings_saved', [ $this, 'adminease_settings_saved' ] ); add_action( 'wp_ajax_adminease_refresh_debug_log', [ $this, 'ajax_refresh_debug_log' ] ); add_action( 'wp_ajax_adminease_clear_debug_log', [ $this, 'ajax_clear_debug_log' ] ); } /** * Adds custom settings fields related to debugging to the Adminease security settings section. * * @param array $fields The original array of settings fields before modification. * * @return array The modified array of settings fields with added debugging options. */ public function adminease_settings_fields( array $fields ): array { $fields['debug']['fields'][] = [ 'type' => 'switch', 'id' => 'wp-debug', 'name' => 'adminease[debug][wp_debug]', 'value' => $this->settings['wp_debug'] ?? '', 'label_class' => 'adminease-switch', 'input_class' => 'form-control toggle-field', 'label' => __( 'WP Debug', 'adminease' ), 'description' => __( 'The WP_DEBUG setting in WordPress is used to turn on debugging mode. When enabled, it displays PHP errors, warnings, and notices directly on your site—helping developers and site owners identify problems during development or troubleshooting. It’s especially useful when plugins or themes aren’t working as expected. However, it should never be left on in a live site as it can expose sensitive information to visitors.', 'adminease' ), 'child_fields' => [ [ 'type' => 'switch', 'id' => 'wp-debug-log', 'name' => 'adminease[debug][wp_debug_log]', 'value' => $this->settings['wp_debug_log'] ?? '', 'label_class' => 'adminease-switch', 'input_class' => 'form-control', 'wrapper_class' => 'form-group-child', 'label' => __( 'WP Debug Log', 'adminease' ), 'description' => __( 'Enable WP Debug Log.', 'adminease' ), 'attributes' => [ 'data-parent' => 'wp-debug', ], ], [ 'type' => 'switch', 'id' => 'wp-debug-display', 'name' => 'adminease[debug][wp_debug_display]', 'value' => $this->settings['wp_debug_display'] ?? '', 'label_class' => 'adminease-switch', 'input_class' => 'form-control', 'wrapper_class' => 'form-group-child', 'label' => __( 'WP Debug Display', 'adminease' ), 'description' => __( 'Enable WP Debug Display.', 'adminease' ), 'attributes' => [ 'data-parent' => 'wp-debug', ], ], ], ]; return $fields; } /** * Localizes script data by adding security nonces and internationalized strings. * * @param array $data An array used for script localization. It can be updated with additional localized data. * * @return array Returns an array containing localized data including security nonces and internationalized strings. */ public function adminease_localize_script( array $data ): array { $data['security']['refreshDebugLog'] = wp_create_nonce( 'adminease_refresh_debug_log' ); $data['security']['clearDebugLog'] = wp_create_nonce( 'adminease_clear_debug_log' ); $data['i18n']['confirmClearDebugLog'] = __( 'Are you sure you want to clear the debug log?', 'adminease' ); $data['i18n']['debugLogEmpty'] = __( 'Debug log is empty.', 'adminease' ); return $data; } /** * Saves the sanitized settings and updates WordPress configuration constants based on provided settings. * * @param array $sanitized_settings An array of sanitized settings which includes debug options such as 'wp_debug', 'wp_debug_log', and 'wp_debug_display'. * * @return void */ public function adminease_settings_saved( array $sanitized_settings ): void { $file_handler = Plugin::$FileHandler; $file_handler->stack_wp_config_constant( 'WP_DEBUG', (bool) $sanitized_settings['debug']['wp_debug'] ); $file_handler->stack_wp_config_constant( 'WP_DEBUG_LOG', (bool) $sanitized_settings['debug']['wp_debug_log'] ); $file_handler->stack_wp_config_constant( 'WP_DEBUG_DISPLAY', (bool) $sanitized_settings['debug']['wp_debug_display'] ); } /** * Handles the AJAX request to refresh and retrieve the contents of the debug log file. * Verifies user permissions and nonce for security. * @return void */ public function ajax_refresh_debug_log(): void { // Check user capabilities if( !current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Insufficient permissions.', 'adminease' ), 403 ); } // Verify nonce if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_refresh_debug_log' ) ) { wp_send_json_error( __( 'Security check failed.', 'adminease' ), 403 ); } $file_handler = Plugin::$FileHandler; $contents = $file_handler->read_debug_log(); if( false === $contents ) { $contents = esc_html__( 'Debug log file does not exist.', 'adminease' ); } else { // Use htmlspecialchars to avoid breaking HTML $contents = htmlspecialchars( $contents ); } wp_send_json_success( [ 'contents' => $contents ] ); } /** * Handles an AJAX request to clear the debug log. * Checks user capabilities and verifies the security nonce before clearing * the debug log. Responds with a success or error message based on the * operation result. * @return void */ public function ajax_clear_debug_log(): void { // Check user capabilities if( !current_user_can( 'manage_options' ) ) { wp_send_json_error( __( 'Insufficient permissions.', 'adminease' ), 403 ); } // Verify nonce if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_clear_debug_log' ) ) { wp_send_json_error( __( 'Security check failed.', 'adminease' ), 403 ); } $file_handler = Plugin::$FileHandler; $result = $file_handler->clear_debug_log(); if( $result ) { wp_send_json_success( __( 'Debug log cleared successfully.', 'adminease' ) ); } else { wp_send_json_error( __( 'Failed to clear debug log.', 'adminease' ) ); } } }