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

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