| 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 |
* |
| 20 |
* @since 2.4.6 |
| 21 |
* |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
public $exclude_hosts = array( |
| 25 |
'cdnjs.cloudflare.com', |
| 26 |
'pages.kit.com', |
| 27 |
'kit.com', |
| 28 |
'pages.convertkit.com', |
| 29 |
'convertkit.com', |
| 30 |
); |
| 31 |
|
| 32 |
/** |
| 33 |
* Holds internal JS paths to exclude from JS minification. |
| 34 |
* Supports regular expressions. |
| 35 |
* |
| 36 |
* @since 2.4.6 |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
public $exclude_plugin_js = array( |
| 41 |
'convertkit/resources/frontend/js/(.*).js', |
| 42 |
); |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor |
| 46 |
* |
| 47 |
* @since 2.4.6 |
| 48 |
*/ |
| 49 |
public function __construct() { |
| 50 |
|
| 51 |
// Autoptimize: Exclude Forms from JS defer. |
| 52 |
add_filter( 'convertkit_output_script_footer', array( $this, 'autoptimize_exclude_js_defer' ) ); |
| 53 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'autoptimize_exclude_js_defer' ) ); |
| 54 |
|
| 55 |
// Jetpack Boost: Exclude Forms from JS defer. |
| 56 |
add_filter( 'convertkit_output_script_footer', array( $this, 'jetpack_boost_exclude_js_defer' ) ); |
| 57 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'jetpack_boost_exclude_js_defer' ) ); |
| 58 |
|
| 59 |
// LiteSpeed: Exclude Forms from JS defer. |
| 60 |
add_filter( 'convertkit_output_script_footer', array( $this, 'litespeed_cache_exclude_js_defer' ) ); |
| 61 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'litespeed_cache_exclude_js_defer' ) ); |
| 62 |
|
| 63 |
// Perfmatters: Exclude Forms from Delay JavaScript. |
| 64 |
add_filter( 'convertkit_output_script_footer', array( $this, 'perfmatters_exclude_delay_js' ) ); |
| 65 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'perfmatters_exclude_delay_js' ) ); |
| 66 |
add_filter( 'perfmatters_lazyload', array( $this, 'perfmatters_disable_lazy_loading_on_landing_pages' ) ); |
| 67 |
|
| 68 |
// Siteground Speed Optimizer: Exclude Forms from JS combine. |
| 69 |
add_filter( 'convertkit_output_script_footer', array( $this, 'siteground_speed_optimizer_exclude_js_combine' ) ); |
| 70 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'siteground_speed_optimizer_exclude_js_combine' ) ); |
| 71 |
|
| 72 |
// WP Rocket: Disable Caching and Minification on Landing Pages. |
| 73 |
add_action( 'convertkit_output_landing_page_before', array( $this, 'wp_rocket_disable_caching_and_minification_on_landing_pages' ) ); |
| 74 |
|
| 75 |
// WP Rocket: Exclude Forms from Delay JavaScript execution. |
| 76 |
add_filter( 'convertkit_output_script_footer', array( $this, 'wp_rocket_exclude_delay_js_execution' ) ); |
| 77 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'wp_rocket_exclude_delay_js_execution' ) ); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Exclude ConvertKit scripts from Autoptimize's "Defer JS" setting. |
| 83 |
* |
| 84 |
* @since 2.4.9 |
| 85 |
* |
| 86 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function autoptimize_exclude_js_defer( $script ) { |
| 90 |
|
| 91 |
add_filter( |
| 92 |
'autoptimize_filter_js_exclude', |
| 93 |
function ( $exclusions ) use ( $script ) { |
| 94 |
|
| 95 |
$exclusions .= ', ' . $script['src']; |
| 96 |
return $exclusions; |
| 97 |
|
| 98 |
} |
| 99 |
); |
| 100 |
|
| 101 |
// Return original script. |
| 102 |
return $script; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Disable JS defer on ConvertKit scripts when the Jetpack Boost Plugin is installed, active |
| 108 |
* and its "Defer Non-Essential JavaScript" setting is enabled. |
| 109 |
* |
| 110 |
* @since 2.4.6 |
| 111 |
* |
| 112 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 113 |
* @return array |
| 114 |
*/ |
| 115 |
public function jetpack_boost_exclude_js_defer( $script ) { |
| 116 |
|
| 117 |
return array_merge( |
| 118 |
$script, |
| 119 |
array( |
| 120 |
'data-jetpack-boost' => 'ignore', |
| 121 |
) |
| 122 |
); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Disable JS defer on ConvertKit scripts when the LiteSpeed Cache Plugin is installed, active |
| 128 |
* and its "Load JS Deferred" 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 litespeed_cache_exclude_js_defer( $script ) { |
| 136 |
|
| 137 |
return array_merge( |
| 138 |
$script, |
| 139 |
array( |
| 140 |
'data-no-defer' => '1', |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Exclude ConvertKit scripts from Perfmatters' "Delay JS" setting. |
| 148 |
* |
| 149 |
* @since 2.4.7 |
| 150 |
* |
| 151 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 152 |
* @return array |
| 153 |
*/ |
| 154 |
public function perfmatters_exclude_delay_js( $script ) { |
| 155 |
|
| 156 |
add_filter( |
| 157 |
'perfmatters_delay_js_exclusions', |
| 158 |
function ( $exclusions ) use ( $script ) { |
| 159 |
|
| 160 |
$exclusions[] = $script['src']; |
| 161 |
return $exclusions; |
| 162 |
|
| 163 |
} |
| 164 |
); |
| 165 |
|
| 166 |
// Return original script. |
| 167 |
return $script; |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Disable lazy loading in Perfmatters when a WordPress Page configured to display a |
| 173 |
* ConvertKit Landing Page is viewed. |
| 174 |
* |
| 175 |
* @since 2.5.1 |
| 176 |
* |
| 177 |
* @param bool $enabled Lazy loading enabled. |
| 178 |
* @return bool |
| 179 |
*/ |
| 180 |
public function perfmatters_disable_lazy_loading_on_landing_pages( $enabled ) { |
| 181 |
|
| 182 |
// If the request isn't for a Page, don't change lazy loading settings. |
| 183 |
if ( ! is_page( get_the_ID() ) ) { |
| 184 |
return $enabled; |
| 185 |
} |
| 186 |
|
| 187 |
// If no landing page is specified for the Post, don't change lazy loading settings. |
| 188 |
$post_settings = new ConvertKit_Post( get_the_ID() ); |
| 189 |
if ( ! $post_settings->has_landing_page() ) { |
| 190 |
return $enabled; |
| 191 |
} |
| 192 |
|
| 193 |
// ConvertKit Landing Page is going to be displayed. |
| 194 |
// Disable Perfmatters Lazy Loading so that the Landing Page images display. |
| 195 |
return false; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Disable JS combining on ConvertKit scripts when the Siteground Speed Optimizer Plugin is installed, active |
| 201 |
* and its "Combine JavaScript Files" setting is enabled. |
| 202 |
* |
| 203 |
* @since 2.4.6 |
| 204 |
* |
| 205 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
public function siteground_speed_optimizer_exclude_js_combine( $script ) { |
| 209 |
|
| 210 |
add_filter( |
| 211 |
'sgo_javascript_combine_excluded_external_paths', |
| 212 |
function ( $excluded_paths ) use ( $script ) { |
| 213 |
|
| 214 |
$excluded_paths[] = $script['src']; |
| 215 |
return $excluded_paths; |
| 216 |
|
| 217 |
} |
| 218 |
); |
| 219 |
|
| 220 |
// Return original script. |
| 221 |
return $script; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Disable caching and minification when a WordPress Page configured to display a |
| 227 |
* ConvertKit Landing Page is viewed. |
| 228 |
* |
| 229 |
* @since 2.4.6 |
| 230 |
*/ |
| 231 |
public function wp_rocket_disable_caching_and_minification_on_landing_pages() { |
| 232 |
|
| 233 |
add_filter( 'rocket_minify_excluded_external_js', array( $this, 'exclude_hosts_from_minification' ) ); |
| 234 |
add_filter( 'rocket_exclude_css', array( $this, 'exclude_hosts_from_minification' ) ); |
| 235 |
add_filter( 'rocket_exclude_js', array( $this, 'exclude_local_js_from_minification' ) ); |
| 236 |
add_filter( 'do_rocket_lazyload', '__return_false' ); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Disable WP Rocket's "Delay JavaScript execution" on ConvertKit scripts. |
| 242 |
* |
| 243 |
* @since 2.4.7 |
| 244 |
* |
| 245 |
* @see https://docs.wp-rocket.me/article/1349-delay-javascript-execution |
| 246 |
* |
| 247 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 248 |
* @return array |
| 249 |
*/ |
| 250 |
public function wp_rocket_exclude_delay_js_execution( $script ) { |
| 251 |
|
| 252 |
return array_merge( |
| 253 |
$script, |
| 254 |
array( |
| 255 |
'nowprocket' => '', |
| 256 |
) |
| 257 |
); |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Appends the $exclude_hosts property to an array of existing hosts excluded from |
| 263 |
* minification. |
| 264 |
* |
| 265 |
* @since 2.4.6 |
| 266 |
* |
| 267 |
* @param array $hosts External hosts to ignore. |
| 268 |
* @return array |
| 269 |
*/ |
| 270 |
public function exclude_hosts_from_minification( $hosts ) { |
| 271 |
|
| 272 |
return array_merge( $hosts, $this->exclude_hosts ); |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Appends the $exclude_plugin_js property to an array of existing JS files excluded from |
| 278 |
* minification. |
| 279 |
* |
| 280 |
* @since 2.4.6 |
| 281 |
* |
| 282 |
* @param array $scripts Internal JS scripts to ignore. |
| 283 |
* @return array |
| 284 |
*/ |
| 285 |
public function exclude_local_js_from_minification( $scripts ) { |
| 286 |
|
| 287 |
return array_merge( $scripts, $this->exclude_plugin_js ); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|