<?php
/**
 * SureContact Uninstall
 *
 * Fired when the plugin is deleted (not deactivated).
 * Cleans up all plugin data from the database.
 *
 * @since 0.0.4
 *
 * @package SureContact
 */

// Exit if not called by WordPress uninstall.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	die;
}

global $wpdb;

// Delete plugin options.
delete_option( 'surecontact_bearer_token' );
delete_option( 'surecontact_connection_id' );
delete_option( 'surecontact_workspace_uuid' );
delete_option( 'surecontact_last_status_sync' );
delete_option( 'surecontact_last_bulk_sync' );
delete_option( 'surecontact_last_field_sync' );
delete_option( 'surecontact_sync_batches' );
delete_option( 'surecontact_sync_job_cancelled' );
delete_option( 'surecontact_synced_metadata' );
delete_option( 'surecontact_contact_fields' );
delete_option( 'surecontact_custom_metafields' );
delete_option( 'surecontact_general_settings' );
delete_option( 'surecontact_db_version' );
delete_option( 'surecontact_api_queue_table_version' );
delete_option( 'surecontact_integrations_table_version' );
delete_option( 'surecontact_third_party_integrations' );

// Delete site options (for multisite).
delete_site_option( 'surecontact_bearer_token' );
delete_site_option( 'surecontact_connection_id' );
delete_site_option( 'surecontact_workspace_uuid' );

// Delete dynamic options (surecontact_job_*).
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
		$wpdb->esc_like( 'surecontact_job_' ) . '%'
	)
);

// Delete transients.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options}
		WHERE option_name LIKE %s
		OR option_name LIKE %s",
		$wpdb->esc_like( '_transient_surecontact_' ) . '%',
		$wpdb->esc_like( '_transient_timeout_surecontact_' ) . '%'
	)
);

// Drop custom database tables.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}surecontact_integrations" );

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}surecontact_api_queue" );
