PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.6.8
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.6.8
3.4.2 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 All 195 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 2.6.8, at includes/class-convertkit-cache-plugins.php

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