| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Caching Plugins class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Programmatically changes settings for third party caching Plugins which interfere with Forms |
| 11 |
* and Landing Pages loading correctly. |
| 12 |
* |
| 13 |
* @since 2.4.6 |
| 14 |
*/ |
| 15 |
class ConvertKit_Cache_Plugins { |
| 16 |
|
| 17 |
/** |
| 18 |
* Holds external hosts to exclude from CSS and JS minification |
| 19 |
* and lazy loading of images. |
| 20 |
* |
| 21 |
* @since 2.4.6 |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
public $exclude_hosts = array( |
| 26 |
'cdnjs.cloudflare.com', |
| 27 |
'pages.kit.com', |
| 28 |
'kit.com', |
| 29 |
'pages.convertkit.com', |
| 30 |
'convertkit.com', |
| 31 |
'filekitcdn.com', |
| 32 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* Holds internal JS paths to exclude from JS minification. |
| 36 |
* Supports regular expressions. |
| 37 |
* |
| 38 |
* @since 2.4.6 |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
public $exclude_plugin_js = array( |
| 43 |
'convertkit/resources/frontend/js/(.*).js', |
| 44 |
); |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructor |
| 48 |
* |
| 49 |
* @since 2.4.6 |
| 50 |
*/ |
| 51 |
public function __construct() { |
| 52 |
|
| 53 |
// Autoptimize: Exclude Forms from JS defer. |
| 54 |
add_filter( 'convertkit_output_script_footer', array( $this, 'autoptimize_exclude_js_defer' ) ); |
| 55 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'autoptimize_exclude_js_defer' ) ); |
| 56 |
add_filter( 'autoptimize_filter_imgopt_should_lazyload', array( $this, 'disable_image_lazy_loading_on_landing_pages' ) ); |
| 57 |
|
| 58 |
// Debloat: Exclude Forms from Delay Load JS. |
| 59 |
add_filter( 'debloat/defer_js_excludes', array( $this, 'exclude_hosts' ) ); |
| 60 |
add_filter( 'debloat/delay_js_excludes', array( $this, 'exclude_hosts' ) ); |
| 61 |
|
| 62 |
// Jetpack Boost: Exclude Forms from JS defer. |
| 63 |
add_filter( 'convertkit_output_script_footer', array( $this, 'jetpack_boost_exclude_js_defer' ) ); |
| 64 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'jetpack_boost_exclude_js_defer' ) ); |
| 65 |
|
| 66 |
// LiteSpeed: Exclude Forms from JS defer. |
| 67 |
add_filter( 'convertkit_output_script_footer', array( $this, 'litespeed_cache_exclude_js_defer' ) ); |
| 68 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'litespeed_cache_exclude_js_defer' ) ); |
| 69 |
|
| 70 |
// LiteSpeed: Exclude Forms from JS optimization. |
| 71 |
add_filter( 'convertkit_output_script_footer', array( $this, 'litespeed_cache_exclude_js_optimize' ) ); |
| 72 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'litespeed_cache_exclude_js_optimize' ) ); |
| 73 |
|
| 74 |
// Perfmatters: Exclude Forms from Delay JavaScript. |
| 75 |
add_filter( 'convertkit_output_script_footer', array( $this, 'perfmatters_exclude_delay_js' ) ); |
| 76 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'perfmatters_exclude_delay_js' ) ); |
| 77 |
add_filter( 'perfmatters_lazyload', array( $this, 'disable_image_lazy_loading_on_landing_pages' ) ); |
| 78 |
|
| 79 |
// Siteground Speed Optimizer: Exclude Forms from JS combine. |
| 80 |
add_filter( 'convertkit_output_script_footer', array( $this, 'siteground_speed_optimizer_exclude_js_combine' ) ); |
| 81 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'siteground_speed_optimizer_exclude_js_combine' ) ); |
| 82 |
|
| 83 |
// Siteground Speed Optimizer: Set Member Content Pages in "Exclude URLs from Caching". |
| 84 |
add_filter( 'sgo_exclude_urls_from_cache', array( $this, 'exclude_restrict_content_pages_from_caching' ) ); |
| 85 |
|
| 86 |
// Rocket LazyLoad: Exclude images from lazy loading. |
| 87 |
add_filter( 'rocket_lazyload_excluded_src', array( $this, 'exclude_hosts' ) ); |
| 88 |
|
| 89 |
// WP Rocket: Disable Caching and Minification on Landing Pages. |
| 90 |
add_action( 'convertkit_output_landing_page_before', array( $this, 'wp_rocket_disable_caching_and_minification_on_landing_pages' ) ); |
| 91 |
|
| 92 |
// WP Rocket: Exclude Forms from JS minification and combine. |
| 93 |
add_filter( 'rocket_minify_excluded_external_js', array( $this, 'exclude_hosts' ) ); |
| 94 |
|
| 95 |
// WP Rocket: Exclude Forms from Delay JavaScript execution. |
| 96 |
add_filter( 'convertkit_output_script_footer', array( $this, 'wp_rocket_exclude_delay_js_execution' ) ); |
| 97 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'wp_rocket_exclude_delay_js_execution' ) ); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Exclude ConvertKit scripts from Autoptimize's "Defer JS" setting. |
| 103 |
* |
| 104 |
* @since 2.4.9 |
| 105 |
* |
| 106 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function autoptimize_exclude_js_defer( $script ) { |
| 110 |
|
| 111 |
add_filter( |
| 112 |
'autoptimize_filter_js_exclude', |
| 113 |
function ( $exclusions ) use ( $script ) { |
| 114 |
|
| 115 |
$exclusions .= ', ' . $script['src']; |
| 116 |
return $exclusions; |
| 117 |
|
| 118 |
} |
| 119 |
); |
| 120 |
|
| 121 |
// Return original script. |
| 122 |
return $script; |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Disable JS defer on ConvertKit scripts when the Jetpack Boost Plugin is installed, active |
| 128 |
* and its "Defer Non-Essential JavaScript" setting is enabled. |
| 129 |
* |
| 130 |
* @since 2.4.6 |
| 131 |
* |
| 132 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 133 |
* @return array |
| 134 |
*/ |
| 135 |
public function jetpack_boost_exclude_js_defer( $script ) { |
| 136 |
|
| 137 |
return array_merge( |
| 138 |
$script, |
| 139 |
array( |
| 140 |
'data-jetpack-boost' => 'ignore', |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Disable JS defer on ConvertKit scripts when the LiteSpeed Cache Plugin is installed, active |
| 148 |
* and its "Load JS Deferred" setting is enabled. |
| 149 |
* |
| 150 |
* @since 2.4.6 |
| 151 |
* |
| 152 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
public function litespeed_cache_exclude_js_defer( $script ) { |
| 156 |
|
| 157 |
return array_merge( |
| 158 |
$script, |
| 159 |
array( |
| 160 |
'data-no-defer' => '1', |
| 161 |
) |
| 162 |
); |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Disable JS Optimization on Kit scripts when the LiteSpeed Cache Plugin is installed, active |
| 168 |
* and its "JS Combine" setting is enabled. |
| 169 |
* |
| 170 |
* @since 3.3.1 |
| 171 |
* |
| 172 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function litespeed_cache_exclude_js_optimize( $script ) { |
| 176 |
|
| 177 |
return array_merge( |
| 178 |
$script, |
| 179 |
array( |
| 180 |
'data-no-optimize' => '1', |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Exclude ConvertKit scripts from Perfmatters' "Delay JS" setting. |
| 188 |
* |
| 189 |
* @since 2.4.7 |
| 190 |
* |
| 191 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
public function perfmatters_exclude_delay_js( $script ) { |
| 195 |
|
| 196 |
add_filter( |
| 197 |
'perfmatters_delay_js_exclusions', |
| 198 |
function ( $exclusions ) use ( $script ) { |
| 199 |
|
| 200 |
$exclusions[] = $script['src']; |
| 201 |
return $exclusions; |
| 202 |
|
| 203 |
} |
| 204 |
); |
| 205 |
|
| 206 |
// Return original script. |
| 207 |
return $script; |
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Disable image lazy loading when a WordPress Page configured to display a |
| 213 |
* ConvertKit Landing Page is viewed. |
| 214 |
* |
| 215 |
* @since 2.5.1 |
| 216 |
* |
| 217 |
* @param bool $enabled Lazy loading enabled. |
| 218 |
* @return bool |
| 219 |
*/ |
| 220 |
public function disable_image_lazy_loading_on_landing_pages( $enabled ) { |
| 221 |
|
| 222 |
// If the request isn't for a Page, don't change lazy loading settings. |
| 223 |
if ( ! is_page( get_the_ID() ) ) { |
| 224 |
return $enabled; |
| 225 |
} |
| 226 |
|
| 227 |
// If no landing page is specified for the Post, don't change lazy loading settings. |
| 228 |
$post_settings = new ConvertKit_Post( get_the_ID() ); |
| 229 |
if ( ! $post_settings->has_landing_page() ) { |
| 230 |
return $enabled; |
| 231 |
} |
| 232 |
|
| 233 |
// ConvertKit Landing Page is going to be displayed. |
| 234 |
// Disable image lazy loading so that the Landing Page images display. |
| 235 |
return false; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Disable JS combining on ConvertKit scripts when the Siteground Speed Optimizer Plugin is installed, active |
| 241 |
* and its "Combine JavaScript Files" setting is enabled. |
| 242 |
* |
| 243 |
* @since 2.4.6 |
| 244 |
* |
| 245 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 246 |
* @return array |
| 247 |
*/ |
| 248 |
public function siteground_speed_optimizer_exclude_js_combine( $script ) { |
| 249 |
|
| 250 |
add_filter( |
| 251 |
'sgo_javascript_combine_excluded_external_paths', |
| 252 |
function ( $excluded_paths ) use ( $script ) { |
| 253 |
|
| 254 |
$excluded_paths[] = $script['src']; |
| 255 |
return $excluded_paths; |
| 256 |
|
| 257 |
} |
| 258 |
); |
| 259 |
|
| 260 |
// Return original script. |
| 261 |
return $script; |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Disable caching and minification when a WordPress Page configured to display a |
| 267 |
* ConvertKit Landing Page is viewed. |
| 268 |
* |
| 269 |
* @since 2.4.6 |
| 270 |
*/ |
| 271 |
public function wp_rocket_disable_caching_and_minification_on_landing_pages() { |
| 272 |
|
| 273 |
add_filter( 'rocket_minify_excluded_external_js', array( $this, 'exclude_hosts' ) ); |
| 274 |
add_filter( 'rocket_exclude_css', array( $this, 'exclude_hosts' ) ); |
| 275 |
add_filter( 'rocket_exclude_js', array( $this, 'exclude_local_js_from_minification' ) ); |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Disable WP Rocket's "Delay JavaScript execution" on ConvertKit scripts. |
| 281 |
* |
| 282 |
* @since 2.4.7 |
| 283 |
* |
| 284 |
* @see https://docs.wp-rocket.me/article/1349-delay-javascript-execution |
| 285 |
* |
| 286 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 287 |
* @return array |
| 288 |
*/ |
| 289 |
public function wp_rocket_exclude_delay_js_execution( $script ) { |
| 290 |
|
| 291 |
return array_merge( |
| 292 |
$script, |
| 293 |
array( |
| 294 |
'nowprocket' => '', |
| 295 |
) |
| 296 |
); |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Appends the $exclude_hosts property to an array of existing hosts excluded from |
| 302 |
* minification. |
| 303 |
* |
| 304 |
* @since 2.4.6 |
| 305 |
* |
| 306 |
* @param array $hosts External hosts to ignore. |
| 307 |
* @return array |
| 308 |
*/ |
| 309 |
public function exclude_hosts_from_minification( $hosts ) { |
| 310 |
|
| 311 |
_doing_it_wrong( __FUNCTION__, 'Use exclude_hosts() instead.', '3.1.0' ); |
| 312 |
|
| 313 |
return $this->exclude_hosts( $hosts ); |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Appends the $exclude_hosts property to an array of existing hosts. |
| 319 |
* |
| 320 |
* @since 3.1.0 |
| 321 |
* |
| 322 |
* @param array $hosts External hosts to ignore. |
| 323 |
* @return array |
| 324 |
*/ |
| 325 |
public function exclude_hosts( $hosts ) { |
| 326 |
|
| 327 |
return array_merge( $hosts, $this->exclude_hosts ); |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Appends the $exclude_plugin_js property to an array of existing JS files excluded from |
| 333 |
* minification. |
| 334 |
* |
| 335 |
* @since 2.4.6 |
| 336 |
* |
| 337 |
* @param array $scripts Internal JS scripts to ignore. |
| 338 |
* @return array |
| 339 |
*/ |
| 340 |
public function exclude_local_js_from_minification( $scripts ) { |
| 341 |
|
| 342 |
return array_merge( $scripts, $this->exclude_plugin_js ); |
| 343 |
|
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Exclude Restrict Content Pages from caching when the Siteground Speed Optimizer Plugin is installed, active |
| 348 |
* and its "Exclude URLs from Caching" setting is enabled. |
| 349 |
* |
| 350 |
* @since 3.3.6 |
| 351 |
* |
| 352 |
* @param array $excluded_urls URLs to exclude from caching. |
| 353 |
* @return array |
| 354 |
*/ |
| 355 |
public function exclude_restrict_content_pages_from_caching( $excluded_urls ) { |
| 356 |
|
| 357 |
// Get the list of relative URLs for posts with restrict_content enabled |
| 358 |
// from the Plugin's cache. |
| 359 |
$restrict_content_cache = WP_ConvertKit()->get_class( 'restrict_content_cache' ); |
| 360 |
if ( ! $restrict_content_cache ) { |
| 361 |
return $excluded_urls; |
| 362 |
} |
| 363 |
|
| 364 |
// Cache stores Restrict Content pages as id => relative url pairs. |
| 365 |
// Siteground just needs to the relative URLs. |
| 366 |
$restrict_content_urls = array_values( $restrict_content_cache->get() ); |
| 367 |
|
| 368 |
// Merge the list of relative URLs with the list of URLs already excluded from caching. |
| 369 |
return array_merge( $excluded_urls, $restrict_content_urls ); |
| 370 |
|
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|