<?php
/**
 * Plugin Name: Shrinkwell Image Optimizer
 * Description: Optimize images with WebP and AVIF, then review possibly unused media safely.
 * Version: 1.16.4
 * Author: Levmedia
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: shrinkwell-image-optimizer
 * Domain Path: /languages
 */

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

$shrinkwell_freemius_file = __DIR__ . '/shrinkwell-freemius.php';
if (is_readable($shrinkwell_freemius_file)) {
    require_once $shrinkwell_freemius_file;
}

define('SHRINKWELL_PLUGIN_FILE', __FILE__);
define('SHRINKWELL_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('SHRINKWELL_PLUGIN_URL', plugin_dir_url(__FILE__));

if (!defined('SHRINKWELL_FREE_BULK_LIMIT')) {
    define('SHRINKWELL_FREE_BULK_LIMIT', 250);
}

function shrinkwell_is_pro(): bool
{
    if (function_exists('shrinkwell_fs')) {
        $freemius = shrinkwell_fs();
        if (
            is_object($freemius)
            && method_exists($freemius, 'is_premium')
            && method_exists($freemius, 'can_use_premium_code')
        ) {
            return (bool) $freemius->is_premium() && (bool) $freemius->can_use_premium_code();
        }
        if (
            is_object($freemius)
            && method_exists($freemius, 'is_premium')
            && method_exists($freemius, 'is_plan')
        ) {
            return (bool) $freemius->is_premium() && (bool) $freemius->is_plan('pro', true);
        }
    }

    return false;
}

function shrinkwell_get_plan(): string
{
    return shrinkwell_is_pro() ? 'pro' : 'free';
}

function shrinkwell_get_free_bulk_limit(): int
{
    return (int) SHRINKWELL_FREE_BULK_LIMIT;
}

function shrinkwell_feature_available(string $feature_key): bool
{
    $free_features = [
        'webp' => true,
        'avif' => true,
        'manual_bulk' => true,
        'basic_bulk' => true,
        'basic_seo' => true,
        'backups' => true,
        'single_restore' => true,
        'backup_storage_check' => true,
        'media_cleanup_scan' => true,
        'media_cleanup_preview' => true,
    ];

    if (isset($free_features[$feature_key])) {
        return true;
    }

    $pro_features = [
        'usage_unlimited' => true,
        'auto_optimize' => true,
        'background_bulk' => true,
        'media_cleanup_actions' => true,
        'media_cleanup_bulk' => true,
        'media_cleanup_permanent_delete' => true,
        'backup_size_impact' => true,
    ];

    return isset($pro_features[$feature_key]) && shrinkwell_is_pro();
}

function shrinkwell_can_use_unlimited_optimization(): bool
{
    return shrinkwell_feature_available('usage_unlimited');
}

function shrinkwell_can_use_avif(): bool
{
    return shrinkwell_feature_available('avif');
}

function shrinkwell_can_run_media_cleanup_scan(): bool
{
    return shrinkwell_feature_available('media_cleanup_scan');
}

function shrinkwell_can_move_to_shrinkwell_trash(): bool
{
    return shrinkwell_feature_available('media_cleanup_actions');
}

function shrinkwell_can_bulk_cleanup(): bool
{
    return shrinkwell_feature_available('media_cleanup_bulk');
}

function shrinkwell_can_permanently_delete_media(): bool
{
    return shrinkwell_feature_available('media_cleanup_permanent_delete');
}

function shrinkwell_can_use_auto_optimize(): bool
{
    return shrinkwell_feature_available('auto_optimize');
}

function shrinkwell_can_use_background_optimization(): bool
{
    return shrinkwell_feature_available('background_bulk');
}

function shrinkwell_get_upgrade_url(): string
{
    $url = 'https://shrinkwell.com/';

    if (function_exists('shrinkwell_fs')) {
        $freemius = shrinkwell_fs();
        if (is_object($freemius) && method_exists($freemius, 'get_upgrade_url')) {
            $url = (string) $freemius->get_upgrade_url();
        } elseif (is_object($freemius) && method_exists($freemius, 'checkout_url')) {
            $url = (string) $freemius->checkout_url();
        }
    }

    return (string) apply_filters('shrinkwell_upgrade_url', $url);
}

function shrinkwell_server_supports_webp(): bool
{
    return (bool) wp_image_editor_supports(['mime_type' => 'image/webp']);
}

function shrinkwell_server_supports_avif(): bool
{
    return (bool) wp_image_editor_supports(['mime_type' => 'image/avif']);
}

function shrinkwell_avif_available_for_site(): bool
{
    return shrinkwell_feature_available('avif') && shrinkwell_server_supports_avif();
}

require_once SHRINKWELL_PLUGIN_DIR . 'includes/class-shrinkwell-usage-tracker.php';
require_once SHRINKWELL_PLUGIN_DIR . 'includes/class-shrinkwell-image-optimizer.php';

add_action('plugins_loaded', static function () {
    if (function_exists('shrinkwell_fs')) {
        $freemius = shrinkwell_fs();
        if (is_object($freemius) && method_exists($freemius, 'add_action')) {
            $freemius->add_action('after_uninstall', ['Shrinkwell_Image_Optimizer', 'uninstall']);
        }
    }

    $GLOBALS['shrinkwell_usage_tracker'] = new Shrinkwell_Usage_Tracker();
    Shrinkwell_Image_Optimizer::instance();
});
