<?php
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}

global $wpdb;


// 1. Delete all custom DB tables with 'surftrust_setting' in the name
$like = $wpdb->esc_like($wpdb->prefix . 'surftrust_settings') . '%';
$tables = $wpdb->get_col("SHOW TABLES LIKE '$like'");

foreach ($tables as $table) {

    $wpdb->query("DROP TABLE IF EXISTS `$table`");
}


// 2. Delete all custom DB tables with 'surftrust_analytics' in the name
$like = $wpdb->esc_like($wpdb->prefix . 'surftrust_analytics') . '%';
$tables = $wpdb->get_col("SHOW TABLES LIKE '$like'");

foreach ($tables as $table) {

    $wpdb->query("DROP TABLE IF EXISTS `$table`");
}





// 2. Delete options with 'surftrust_' in the option name
$options = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '%surftrust_%'");
foreach ($options as $option) {
    delete_option($option);
}

// 3. Delete transients with 'surftrust_' in their name
$transients = $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '%_transient_%surftrust_%'");

foreach ($transients as $transient_option) {
    // Extract the transient key
    if (preg_match('/_transient_timeout_(.+)/', $transient_option, $matches)) {
        delete_transient($matches[1]);
    } elseif (preg_match('/_transient_(.+)/', $transient_option, $matches)) {
        delete_transient($matches[1]);
    }
}
