<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class LVSW_Admin {
	public static function init() {
		add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) );
		add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
	}

	public static function admin_menu() {
		add_menu_page(
			esc_html__( 'Security Watchdog', LVSW_Plugin::TEXT_DOMAIN ),
			esc_html__( 'Security Watchdog', LVSW_Plugin::TEXT_DOMAIN ),
			'manage_options',
			LVSW_Plugin::MENU_SLUG,
			array( __CLASS__, 'settings_page' ),
			'dashicons-shield-alt',
			80
		);
	}

	public static function admin_init() {
		register_setting(
			'lvsw_settings',
			LVSW_Plugin::OPTION_KEY,
			array(
				'type'              => 'array',
				'sanitize_callback' => array( __CLASS__, 'validate_options' ),
			)
		);

		foreach ( array(
			'scanning'  => esc_html__( 'Scanning', LVSW_Plugin::TEXT_DOMAIN ),
			'hardening' => esc_html__( 'Hardening', LVSW_Plugin::TEXT_DOMAIN ),
			'alerts'    => esc_html__( 'Alerts', LVSW_Plugin::TEXT_DOMAIN ),
			'advanced'  => esc_html__( 'Advanced', LVSW_Plugin::TEXT_DOMAIN ),
		) as $tab => $title ) {
			add_settings_section( 'lvsw_' . $tab, $title, '__return_false', 'lvsw-tab-' . $tab );
		}

		// Scanning.
		self::add_field( 'enabled', 'Enable scanning and alerts', 'field_enabled', 'scanning' );
		self::add_field( 'scan_interval_hours', 'Scan interval', 'field_scan_interval', 'scanning' );
		self::add_field( 'fast_mode_enabled', 'Fast mode', 'field_fast_mode_enabled', 'scanning' );
		self::add_field( 'fast_mode_days', 'Fast mode window', 'field_fast_mode_days', 'scanning' );
		self::add_field( 'scan_core_recent_mtime', 'Core: recent changes (mtime)', 'field_scan_core_recent_mtime', 'scanning' );
		self::add_field( 'scan_core_suspicious', 'Core: suspicious PHP (last 7 days)', 'field_scan_core_suspicious', 'scanning' );
		self::add_field( 'scan_js', 'Scan JS files', 'field_scan_js', 'scanning' );
		self::add_field( 'scan_plugins', 'Scan plugins', 'field_scan_plugins', 'scanning' );
		self::add_field( 'scan_themes', 'Scan themes', 'field_scan_themes', 'scanning' );
		self::add_field( 'scan_muplugins', 'Scan MU-plugins', 'field_scan_muplugins', 'scanning' );
		self::add_field( 'scan_wpcontent_root', 'Scan wp-content root PHP', 'field_scan_wpcontent_root', 'scanning' );
		self::add_field( 'scan_uploads_php', 'Scan uploads PHP files', 'field_scan_uploads_php', 'scanning' );
		self::add_field( 'uploads_exec_detection', 'Detect executables in uploads', 'field_uploads_exec_detection', 'scanning' );
		self::add_field( 'scan_admins', 'Detect new / suspicious admins', 'field_scan_admins', 'scanning' );

		// Hardening.
		self::add_field( 'block_xmlrpc', 'Block XML-RPC', 'field_block_xmlrpc', 'hardening' );
		self::add_field( 'disable_application_passwords', 'Disable application passwords', 'field_disable_application_passwords', 'hardening' );
		self::add_field( 'block_file_mods', 'Block file editing', 'field_block_file_mods', 'hardening' );
		self::add_field( 'disallow_file_mods', 'Block file installs / updates', 'field_disallow_file_mods', 'hardening' );
		self::add_field( 'disable_deletion', 'Disable plugin / theme deletion', 'field_disable_deletion', 'hardening' );
		self::add_field( 'secure_head_cleanup', 'Secure head cleanup', 'field_secure_head_cleanup', 'hardening' );
		self::add_field( 'hide_login_errors', 'Hide login error messages', 'field_hide_login_errors', 'hardening' );
		self::add_field( 'limit_login_attempts', 'Limit login attempts', 'field_limit_login_attempts', 'hardening' );
		self::add_field( 'block_pingbacks', 'Block pingbacks', 'field_block_pingbacks', 'hardening' );
		self::add_field( 'block_user_enum', 'Block user enumeration', 'field_block_user_enum', 'hardening' );
		self::add_field( 'block_comments', 'Block comments (sitewide)', 'field_block_comments', 'hardening' );
		self::add_field( 'block_admin_password_reset', 'Block admin password reset', 'field_block_admin_password_reset', 'hardening' );

		// Alerts.
		self::add_field( 'notify_email', 'Notification email', 'field_notify_email', 'alerts' );
		self::add_field( 'alert_users_can_register', 'Alert: anyone can register', 'field_alert_users_can_register', 'alerts' );
		self::add_field( 'fix_disable_user_registration', 'Optional fix: disable registration', 'field_fix_disable_user_registration', 'alerts' );

		// Advanced.
		self::add_field( 'admin_hygiene_checks', 'Admin hygiene warnings', 'field_admin_hygiene_checks', 'advanced' );
	}

	private static function add_field( $id, $title, $callback, $tab ) {
		add_settings_field(
			$id,
			esc_html__( $title, LVSW_Plugin::TEXT_DOMAIN ),
			array( __CLASS__, $callback ),
			'lvsw-tab-' . $tab,
			'lvsw_' . $tab
		);
	}

	public static function enqueue_assets( $hook ) {
		if ( 'toplevel_page_' . LVSW_Plugin::MENU_SLUG !== $hook ) {
			return;
		}

		wp_enqueue_style(
			'lvsw-admin',
			LVSW_URL . 'assets/css/admin.css',
			array(),
			LVSW_VERSION
		);
	}

	public static function validate_options( $input ) {
		$input    = is_array( $input ) ? $input : array();
		$existing = LVSW_Plugin::get_opts();
		$clean    = $existing;

		$current_tab = isset( $_POST['lvsw_settings_tab'] )
			? sanitize_key( wp_unslash( $_POST['lvsw_settings_tab'] ) )
			: 'scanning';

		$tab_fields = array(
			'scanning' => array(
				'enabled',
				'scan_interval_hours',
				'fast_mode_enabled',
				'fast_mode_days',
				'scan_core_recent_mtime',
				'scan_core_suspicious',
				'scan_js',
				'scan_plugins',
				'scan_themes',
				'scan_muplugins',
				'scan_wpcontent_root',
				'scan_uploads_php',
				'uploads_exec_detection',
				'scan_admins',
			),
			'hardening' => array(
				'block_xmlrpc',
				'disable_application_passwords',
				'block_file_mods',
				'disallow_file_mods',
				'disable_deletion',
				'secure_head_cleanup',
				'hide_login_errors',
				'limit_login_attempts',
				'block_pingbacks',
				'block_user_enum',
				'block_comments',
				'block_admin_password_reset',
			),
			'alerts' => array(
				'notify_email',
				'alert_users_can_register',
				'fix_disable_user_registration',
			),
			'advanced' => array(
				'admin_hygiene_checks',
			),
		);

		if ( ! isset( $tab_fields[ $current_tab ] ) ) {
			$current_tab = 'scanning';
		}

		$checkbox_fields = array(
			'enabled',
			'fast_mode_enabled',
			'scan_core_recent_mtime',
			'scan_core_suspicious',
			'scan_js',
			'scan_plugins',
			'scan_themes',
			'scan_muplugins',
			'scan_wpcontent_root',
			'scan_uploads_php',
			'uploads_exec_detection',
			'scan_admins',
			'block_xmlrpc',
			'disable_application_passwords',
			'block_file_mods',
			'disallow_file_mods',
			'disable_deletion',
			'secure_head_cleanup',
			'hide_login_errors',
			'limit_login_attempts',
			'block_pingbacks',
			'block_user_enum',
			'block_comments',
			'block_admin_password_reset',
			'alert_users_can_register',
			'fix_disable_user_registration',
			'admin_hygiene_checks',
		);

		foreach ( $tab_fields[ $current_tab ] as $field ) {
			if ( in_array( $field, $checkbox_fields, true ) ) {
				$clean[ $field ] = ! empty( $input[ $field ] ) ? 1 : 0;
				continue;
			}

			switch ( $field ) {
				case 'notify_email':
					$clean['notify_email'] = isset( $input['notify_email'] ) ? sanitize_email( $input['notify_email'] ) : '';
					break;

				case 'scan_interval_hours':
					$clean['scan_interval_hours'] = isset( $input['scan_interval_hours'] )
						? (int) $input['scan_interval_hours']
						: $existing['scan_interval_hours'];
					break;

				case 'fast_mode_days':
					$clean['fast_mode_days'] = isset( $input['fast_mode_days'] )
						? (int) $input['fast_mode_days']
						: $existing['fast_mode_days'];
					break;
			}
		}

		$clean['signatures']   = ! empty( $existing['signatures'] ) && is_array( $existing['signatures'] )
			? $existing['signatures']
			: LVSW_Scanner::default_signatures();
		$clean['last_run']     = ! empty( $existing['last_run'] ) ? (int) $existing['last_run'] : 0;
		$clean['_v3_upgraded'] = 1;
		$clean                 = LVSW_Plugin::normalize_options( $clean );

		LVSW_Cron::reschedule_cron( true );

		return $clean;
	}

	private static function render_toggle( $key, $desc = '' ) {
		$opts    = LVSW_Plugin::get_opts();
		$checked = ! empty( $opts[ $key ] ) ? 1 : 0;

		echo '<label class="lvsw-toggle">';
		echo '<input type="checkbox" name="' . esc_attr( LVSW_Plugin::OPTION_KEY ) . '[' . esc_attr( $key ) . ']" value="1" ' . checked( 1, $checked, false ) . ' />';
		echo '<span class="lvsw-toggle-ui" aria-hidden="true"></span>';
		echo '</label>';

		if ( $desc ) {
			echo '<p class="description">' . wp_kses_post( $desc ) . '</p>';
		}
	}

	public static function settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'Insufficient permissions', LVSW_Plugin::TEXT_DOMAIN ) );
		}

		$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'scanning';

		$tabs = array(
			'scanning'  => esc_html__( 'Scanning', LVSW_Plugin::TEXT_DOMAIN ),
			'hardening' => esc_html__( 'Hardening', LVSW_Plugin::TEXT_DOMAIN ),
			'alerts'    => esc_html__( 'Alerts', LVSW_Plugin::TEXT_DOMAIN ),
			'advanced'  => esc_html__( 'Advanced', LVSW_Plugin::TEXT_DOMAIN ),
		);

		$opts      = LVSW_Plugin::get_opts();
		$stats     = get_option( LVSW_Plugin::STATS_OPTION, array() );
		$next      = wp_next_scheduled( LVSW_Plugin::CRON_HOOK );
		$blocked   = LVSW_Helpers::get_blocked_ips_this_month_count();
		$env       = LVSW_Helpers::get_environment_notes();
		$hardening = array_keys( array_filter( LVSW_Helpers::get_hardening_status( $opts ) ) );
		?>
		<div class="wrap lvsw-wrap">
			<h1><?php esc_html_e( 'Lumiverse Security Watchdog', LVSW_Plugin::TEXT_DOMAIN ); ?></h1>
            <img class="lvsw-banner" src="<?php echo esc_url( LVSW_URL . 'wp-security-watchdog-lite.png' ); ?>" alt="<?php esc_attr_e( 'Lumiverse Security Watchdog', LVSW_Plugin::TEXT_DOMAIN ); ?>"/>
			<?php settings_errors( 'lvsw_messages' ); ?>

			<div class="lvsw-card lvsw-status-card">
				<h2><?php esc_html_e( 'Health / Status', LVSW_Plugin::TEXT_DOMAIN ); ?></h2>
				<table class="widefat striped">
					<tbody>
						<tr>
							<td><strong><?php esc_html_e( 'Last scan time', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo ! empty( $opts['last_run'] ) ? esc_html( date_i18n( 'Y-m-d H:i:s', (int) $opts['last_run'] ) ) : esc_html__( 'Never', LVSW_Plugin::TEXT_DOMAIN ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'Next scheduled run', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo $next ? esc_html( date_i18n( 'Y-m-d H:i:s', $next ) ) : esc_html__( 'Not scheduled', LVSW_Plugin::TEXT_DOMAIN ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'Files scanned (last run)', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo esc_html( (string) ( (int) ( $stats['files_scanned'] ?? 0 ) ) ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'Warnings (last run)', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo esc_html( (string) ( (int) ( $stats['warnings'] ?? 0 ) ) ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'IPs blocked this month', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo esc_html( (string) (int) $blocked ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'Hardening enabled', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo empty( $hardening ) ? esc_html__( 'None', LVSW_Plugin::TEXT_DOMAIN ) : esc_html( implode( ', ', $hardening ) ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'Server environment notes', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td><?php echo esc_html( $env ); ?></td>
						</tr>
						<tr>
							<td><strong><?php esc_html_e( 'Fast mode', LVSW_Plugin::TEXT_DOMAIN ); ?></strong></td>
							<td>
								<?php
								echo empty( $opts['fast_mode_enabled'] )
									? esc_html__( 'Off', LVSW_Plugin::TEXT_DOMAIN )
									: esc_html( sprintf( __( 'On (%d day window)', LVSW_Plugin::TEXT_DOMAIN ), (int) $opts['fast_mode_days'] ) );
								?>
							</td>
						</tr>
					</tbody>
				</table>

				<div class="lvsw-status-actions">
					<form method="post">
						<?php wp_nonce_field( 'lvsw_run_scan_now', 'lvsw_run_scan_now_nonce' ); ?>
						<input type="hidden" name="lvsw_run_scan_now" value="1" />
						<?php submit_button( __( 'Run Scan Now', LVSW_Plugin::TEXT_DOMAIN ), 'secondary', 'submit', false ); ?>
					</form>

					<form method="post">
						<?php wp_nonce_field( 'lvsw_rebuild_baselines', 'lvsw_rebuild_baselines_nonce' ); ?>
						<input type="hidden" name="lvsw_rebuild_baselines" value="1" />
						<?php submit_button( __( 'Rebuild Baselines', LVSW_Plugin::TEXT_DOMAIN ), 'secondary', 'submit', false ); ?>
					</form>
				</div>
			</div>

			<h2 class="nav-tab-wrapper lvsw-nav">
				<?php foreach ( $tabs as $key => $label ) : ?>
					<a class="nav-tab <?php echo $tab === $key ? 'nav-tab-active' : ''; ?>" href="<?php echo esc_url( admin_url( 'admin.php?page=' . LVSW_Plugin::MENU_SLUG . '&tab=' . $key ) ); ?>">
						<?php echo esc_html( $label ); ?>
					</a>
				<?php endforeach; ?>
			</h2>

			<div class="lvsw-card">
				<form method="post" action="options.php">
					<?php settings_fields( 'lvsw_settings' ); ?>
					<input type="hidden" name="lvsw_settings_tab" value="<?php echo esc_attr( $tab ); ?>" />
					<?php do_settings_sections( 'lvsw-tab-' . ( isset( $tabs[ $tab ] ) ? $tab : 'scanning' ) ); ?>
					<?php submit_button(); ?>
				</form>
			</div>

			<div class="lvsw-card">
				<h2><?php esc_html_e( 'Recent Logs', LVSW_Plugin::TEXT_DOMAIN ); ?></h2>
				<?php $logs = get_option( LVSW_Plugin::LOG_OPTION, array() ); ?>

				<?php if ( empty( $logs ) ) : ?>
					<p class="lvsw-empty"><?php esc_html_e( 'No logs yet.', LVSW_Plugin::TEXT_DOMAIN ); ?></p>
				<?php else : ?>
					<ul class="lvsw-logs">
						<?php foreach ( array_slice( array_reverse( $logs ), 0, 30 ) as $entry ) : ?>
							<li>
								<strong class="lvsw-log-time"><?php echo esc_html( $entry['time'] ?? '' ); ?></strong>:
								<?php echo esc_html( $entry['summary'] ?? '' ); ?>
							</li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>
			</div>
		</div>
		<?php
	}

	public static function field_enabled() {
		self::render_toggle(
			'enabled',
			__( 'When enabled the plugin runs scheduled checks and sends alerts to the configured email.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_notify_email() {
		$opts = LVSW_Plugin::get_opts();

		echo '<input type="email" class="regular-text" name="' . esc_attr( LVSW_Plugin::OPTION_KEY ) . '[notify_email]" value="' . esc_attr( $opts['notify_email'] ) . '" />';
		echo '<p class="description">' . esc_html__( 'Leave empty to disable email reports.', LVSW_Plugin::TEXT_DOMAIN ) . '</p>';
	}

	public static function field_scan_interval() {
		$opts = LVSW_Plugin::get_opts();

		echo '<select name="' . esc_attr( LVSW_Plugin::OPTION_KEY ) . '[scan_interval_hours]">';
		foreach ( LVSW_Plugin::$allowed_intervals as $hours ) {
			echo '<option value="' . (int) $hours . '" ' . selected( (int) $opts['scan_interval_hours'], (int) $hours, false ) . '>' .
				esc_html( sprintf( __( 'Every %d hours', LVSW_Plugin::TEXT_DOMAIN ), (int) $hours ) ) .
			'</option>';
		}
		echo '</select>';
	}

	public static function field_fast_mode_enabled() {
		self::render_toggle(
			'fast_mode_enabled',
			__( 'Only scan files modified in the selected time window. Recommended for larger sites.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_fast_mode_days() {
		$opts = LVSW_Plugin::get_opts();

		echo '<select name="' . esc_attr( LVSW_Plugin::OPTION_KEY ) . '[fast_mode_days]">';
		foreach ( LVSW_Plugin::$allowed_fast_days as $days ) {
			echo '<option value="' . (int) $days . '" ' . selected( (int) $opts['fast_mode_days'], (int) $days, false ) . '>' .
				esc_html( sprintf( __( 'Last %d day(s)', LVSW_Plugin::TEXT_DOMAIN ), (int) $days ) ) .
			'</option>';
		}
		echo '</select>';
	}

	public static function field_scan_core_recent_mtime() {
		self::render_toggle(
			'scan_core_recent_mtime',
			__( 'Flags important core files modified in the last 7 days.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_scan_core_suspicious() {
		self::render_toggle(
			'scan_core_suspicious',
			__( 'Scans recent wp-admin and wp-includes PHP files for suspicious patterns.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_scan_js() { self::render_toggle( 'scan_js' ); }
	public static function field_scan_plugins() { self::render_toggle( 'scan_plugins' ); }
	public static function field_scan_themes() { self::render_toggle( 'scan_themes' ); }
	public static function field_scan_muplugins() { self::render_toggle( 'scan_muplugins' ); }

	public static function field_scan_wpcontent_root() {
		self::render_toggle(
			'scan_wpcontent_root',
			__( 'Checks drop-ins like db.php, cache.php, object-cache.php, advanced-cache.php and unexpected PHP in wp-content root.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_scan_uploads_php() {
		self::render_toggle(
			'scan_uploads_php',
			__( 'Scans uploads for PHP files. Malware often hides there.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_uploads_exec_detection() {
		self::render_toggle(
			'uploads_exec_detection',
			__( 'Flags executable files in uploads such as php, phtml, phar, cgi, asp, sh.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_scan_admins() { self::render_toggle( 'scan_admins' ); }
	public static function field_block_xmlrpc() { self::render_toggle( 'block_xmlrpc' ); }

	public static function field_disable_application_passwords() {
		self::render_toggle(
			'disable_application_passwords',
			__( 'Useful if the site does not use REST/API integrations with application passwords.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_block_file_mods() {
		self::render_toggle(
			'block_file_mods',
			__( 'Disables plugin/theme editing inside wp-admin. Keeps installs and updates allowed.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_disallow_file_mods() {
		self::render_toggle(
			'disallow_file_mods',
			__( 'Disables plugin/theme installs and updates. More aggressive than file editing block.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_disable_deletion() {
		self::render_toggle(
			'disable_deletion',
			__( 'Removes delete actions for plugins and themes in wp-admin.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_secure_head_cleanup() { self::render_toggle( 'secure_head_cleanup' ); }
	public static function field_hide_login_errors() { self::render_toggle( 'hide_login_errors' ); }

	public static function field_limit_login_attempts() {
		self::render_toggle(
			'limit_login_attempts',
			__( 'Locks login for 30 minutes after 3 failed attempts from the same IP.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_block_pingbacks() { self::render_toggle( 'block_pingbacks' ); }
	public static function field_block_user_enum() { self::render_toggle( 'block_user_enum' ); }
	public static function field_block_comments() { self::render_toggle( 'block_comments' ); }

	public static function field_block_admin_password_reset() {
		self::render_toggle(
			'block_admin_password_reset',
			__( 'Disables the standard WordPress password reset flow for administrator accounts only.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_alert_users_can_register() {
		self::render_toggle(
			'alert_users_can_register',
			__( 'Alerts if “Anyone can register” is enabled in WordPress settings.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_fix_disable_user_registration() {
		self::render_toggle(
			'fix_disable_user_registration',
			__( 'If registration is enabled, automatically disable it.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}

	public static function field_admin_hygiene_checks() {
		self::render_toggle(
			'admin_hygiene_checks',
			__( 'Warning-only checks such as common admin usernames.', LVSW_Plugin::TEXT_DOMAIN )
		);
	}
}

add_action(
	'admin_init',
	static function () {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if (
			! empty( $_POST['lvsw_run_scan_now'] ) &&
			! empty( $_POST['lvsw_run_scan_now_nonce'] ) &&
			wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['lvsw_run_scan_now_nonce'] ) ), 'lvsw_run_scan_now' )
		) {
			LVSW_Scanner::do_scan();
			LVSW_Helpers::log_event( 'Manual scan executed from admin' );
			add_settings_error( 'lvsw_messages', 'lvsw_scan_now', __( 'Scan completed.', LVSW_Plugin::TEXT_DOMAIN ), 'updated' );
		}

		if (
			! empty( $_POST['lvsw_rebuild_baselines'] ) &&
			! empty( $_POST['lvsw_rebuild_baselines_nonce'] ) &&
			wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['lvsw_rebuild_baselines_nonce'] ) ), 'lvsw_rebuild_baselines' )
		) {
			LVSW_Scanner::initialize_baselines();
			add_settings_error( 'lvsw_messages', 'lvsw_rebuild_baselines', __( 'Baselines rebuilt.', LVSW_Plugin::TEXT_DOMAIN ), 'updated' );
		}
	}
);