<?php
/**
 * Uninstall: remove all Defog data when the plugin is deleted.
 *
 * Runs only on explicit deletion from the Plugins screen.
 *
 * @package Defog
 */

if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

global $wpdb;

// Drop tables.
$defog_tables = array(
	$wpdb->prefix . 'defog_events',
	$wpdb->prefix . 'defog_stats_daily',
	$wpdb->prefix . 'defog_dim_daily',
	$wpdb->prefix . 'defog_orders',
	$wpdb->prefix . 'defog_order_items',
	$wpdb->prefix . 'defog_actions',
	$wpdb->prefix . 'defog_funnel',
);
foreach ( $defog_tables as $defog_table ) {
	$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $defog_table ) );
}

// Remove options.
delete_option( 'defog_settings' );
delete_option( 'defog_db_version' );
delete_option( 'defog_daily_salt' );

// Clear scheduled events.
$defog_ts = wp_next_scheduled( 'defog_rollup_event' );
if ( $defog_ts ) {
	wp_unschedule_event( $defog_ts, 'defog_rollup_event' );
}
wp_clear_scheduled_hook( 'defog_rollup_event' );

// Clean up any leftover rate-limit transients.
$wpdb->query(
	$wpdb->prepare(
		'DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s',
		$wpdb->options,
		'_transient_defog_rl_%',
		'_transient_timeout_defog_rl_%'
	)
);
