<?php
/*
Plugin Name: Cloud S3 Storage
Description: The plugin allows you to manage WordPress media on S3 compatible object storage services.
Version: 1.4.6
Author: VHosting & Matteo Enna
Text Domain: cloud-s3-storage
License: GPL2
*/

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

require_once(dirname(__FILE__) . '/class/cloud-s3-storage_include.php');
register_uninstall_hook(__FILE__, 'cloud_s3_storage_plugin_uninstall');

$cloud_s3_storage = new cloudS3StorageClass();

function cloud_s3_storage_plugin_uninstall()
{
    delete_option('cloud_s3_settings');
    cloud_s3_storage_uninstall();
}

function cloud_s3_storage_uninstall() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'cloud_s3_storage_url_linked'; 

    $wpdb->query("DROP TABLE IF EXISTS $table_name;");
}

function cloud_s3_storage_update_cron_jobs() {
    global $cloud_s3_storage;
    if (!$cloud_s3_storage || !($cloud_s3_storage instanceof cloudS3StorageClass)) {
        $cloud_s3_storage = new cloudS3StorageClass();
    }

    if ($cloud_s3_storage->options['cron_job_timing'] != 'never') {
        if (!wp_next_scheduled('cloud_s3_storage_action_reindex_image')) {
            wp_schedule_event(time(), $cloud_s3_storage->options['cron_job_timing'], 'cloud_s3_storage_action_reindex_image');
        }
        if (!wp_next_scheduled('cloud_s3_storage_action_clear_image')) {
            wp_schedule_event(time(), $cloud_s3_storage->options['cron_job_timing'], 'cloud_s3_storage_action_clear_image');
        }
    } else {
        $timestamp = wp_next_scheduled('cloud_s3_storage_action_reindex_image');
        if ($timestamp) {
            wp_unschedule_event($timestamp, 'cloud_s3_storage_action_reindex_image');
        }
        $timestamp_due = wp_next_scheduled('cloud_s3_storage_action_clear_image');
        if ($timestamp_due) {
            wp_unschedule_event($timestamp_due, 'cloud_s3_storage_action_clear_image');
        }
    }
}

add_action('init', 'cloud_s3_storage_update_cron_jobs');

register_activation_hook(__FILE__, 'cloud_s3_storage_job_reindex_image');
register_deactivation_hook(__FILE__, 'cloud_s3_storage_job_clear_image');
add_action('cloud_s3_storage_action_reindex_image', 'cloud_s3_storage_function_reindex_image');
add_action('cloud_s3_storage_action_clear_image', 'cloud_s3_storage_function_clear_image');

function cloud_s3_storage_job_reindex_image()
{
    global $cloud_s3_storage;
    if ( ! isset($cloud_s3_storage) || ! is_object($cloud_s3_storage) || ! isset($cloud_s3_storage->options) || ! is_array($cloud_s3_storage->options) ) {
        return;
    }
    if(!array_key_exists('cron_job_timing',$cloud_s3_storage->options)) return;
    if (!wp_next_scheduled('cloud_s3_storage_action_reindex_image')) {
        wp_schedule_event(time(), $cloud_s3_storage->options['cron_job_timing'], 'cloud_s3_storage_action_reindex_image');
    }
    if (!wp_next_scheduled('cloud_s3_storage_action_clear_image')) {
        wp_schedule_event(time(), $cloud_s3_storage->options['cron_job_timing'], 'cloud_s3_storage_action_clear_image');
    }
}

function cloud_s3_storage_job_clear_image()
{
    $timestamp = wp_next_scheduled('cloud_s3_storage_action_reindex_image');
    if ($timestamp) {
        wp_unschedule_event($timestamp, 'cloud_s3_storage_action_reindex_image');
    }
    $timestamp_due = wp_next_scheduled('cloud_s3_storage_action_clear_image');
    if ($timestamp_due) {
        wp_unschedule_event($timestamp_due, 'cloud_s3_storage_action_clear_image');
    }
}

function cloud_s3_storage_function_reindex_image()
{
    global $cloud_s3_storage;
    if (!$cloud_s3_storage || !($cloud_s3_storage instanceof cloudS3StorageClass)) {
        $cloud_s3_storage = new cloudS3StorageClass();
    }

    if ($cloud_s3_storage) {
        $data = $cloud_s3_storage->getImageToConvert($cloud_s3_storage->getLimitImageToConvert());

        foreach ($data as $img) {
            $cloud_s3_storage->uploadImage($img['url']);

        }
        delete_transient('cloud_s3_storage_cached_id_list');

        wp_send_json($data);
    } 

    wp_die();
}

function cloud_s3_storage_function_clear_image()
{
    global $cloud_s3_storage;

    if (!$cloud_s3_storage || !($cloud_s3_storage instanceof cloudS3StorageClass)) {
        $cloud_s3_storage = new cloudS3StorageClass();
    }

    if ($cloud_s3_storage) {
        $delete_status = [];
        if ($cloud_s3_storage->ibrid_mode()) {
            wp_send_json(["description" => __("Hybrid mode activated", 'cloud-s3-storage')]);
            wp_die();
        }
        foreach ($cloud_s3_storage->getfileToDelete($cloud_s3_storage->getLimitImageToConvert()) as $image) {
            $delete_status[$image['url']] = false;

            if (cloudS3StorageMedia::deleteFile($image['complete_path'])) {
                $delete_status[$image['url']] = true;
            }
        }
        delete_transient('cloud_s3_storage_cached_id_list');

        wp_send_json($delete_status);
        wp_die();
    } 
    wp_die();
}
