<?php
/**
 * Restoring the plugin settings to their defaults.
 *
 * @package Vanquish\MultipleAddresses\Admin
 */

namespace Vanquish\MultipleAddresses\Admin;

use Vanquish\MultipleAddresses\Com\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * A deliberate "start over" for the settings.
 *
 * It exists because uninstalling deliberately does NOT clear them. A merchant's
 * configuration can be an afternoon's work — excluded categories, fee ranges,
 * per-language labels — and anyone who deactivates the plugin to reinstall it, or
 * to test something, would lose all of it without ever being asked. Keeping the
 * options makes that impossible, and moves the destruction here, where it is a
 * choice with a button and a confirmation rather than a side effect.
 *
 * Customer addresses are never touched, by uninstall or by this. They are not the
 * merchant's configuration; they belong to the customers.
 */
class ResetSettings {

	/**
	 * admin-post action name.
	 */
	const ACTION = 'vanmuad_reset_settings';

	/**
	 * Nonce action.
	 */
	const NONCE = 'vanmuad_reset_settings';

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) );
	}

	/**
	 * Render the reset form.
	 *
	 * Its own <form>, posting to admin-post.php: it must not sit inside the
	 * settings form, or saving the settings would submit it too.
	 *
	 * @return void
	 */
	public static function render() {
		?>
		<div class="card" style="max-width:90%;padding:2px 20px 14px;">
			<h2><?php esc_html_e( 'Reset settings', 'vanquish-multiple-addresses-for-woocommerce' ); ?></h2>

			<p class="description">
				<?php esc_html_e( 'Puts every setting on this page back to its default. Your customers\' saved addresses are not affected — this only clears the plugin configuration.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</p>

			<p class="description">
				<?php esc_html_e( 'Uninstalling the plugin does not do this: your settings are kept, so reinstalling finds them where you left them.', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
			</p>

			<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
				<input type="hidden" name="action" value="<?php echo esc_attr( self::ACTION ); ?>" />
				<?php wp_nonce_field( self::NONCE ); ?>

				<p>
					<button type="submit"
						class="button button-secondary"
						onclick="return confirm( '<?php echo esc_js( __( 'Reset all settings to their defaults? This cannot be undone.', 'vanquish-multiple-addresses-for-woocommerce' ) ); ?>' );">
						<?php esc_html_e( 'Reset settings', 'vanquish-multiple-addresses-for-woocommerce' ); ?>
					</button>
				</p>
			</form>
		</div>
		<?php
	}

	/**
	 * Perform the reset.
	 *
	 * @return void
	 */
	public function handle() {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_die( esc_html__( 'You are not allowed to do this.', 'vanquish-multiple-addresses-for-woocommerce' ), '', array( 'response' => 403 ) );
		}

		check_admin_referer( self::NONCE );

		/*
		 * The premium option is cleared whether or not premium is running. The
		 * merchant asked to start over, and leaving a stale set of premium
		 * settings behind to reappear on the day they upgrade is not what
		 * "reset" means. Naming the option directly rather than going through
		 * the premium class keeps this working in the free build, where that
		 * class does not exist.
		 */
		delete_option( Settings::OPTION_KEY );
		delete_option( 'vanmuad_premium_settings' );

		wp_safe_redirect(
			add_query_arg(
				array(
					'page'         => SettingsPage::SLUG,
					'vanmuad_reset' => '1',
				),
				admin_url( 'admin.php' )
			)
		);
		exit;
	}
}
