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

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