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( 'adminease_after_field_render', [ $this, 'adminease_after_field_render' ] );
add_action( 'wp_ajax_adminease_get_debug_log', [ $this, 'ajax_get_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',
],
],
[
'type' => 'select',
'id' => 'debug-log-auto-refresh-interval',
'name' => 'adminease[debug][debug_log_auto_refresh_interval]',
'value' => $this->settings['debug_log_auto_refresh_interval'] ?? '10',
'label_class' => 'adminease-label',
'input_class' => 'form-control',
'wrapper_class' => 'form-group-child',
'label' => __( 'Debug Log Auto-Refresh Interval', 'adminease' ),
'description' => __( 'Set the interval for auto-refreshing the debug log viewer.', 'adminease' ),
'options' => [
/* translators: %d: number of seconds for the auto-refresh interval */
'3' => sprintf( __( 'Every %d seconds', 'adminease' ), 3 ),
/* translators: %d: number of seconds for the auto-refresh interval */
'5' => sprintf( __( 'Every %d seconds', 'adminease' ), 5 ),
/* translators: %d: number of seconds for the auto-refresh interval */
'10' => sprintf( __( 'Every %d seconds', 'adminease' ), 10 ),
/* translators: %d: number of seconds for the auto-refresh interval */
'30' => sprintf( __( 'Every %d seconds', 'adminease' ), 30 ),
/* translators: %d: number of seconds for the auto-refresh interval */
'60' => sprintf( __( 'Every %d seconds', 'adminease' ), 60 ),
],
'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_get_debug_log' );
$data['security']['clearDebugLog'] = wp_create_nonce( 'adminease_clear_debug_log' );
$data['i18n']['confirmClearDebugLog'] = esc_html__( 'Are you sure you want to clear the debug log?', 'adminease' );
$data['i18n']['debugLogEmpty'] = esc_html__( 'Debug log is empty.', 'adminease' );
$data['i18n']['debugLogRefreshError'] = esc_html__( 'Failed to get debug log. Refresh the page and try again.', '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 after render logic for the specified field.
*
* @param array $field The field information, including the 'id' property used for conditional logic.
*
* @return void
*/
public function adminease_after_field_render( array $field ) {
if( 'wp-debug' !== $field['id'] ) {
return;
}
$settings = $this->settings;
include_once ADMINEASE_DIR . 'partials/wp-debug.php';
}
/**
* Handles the AJAX request to get and retrieve the contents of the debug log file.
* Verifies user permissions and nonce for security.
* @return void
*/
public function ajax_get_debug_log(): void {
// Verify nonce
if( !isset( $_POST['security'] ) || !wp_verify_nonce( sanitize_key( wp_unslash( $_POST['security'] ) ), 'adminease_get_debug_log' ) ) {
wp_send_json_error( new WP_Error( 'security_error', esc_html__( 'Security check failed.', 'adminease' ) ) );
}
// Check user capabilities
if( !current_user_can( 'manage_options' ) ) {
wp_send_json_error( new WP_Error( 'permission_error', esc_html__( 'Insufficient permissions.', 'adminease' ) ) );
}
$file_handler = Plugin::$FileHandler;
$contents = $file_handler->read_debug_log();
if( false === $contents ) {
$contents = esc_html__( 'Debug log file does not exist.', 'adminease' );
} else {
$contents = esc_html( $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( esc_html__( '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( esc_html__( 'Security check failed.', 'adminease' ), 403 );
}
$file_handler = Plugin::$FileHandler;
$result = $file_handler->clear_debug_log();
if( $result ) {
wp_send_json_success( esc_html__( 'Debug log cleared successfully.', 'adminease' ) );
} else {
wp_send_json_error( new WP_Error( 'clear_error', esc_html__( 'Failed to clear debug log.', 'adminease' ) ) );
}
}
}