Skip to content

Commit a4ddc1d

Browse files
committed
Added: W3 Total Cache compatibility
1 parent 4fa1490 commit a4ddc1d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/Compatibility.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function __construct() {
4343
add_filter( 'sgo_javascript_combine_excluded_external_paths', [ $this, 'exclude_plausible_js' ] );
4444
}
4545

46+
// W3 Total Cache
47+
if ( defined( 'W3TC_VERSION' ) ) {
48+
add_filter( 'w3tc_minify_js_script_tags', [ $this, 'unset_plausible_js' ] );
49+
}
50+
4651
// WPML
4752
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
4853
add_filter( 'rest_url', [ $this, 'wpml_compatibility' ], 10, 1 );
@@ -110,6 +115,26 @@ public function exclude_plausible_js( $excluded_js ) {
110115
return $excluded_js;
111116
}
112117

118+
/**
119+
* Remove Plausible.js (or the local file, when proxy is enabled) of the list of JS files to minify.
120+
*
121+
* @filter w3tc_minify_js_script_tags
122+
* @since 2.4.0
123+
*
124+
* @param $script_tags
125+
*
126+
* @return array
127+
* @throws Exception
128+
*/
129+
public function unset_plausible_js( $script_tags ) {
130+
return array_filter(
131+
$script_tags,
132+
function ( $tag ) {
133+
return str_contains( $tag, Helpers::get_js_url( true ) ) === false;
134+
}
135+
);
136+
}
137+
113138
/**
114139
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine/delay our external JS, please.
115140
*
@@ -128,7 +153,11 @@ public function exclude_plausible_js_by_relative_url( $excluded_js ) {
128153
}
129154

130155
/**
131-
* Dear WP Rocket/SG Optimizer/Etc. don't minify/combine/delay our API endpoint, please.
156+
* Some optimization plugins (WP Rocket) replace the JS src URL with their own URL, before being able to exclude it.
157+
* So, when the proxy is enabled, exclusion fails. That's why we exclude again by proxy endpoint.
158+
*
159+
* @filter rocket_delay_js_exclusions
160+
* @since 2.4.0
132161
*
133162
* @param $excluded_js
134163
*

0 commit comments

Comments
 (0)