PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.3.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.3.1
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / class-convertkit-cache-plugins.php

class-convertkit-cache-plugins.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.3.1, at includes/class-convertkit-cache-plugins.php

344 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Rocket LazyLoad: Exclude images from lazy loading.
84 add_filter( 'rocket_lazyload_excluded_src', array( $this, 'exclude_hosts' ) );
85
86 // WP Rocket: Disable Caching and Minification on Landing Pages.
87 add_action( 'convertkit_output_landing_page_before', array( $this, 'wp_rocket_disable_caching_and_minification_on_landing_pages' ) );
88
89 // WP Rocket: Exclude Forms from JS minification and combine.
90 add_filter( 'rocket_minify_excluded_external_js', array( $this, 'exclude_hosts' ) );
91
92 // WP Rocket: Exclude Forms from Delay JavaScript execution.
93 add_filter( 'convertkit_output_script_footer', array( $this, 'wp_rocket_exclude_delay_js_execution' ) );
94 add_filter( 'convertkit_resource_forms_output_script', array( $this, 'wp_rocket_exclude_delay_js_execution' ) );
95
96 }
97
98 /**
99 * Exclude ConvertKit scripts from Autoptimize's "Defer JS" setting.
100 *
101 * @since 2.4.9
102 *
103 * @param array $script Script key/value pairs to output as <script> tag.
104 * @return array
105 */
106 public function autoptimize_exclude_js_defer( $script ) {
107
108 add_filter(
109 'autoptimize_filter_js_exclude',
110 function ( $exclusions ) use ( $script ) {
111
112 $exclusions .= ', ' . $script['src'];
113 return $exclusions;
114
115 }
116 );
117
118 // Return original script.
119 return $script;
120
121 }
122
123 /**
124 * Disable JS defer on ConvertKit scripts when the Jetpack Boost Plugin is installed, active
125 * and its "Defer Non-Essential JavaScript" setting is enabled.
126 *
127 * @since 2.4.6
128 *
129 * @param array $script Script key/value pairs to output as <script> tag.
130 * @return array
131 */
132 public function jetpack_boost_exclude_js_defer( $script ) {
133
134 return array_merge(
135 $script,
136 array(
137 'data-jetpack-boost' => 'ignore',
138 )
139 );
140
141 }
142
143 /**
144 * Disable JS defer on ConvertKit scripts when the LiteSpeed Cache Plugin is installed, active
145 * and its "Load JS Deferred" setting is enabled.
146 *
147 * @since 2.4.6
148 *
149 * @param array $script Script key/value pairs to output as <script> tag.
150 * @return array
151 */
152 public function litespeed_cache_exclude_js_defer( $script ) {
153
154 return array_merge(
155 $script,
156 array(
157 'data-no-defer' => '1',
158 )
159 );
160
161 }
162
163 /**
164 * Disable JS Optimization on Kit scripts when the LiteSpeed Cache Plugin is installed, active
165 * and its "JS Combine" setting is enabled.
166 *
167 * @since 3.3.1
168 *
169 * @param array $script Script key/value pairs to output as <script> tag.
170 * @return array
171 */
172 public function litespeed_cache_exclude_js_optimize( $script ) {
173
174 return array_merge(
175 $script,
176 array(
177 'data-no-optimize' => '1',
178 )
179 );
180
181 }
182
183 /**
184 * Exclude ConvertKit scripts from Perfmatters' "Delay JS" setting.
185 *
186 * @since 2.4.7
187 *
188 * @param array $script Script key/value pairs to output as <script> tag.
189 * @return array
190 */
191 public function perfmatters_exclude_delay_js( $script ) {
192
193 add_filter(
194 'perfmatters_delay_js_exclusions',
195 function ( $exclusions ) use ( $script ) {
196
197 $exclusions[] = $script['src'];
198 return $exclusions;
199
200 }
201 );
202
203 // Return original script.
204 return $script;
205
206 }
207
208 /**
209 * Disable image lazy loading when a WordPress Page configured to display a
210 * ConvertKit Landing Page is viewed.
211 *
212 * @since 2.5.1
213 *
214 * @param bool $enabled Lazy loading enabled.
215 * @return bool
216 */
217 public function disable_image_lazy_loading_on_landing_pages( $enabled ) {
218
219 // If the request isn't for a Page, don't change lazy loading settings.
220 if ( ! is_page( get_the_ID() ) ) {
221 return $enabled;
222 }
223
224 // If no landing page is specified for the Post, don't change lazy loading settings.
225 $post_settings = new ConvertKit_Post( get_the_ID() );
226 if ( ! $post_settings->has_landing_page() ) {
227 return $enabled;
228 }
229
230 // ConvertKit Landing Page is going to be displayed.
231 // Disable image lazy loading so that the Landing Page images display.
232 return false;
233
234 }
235
236 /**
237 * Disable JS combining on ConvertKit scripts when the Siteground Speed Optimizer Plugin is installed, active
238 * and its "Combine JavaScript Files" setting is enabled.
239 *
240 * @since 2.4.6
241 *
242 * @param array $script Script key/value pairs to output as <script> tag.
243 * @return array
244 */
245 public function siteground_speed_optimizer_exclude_js_combine( $script ) {
246
247 add_filter(
248 'sgo_javascript_combine_excluded_external_paths',
249 function ( $excluded_paths ) use ( $script ) {
250
251 $excluded_paths[] = $script['src'];
252 return $excluded_paths;
253
254 }
255 );
256
257 // Return original script.
258 return $script;
259
260 }
261
262 /**
263 * Disable caching and minification when a WordPress Page configured to display a
264 * ConvertKit Landing Page is viewed.
265 *
266 * @since 2.4.6
267 */
268 public function wp_rocket_disable_caching_and_minification_on_landing_pages() {
269
270 add_filter( 'rocket_minify_excluded_external_js', array( $this, 'exclude_hosts' ) );
271 add_filter( 'rocket_exclude_css', array( $this, 'exclude_hosts' ) );
272 add_filter( 'rocket_exclude_js', array( $this, 'exclude_local_js_from_minification' ) );
273
274 }
275
276 /**
277 * Disable WP Rocket's "Delay JavaScript execution" on ConvertKit scripts.
278 *
279 * @since 2.4.7
280 *
281 * @see https://docs.wp-rocket.me/article/1349-delay-javascript-execution
282 *
283 * @param array $script Script key/value pairs to output as <script> tag.
284 * @return array
285 */
286 public function wp_rocket_exclude_delay_js_execution( $script ) {
287
288 return array_merge(
289 $script,
290 array(
291 'nowprocket' => '',
292 )
293 );
294
295 }
296
297 /**
298 * Appends the $exclude_hosts property to an array of existing hosts excluded from
299 * minification.
300 *
301 * @since 2.4.6
302 *
303 * @param array $hosts External hosts to ignore.
304 * @return array
305 */
306 public function exclude_hosts_from_minification( $hosts ) {
307
308 _doing_it_wrong( __FUNCTION__, 'Use exclude_hosts() instead.', '3.1.0' );
309
310 return $this->exclude_hosts( $hosts );
311
312 }
313
314 /**
315 * Appends the $exclude_hosts property to an array of existing hosts.
316 *
317 * @since 3.1.0
318 *
319 * @param array $hosts External hosts to ignore.
320 * @return array
321 */
322 public function exclude_hosts( $hosts ) {
323
324 return array_merge( $hosts, $this->exclude_hosts );
325
326 }
327
328 /**
329 * Appends the $exclude_plugin_js property to an array of existing JS files excluded from
330 * minification.
331 *
332 * @since 2.4.6
333 *
334 * @param array $scripts Internal JS scripts to ignore.
335 * @return array
336 */
337 public function exclude_local_js_from_minification( $scripts ) {
338
339 return array_merge( $scripts, $this->exclude_plugin_js );
340
341 }
342
343 }
344