<?php
/**
 * BitForgeNet Guest Tickets - Uninstall routine.
 *
 * Data is preserved by default. Everything is only destroyed when the site owner
 * has explicitly ticked "Delete all BitForgeNet Guest Tickets data" in the plugin settings.
 *
 * @package BitForgeNet_Guest_Tickets
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

// This file talks to the plugin's own tables. WordPress has no API for them --
// they are not posts and not meta -- so $wpdb is the only way in. The rows
// change on every booking and every scan, which makes an object cache wrong
// more often than it is right. Table names are built from $wpdb->prefix and
// never from a request, so interpolating one carries nothing to inject; every
// value goes through $wpdb->prepare(), and the few identifiers that cannot
// (ORDER BY columns) are matched against a whitelist first.
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:disable WordPress.DB.DirectDatabaseQuery.SchemaChange
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter


$bfnet_settings = get_option( 'bfnet_settings', array() );
$bfnet_purge    = is_array( $bfnet_settings ) && ! empty( $bfnet_settings['delete_data_on_uninstall'] );

if ( ! $bfnet_purge ) {
	return;
}

require_once plugin_dir_path( __FILE__ ) . 'includes/class-bfnet-install.php';

global $wpdb;

// Drop every plugin table.
$bfnet_tables = array( 'archive_records', 'archive_batches', 'logs', 'emails', 'scans', 'tickets', 'attendees', 'orders', 'ticket_types', 'templates', 'events', 'venues', 'organizers' );
foreach ( $bfnet_tables as $bfnet_table ) {
	$bfnet_name = $wpdb->prefix . 'bfnet_' . $bfnet_table;
	$wpdb->query( "DROP TABLE IF EXISTS {$bfnet_name}" ); // phpcs:ignore WordPress.DB.PreparedSQL
}

// Remove options and transients.
delete_option( 'bfnet_settings' );
delete_option( 'bfnet_db_version' );
delete_option( 'bfnet_activated_at' );
delete_option( 'bfnet_rewrite_endpoint' );

$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_bfnet\\_%' OR option_name LIKE '_transient_timeout_bfnet\\_%'" ); // phpcs:ignore WordPress.DB.PreparedSQL

// Remove roles and capabilities.
if ( class_exists( 'BFNET_Install' ) ) {
	BFNET_Install::remove_roles();
} else {
	remove_role( 'bfnet_manager' );
	remove_role( 'bfnet_door_agent' );
}

// Remove stored tickets and ticket artwork.
$bfnet_uploads = wp_upload_dir();
$bfnet_dir     = trailingslashit( $bfnet_uploads['basedir'] ) . 'bitforgenet-guest-tickets';

if ( is_dir( $bfnet_dir ) ) {
	$bfnet_iterator = new RecursiveIteratorIterator(
		new RecursiveDirectoryIterator( $bfnet_dir, RecursiveDirectoryIterator::SKIP_DOTS ),
		RecursiveIteratorIterator::CHILD_FIRST
	);

	foreach ( $bfnet_iterator as $bfnet_file ) {
		if ( $bfnet_file->isDir() ) {
			@rmdir( $bfnet_file->getRealPath() ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions
		} else {
			@unlink( $bfnet_file->getRealPath() ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions
		}
	}

	@rmdir( $bfnet_dir ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions
}

// Clear scheduled tasks.
wp_clear_scheduled_hook( 'bfnet_cron_hourly' );
wp_clear_scheduled_hook( 'bfnet_cron_daily' );
