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