<?php
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}
// Remove plugin options
$options = get_option('ultimate_security_options', []);
$uninstall_enabled = isset($options['more']['extra']['enable_delete_plugin_data']) ? $options['more']['extra']['enable_delete_plugin_data'] : false;

if ($uninstall_enabled) {
    global $wpdb;

    // Drop all plugin tables starts with ultimate_security_
    $tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}ultimate_security_%'");
    foreach ($tables as $table) {
        $wpdb->query("DROP TABLE IF EXISTS {$table}");
    }

    // Delete plugin options
    $wpdb->query(
        "DELETE FROM {$wpdb->options}
        WHERE option_name LIKE 'ultimate_security%'
        OR option_name LIKE 'ultimate-security%'"
    );

    // Delete user meta
    $wpdb->query(
        "DELETE FROM {$wpdb->usermeta}
        WHERE meta_key LIKE 'ultimate_security%'
        OR meta_key LIKE 'ultimate-security%'"
    );
}
