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

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