<?php

/**
 * Fired during plugin deactivation.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 */

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

/**
 * NudgeSells_Deactivator
 *
 * Defines everything that needs to run once, when the plugin is deactivated.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_Deactivator
{

	/**
	 * Deactivate.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public static function deactivate() {}

	/**
	 * Uninstall.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public static function uninstall() {
		$settings = get_option('nudgesells_settings', array());
		if (empty($settings['delete_data_on_uninstall']) || 'yes' !== $settings['delete_data_on_uninstall']) {
			return;
		}

		$offer_ids = get_posts(array(
			'post_type'      => 'nudgesells_offer',
			'post_status'    => 'any',
			'posts_per_page' => -1,
			'fields'         => 'ids',
			'no_found_rows'  => true,
		));

		foreach ($offer_ids as $offer_id) {
			wp_delete_post($offer_id, true);
		}

		$experiment_ids = get_posts(array(
			'post_type'      => 'nudgesells_ab_test',
			'post_status'    => 'any',
			'posts_per_page' => -1,
			'fields'         => 'ids',
			'no_found_rows'  => true,
		));

		foreach ($experiment_ids as $experiment_id) {
			wp_delete_post($experiment_id, true);
		}

		delete_option('nudgesells_settings');
		delete_option('nudgesells_version');
		delete_option('nudgesells_show_welcome_notice');
	}
}
