<?php

/**
 * Fired when the plugin is uninstalled.
 *
 * @link       https://themeatelier.net
 * @since      1.0.0
 *
 * @package domain-for-sale
 * @subpackage domain-for-sale
 * @author     ThemeAtelier<themeatelierbd@gmail.com>
 */

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

/**
 * Delete plugin data function.
 *
 * @return void
 */
function domain_for_sale_delete_plugin_data()
{
    global $wpdb;
    // Delete plugin option settings.
    $options = [
        'domain_for_sale_version',
        'domain_for_sale_db_version',
        'domain_for_sale_first_version',
        'domain_for_sale_activation_date',
        'dfs-opt',
        'themeatelier_offer_banner_dismissed_new_year_2026',
    ];

    foreach ($options as $option_name) {
        delete_option($option_name);       // Delete regular option.
        delete_site_option($option_name); // Delete multisite option.
    }

    // Delete domain for sale template post type.

    $domain_posts = get_posts(
		array(
			'post_type'   => array( 'dfs_template', 'domain_listing' ),
            'numberposts' => -1,
			'post_status' => 'any,trash,auto-draft',
            'fields'         => 'ids',
		)
	);

    $domain_meta_keys = [
        'dfs_layouts',
        'dfs_template_options',
        'dfs_shortcode',
        'listing_layouts',
        'listing_template_options',
        'listing_shortcode',
    ];
    
	foreach ( $domain_posts as $post_id ) {
        foreach ($domain_meta_keys as $meta_key) {
            delete_post_meta($post_id, $meta_key);
        }
		wp_delete_post( $post_id, true );
	}

        // delete leads table
	$domain_views = $wpdb->prefix . 'dfs_domain_views';
	$table_name = $wpdb->prefix . 'domain_for_sale_offers';
	$wpdb->query("DROP TABLE IF EXISTS `$table_name`");
	$wpdb->query("DROP TABLE IF EXISTS `$domain_views`");
}

// Load WPTP file.
require plugin_dir_path(__FILE__) . '/domain-for-sale.php';
$settings = get_option('dfs-opt');
$domain_for_sale_data_delete     = isset($settings['cleanup_data_deletion']) ? $settings['cleanup_data_deletion'] : false;

if ($domain_for_sale_data_delete) {
    domain_for_sale_delete_plugin_data();
}