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

437 lines 12.1 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 ( ! $this->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 * Check if any Pages, Posts or CPTs are configured to use Restrict Content.
74 *
75 * @since 2.7.6
76 *
77 * @return bool
78 */
79 private function restrict_content_enabled() {
80
81 // Check if any Pages, Posts or CPTs are configured to use Restrict Content.
82 $query = new WP_Query(
83 array(
84 'post_type' => convertkit_get_supported_post_types(),
85 'post_status' => 'publish',
86 'meta_query' => array(
87 array(
88 'key' => '_wp_convertkit_post_meta',
89 'value' => '"restrict_content";',
90 'compare' => 'LIKE',
91 ),
92 array(
93 'key' => '_wp_convertkit_post_meta',
94 'value' => '"restrict_content";s:1:"0"',
95 'compare' => 'NOT LIKE',
96 ),
97 ),
98 'posts_per_page' => 1,
99 'fields' => 'ids',
100 )
101 );
102
103 return $query->have_posts();
104
105 }
106
107 /**
108 * Show a notice in the WordPress Administration interface if
109 * Litespeed Cache is active, its caching enabled and no rule to disable caching
110 * exists when the ck_subscriber_id cookie is present.
111 *
112 * @since 2.2.2
113 */
114 public function litespeed_cache() {
115
116 // Bail if the Litespeed Cache plugin is not active.
117 if ( ! is_plugin_active( 'litespeed-cache/litespeed-cache.php' ) ) {
118 return;
119 }
120
121 // Bail if caching isn't enabled in the Litespeed Plugin.
122 $config = new \LiteSpeed\Base();
123 if ( ! $config->conf( \LiteSpeed\Base::O_CACHE ) ) {
124 return;
125 }
126
127 // If the exclusion rule exists, no need to modify anything.
128 if ( in_array( $this->key, $config->conf( \LiteSpeed\Base::O_CACHE_EXC_COOKIES ), true ) ) {
129 return;
130 }
131
132 // Litespeed Cache is active, with page caching enabled, but has not been configured to disable
133 // caching when the ck_subscriber_id cookie is present.
134 // Show a notice in the WordPress Administration, as we can't directly write to the
135 // Litespeed Cache configuration files.
136 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'litespeed_cache' );
137
138 // Define the output of the persistent notice.
139 add_filter( 'convertkit_admin_notices_output_litespeed_cache', array( $this, 'litespeed_cache_notice' ) );
140
141 }
142
143 /**
144 * Define the notice text to display in the WordPress Administration interface
145 * when Litespeed 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 * @param string $notice Notice text.
151 * @return string Notice text
152 */
153 public function litespeed_cache_notice( $notice ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
154
155 return sprintf(
156 '%s %s %s %s',
157 esc_html__( 'Kit: Member Content: Please add', 'convertkit' ),
158 '<code>ck_subscriber_id</code>',
159 sprintf(
160 '<a href="%s">%s</a>',
161 esc_url(
162 admin_url(
163 add_query_arg(
164 array(
165 'page' => 'litespeed-cache#excludes',
166 ),
167 'admin.php'
168 )
169 )
170 ),
171 esc_html__( 'to Litespeed Cache\'s "Do Not Cache Cookies" setting by clicking here.', 'convertkit' )
172 ),
173 esc_html__( 'Failing to do so will result in errors.', 'convertkit' )
174 );
175
176 }
177
178 /**
179 * Show a notice in the WordPress Administration interface if
180 * W3 Total Cache is active, its caching enabled and no rule to disable caching
181 * exists when the ck_subscriber_id cookie is present.
182 *
183 * @since 2.2.2
184 */
185 public function w3_total_cache() {
186
187 // Bail if the W3 Total Cache plugin is not active.
188 if ( ! is_plugin_active( 'w3-total-cache/w3-total-cache.php' ) ) {
189 return;
190 }
191
192 // Bail if caching isn't enabled in the W3 Total Cache Plugin.
193 $config = new \W3TC\Config();
194 if ( ! $config->get_boolean( 'pgcache.enabled' ) ) {
195 return;
196 }
197
198 // If the exclusion rule exists, no need to modify anything.
199 if ( in_array( $this->key, $config->get_array( 'pgcache.reject.cookie' ), true ) ) {
200 return;
201 }
202
203 // W3 Total Cache is active, with page caching enabled, but has not been configured to disable
204 // caching when the ck_subscriber_id cookie is present.
205 // Show a notice in the WordPress Administration, as we can't directly write to the W3
206 // Total Cache configuration files.
207 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'w3_total_cache' );
208
209 // Define the output of the persistent notice.
210 add_filter( 'convertkit_admin_notices_output_w3_total_cache', array( $this, 'w3_total_cache_notice' ) );
211
212 }
213
214 /**
215 * Define the notice text to display in the WordPress Administration interface
216 * when W3 Total Cache is active, its caching enabled and no rule to disable caching
217 * exists when the ck_subscriber_id cookie is present.
218 *
219 * @since 2.2.2
220 *
221 * @param string $notice Notice text.
222 * @return string Notice text
223 */
224 public function w3_total_cache_notice( $notice ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
225
226 return sprintf(
227 '%s %s %s %s',
228 esc_html__( 'Kit: Member Content: Please add', 'convertkit' ),
229 '<code>ck_subscriber_id</code>',
230 sprintf(
231 '<a href="%s">%s</a>',
232 esc_url(
233 admin_url(
234 add_query_arg(
235 array(
236 'page' => 'w3tc_pgcache#pgcache_reject_cookie',
237 ),
238 'admin.php'
239 )
240 )
241 ),
242 esc_html__( 'to W3 Total Cache\'s "Rejected Cookies" setting by clicking here.', 'convertkit' )
243 ),
244 esc_html__( 'Failing to do so will result in errors.', 'convertkit' )
245 );
246
247 }
248
249 /**
250 * Add a rule to WP Fastest Cache to prevent caching when
251 * the ck_subscriber_id cookie is present.
252 *
253 * @since 2.2.2
254 */
255 public function wp_fastest_cache() {
256
257 // Bail if the WP Fastest Cache plugin is not active.
258 if ( ! is_plugin_active( 'wp-fastest-cache/wpFastestCache.php' ) ) {
259 return;
260 }
261
262 // Fetch exclusion rules.
263 $exclusion_rules = get_option( 'WpFastestCacheExclude' );
264
265 // If the exclusion exists, no need to modify anything.
266 if ( strpos( $exclusion_rules, $this->key ) !== false ) {
267 return;
268 }
269
270 // Define the rule.
271 $rule = array(
272 'prefix' => 'contain',
273 'content' => $this->key,
274 'type' => 'cookie',
275 );
276
277 // If no rules exist, add the rule.
278 if ( ! $exclusion_rules ) {
279 return update_option( 'WpFastestCacheExclude', wp_json_encode( array( $rule ) ) );
280 }
281
282 // Append the rule to the existing ruleset.
283 $exclusion_rules = json_decode( $exclusion_rules );
284 $exclusion_rules[] = $rule;
285 return update_option( 'WpFastestCacheExclude', wp_json_encode( $exclusion_rules ) );
286
287 }
288
289 /**
290 * Add a rule to WP-Optimize to prevent caching when
291 * the ck_subscriber_id cookie is present.
292 *
293 * @since 2.2.2
294 */
295 public function wp_optimize() {
296
297 // Bail if the WP Fastest Cache plugin is not active.
298 if ( ! is_plugin_active( 'wp-optimize/wp-optimize.php' ) ) {
299 return;
300 }
301
302 // Sanity check that we can access WP-Optimize's configuration class.
303 if ( ! class_exists( 'WPO_Cache_Config' ) ) {
304 return;
305 }
306
307 // Fetch settings.
308 $config = new WPO_Cache_Config();
309 $settings = $config->get();
310
311 // Check the exception cookies array key exists in the settings.
312 if ( ! array_key_exists( 'cache_exception_cookies', $settings ) ) {
313 $settings['cache_exception_cookies'] = array();
314 }
315
316 // If the cookie exception exists, no need to modify anything.
317 if ( in_array( $this->key, $config->get_option( 'cache_exception_cookies' ), true ) !== false ) {
318 return;
319 }
320
321 // Add cookie to exceptions.
322 $settings['cache_exception_cookies'][] = $this->key;
323
324 // Update configuration to include excluding caching for the ck_subscriber_id cookie.
325 return $config->update( $settings );
326
327 }
328
329 /**
330 * Add a rule to WP Rocket to prevent caching when
331 * the ck_subscriber_id cookie is present.
332 *
333 * @since 2.7.0
334 */
335 public function wp_rocket() {
336
337 // Bail if the WP Rocket plugin is not active.
338 if ( ! is_plugin_active( 'wp-rocket/wp-rocket.php' ) ) {
339 return;
340 }
341
342 // Sanity check that we can access WP-Optimize's configuration class.
343 if ( ! function_exists( 'update_rocket_option' ) ) {
344 return;
345 }
346
347 // Fetch settings.
348 $settings = get_rocket_option( 'cache_reject_cookies', array() );
349
350 // If the cookie exception exists, no need to modify anything.
351 if ( in_array( $this->key, $settings, true ) ) {
352 return;
353 }
354
355 // Add cookie to exceptions.
356 $settings[] = $this->key;
357
358 // Update configuration to include excluding caching for the ck_subscriber_id cookie.
359 update_rocket_option( 'cache_reject_cookies', $settings );
360
361 }
362
363 /**
364 * Show a notice in the WordPress Administration interface if
365 * WP Super Cache is active, its caching enabled and no rule to disable caching
366 * exists when the ck_subscriber_id cookie is present.
367 *
368 * @since 2.2.2
369 */
370 public function wp_super_cache() {
371
372 // Bail if the WP Super Cache plugin is not active.
373 if ( ! is_plugin_active( 'wp-super-cache/wp-cache.php' ) ) {
374 return;
375 }
376
377 // Bail if caching isn't enabled in the WP Super Cache Plugin.
378 global $cache_enabled;
379 if ( ! $cache_enabled ) {
380 return;
381 }
382
383 // If the exclusion rule exists, no need to modify anything.
384 global $wpsc_rejected_cookies;
385 if ( is_array( $wpsc_rejected_cookies ) && in_array( $this->key, $wpsc_rejected_cookies, true ) ) {
386 return;
387 }
388
389 // WP Super Cache is active, with caching enabled, but has not been configured to disable
390 // caching when the ck_subscriber_id cookie is present.
391 // Show a notice in the WordPress Administration, as we can't directly write to the WP
392 // Super Cache configuration files.
393 WP_ConvertKit()->get_class( 'admin_notices' )->add( 'wp_super_cache' );
394
395 // Define the output of the persistent notice.
396 add_filter( 'convertkit_admin_notices_output_wp_super_cache', array( $this, 'wp_super_cache_notice' ) );
397
398 }
399
400 /**
401 * Define the notice text to display in the WordPress Administration interface
402 * when WP Super Cache is active, its caching enabled and no rule to disable caching
403 * exists when the ck_subscriber_id cookie is present.
404 *
405 * @since 2.2.2
406 *
407 * @param string $notice Notice text.
408 * @return string Notice text
409 */
410 public function wp_super_cache_notice( $notice ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
411
412 return sprintf(
413 '%s %s %s %s',
414 esc_html__( 'Kit: Member Content: Please add', 'convertkit' ),
415 '<code>ck_subscriber_id</code>',
416 sprintf(
417 '<a href="%s">%s</a>',
418 esc_url(
419 admin_url(
420 add_query_arg(
421 array(
422 'page' => 'wpsupercache',
423 'tab' => 'settings#rejectcookies',
424 ),
425 'options-general.php'
426 )
427 )
428 ),
429 esc_html__( 'to WP Super Cache\'s "Rejected Cookies" setting by clicking here.', 'convertkit' )
430 ),
431 esc_html__( 'Failing to do so will result in errors.', 'convertkit' )
432 );
433
434 }
435
436 }
437