<?php
/**
 * Uninstall cleanup.
 *
 * Moved out of WordPress' uninstall.php so that Freemius can own the uninstall
 * hook and report the uninstall event first. Hooked to the Freemius
 * `after_uninstall` action from the main plugin file.
 *
 * @package CartGiftWrapping
 */

defined( 'ABSPATH' ) || exit;

if ( ! function_exists( 'cgwfw_uninstall_cleanup' ) ) {
	/**
	 * Remove all of the plugin's options, but only on sites where the merchant
	 * opted in to deletion. Multisite-aware.
	 *
	 * @return void
	 */
	function cgwfw_uninstall_cleanup() {
		$options = array(
			'cgwfw_delete_on_uninstall',
			'cgwfw_mode',
			'cgwfw_enable_product_page',
			'cgwfw_pp_style',
			'cgwfw_fee',
			'cgwfw_fee_per',
			'cgwfw_taxable',
			'cgwfw_tax_class',
			'cgwfw_targeting',
			'cgwfw_categories',
			'cgwfw_enable_product_exclusions',
			'cgwfw_exclude_products',
			'cgwfw_enable_included',
			'cgwfw_included_categories',
			'cgwfw_included_products',
			'cgwfw_included_text',
			'cgwfw_wrap_options',
			'cgwfw_designs_display',
			'cgwfw_designs_label',
			'cgwfw_designs_none_label',
			'cgwfw_enable_message',
			'cgwfw_message_limit',
			'cgwfw_enable_tooltip',
			'cgwfw_tooltip_text',
			'cgwfw_item_style',
			'cgwfw_button_size',
			'cgwfw_button_label',
			'cgwfw_button_label_added',
			'cgwfw_order_label',
			'cgwfw_order_style',
			'cgwfw_order_show_on',
			'cgwfw_order_subtitle',
			'cgwfw_card_add_label',
			'cgwfw_card_added_label',
			'cgwfw_message_label',
			'cgwfw_fee_label',
			'cgwfw_btn_bg',
			'cgwfw_btn_bg_added',
			'cgwfw_btn_color',
			'cgwfw_btn_color_added',
			'cgwfw_btn_radius',
		);

		$delete_for_current_site = function () use ( $options ) {
			if ( 'yes' !== get_option( 'cgwfw_delete_on_uninstall' ) ) {
				return;
			}
			foreach ( $options as $option ) {
				delete_option( $option );
			}
		};

		if ( is_multisite() ) {
			foreach ( get_sites( array( 'fields' => 'ids' ) ) as $site_id ) {
				switch_to_blog( $site_id );
				$delete_for_current_site();
				restore_current_blog();
			}
		} else {
			$delete_for_current_site();
		}
	}
}
