<?php
/**
 * Uninstall handler.
 *
 * Removes plugin data only when the "delete data on uninstall" setting is enabled.
 *
 * @package Vist360
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

$vist360_settings = (array) get_option( 'vist360_settings', array() );

if ( empty( $vist360_settings['delete_data'] ) ) {
	return;
}

global $wpdb;

// Remove tours and their meta.
$vist360_tour_ids = get_posts(
	array(
		'post_type'     => 'vist360_tour',
		'post_status'   => 'any',
		'numberposts'   => -1,
		'fields'        => 'ids',
		'no_found_rows' => true,
	)
);

foreach ( $vist360_tour_ids as $vist360_tour_id ) {
	wp_delete_post( $vist360_tour_id, true );
}

// Drop the analytics table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Intentional cleanup at uninstall.
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}vist360_events" );

// Remove options.
delete_option( 'vist360_settings' );
delete_option( 'vist360_db_version' );
