<?php
/**
 * Mati — Uninstall handler v7.0
 *
 * Solo borra la API Key y config local. Los datos de negocio
 * viven en mati.com y no se tocan desde WordPress.
 */
if (!defined('WP_UNINSTALL_PLUGIN')) exit;

// Limpiar MU-plugin guardián
$mu_guard = WPMU_PLUGIN_DIR . '/mati-uninstall-guard.php';
if (file_exists($mu_guard)) @unlink($mu_guard);

// Verificar si el usuario quiere eliminar TODO
$delete_all = get_option('mati_delete_data_on_uninstall', 'no');
if ($delete_all !== 'yes') return;

// Eliminar datos locales (solo api_key, slug, y config de widget)
$options = [
    'mati_api_key', 'mati_slug', 'mati_position', 'mati_theme',
    'mati_language', 'mati_color', 'mati_setup_complete',
    'mati_delete_data_on_uninstall',
];
foreach ($options as $opt) delete_option($opt);

// Multisite cleanup
if (is_multisite()) {
    global $wpdb;
    $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
    $original = get_current_blog_id();
    foreach ($blog_ids as $bid) {
        switch_to_blog($bid);
        foreach ($options as $opt) delete_option($opt);
    }
    switch_to_blog($original);
}

wp_cache_flush();
