<?php
/**
 * Uninstall cleanup.
 *
 * There is deliberately no uninstall.php (Freemius deployment rejects one, and
 * WP runs uninstall.php OR the registered hook, never both). Cleanup hangs off
 * Freemius's `after_uninstall` action, with a core `uninstall_{basename}` hook
 * as the safety net for when the SDK instance can't be resolved.
 *
 * Uninstalling also removes the deny rules Upload Vault wrote, restoring
 * Gravity Forms' stock direct-access behaviour — otherwise files would stay
 * blocked with the download proxy gone.
 *
 * @package Upload_Vault
 */

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

/**
 * Class Uvgf_Uninstall
 */
class Uvgf_Uninstall {

	/**
	 * Guard so only one entry point does the work.
	 *
	 * @var bool
	 */
	private static $done = false;

	/**
	 * Every option this plugin has ever written.
	 *
	 * @return string[]
	 */
	private static function options() {
		return array(
			'uvgf_settings',
			'uvgf_db_version',
			'uvgf_state',
			'uvgf_audit',
			'uvgf_dismissed',
			// Written only by the paid build; removed here so uninstalling the
			// free copy leaves no residue. Never read here.
			'uvgf_pro_settings',
			'uvgf_pro_db_version',
			'uvgf_share_secret',
		);
	}

	/**
	 * Freemius-sanctioned entry point.
	 */
	public static function run() {
		self::cleanup();
	}

	/**
	 * Core-hook safety net (late priority; no-ops if the SDK path already ran).
	 */
	public static function safety_net() {
		if ( self::$done ) {
			return;
		}
		self::cleanup();
	}

	/**
	 * Remove our deny rules, drop tables, options, transients, schedules.
	 */
	private static function cleanup() {
		self::$done = true;
		global $wpdb;

		// Restore stock behaviour: remove the marker blocks Upload Vault wrote.
		if ( class_exists( 'Uvgf_Protector' ) ) {
			Uvgf_Protector::unprotect();
		}

		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}uvgf_files" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery -- own table, uninstall.
		// Created by the paid build only; dropped here so uninstalling the free
		// copy leaves no residue. IF EXISTS makes this safe when they never existed.
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}uvgf_shares" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery -- own table, uninstall.
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}uvgf_log" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery -- own table, uninstall.

		foreach ( self::options() as $uvgf_option ) {
			delete_option( $uvgf_option );
		}

		wp_clear_scheduled_hook( 'uvgf_weekly_audit' );
		wp_clear_scheduled_hook( 'uvgf_retention_purge' );

		$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_uvgf\_%' OR option_name LIKE '\_transient\_timeout\_uvgf\_%' OR option_name LIKE '\_site\_transient\_uvgf\_%' OR option_name LIKE '\_site\_transient\_timeout\_uvgf\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- transient sweep at uninstall.
	}
}
