LiteSpeedCacheCompatibility.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * LiteSpeed Cache Compatibility |
| 5 | * |
| 6 | * Automatically adds Amelia scripts to LiteSpeed Cache exclusion lists |
| 7 | * to prevent JavaScript errors caused by script optimization interfering with wp_localize_script data. |
| 8 | * |
| 9 | * @since 9.1 |
| 10 | */ |
| 11 | |
| 12 | namespace AmeliaBooking\Infrastructure\WP\Compatibility; |
| 13 | |
| 14 | /** |
| 15 | * Class LiteSpeedCacheCompatibility |
| 16 | * |
| 17 | * @package AmeliaBooking\Infrastructure\WP\Compatibility |
| 18 | */ |
| 19 | class LiteSpeedCacheCompatibility |
| 20 | { |
| 21 | /** |
| 22 | * Initialize LiteSpeed Cache compatibility hooks |
| 23 | */ |
| 24 | public static function init() |
| 25 | { |
| 26 | if (self::isLiteSpeedActive()) { |
| 27 | add_filter('litespeed_optimize_js_excludes', array(__CLASS__, 'excludeAmeliaScripts')); |
| 28 | add_filter('litespeed_optm_js_defer_exc', array(__CLASS__, 'excludeAmeliaScripts')); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Exclude Amelia scripts from LiteSpeed Cache optimization |
| 34 | */ |
| 35 | public static function excludeAmeliaScripts(array $excluded_js): array |
| 36 | { |
| 37 | if (!is_array($excluded_js)) { |
| 38 | $excluded_js = array(); |
| 39 | } |
| 40 | |
| 41 | $amelia_patterns = array( |
| 42 | 'amelia', |
| 43 | 'wpAmeliaUrls', |
| 44 | 'wpAmeliaLabels', |
| 45 | 'wpAmeliaSettings', |
| 46 | 'amelia_booking_script', |
| 47 | 'amelia_booking_scripts', |
| 48 | '/ameliabooking/', |
| 49 | '/wpamelia-', |
| 50 | ); |
| 51 | |
| 52 | return array_merge($excluded_js, $amelia_patterns); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Check if LiteSpeed Cache is active |
| 57 | */ |
| 58 | private static function isLiteSpeedActive(): bool |
| 59 | { |
| 60 | return defined('LSCWP_V') || class_exists('LiteSpeed\Core'); |
| 61 | } |
| 62 | } |
| 63 |