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

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

402 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Cache Plugins class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * If Restrict Content is enabled, run functions for common caching plugins
11 * to either:
12 * - automatically configure them to disable caching when the ck_subscriber_id is present,
13 * - show a notice in the WordPress Administration interface where we cannot automatically configure a caching plugin.
14 *
15 * Currently supported third party Plugins:
16 * - Litespeed Cache
17 * - WP-Optimize
18 * - W3 Total Cache
19 * - WP Fastest Cache
20 * - WP Super Cache
21 *
22 * @package ConvertKit
23 * @author ConvertKit
24 */
25 class ConvertKit_Admin_Cache_Plugins {
26
27 /**
28 * Holds the cookie name.
29 *
30 * @since 2.2.2
31 *
32 * @var string
33 */
34 private $key = 'ck_subscriber_id';
35
36 /**
37 * Registers action and filter hooks.
38 *
39 * @since 2.2.2
40 */
41 public function __construct() {
42
43 add_action( 'admin_init', array( $this, 'maybe_configure_cache_plugins' ) );
44
45 }
46
47 /**
48 * If Restrict Content is enabled, run functions for common caching plugins
49 * to either configure them to disable caching when the ck_subscriber_id
50 * is present, or show a notice in the WordPress Administration interface
51 * where we cannot automatically configure a caching plugin.
52 *
53 * @since 2.2.2
54 */
55 public function maybe_configure_cache_plugins() {
56
57 // If no Pages, Posts or CPTs are configured to use Restrict Content, don't
58 // configure any caching plugins.
59 if ( ! WP_ConvertKit()->get_class( 'admin_restrict_content' )->restrict_content_enabled() ) {
60 return;
61 }
62
63 $this->litespeed_cache();
64 $this->w3_total_cache();
65 $this->wp_fastest_cache();
66 $this->wp_optimize();
67 $this->wp_rocket();
68 $this->wp_super_cache();
69
70 }
71
72 /**
73 * Show a notice in the WordPress Administration interface if
74 * Litespeed Cache is active, its caching enabled and no rule to disable caching
75 * exists when the ck_subscriber_id cookie is present.
76 *
77 * @since 2.2.2
78 */
79 public function litespeed_cache() {
80
81 // Bail if the Litespeed Cache plugin is not active.
82 if ( ! is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
83 return;
84 }
85
86 // Bail if caching isn't enabled in the Litespeed Plugin.
87 $config = new \LiteSpeed\Base();
88 if ( ! $config->conf( \LiteSpeed\Base::O_CACHE ) ) {
89 return;
90 }
91
92 // If the exclusion rule exists, no need to modify anything.
93 if ( in_array( $this->key, $config->conf( \LiteSpeed\Base::O_CACHE_EXC_COOKIES ), true ) ) {
94 return;
95 }
96
97 // Litespeed Cache is active, with page caching enabled, but has not been configured to disable
98 // caching when the ck_subscriber_id cookie is present.
99 // Show a notice in the WordPress Administration, as we can't directly write to the
100 // Litespeed Cache configuration files.
101 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'litespeed_cache' );
102
103 // Define the output of the persistent notice.
104 add_filter( 'convertkit_admin_notices_output_litespeed_cache', array( $this, 'litespeed_cache_notice' ) );
105
106 }
107
108 /**
109 * Define the notice text to display in the WordPress Administration interface
110 * when Litespeed Cache is active, its caching enabled and no rule to disable caching
111 * exists when the ck_subscriber_id cookie is present.
112 *
113 * @since 2.2.2
114 *
115 * @param string $notice Notice text.
116 * @return string Notice text
117 */
118 public function litespeed_cache_notice( $notice ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
119
120 return sprintf(
121 '%s %s %s %s',
122 esc_html__( 'Kit: Member Content: Please add', 'convertkit' ),
123 '<code>ck_subscriber_id</code>',
124 sprintf(
125 '<a href="%s">%s</a>',
126 esc_url(
127 admin_url(
128 add_query_arg(
129 array(
130 'page' => 'litespeed-cache#excludes',
131 ),
132 'admin.php'
133 )
134 )
135 ),
136 esc_html__( 'to Litespeed Cache\'s "Do Not Cache Cookies" setting by clicking here.', 'convertkit' )
137 ),
138 esc_html__( 'Failing to do so will result in errors.', 'convertkit' )
139 );
140
141 }
142
143 /**
144 * Show a notice in the WordPress Administration interface if
145 * W3 Total Cache is active, its caching enabled and no rule to disable caching
146 * exists when the ck_subscriber_id cookie is present.
147 *
148 * @since 2.2.2
149 */
150 public function w3_total_cache() {
151
152 // Bail if the W3 Total Cache plugin is not active.
153 if ( ! is_plugin_active( 'w3-total-cache/w3-total-cache.php' ) ) {
154 return;
155 }
156
157 // Bail if caching isn't enabled in the W3 Total Cache Plugin.
158 $config = new \W3TC\Config();
159 if ( ! $config->get_boolean( 'pgcache.enabled' ) ) {
160 return;
161 }
162
163 // If the exclusion rule exists, no need to modify anything.
164 if ( in_array( $this->key, $config->get_array( 'pgcache.reject.cookie' ), true ) ) {
165 return;
166 }
167
168 // W3 Total Cache is active, with page caching enabled, but has not been configured to disable
169 // caching when the ck_subscriber_id cookie is present.
170 // Show a notice in the WordPress Administration, as we can't directly write to the W3
171 // Total Cache configuration files.
172 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'w3_total_cache' );
173
174 // Define the output of the persistent notice.
175 add_filter( 'convertkit_admin_notices_output_w3_total_cache', array( $this, 'w3_total_cache_notice' ) );
176
177 }
178
179 /**
180 * Define the notice text to display in the WordPress Administration interface
181 * when W3 Total Cache is active, its caching enabled and no rule to disable caching
182 * exists when the ck_subscriber_id cookie is present.
183 *
184 * @since 2.2.2
185 *
186 * @param string $notice Notice text.
187 * @return string Notice text
188 */
189 public function w3_total_cache_notice( $notice ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
190
191 return sprintf(
192 '%s %s %s %s',
193 esc_html__( 'Kit: Member Content: Please add', 'convertkit' ),
194 '<code>ck_subscriber_id</code>',
195 sprintf(
196 '<a href="%s">%s</a>',
197 esc_url(
198 admin_url(
199 add_query_arg(
200 array(
201 'page' => 'w3tc_pgcache#pgcache_reject_cookie',
202 ),
203 'admin.php'
204 )
205 )
206 ),
207 esc_html__( 'to W3 Total Cache\'s "Rejected Cookies" setting by clicking here.', 'convertkit' )
208 ),
209 esc_html__( 'Failing to do so will result in errors.', 'convertkit' )
210 );
211
212 }
213
214 /**
215 * Add a rule to WP Fastest Cache to prevent caching when
216 * the ck_subscriber_id cookie is present.
217 *
218 * @since 2.2.2
219 */
220 public function wp_fastest_cache() {
221
222 // Bail if the WP Fastest Cache plugin is not active.
223 if ( ! is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) {
224 return;
225 }
226
227 // Fetch exclusion rules.
228 $exclusion_rules = get_option( 'WpFastestCacheExclude' );
229
230 // If the exclusion exists, no need to modify anything.
231 if ( strpos( $exclusion_rules, $this->key ) !== false ) {
232 return;
233 }
234
235 // Define the rule.
236 $rule = array(
237 'prefix' => 'contain',
238 'content' => $this->key,
239 'type' => 'cookie',
240 );
241
242 // If no rules exist, add the rule.
243 if ( ! $exclusion_rules ) {
244 return update_option( 'WpFastestCacheExclude', wp_json_encode( array( $rule ) ) );
245 }
246
247 // Append the rule to the existing ruleset.
248 $exclusion_rules = json_decode( $exclusion_rules );
249 $exclusion_rules[] = $rule;
250 return update_option( 'WpFastestCacheExclude', wp_json_encode( $exclusion_rules ) );
251
252 }
253
254 /**
255 * Add a rule to WP-Optimize to prevent caching when
256 * the ck_subscriber_id cookie is present.
257 *
258 * @since 2.2.2
259 */
260 public function wp_optimize() {
261
262 // Bail if the WP Fastest Cache plugin is not active.
263 if ( ! is_plugin_active( 'wp-optimize/wp-optimize.php' ) ) {
264 return;
265 }
266
267 // Sanity check that we can access WP-Optimize's configuration class.
268 if ( ! class_exists( 'WPO_Cache_Config' ) ) {
269 return;
270 }
271
272 // Fetch settings.
273 $config = new WPO_Cache_Config();
274 $settings = $config->get();
275
276 // Check the exception cookies array key exists in the settings.
277 if ( ! array_key_exists( 'cache_exception_cookies', $settings ) ) {
278 $settings['cache_exception_cookies'] = array();
279 }
280
281 // If the cookie exception exists, no need to modify anything.
282 if ( in_array( $this->key, $config->get_option( 'cache_exception_cookies' ), true ) !== false ) {
283 return;
284 }
285
286 // Add cookie to exceptions.
287 $settings['cache_exception_cookies'][] = $this->key;
288
289 // Update configuration to include excluding caching for the ck_subscriber_id cookie.
290 return $config->update( $settings );
291
292 }
293
294 /**
295 * Add a rule to WP Rocket to prevent caching when
296 * the ck_subscriber_id cookie is present.
297 *
298 * @since 2.7.0
299 */
300 public function wp_rocket() {
301
302 // Bail if the WP Rocket plugin is not active.
303 if ( ! is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) {
304 return;
305 }
306
307 // Sanity check that we can access WP-Optimize's configuration class.
308 if ( ! function_exists( 'update_rocket_option' ) ) {
309 return;
310 }
311
312 // Fetch settings.
313 $settings = get_rocket_option( 'cache_reject_cookies', array() );
314
315 // If the cookie exception exists, no need to modify anything.
316 if ( in_array( $this->key, $settings, true ) ) {
317 return;
318 }
319
320 // Add cookie to exceptions.
321 $settings[] = $this->key;
322
323 // Update configuration to include excluding caching for the ck_subscriber_id cookie.
324 update_rocket_option( 'cache_reject_cookies', $settings );
325
326 }
327
328 /**
329 * Show a notice in the WordPress Administration interface if
330 * WP Super Cache is active, its caching enabled and no rule to disable caching
331 * exists when the ck_subscriber_id cookie is present.
332 *
333 * @since 2.2.2
334 */
335 public function wp_super_cache() {
336
337 // Bail if the WP Super Cache plugin is not active.
338 if ( ! is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
339 return;
340 }
341
342 // Bail if caching isn't enabled in the WP Super Cache Plugin.
343 global $cache_enabled;
344 if ( ! $cache_enabled ) {
345 return;
346 }
347
348 // If the exclusion rule exists, no need to modify anything.
349 global $wpsc_rejected_cookies;
350 if ( is_array( $wpsc_rejected_cookies ) && in_array( $this->key, $wpsc_rejected_cookies, true ) ) {
351 return;
352 }
353
354 // WP Super Cache is active, with caching enabled, but has not been configured to disable
355 // caching when the ck_subscriber_id cookie is present.
356 // Show a notice in the WordPress Administration, as we can't directly write to the WP
357 // Super Cache configuration files.
358 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'wp_super_cache' );
359
360 // Define the output of the persistent notice.
361 add_filter( 'convertkit_admin_notices_output_wp_super_cache', array( $this, 'wp_super_cache_notice' ) );
362
363 }
364
365 /**
366 * Define the notice text to display in the WordPress Administration interface
367 * when WP Super Cache is active, its caching enabled and no rule to disable caching
368 * exists when the ck_subscriber_id cookie is present.
369 *
370 * @since 2.2.2
371 *
372 * @param string $notice Notice text.
373 * @return string Notice text
374 */
375 public function wp_super_cache_notice( $notice ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
376
377 return sprintf(
378 '%s %s %s %s',
379 esc_html__( 'Kit: Member Content: Please add', 'convertkit' ),
380 '<code>ck_subscriber_id</code>',
381 sprintf(
382 '<a href="%s">%s</a>',
383 esc_url(
384 admin_url(
385 add_query_arg(
386 array(
387 'page' => 'wpsupercache',
388 'tab' => 'settings#rejectcookies',
389 ),
390 'options-general.php'
391 )
392 )
393 ),
394 esc_html__( 'to WP Super Cache\'s "Rejected Cookies" setting by clicking here.', 'convertkit' )
395 ),
396 esc_html__( 'Failing to do so will result in errors.', 'convertkit' )
397 );
398
399 }
400
401 }
402