PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.2.2.1
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.2.2.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.2.2.1, at includes/class-convertkit-cache-plugins.php

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