| 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.convertkit.com', |
| 27 |
'convertkit.com', |
| 28 |
); |
| 29 |
|
| 30 |
/** |
| 31 |
* Holds internal JS paths to exclude from JS minification. |
| 32 |
* Supports regular expressions. |
| 33 |
* |
| 34 |
* @since 2.4.6 |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
public $exclude_plugin_js = array( |
| 39 |
'convertkit/resources/frontend/js/(.*).js', |
| 40 |
); |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor |
| 44 |
* |
| 45 |
* @since 2.4.6 |
| 46 |
*/ |
| 47 |
public function __construct() { |
| 48 |
|
| 49 |
// Jetpack Boost: Exclude Forms from JS defer. |
| 50 |
add_filter( 'convertkit_output_script_footer', array( $this, 'jetpack_boost_exclude_js_defer' ) ); |
| 51 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'jetpack_boost_exclude_js_defer' ) ); |
| 52 |
|
| 53 |
// LiteSpeed: Exclude Forms from JS defer. |
| 54 |
add_filter( 'convertkit_output_script_footer', array( $this, 'litespeed_cache_exclude_js_defer' ) ); |
| 55 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'litespeed_cache_exclude_js_defer' ) ); |
| 56 |
|
| 57 |
// Perfmatters: Exclude Forms from Delay JavaScript. |
| 58 |
add_filter( 'convertkit_output_script_footer', array( $this, 'perfmatters_exclude_delay_js' ) ); |
| 59 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'perfmatters_exclude_delay_js' ) ); |
| 60 |
|
| 61 |
// Siteground Speed Optimizer: Exclude Forms from JS combine. |
| 62 |
add_filter( 'convertkit_output_script_footer', array( $this, 'siteground_speed_optimizer_exclude_js_combine' ) ); |
| 63 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'siteground_speed_optimizer_exclude_js_combine' ) ); |
| 64 |
|
| 65 |
// WP Rocket: Disable Caching and Minification on Landing Pages. |
| 66 |
add_action( 'convertkit_output_landing_page_before', array( $this, 'wp_rocket_disable_caching_and_minification_on_landing_pages' ) ); |
| 67 |
|
| 68 |
// WP Rocket: Exclude Forms from Delay JavaScript execution. |
| 69 |
add_filter( 'convertkit_output_script_footer', array( $this, 'wp_rocket_exclude_delay_js_execution' ) ); |
| 70 |
add_filter( 'convertkit_resource_forms_output_script', array( $this, 'wp_rocket_exclude_delay_js_execution' ) ); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Disable JS defer on ConvertKit scripts when the Jetpack Boost Plugin is installed, active |
| 76 |
* and its "Defer Non-Essential JavaScript" setting is enabled. |
| 77 |
* |
| 78 |
* @since 2.4.6 |
| 79 |
* |
| 80 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function jetpack_boost_exclude_js_defer( $script ) { |
| 84 |
|
| 85 |
return array_merge( |
| 86 |
$script, |
| 87 |
array( |
| 88 |
'data-jetpack-boost' => 'ignore', |
| 89 |
) |
| 90 |
); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Disable JS defer on ConvertKit scripts when the LiteSpeed Cache Plugin is installed, active |
| 96 |
* and its "Load JS Deferred" setting is enabled. |
| 97 |
* |
| 98 |
* @since 2.4.6 |
| 99 |
* |
| 100 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function litespeed_cache_exclude_js_defer( $script ) { |
| 104 |
|
| 105 |
return array_merge( |
| 106 |
$script, |
| 107 |
array( |
| 108 |
'data-no-defer' => '1', |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Exclude ConvertKit scripts from Perfmatters' "Delay JS" setting. |
| 116 |
* |
| 117 |
* @since 2.4.7 |
| 118 |
* |
| 119 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 120 |
* @return array |
| 121 |
*/ |
| 122 |
public function perfmatters_exclude_delay_js( $script ) { |
| 123 |
|
| 124 |
add_filter( |
| 125 |
'perfmatters_delay_js_exclusions', |
| 126 |
function ( $exclusions ) use ( $script ) { |
| 127 |
|
| 128 |
$exclusions[] = $script['src']; |
| 129 |
return $exclusions; |
| 130 |
|
| 131 |
} |
| 132 |
); |
| 133 |
|
| 134 |
// Return original script. |
| 135 |
return $script; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Disable JS combining on ConvertKit scripts when the Siteground Speed Optimizer Plugin is installed, active |
| 141 |
* and its "Combine JavaScript Files" setting is enabled. |
| 142 |
* |
| 143 |
* @since 2.4.6 |
| 144 |
* |
| 145 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public function siteground_speed_optimizer_exclude_js_combine( $script ) { |
| 149 |
|
| 150 |
add_filter( |
| 151 |
'sgo_javascript_combine_excluded_external_paths', |
| 152 |
function ( $excluded_paths ) use ( $script ) { |
| 153 |
|
| 154 |
$excluded_paths[] = $script['src']; |
| 155 |
return $excluded_paths; |
| 156 |
|
| 157 |
} |
| 158 |
); |
| 159 |
|
| 160 |
// Return original script. |
| 161 |
return $script; |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Disable caching and minification when a WordPress Page configured to display a |
| 167 |
* ConvertKit Landing Page is viewed. |
| 168 |
* |
| 169 |
* @since 2.4.6 |
| 170 |
*/ |
| 171 |
public function wp_rocket_disable_caching_and_minification_on_landing_pages() { |
| 172 |
|
| 173 |
add_filter( 'rocket_minify_excluded_external_js', array( $this, 'exclude_hosts_from_minification' ) ); |
| 174 |
add_filter( 'rocket_exclude_css', array( $this, 'exclude_hosts_from_minification' ) ); |
| 175 |
add_filter( 'rocket_exclude_js', array( $this, 'exclude_local_js_from_minification' ) ); |
| 176 |
add_filter( 'do_rocket_lazyload', '__return_false' ); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Disable WP Rocket's "Delay JavaScript execution" on ConvertKit scripts. |
| 182 |
* |
| 183 |
* @since 2.4.7 |
| 184 |
* |
| 185 |
* @see https://docs.wp-rocket.me/article/1349-delay-javascript-execution |
| 186 |
* |
| 187 |
* @param array $script Script key/value pairs to output as <script> tag. |
| 188 |
* @return array |
| 189 |
*/ |
| 190 |
public function wp_rocket_exclude_delay_js_execution( $script ) { |
| 191 |
|
| 192 |
return array_merge( |
| 193 |
$script, |
| 194 |
array( |
| 195 |
'nowprocket' => '', |
| 196 |
) |
| 197 |
); |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Appends the $exclude_hosts property to an array of existing hosts excluded from |
| 203 |
* minification. |
| 204 |
* |
| 205 |
* @since 2.4.6 |
| 206 |
* |
| 207 |
* @param array $hosts External hosts to ignore. |
| 208 |
* @return array |
| 209 |
*/ |
| 210 |
public function exclude_hosts_from_minification( $hosts ) { |
| 211 |
|
| 212 |
return array_merge( $hosts, $this->exclude_hosts ); |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Appends the $exclude_plugin_js property to an array of existing JS files excluded from |
| 218 |
* minification. |
| 219 |
* |
| 220 |
* @since 2.4.6 |
| 221 |
* |
| 222 |
* @param array $scripts Internal JS scripts to ignore. |
| 223 |
* @return array |
| 224 |
*/ |
| 225 |
public function exclude_local_js_from_minification( $scripts ) { |
| 226 |
|
| 227 |
return array_merge( $scripts, $this->exclude_plugin_js ); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
} |
| 232 |
|