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

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