Repository navigation
Expand file tree
/
Copy pathCompatibility.php
More file actions
335 lines (297 loc) · 10 KB
/
Copy pathCompatibility.php
File metadata and controls
335 lines (297 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* Plausible Analytics | Compatibility.
*
* @since 1.2.5
* @package WordPress
* @subpackage Plausible Analytics
*/
namespace Plausible\Analytics\WP;
use Exception;
/**
* @codeCoverageIgnore Because this is to be tested in a headless browser.
*/
class Compatibility {
/**
* A list of filters and actions to prevent our script from being manipulated by other plugins, known to cause issues.
* Our script is already <1KB, so there's no need to minify, combine or optimize it in any other way.
*
* @return void
*/
public function __construct() {
// Autoptimize
if ( defined( 'AUTOPTIMIZE_PLUGIN_VERSION' ) ) {
add_filter( 'autoptimize_filter_js_exclude', [ $this, 'exclude_plausible_js_as_string' ] );
}
// Cloudflare Rocket Loader
add_filter( 'script_loader_tag', [ $this, 'exclude_from_cloudflare_rocket_loader' ], 10, 2 );
// Global Exclusion from Minification
add_filter( 'plausible_analytics_script_params', [ $this, 'exclude_from_minification' ] );
// Hummingbird
if ( defined( 'WPHB_VERSION' ) ) {
add_filter( 'wphb_dont_add_handle_to_collection', [ $this, 'exclude_from_hummingbird_asset_optimization' ], 10, 4 );
add_filter( 'wphb_delay_js_exclusions', [ $this, 'exclude_from_hummingbird_delay_js' ] );
}
// LiteSpeed Cache
if ( defined( 'LSCWP_V' ) ) {
add_filter( 'litespeed_optimize_js_excludes', [ $this, 'exclude_plausible_js' ] );
add_filter( 'litespeed_optm_js_defer_exc', [ $this, 'exclude_plausible_inline_js' ] );
add_filter( 'litespeed_optm_gm_js_exc', [ $this, 'exclude_plausible_inline_js' ] );
}
// SG Optimizer
if ( defined( '\SiteGround_Optimizer\VERSION' ) ) {
add_filter( 'sgo_javascript_combine_exclude', [ $this, 'exclude_js_by_handle' ] );
add_filter( 'sgo_js_minify_exclude', [ $this, 'exclude_js_by_handle' ] );
add_filter( 'sgo_js_async_exclude', [ $this, 'exclude_js_by_handle' ] );
add_filter( 'sgo_javascript_combine_excluded_inline_content', [ $this, 'exclude_plausible_inline_js' ] );
add_filter( 'sgo_javascript_combine_excluded_external_paths', [ $this, 'exclude_plausible_js' ] );
}
// TranslatePress
if ( defined( 'TRP_PLUGIN_VERSION' ) ) {
add_filter( 'rest_url', [ $this, 'multilingual_compatibility' ], 10, 1 );
}
// W3 Total Cache
if ( defined( 'W3TC_VERSION' ) ) {
add_filter( 'w3tc_minify_js_script_tags', [ $this, 'unset_plausible_js' ] );
}
// WPML
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
add_filter( 'rest_url', [ $this, 'multilingual_compatibility' ], 10, 1 );
}
// WP Optimize
if ( defined( 'WPO_VERSION' ) ) {
add_filter( 'wp-optimize-minify-default-exclusions', [ $this, 'exclude_plausible_js' ] );
}
// WP Rocket
if ( defined( 'WP_ROCKET_VERSION' ) ) {
add_filter( 'rocket_excluded_inline_js_content', [ $this, 'exclude_plausible_inline_js' ] );
add_filter( 'rocket_exclude_js', [ $this, 'exclude_plausible_js' ] );
add_filter( 'rocket_minify_excluded_external_js', [ $this, 'exclude_plausible_js' ] );
add_filter( 'rocket_delay_js_exclusions', [ $this, 'exclude_plausible_inline_js' ] );
add_filter( 'rocket_delay_js_exclusions', [ $this, 'exclude_by_proxy_endpoint' ] );
add_filter( 'rocket_exclude_defer_js', [ $this, 'exclude_plausible_js_by_relative_url' ] );
}
}
/**
* Adds window.plausible
*
* @param mixed $exclude_js
*
* @return string
* @throws Exception
*/
public function exclude_plausible_js_as_string( $exclude_js ) {
$exclude_js .= ', window.plausible, ' . Helpers::get_js_url( true );
return $exclude_js;
}
/**
* Add Plausible Analytics attributes.
*
* @since 1.0.0
* @access public
*
* @param string $tag Script tag.
*
* @param string $handle Script handle.
*
* @return string
*/
public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) {
// Bail if it's not our script.
if ( 'plausible-analytics' !== $handle ) {
return $tag; // @codeCoverageIgnore
}
/**
* async (like Plausible Analytics' own snippet) instead of defer: a deferred script holds up DOMContentLoaded
* until it has loaded, so a script that can't be reached (e.g. a proxied script on a server that's gone offline)
* stalls everything waiting for that event. The tracker handles loading before or after plausible.init().
*
* the data-cfasync ensures this script isn't processed by CF Rocket Loader @see https://developers.cloudflare.com/speed/optimization/content/rocket-loader/ignore-javascripts/
*
* @since 2.6.3 async instead of defer.
*/
$params = "async data-cfasync='false'";
$params = apply_filters( 'plausible_analytics_script_params', $params );
return str_replace( ' src', " {$params} src", $tag );
}
/**
* Append attributes to the script tag, to make sure optimization plugins who respect this attribute don't minify our script.
*
* "data-no-minify" is respected by:
* - WP Rocket
* - WP Optimize
* - WP Fastest Cache
*
* "data-no-optimize" is respected by:
* - LiteSpeed Cache
*
* "data-noptimize" is respected by:
* - Autoptimize
*
* @param $params
*
* @return string
*/
public function exclude_from_minification( $params ) {
$params .= ' data-no-minify="true" data-no-optimize="1" data-noptimize="1"';
return $params;
}
/**
* Dear Hummingbird, leave our scripts alone, please. Its Asset Optimization combines the tracker with other scripts
* and moves (or duplicates) the inline script that initializes it, which can leave calls to plausible() without the
* initialization ("plausible is not a function"), and serves its own copy of the tracker, which goes stale.
*
* Handles that aren't added to the collection are returned to WordPress untouched.
*
* @filter wphb_dont_add_handle_to_collection
* @since 2.6.3
*
* @param bool $value Whether to leave the handle alone.
* @param string $handle Resource handle.
* @param string $source_url Resource URL.
* @param string $type scripts|styles
*
* @return bool
*/
public function exclude_from_hummingbird_asset_optimization( $value, $handle, $source_url, $type ) {
if ( $type === 'scripts' && strpos( (string) $handle, 'plausible-' ) === 0 ) {
return true;
}
return $value;
}
/**
* Dear Hummingbird, don't delay our scripts, please. The exclusions are matched against each script tag, including
* its inline code, so this covers our handles (plausible-*), our URLs (plausible.io, /plugins/plausible-analytics/)
* and the inline code calling window.plausible.
*
* @filter wphb_delay_js_exclusions
* @since 2.6.3
*
* @param array $exclusions
*
* @return array
*/
public function exclude_from_hummingbird_delay_js( $exclusions ) {
$exclusions[] = 'plausible';
return $exclusions;
}
/**
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine our inline JS, please.
*
* @filter rocket_excluded_inline_js_content
* @since 1.2.5
*
* @param array $inline_js
*
* @return array
*/
public function exclude_plausible_inline_js( $inline_js ) {
$inline_js['plausible'] = 'window.plausible';
return $inline_js;
}
/**
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine/delay our external JS, please.
*
* @filter rocket_exclude_js
* @filter rocket_minify_excluded_external_js
* @since 1.2.5
*
* @param array $excluded_js
*
* @return array
* @throws Exception
*/
public function exclude_plausible_js( $excluded_js ) {
$excluded_js[] = Helpers::get_js_url( true );
return $excluded_js;
}
/**
* Remove Plausible.js (or the local file, when proxy is enabled) of the list of JS files to minify.
*
* @filter w3tc_minify_js_script_tags
* @since 2.4.0
*
* @param $script_tags
*
* @return array
* @throws Exception
*/
public function unset_plausible_js( $script_tags ) {
return array_filter(
$script_tags,
function ( $tag ) {
return str_contains( $tag, Helpers::get_js_url( true ) ) === false;
}
);
}
/**
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine/delay our external JS, please.
*
* @filter rocket_exclude_js
* @filter rocket_minify_excluded_external_js
* @since 1.2.5
*
* @param array $excluded_js
*
* @return array
* @throws Exception
*/
public function exclude_plausible_js_by_relative_url( $excluded_js ) {
$excluded_js[] = preg_replace( '/http[s]?:\/\/.*?(\/)/', '$1', Helpers::get_js_url( true ) );
return $excluded_js;
}
/**
* Some optimization plugins (WP Rocket) replace the JS src URL with their own URL, before being able to exclude it.
* So, when the proxy is enabled, exclusion fails. That's why we exclude again by proxy endpoint.
*
* @filter rocket_delay_js_exclusions
* @since 2.4.0
*
* @param $excluded_js
*
* @return mixed
* @throws Exception
*/
public function exclude_by_proxy_endpoint( $excluded_js ) {
$excluded_js[] = Helpers::get_rest_endpoint( false );
return $excluded_js;
}
/**
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine/delay our external JS, please.
*
* @filter rocket_exclude_js
* @filter rocket_minify_excluded_external_js
* @since 1.2.5
*
* @param $excluded_handles
*
* @return array
*/
public function exclude_js_by_handle( $excluded_handles ) {
$excluded_handles[] = 'plausible-analytics';
return $excluded_handles;
}
/**
* Multilingual plugins, e.g., TranslatePress and WPML, override the REST API URL to include
* the language 'subdirectory', which leads to 404 errors.
*
* This filter strips that subdirectory. In "domain per language" mode it then moves the endpoint to the
* language domain we're currently on, because pointing it at the default domain would make the tracker fire
* cross-origin (CORS) requests.
*
* @filter rest_url
* @since 2.6.2 Keep the endpoint on the current language domain.
*
* @param mixed $url
*
* @return string|void
* @throws Exception
*/
public function multilingual_compatibility( $url ) {
$rest_endpoint = Helpers::get_rest_endpoint( false );
if ( str_contains( $url, $rest_endpoint ) ) {
return Helpers::maybe_use_current_language_domain( get_option( 'home' ) . $rest_endpoint );
}
return $url;
}
}