<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * @link       https://webequipe.com
 * @since      1.0.0
 * @package    Webequipe_Spin_Win_Wheel
 */

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

// Extra safety: only allow authorized users to trigger uninstall.
if ( ! current_user_can( 'activate_plugins' ) ) {
	exit;
}

global $wpdb;

// Remove plugin options.
delete_option( 'wsww_plugin_version' );
delete_option( 'wsww_settings' );
delete_option( 'wsww_db_version' );

if ( is_multisite() ) {
	delete_site_option( 'wsww_plugin_version' );
	delete_site_option( 'wsww_settings' );
}

/**
 * Drop plugin tables securely.
 *
 * Direct schema queries are necessary here.
 * Table names are fully sanitized and not user-controlled.
 */

$webequipe_sww_allowed_tables = array(
	$wpdb->prefix . 'spin_emails',
	$wpdb->prefix . 'spin_posts',
);

foreach ( $webequipe_sww_allowed_tables as $webequipe_sww_table_name ) {

	// Defensive sanitization (for static analysis and extra safety).
	$webequipe_sww_table_name = preg_replace( '/[^a-zA-Z0-9_]/', '', $webequipe_sww_table_name );

	if ( '' === $webequipe_sww_table_name ) {
		continue;
	}

	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Uninstall cleanup; table name sanitized above.
	$wpdb->query( 'DROP TABLE IF EXISTS `' . $webequipe_sww_table_name . '`' );
}
