<?php

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

delete_option('bs2fa_options');
delete_option('bs2fa_logs');

global $wpdb;
$bs2fa_meta_keys = [
    'bs2fa_totp_secret',
    'bs2fa_pending_totp_secret',
    'bs2fa_backup_codes',
    'bs2fa_webauthn_credentials',
    'bs2fa_webauthn_register_challenge',
    'bs2fa_webauthn_register_challenge_time',
    'bs2fa_enrollment_started_at',
    'bs2fa_trusted_devices',
    'bs2fa_login_contexts',
];

foreach ($bs2fa_meta_keys as $bs2fa_key) {
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->delete($wpdb->usermeta, ['meta_key' => $bs2fa_key], ['%s']);
}

$bs2fa_transient_patterns = [
    '_transient_bs2fa_%',
    '_transient_timeout_bs2fa_%',
    '_site_transient_bs2fa_%',
    '_site_transient_timeout_bs2fa_%',
];

foreach ($bs2fa_transient_patterns as $bs2fa_pattern) {
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->query(
        $wpdb->prepare(
            "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
            $wpdb->esc_like(str_replace('%', '', $bs2fa_pattern)) . '%'
        )
    );
}