<?php
/**
 * Uninstall routine.
 *
 * @package NaveenCodesImageOptimizer
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

(
	static function () {
		global $wpdb;

		$ultra_image_optimizer_delete_current_site = static function () use ( $wpdb ) {
			delete_option( 'naveencodes_settings' );
			delete_option( 'naveencodes_stats' );
			delete_option( 'naveencodes_logs' );
			delete_option( 'naveencodes_activated_at' );
			delete_option( 'naveencodes_engine_api_key' );
			delete_option( 'naveencodes_engine_challenge' );
			delete_transient( 'naveencodes_health_status' );
			delete_transient( 'naveencodes_provision_attempt' );

			$wpdb->query( 'DROP TABLE IF EXISTS `' . esc_sql( $wpdb->prefix . 'naveencodes_queue' ) . '`' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
			delete_metadata( 'user', 0, 'naveencodes_donation_notice_dismissed', '', true );
		};

		if ( is_multisite() ) {
			foreach ( get_sites( array( 'fields' => 'ids' ) ) as $ultra_image_optimizer_blog_id ) {
				switch_to_blog( $ultra_image_optimizer_blog_id );
				$ultra_image_optimizer_delete_current_site();
				restore_current_blog();
			}
		} else {
			$ultra_image_optimizer_delete_current_site();
		}
	}
)();
