<?php
/**
 * Uninstall Class
 *
 * This class defines tasks that must be run when uninstalling the plugin.
 */

namespace CarDealerPress\Includes\Uninstall;

use CarDealerPress\Includes\Cron\Cron;
use CarDealerPress\Includes\Database\Update_Delete;
use CarDealerPress\includes\functions\Logger;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\Includes\Helpers\Transients;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Uninstall {
	use Logger;
	public function cdp_uninstall() {
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
			if ( false === is_super_admin() ) {
				return;
			}
			$sites = get_sites();
			foreach ( $sites as $site ) {
				$blog_id = $site->blog_id;
				switch_to_blog( $blog_id );
				$this->uninstall_actions( $blog_id );
				restore_current_blog();
			}
		} else {
			if ( ! current_user_can( 'activate_plugins' ) ) {
				return;
			}
			$blog_id = get_current_blog_id();
			$this->uninstall_actions( $blog_id );
		}

	}

	public function uninstall_actions( $blog_id ) {
		Option::delete_options(
			array(
				'blog_id' => $blog_id,
			)
		);
		$this->delete_transients( $blog_id );
		( new Update_Delete() )->remove_all_cdp_tables();
		( new Cron() )->remove_cron_schedule();
		// @codingStandardsIgnoreStart
		error_log( "CDP Message: CarDealerPress has been uninstalled, on site id: $blog_id" );
		// @codingStandardsIgnoreEnd
	}

	private function delete_transients( $blog_id ) {
		Transients::delete_all( $blog_id );
		// @codingStandardsIgnoreStart
		error_log( "CDP Message: CarDealerPress transients been deleted, on site id: $blog_id" );
		// @codingStandardsIgnoreEnd
	}

}
