<?php

use WP_Rocket\Engine\Optimization\RUCSS\Database\Tables\Resources;
use WP_Rocket\Engine\Optimization\RUCSS\Database\Tables\UsedCSS;
class WPRocketUninstall
{
    private $cache_path;
    private $config_path;
    private $options = array('wp_rocket_settings', 'rocket_analytics_notice_displayed', 'rocketcdn_user_token', 'rocketcdn_process');
    private $transients = array('wp_rocket_customer_data', 'rocket_notice_missing_tags', 'rocket_clear_cache', 'rocket_check_key_errors', 'rocket_send_analytics_data', 'rocket_critical_css_generation_process_running', 'rocket_critical_css_generation_process_complete', 'rocket_critical_css_generation_triggered', 'rocketcdn_status', 'rocketcdn_pricing', 'rocketcdn_purge_cache_response', 'rocket_cloudflare_ips', 'rocket_cloudflare_is_api_keys_valid', 'rocket_preload_triggered', 'rocket_preload_complete', 'rocket_preload_complete_time', 'rocket_preload_errors', 'rocket_database_optimization_process', 'rocket_database_optimization_process_complete');
    private $events = array('rocket_purge_time_event', 'rocket_database_optimization_time_event', 'rocket_cache_dir_size_check', 'rocketcdn_check_subscription_status_event', 'rocket_cron_deactivate_cloudflare_devmode');
    private $cache_dirs = array('wp-rocket', 'min', 'busting', 'critical-css');
    private $rucss_resources_table;
    private $rucss_usedcss_table;
    public function __construct($cache_path, $config_path, $rucss_resources_table, $rucss_usedcss_table)
    {
        $this->cache_path = trailingslashit($cache_path);
        $this->config_path = $config_path;
        $this->rucss_resources_table = $rucss_resources_table;
        $this->rucss_usedcss_table = $rucss_usedcss_table;
    }
    public function uninstall()
    {
        $this->delete_plugin_data();
        $this->delete_cache_files();
        $this->delete_config_files();
        $this->drop_rucss_database_tables();
    }
    private function drop_rucss_database_tables()
    {
        $this->drop_rucss_current_site_tables();
        if (!is_multisite()) {
            return;
        }
        foreach (get_sites(['fields' => 'ids']) as $site_id) {
            switch_to_blog($site_id);
            $this->drop_rucss_current_site_tables();
            restore_current_blog();
        }
    }
    private function drop_rucss_current_site_tables()
    {
        if ($this->rucss_resources_table->exists()) {
            $this->rucss_resources_table->uninstall();
        }
        if ($this->rucss_usedcss_table->exists()) {
            $this->rucss_usedcss_table->uninstall();
        }
    }
    private function delete_plugin_data()
    {
        delete_site_transient('wp_rocket_update_data');
        delete_metadata('user', '', 'rocket_boxes', '', true);
        array_walk($this->transients, 'delete_transient');
        array_walk($this->options, 'delete_option');
        foreach ($this->events as $event) {
            wp_clear_scheduled_hook($event);
        }
    }
    private function delete_cache_files()
    {
        foreach ($this->cache_dirs as $dir) {
            $this->delete($this->cache_path . $dir);
        }
    }
    private function delete_config_files()
    {
        $this->delete($this->config_path);
    }
    private function delete($file)
    {
        if (!is_dir($file)) {
            @unlink($file);
            return;
        }
        try {
            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($file, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
        } catch (UnexpectedValueException $e) {
            return;
        }
        foreach ($iterator as $item) {
            if ($item->isDir()) {
                @rmdir($item);
                continue;
            }
            @unlink($item);
        }
        @rmdir($file);
    }
}