PluginProbe
Ultimate Cursor – Interactive and Animated Custom Cursor and Background Effects Toolkit / trunk
Ultimate Cursor – Interactive and Animated Custom Cursor and Background Effects Toolkit vtrunk
2.4.1 2.4.0 2.3.3 2.3.2 2.3.1 2.3.0 2.2.3 2.2.2 2.2.1 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 All 56 releases
ultimate-cursor / classes / class-cache-compatibility.php

class-cache-compatibility.php in Ultimate Cursor – Interactive and Animated Custom Cursor and Background Effects Toolkit trunk, at classes/class-cache-compatibility.php

222 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cache Plugin Compatibility
4 *
5 * Handles compatibility with WP Rocket, LiteSpeed, Cloudflare, and other cache plugins.
6 * Ensures JavaScript files are not delayed/deferred/combined to prevent chunk loading errors.
7 *
8 * @package ultimate-cursor
9 */
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Ultimate Cursor Cache Compatibility class.
17 */
18 class Ultimate_Cursor_Cache_Compatibility {
19 /**
20 * The single class instance.
21 *
22 * @var $instance
23 */
24 private static $instance = null;
25
26 /**
27 * Get instance
28 */
29 public static function instance() {
30 if ( is_null( self::$instance ) ) {
31 self::$instance = new self();
32 }
33 return self::$instance;
34 }
35
36 /**
37 * Constructor
38 */
39 private function __construct() {
40 $this->init_hooks();
41 }
42
43 /**
44 * Initialize hooks for cache plugin compatibility
45 */
46 private function init_hooks() {
47 // WP Rocket compatibility
48 add_filter( 'rocket_exclude_defer_js', array( $this, 'exclude_from_defer' ), 10, 1 );
49 add_filter( 'rocket_exclude_js', array( $this, 'exclude_from_combine' ), 10, 1 );
50 add_filter( 'rocket_delay_js_exclusions', array( $this, 'exclude_from_delay' ), 10, 1 );
51 add_filter( 'rocket_excluded_inline_js_content', array( $this, 'exclude_inline_js' ), 10, 1 );
52
53 // LiteSpeed Cache compatibility
54 add_filter( 'litespeed_optimize_js_excludes', array( $this, 'exclude_from_combine' ), 10, 1 );
55
56 // Autoptimize compatibility
57 add_filter( 'autoptimize_filter_js_exclude', array( $this, 'exclude_from_autoptimize' ), 10, 1 );
58
59 // W3 Total Cache compatibility
60 add_filter( 'w3tc_minify_js_do_tag_minification', array( $this, 'w3tc_exclude_minify' ), 10, 3 );
61
62 // WP Fastest Cache compatibility
63 add_filter( 'wpfc_exclude_current_page', array( $this, 'wpfc_check_scripts' ), 10, 1 );
64
65 // Add CORS headers for CDN compatibility
66 add_action( 'wp_enqueue_scripts', array( $this, 'add_cors_headers' ), 20 );
67 }
68
69 /**
70 * Exclude from WP Rocket defer
71 *
72 * @param array $excluded_files Array of excluded files
73 * @return array Modified array
74 */
75 public function exclude_from_defer( $excluded_files ) {
76 if ( ! is_array( $excluded_files ) ) {
77 $excluded_files = array();
78 }
79
80 // Exclude main frontend script and all chunks
81 $excluded_files[] = '/ultimate-cursor/build/frontend.js';
82 $excluded_files[] = '/ultimate-cursor/build/(.*).js';
83 $excluded_files[] = 'ultimate-cursor';
84
85 return $excluded_files;
86 }
87
88 /**
89 * Exclude from JS combine/minify
90 *
91 * @param array $excluded_files Array of excluded files
92 * @return array Modified array
93 */
94 public function exclude_from_combine( $excluded_files ) {
95 if ( ! is_array( $excluded_files ) ) {
96 $excluded_files = array();
97 }
98
99 // Exclude all plugin JS files to prevent chunk loading issues
100 $excluded_files[] = '/ultimate-cursor/build/';
101 $excluded_files[] = 'ultimate-cursor/build';
102 $excluded_files[] = '/wp-content/plugins/ultimate-cursor/build/';
103
104 return $excluded_files;
105 }
106
107 /**
108 * Exclude from WP Rocket delay JS
109 *
110 * @param array $excluded_patterns Array of excluded patterns
111 * @return array Modified array
112 */
113 public function exclude_from_delay( $excluded_patterns ) {
114 if ( ! is_array( $excluded_patterns ) ) {
115 $excluded_patterns = array();
116 }
117
118 // Exclude the plugin from delay JS execution
119 $excluded_patterns[] = 'ultimate-cursor';
120 $excluded_patterns[] = 'ultimateCursorData';
121 $excluded_patterns[] = '__ultimateCursorPublicPath';
122
123 return $excluded_patterns;
124 }
125
126 /**
127 * Exclude inline JS from optimization
128 *
129 * @param array $excluded_patterns Array of excluded inline JS patterns
130 * @return array Modified array
131 */
132 public function exclude_inline_js( $excluded_patterns ) {
133 if ( ! is_array( $excluded_patterns ) ) {
134 $excluded_patterns = array();
135 }
136
137 // Exclude our inline public path script
138 $excluded_patterns[] = '__ultimateCursorPublicPath';
139 $excluded_patterns[] = 'ultimateCursorData';
140
141 return $excluded_patterns;
142 }
143
144 /**
145 * Exclude from Autoptimize
146 *
147 * @param string $excluded_js Comma-separated list of excluded JS
148 * @return string Modified list
149 */
150 public function exclude_from_autoptimize( $excluded_js ) {
151 $new_excludes = 'ultimate-cursor/build/';
152
153 if ( empty( $excluded_js ) ) {
154 return $new_excludes;
155 }
156
157 return $excluded_js . ', ' . $new_excludes;
158 }
159
160 /**
161 * W3 Total Cache - exclude from minification
162 *
163 * @param bool $do_tag_minification Whether to minify
164 * @param string $script_tag The script tag
165 * @param string $file File path
166 * @return bool Whether to minify
167 */
168 public function w3tc_exclude_minify( $do_tag_minification, $script_tag, $file ) {
169 if ( strpos( $file, 'ultimate-cursor/build' ) !== false ) {
170 return false;
171 }
172 return $do_tag_minification;
173 }
174
175 /**
176 * WP Fastest Cache - check if our scripts are on page
177 *
178 * @param bool $exclude Whether to exclude current page
179 * @return bool Modified exclusion
180 */
181 public function wpfc_check_scripts( $exclude ) {
182 // Check if our plugin scripts are enqueued
183 if ( wp_script_is( 'ultimate-cursor-frontend', 'enqueued' ) ) {
184 // Don't exclude, but ensure proper handling
185 return $exclude;
186 }
187 return $exclude;
188 }
189
190 /**
191 * Add CORS headers for CDN/Cloudflare compatibility
192 * Ensures chunks can be loaded cross-origin
193 */
194 public function add_cors_headers() {
195 // Add crossorigin attribute to our scripts for CDN compatibility
196 add_filter( 'script_loader_tag', array( $this, 'add_crossorigin_attribute' ), 10, 3 );
197 }
198
199 /**
200 * Add crossorigin attribute to script tags
201 *
202 * @param string $tag The script tag
203 * @param string $handle The script handle
204 * @param string $src The script source
205 * @return string Modified script tag
206 */
207 public function add_crossorigin_attribute( $tag, $handle, $src ) {
208 // Only add to our frontend script
209 if ( $handle === 'ultimate-cursor-frontend' ) {
210 // Check if crossorigin is not already set
211 if ( strpos( $tag, 'crossorigin' ) === false ) {
212 // Add crossorigin="anonymous" for CDN compatibility
213 $tag = str_replace( ' src=', ' crossorigin="anonymous" src=', $tag );
214 }
215 }
216 return $tag;
217 }
218 }
219
220 // Initialize the compatibility class
221 Ultimate_Cursor_Cache_Compatibility::instance();
222