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

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