<?php

/**

 * Fired when the plugin is uninstalled.

 *

 * IMPORTANT: This file implements industry-standard data handling for fraud prevention plugins.

 *

 * DEFAULT BEHAVIOR: Keeps all data on uninstall (recommended for fraud prevention)

 * - Fraud history is valuable for compliance, audit trails, and chargeback defense

 * - API credentials preserved for easy reinstallation

 * - Settings maintained for regulatory requirements

 *

 * OPTIONAL: Users can opt-in to delete all data via plugin settings

 * - Setting: 'sensfrx_delete_on_uninstall'

 * - Default: false (keep data)

 * - User must explicitly enable deletion

 *

 * @link       https://sensfrx.ai

 * @since      1.0.0

 * @package    SensFRX

 */



// If uninstall not called from WordPress, then exit

if (! defined('WP_UNINSTALL_PLUGIN')) {

    exit;

}



// Check if user has explicitly requested data deletion

// Default is FALSE (keep data) for fraud prevention compliance

$delete_data = get_option('sensfrx_delete_on_uninstall', false);



if (true === $delete_data) {

    // User explicitly requested full data deletion

    // This will remove ALL plugin data including fraud history

    

    // Delete all plugin options

    delete_option('sensfrx_options');

    delete_option('sensfrx_policy_options');

    delete_option('sensfrx_external_service_notice_dismissed');

    delete_option('sensfrx_activation_time');

    delete_option('sensfrx_delete_on_uninstall');

    delete_option('SensFRX_active');

    delete_option('sensfrx_interval_last_run_time');

    delete_option('sensfrx_cron_logs');

    

    // Delete all plugin tables

    global $wpdb;

    $tables = array(

        'sensfrx_webhook_logout',

        'sensfrx_sleep',

        'sensfrx_api_active_and_webhook',

        'sensfrx_tab_data',

        'sensfrx_custome_filed',

        'sensfrx_real_activity',

        'sensfrx_shadow_activity',

        'sensfrx_pre_payment_order',

        'sensfrx_email_templates',

    );



    foreach ($tables as $table) {

        $table_name = $wpdb->prefix . $table;



		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

        $wpdb->query(

            'DROP TABLE IF EXISTS `' . esc_sql($table_name) . '`'

        );

    }

} else {

    // DEFAULT: Keep all data (recommended for fraud prevention plugins)

    //

    // WHY WE KEEP DATA:

    // 1. Fraud history is valuable for compliance and audit trails

    // 2. PCI DSS, GDPR, and SOX may require data retention

    // 3. Chargeback defense requires historical fraud data

    // 4. Easy reinstallation without reconfiguration

    // 5. Users may be temporarily testing other solutions

    //

    // This follows industry standards from:

    // - Stripe Payment Gateway

    // - PayPal Integration

    // - Google Analytics

    // - Mailchimp

    //

    // To enable data deletion: Go to Sensfrx Settings → Advanced → Enable "Delete all data on uninstall"

}

