<?php
/**
 * Cache plugin compatibility
 *
 * @package TTSWP
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Cache compatibility class
 */
class Mementor_TTS_Cache_Compatibility {
    
    /**
     * Initialize cache compatibility
     */
    public static function init() {
        // LiteSpeed Cache
        add_filter('litespeed_optimize_js_excludes', array(__CLASS__, 'litespeed_exclude_scripts'));
        add_filter('litespeed_cache_exclude_cookies', array(__CLASS__, 'litespeed_exclude_cookies'));
        
        // WP Rocket
        add_filter('rocket_exclude_js', array(__CLASS__, 'rocket_exclude_scripts'));
        add_filter('rocket_exclude_defer_js', array(__CLASS__, 'rocket_exclude_scripts'));
        add_filter('rocket_delay_js_exclusions', array(__CLASS__, 'rocket_exclude_scripts'));
        
        // Autoptimize
        add_filter('autoptimize_filter_js_exclude', array(__CLASS__, 'autoptimize_exclude_scripts'));
        
        // WP Fastest Cache
        add_filter('wpfc_exclude_js', array(__CLASS__, 'wpfc_exclude_scripts'));
        
        // W3 Total Cache
        add_filter('w3tc_minify_js_do_tag_minification', array(__CLASS__, 'w3tc_exclude_scripts'), 10, 2);
        
        // Perfmatters
        add_filter('perfmatters_delay_js_exclusions', array(__CLASS__, 'perfmatters_exclude_scripts'));
    }
    
    /**
     * Exclude scripts from LiteSpeed Cache optimization
     */
    public static function litespeed_exclude_scripts($excludes) {
        $excludes[] = 'mementor-tts';
        $excludes[] = 'text-to-speech-tts/public/js/';
        return $excludes;
    }
    
    /**
     * Exclude cookies from LiteSpeed Cache
     */
    public static function litespeed_exclude_cookies($cookies) {
        $cookies[] = 'mementor_tts_*';
        return $cookies;
    }
    
    /**
     * Exclude scripts from WP Rocket
     */
    public static function rocket_exclude_scripts($excluded_js) {
        if (!is_array($excluded_js)) {
            $excluded_js = array();
        }
        $excluded_js[] = 'mementor-tts';
        $excluded_js[] = 'text-to-speech-tts/public/js/(.*)';
        // jQuery must also be excluded — if WP Rocket delays jQuery but not our script,
        // the IIFE wrapper (function($) { ... })(jQuery) fails with "jQuery is not defined"
        $excluded_js[] = '/jquery-?[0-9.]*(\.min)?\.js';
        $excluded_js[] = '/jquery\.js';
        return $excluded_js;
    }
    
    /**
     * Exclude scripts from Autoptimize
     */
    public static function autoptimize_exclude_scripts($exclude) {
        return $exclude . ',mementor-tts,text-to-speech-tts/public/js/';
    }
    
    /**
     * Exclude scripts from WP Fastest Cache
     */
    public static function wpfc_exclude_scripts($exclude_rules) {
        if (!is_array($exclude_rules)) {
            $exclude_rules = array();
        }
        $exclude_rules[] = 'mementor-tts';
        return $exclude_rules;
    }
    
    /**
     * Exclude scripts from W3 Total Cache
     */
    public static function w3tc_exclude_scripts($do_tag_minification, $script_tag) {
        if (strpos($script_tag, 'mementor-tts') !== false || 
            strpos($script_tag, 'text-to-speech-tts/public/js/') !== false) {
            return false;
        }
        return $do_tag_minification;
    }
    
    /**
     * Exclude scripts from Perfmatters
     */
    public static function perfmatters_exclude_scripts($exclusions) {
        if (!is_array($exclusions)) {
            $exclusions = array();
        }
        $exclusions[] = 'mementor-tts';
        $exclusions[] = 'text-to-speech-tts/public/js/';
        return $exclusions;
    }
}

// Initialize cache compatibility
Mementor_TTS_Cache_Compatibility::init();