<?php
/**
 * Plugin uninstall handler.
 *
 * Fires when the user clicks Delete on the plugin in WordPress admin.
 * Removes ALL plugin data including settings, station presets, and
 * transients. Multisite-aware.
 *
 * @package VMPlayer
 */

// If uninstall not called from WordPress, exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

/**
 * Remove plugin data from a single site.
 *
 * @return void
 */
function vmplayer_uninstall_single_site() {
    global $wpdb;

    // Delete plugin options.
    delete_option( 'vmplayer_settings' );
    delete_option( 'vmplayer_stations' );
    delete_option( 'vmplayer_version' );

    // Delete all VMPlayer transients.
    $wpdb->query(
        $wpdb->prepare(
            "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
            $wpdb->esc_like( '_transient_vmplayer_' ) . '%',
            $wpdb->esc_like( '_transient_timeout_vmplayer_' ) . '%'
        )
    );
}

// Handle multisite networks.
if ( is_multisite() ) {
    // Delete network-level options if any.
    delete_site_option( 'vmplayer_settings' );
    delete_site_option( 'vmplayer_stations' );
    delete_site_option( 'vmplayer_version' );

    // Delete per-site options.
    $site_ids = get_sites( array( 'fields' => 'ids' ) );
    foreach ( $site_ids as $site_id ) {
        switch_to_blog( $site_id );
        vmplayer_uninstall_single_site();
        restore_current_blog();
    }
} else {
    vmplayer_uninstall_single_site();
}
