<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * When the plugin is uninstalled, this file is loaded and provides a function
 * that allows the plugin to clean up its data.
 *
 * @link              https://trynbuy.edubrazeal.com/
 * @since             1.0.0
 * @package           Ai_Try_N_Buy
 */

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

// Get the main settings option from the database.
$options = get_option( 'aitnb_settings' );

// Check if the user has opted-in to remove data upon uninstall.
// This setting will be created in our admin settings page.
if ( isset( $options['delete_data_on_uninstall'] ) && true === $options['delete_data_on_uninstall'] ) {

	// Access the WordPress database object.
	global $wpdb;

	// 1. Delete plugin options.
	delete_option( 'aitnb_settings' );
	// Add any other options you might create here.
	// delete_option( 'aitnb_another_option' );

	// 2. Drop custom database tables.
	// The table name includes the WordPress prefix.
	$analytics_table_name = $wpdb->prefix . 'aitnb_analytics';
	// The following query is a raw DDL statement and does not need $wpdb->prepare.
	// It's safe as the table name is constructed by us, not from user input.
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$wpdb->query( "DROP TABLE IF EXISTS {$analytics_table_name}" );

	// 3. Clean up any other data like post meta, user meta, etc. (if any).
	// Example:
	// delete_post_meta_by_key( 'aitnb_product_data' );

}