<?php
// Silence may be golden, but sometimes words paint the canvas of understanding.

/**
 * Fired when the plugin is uninstalled.
 *
 * When populating this file, consider the following flow
 * of control:
 *
 * - This method should be static
 * - Check if the $_REQUEST content actually is the plugin name
 * - Run an admin referrer check to make sure it goes through authentication
 * - Verify the output of $_GET makes sense
 * - Repeat with other user roles. Best directly by using the links/query string parameters.
 * - Repeat things for multisite. Once for a single site in the network, once sitewide.
 *
 * This file may be updated more in future version of the Boilerplate; however, this is the
 * general skeleton and outline for how the file should work.
 *
 * For more information, see the following discussion:
 * https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/pull/123#issuecomment-28541913
 *
 * @link       http://example.com
 * @since      1.0.0
 *
 * @package    Linksy Search and Replace
 */

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

define('LINKSYAI_SAR_PLUGIN_DIR', plugin_dir_path(__FILE__));

require_once(LINKSYAI_SAR_PLUGIN_DIR . 'constants.php');

function linksy_ai_clear_tables( $wpdb ) {
	$plugin_slug = 'linksy/linksy.php';

    // Check if the linksy is installed
    if ( !function_exists('get_plugins') ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

    $installed_plugins = get_plugins();
    if ( array_key_exists($plugin_slug, $installed_plugins) ) {
        return;
    }

    $table_schema = [ 
		"linksy_links",
		"linksy_broken_links"
	];

    foreach ( $table_schema as $table ) {
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}{$table}" );
	}
}

function linkay_ai_clean_db() {
	global $wpdb;

    if ( is_multisite() ) {
        // get all sites ids
        $sites = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
        if ( ! $sites ) {
            return;
        }

        foreach ( $sites as $site_id ) {
            // switch to the site with the specified id
            switch_to_blog( $site_id );

            // clear all tables and options for site
            linksy_ai_clear_tables( $wpdb );
            linksy_ai_clear_options();

            // return to the current site
            restore_current_blog();
        }
    } else {
        linksy_ai_clear_tables( $wpdb );
        linksy_ai_clear_options();
    }
}



function linksy_ai_clear_options()
{
	$settingOptions = array(
		LINKSYAI_SAR_OPTION_PLUGIN_OPENED,
        LINKSYAI_SAR_OPTION_SETUP_STARTED,
        LINKSYAI_SAR_OPTION_SETUP_COMPLETE,
		LINKSYAI_SAR_OPTION_PLUGIN_VERSION,
        LINKSYAI_SAR_OPTION_DATABASE_VERSION
	);

    $cronOptions = array(
        'linksy_link_analysis_cron_enabled',
        'linksy_link_analysis_offset',
    );

	// Clear up our settings
	foreach ($settingOptions as $settingName) {
		delete_option($settingName);
	}

    foreach ($cronOptions as $cronOption) {
        delete_option($cronOption);
    }
}

linkay_ai_clean_db();