<?php
/**
 * Fired when the plugin is uninstalled
 *
 * @package WebEquipe\PDFSearch
 * @since 1.0.0
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

/**
 * Perform uninstall cleanup.
 *
 * @return void
 */
function webequipe_pdf_search_uninstall_cleanup() {
	global $wpdb;

	// Get plugin settings.
	$webequipe_pdf_settings            = get_option( 'webequipe_pdf_search_settings', array() );
	$webequipe_pdf_delete_on_uninstall = ! empty( $webequipe_pdf_settings['delete_on_uninstall'] );

	// Only delete data if setting is enabled.
	if ( ! $webequipe_pdf_delete_on_uninstall ) {
		return;
	}

	// Drop custom database tables.
	$webequipe_pdf_tables = array(
		$wpdb->prefix . 'webequipe_pdf_search_index',
		$wpdb->prefix . 'webequipe_pdf_search_files',
		$wpdb->prefix . 'webequipe_pdf_search_pages',
		$wpdb->prefix . 'webequipe_pdf_search_activity',
	);
	foreach ( $webequipe_pdf_tables as $webequipe_pdf_table_name ) {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall one-time cleanup.
		$wpdb->query(
			$wpdb->prepare(
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall cleanup requires dropping custom tables.
				'DROP TABLE IF EXISTS %i',
				$webequipe_pdf_table_name
			)
		);
	}

	// Delete plugin options.
	delete_option( 'webequipe_pdf_search_settings' );
	delete_option( 'webequipe_pdf_search_version' );
	delete_option( 'webequipe_pdf_search_logs' );
	delete_option( 'webequipe_pdf_search_error_count' );
	delete_option( 'webequipe_pdf_search_activity_backfilled' );

	// Delete all post meta with plugin prefix.
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup requires direct DB delete.
	$wpdb->query(
		$wpdb->prepare(
			'DELETE FROM %i WHERE meta_key LIKE %s',
			$wpdb->postmeta,
			$wpdb->esc_like( '_webequipe_pdf_' ) . '%'
		)
	);

	// Delete all transients.
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup requires direct DB delete.
	$wpdb->query(
		$wpdb->prepare(
			'DELETE FROM %i WHERE option_name LIKE %s',
			$wpdb->options,
			$wpdb->esc_like( '_transient_webequipe_pdf_' ) . '%'
		)
	);

	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup requires direct DB delete.
	$wpdb->query(
		$wpdb->prepare(
			'DELETE FROM %i WHERE option_name LIKE %s',
			$wpdb->options,
			$wpdb->esc_like( '_transient_timeout_webequipe_pdf_' ) . '%'
		)
	);

	// Clear object cache.
	if ( function_exists( 'wp_cache_flush' ) ) {
		wp_cache_flush();
	}

	// Handle multisite.
	if ( is_multisite() ) {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Multisite blog lookup requires direct query.
		$webequipe_pdf_blog_ids = $wpdb->get_col(
			$wpdb->prepare(
				'SELECT blog_id FROM %i',
				$wpdb->blogs
			)
		);

		foreach ( $webequipe_pdf_blog_ids as $webequipe_pdf_blog_id ) {
			switch_to_blog( $webequipe_pdf_blog_id );

			// Drop tables for this site.
			$webequipe_pdf_tables = array(
				$wpdb->prefix . 'webequipe_pdf_search_index',
				$wpdb->prefix . 'webequipe_pdf_search_files',
				$wpdb->prefix . 'webequipe_pdf_search_pages',
				$wpdb->prefix . 'webequipe_pdf_search_activity',
			);
			foreach ( $webequipe_pdf_tables as $webequipe_pdf_table_name ) {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall one-time cleanup.
				$wpdb->query(
					$wpdb->prepare(
						// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Uninstall cleanup requires dropping custom tables.
						'DROP TABLE IF EXISTS %i',
						$webequipe_pdf_table_name
					)
				);
			}

			// Delete options for this site.
			delete_option( 'webequipe_pdf_search_settings' );
			delete_option( 'webequipe_pdf_search_version' );
			delete_option( 'webequipe_pdf_search_logs' );
			delete_option( 'webequipe_pdf_search_error_count' );
			delete_option( 'webequipe_pdf_search_activity_backfilled' );

			// Delete post meta for this site.
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup requires direct DB delete.
			$wpdb->query(
				$wpdb->prepare(
					'DELETE FROM %i WHERE meta_key LIKE %s',
					$wpdb->postmeta,
					$wpdb->esc_like( '_webequipe_pdf_' ) . '%'
				)
			);

			// Delete transients for this site.
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup requires direct DB delete.
			$wpdb->query(
				$wpdb->prepare(
					'DELETE FROM %i WHERE option_name LIKE %s',
					$wpdb->options,
					$wpdb->esc_like( '_transient_webequipe_pdf_' ) . '%'
				)
			);

			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall cleanup requires direct DB delete.
			$wpdb->query(
				$wpdb->prepare(
					'DELETE FROM %i WHERE option_name LIKE %s',
					$wpdb->options,
					$wpdb->esc_like( '_transient_timeout_webequipe_pdf_' ) . '%'
				)
			);

			restore_current_blog();
		}
	}
}

webequipe_pdf_search_uninstall_cleanup();
