| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin |
| 4 |
* |
| 5 |
* @package PoweredCache |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PoweredCache; |
| 9 |
|
| 10 |
use PoweredCache\Async\CachePurger; |
| 11 |
use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY; |
| 12 |
use function PoweredCache\Utils\clean_page_cache_dir; |
| 13 |
use function PoweredCache\Utils\clean_site_cache_dir; |
| 14 |
use function PoweredCache\Utils\delete_page_cache; |
| 15 |
use function PoweredCache\Utils\get_post_related_urls; |
| 16 |
use const PoweredCache\Constants\PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; // Exit if accessed directly. |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Admin |
| 24 |
* |
| 25 |
* @package PoweredCache |
| 26 |
*/ |
| 27 |
class AdvancedCache { |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds plugin settings |
| 31 |
* |
| 32 |
* @var array $settings |
| 33 |
*/ |
| 34 |
private $settings; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Instance of CachePreloader |
| 39 |
* |
| 40 |
* @var CachePurger |
| 41 |
*/ |
| 42 |
private $cache_purger; |
| 43 |
|
| 44 |
/** |
| 45 |
* Return an instance of the current class |
| 46 |
* |
| 47 |
* @return AdvancedCache |
| 48 |
* @since 1.0 |
| 49 |
*/ |
| 50 |
public static function factory() { |
| 51 |
|
| 52 |
static $instance; |
| 53 |
|
| 54 |
if ( ! $instance ) { |
| 55 |
$instance = new self(); |
| 56 |
$instance->setup(); |
| 57 |
} |
| 58 |
|
| 59 |
return $instance; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Setup hooks |
| 64 |
* |
| 65 |
* @since 1.0 |
| 66 |
*/ |
| 67 |
public function setup() { |
| 68 |
$this->settings = \PoweredCache\Utils\get_settings(); |
| 69 |
|
| 70 |
if ( ! $this->settings['enable_page_cache'] ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$this->cache_purger = CachePurger::factory(); |
| 75 |
|
| 76 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) ); |
| 77 |
add_action( 'admin_post_powered_cache_purge_page_cache', array( $this, 'purge_page_cache' ) ); |
| 78 |
add_action( 'admin_post_powered_cache_purge_page_cache_network', array( $this, 'purge_page_cache_network_wide' ) ); |
| 79 |
add_action( 'transition_post_status', array( $this, 'purge_on_post_update' ), 9999, 3 ); |
| 80 |
add_action( 'switch_theme', array( $this, 'purge_on_switch_theme' ) ); |
| 81 |
add_action( 'wp_set_comment_status', array( $this, 'purge_post_on_comment_update' ) ); |
| 82 |
add_action( 'edit_comment', array( $this, 'purge_post_on_comment_update' ) ); |
| 83 |
add_action( 'set_comment_cookies', array( $this, 'set_comment_cookie' ), 10, 2 ); |
| 84 |
add_filter( 'powered_cache_post_related_urls', array( $this, 'powered_cache_post_related_urls' ) ); |
| 85 |
add_filter( 'powered_cache_page_cache_enable', array( $this, 'maybe_caching_disabled' ) ); |
| 86 |
add_filter( 'powered_cache_mod_rewrite', array( $this, 'maybe_disable_mod_rewrite' ), 99 ); |
| 87 |
add_action( 'wp_update_site', array( $this, 'purge_on_site_update' ), 10, 2 ); |
| 88 |
add_action( 'create_term', array( $this, 'purge_on_term_change' ), 10, 3 ); |
| 89 |
add_action( 'edit_term', array( $this, 'purge_on_term_change' ), 10, 3 ); |
| 90 |
add_action( 'delete_term', array( $this, 'purge_on_term_change' ), 10, 3 ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Add purge button on admin bar |
| 95 |
* |
| 96 |
* @param object $wp_admin_bar Admin bar object |
| 97 |
* |
| 98 |
* @since 1.0 |
| 99 |
*/ |
| 100 |
public function admin_bar_menu( $wp_admin_bar ) { |
| 101 |
if ( POWERED_CACHE_IS_NETWORK && current_user_can( 'manage_network' ) ) { |
| 102 |
$wp_admin_bar->add_menu( |
| 103 |
array( |
| 104 |
'id' => 'advanced-cache-purge-network', |
| 105 |
'title' => __( 'Purge Page Cache [Network Wide - All Sites]', 'powered-cache' ), |
| 106 |
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_page_cache_network' ), 'powered_cache_purge_page_cache_network' ), |
| 107 |
'parent' => 'powered-cache', |
| 108 |
) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
if ( current_user_can( 'manage_options' ) ) { |
| 113 |
$wp_admin_bar->add_menu( |
| 114 |
array( |
| 115 |
'id' => 'advanced-cache-purge', |
| 116 |
'title' => __( 'Purge Page Cache', 'powered-cache' ), |
| 117 |
'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_page_cache' ), 'powered_cache_purge_page_cache' ), |
| 118 |
'parent' => 'powered-cache', |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
if ( is_singular() && ! is_preview() && is_post_publicly_viewable( get_the_ID() ) ) { |
| 123 |
$wp_admin_bar->add_menu( |
| 124 |
array( |
| 125 |
'id' => 'advanced-cache-current-page-purge', |
| 126 |
'title' => esc_html__( 'Purge Current Page', 'powered-cache' ), |
| 127 |
'href' => wp_nonce_url( admin_url( sprintf( 'admin-post.php?action=powered_cache_purge_page_cache&type=current-page&post=%d', get_the_ID() ) ), 'powered_cache_purge_page_cache' ), |
| 128 |
'parent' => 'powered-cache', |
| 129 |
) |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
/** |
| 137 |
* Purge page cache network wide |
| 138 |
* |
| 139 |
* @since 2.0 |
| 140 |
*/ |
| 141 |
public function purge_page_cache_network_wide() { |
| 142 |
$nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; |
| 143 |
if ( ! wp_verify_nonce( $nonce, 'powered_cache_purge_page_cache_network' ) ) { |
| 144 |
wp_nonce_ays( '' ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( current_user_can( 'manage_network' ) ) { |
| 148 |
if ( $this->settings['async_cache_cleaning'] ) { |
| 149 |
$this->cache_purger->push_to_queue( [ 'call' => 'clean_page_cache_dir' ] ); |
| 150 |
$this->cache_purger->save()->dispatch(); |
| 151 |
} else { |
| 152 |
clean_page_cache_dir(); |
| 153 |
} |
| 154 |
$redirect_url = add_query_arg( 'pc_action', 'flush_page_cache_network', wp_get_referer() ); |
| 155 |
} else { |
| 156 |
$redirect_url = add_query_arg( 'pc_action', 'flush_page_cache_network_err_permission', wp_get_referer() ); |
| 157 |
} |
| 158 |
|
| 159 |
delete_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); |
| 160 |
|
| 161 |
wp_safe_redirect( esc_url_raw( $redirect_url ) ); |
| 162 |
exit; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Purge page cache directory |
| 167 |
* |
| 168 |
* @since 1.0 |
| 169 |
* @since 1.1 clean site dir instead of root page caching dir |
| 170 |
*/ |
| 171 |
public function purge_page_cache() { |
| 172 |
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'powered_cache_purge_page_cache' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated |
| 173 |
wp_nonce_ays( '' ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( current_user_can( 'manage_options' ) ) { |
| 177 |
if ( isset( $_GET['type'] ) && 'current-page' === $_GET['type'] && ! empty( $_GET['post'] ) ) { |
| 178 |
$this->purge_post( absint( $_GET['post'] ) ); |
| 179 |
} elseif ( $this->settings['async_cache_cleaning'] ) { |
| 180 |
$this->cache_purger->push_to_queue( [ 'call' => 'clean_site_cache_dir' ] ); |
| 181 |
$this->cache_purger->save()->dispatch(); |
| 182 |
} else { |
| 183 |
clean_site_cache_dir(); |
| 184 |
} |
| 185 |
|
| 186 |
$redirect_url = add_query_arg( 'pc_action', 'flush_page_cache', wp_get_referer() ); |
| 187 |
} else { |
| 188 |
$redirect_url = add_query_arg( 'pc_action', 'flush_page_cache_err_permission', wp_get_referer() ); |
| 189 |
} |
| 190 |
|
| 191 |
delete_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); |
| 192 |
|
| 193 |
wp_safe_redirect( esc_url_raw( $redirect_url ) ); |
| 194 |
exit; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Purge cache when post updated |
| 200 |
* |
| 201 |
* @param string $new_status New Post status. |
| 202 |
* @param string $old_status Old Post status. |
| 203 |
* @param \WP_Post $post Post object. |
| 204 |
* |
| 205 |
* @return void |
| 206 |
* @since 3.4 adjusted for `transition_post_status` hook |
| 207 |
* @since 1.0 |
| 208 |
*/ |
| 209 |
public function purge_on_post_update( $new_status, $old_status, $post ) { |
| 210 |
if ( 'publish' === $new_status || 'publish' === $old_status ) { |
| 211 |
$this->purge_post( $post->ID ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Purge post related page cache |
| 217 |
* |
| 218 |
* @param int $post_id Post ID |
| 219 |
* |
| 220 |
* @since 3.4 |
| 221 |
*/ |
| 222 |
public function purge_post( $post_id ) { |
| 223 |
$current_post = get_post( $post_id ); |
| 224 |
|
| 225 |
if ( ! is_a( $current_post, 'WP_Post' ) ) { |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
if ( ! is_post_type_viewable( $current_post->post_type ) ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
|
| 237 |
if ( ! in_array( $current_post->post_status, array( 'publish', 'private', 'trash', 'pending', 'draft' ), true ) ) { |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
$urls = get_post_related_urls( $post_id ); |
| 242 |
|
| 243 |
/** |
| 244 |
* Page cache purge urls. |
| 245 |
* |
| 246 |
* @hook powered_cache_advanced_cache_purge_urls |
| 247 |
* |
| 248 |
* @param {array} $urls The list of URLs that will purged |
| 249 |
* @param {int} $post_id Post ID. |
| 250 |
* |
| 251 |
* @return {array} New value |
| 252 |
* @since 1.0 |
| 253 |
*/ |
| 254 |
$urls = apply_filters( 'powered_cache_advanced_cache_purge_urls', $urls, $post_id ); |
| 255 |
$deleted_urls = []; |
| 256 |
|
| 257 |
if ( $this->settings['async_cache_cleaning'] ) { |
| 258 |
$this->cache_purger->push_to_queue( |
| 259 |
[ |
| 260 |
'call' => 'delete_page_cache', |
| 261 |
'urls' => $urls, |
| 262 |
] |
| 263 |
); |
| 264 |
$deleted_urls = $urls; |
| 265 |
$this->cache_purger->save()->dispatch(); |
| 266 |
} else { |
| 267 |
foreach ( $urls as $url ) { |
| 268 |
if ( delete_page_cache( $url ) ) { |
| 269 |
$deleted_urls[] = $url; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Fires after purging cache on post update. |
| 276 |
* |
| 277 |
* @hook powered_cache_advanced_cache_purge_post |
| 278 |
* |
| 279 |
* @param {int} $post_id The Post ID. |
| 280 |
* @param {array} $deleted_urls The list of purged urls with the updated post. |
| 281 |
* @param {array} $urls The list of urls that will be purged. @since 3.6 |
| 282 |
* |
| 283 |
* @since 1.0 |
| 284 |
*/ |
| 285 |
do_action( 'powered_cache_advanced_cache_purge_post', $post_id, $deleted_urls, $urls ); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Purge post cache when comment status change |
| 290 |
* |
| 291 |
* @param int $comment_id Comment ID |
| 292 |
* @param string $comment_status Comment status |
| 293 |
* |
| 294 |
* @return void |
| 295 |
* @deprecated since 3.4.4 |
| 296 |
*/ |
| 297 |
public function purge_post_on_comment_status_change( $comment_id, $comment_status ) { |
| 298 |
// deprecate this method in favor of `purge_post_on_comment_update` |
| 299 |
_deprecated_function( __METHOD__, '3.4.4', 'purge_post_on_comment_update' ); |
| 300 |
$this->purge_post_on_comment_update( $comment_id ); |
| 301 |
$comment = get_comment( $comment_id ); |
| 302 |
$post_id = $comment->comment_post_ID; |
| 303 |
$post_url = get_permalink( $post_id ); |
| 304 |
|
| 305 |
do_action( 'powered_cache_advanced_cache_purge_on_comment_update', $post_id, $post_url, $comment_id ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Delete page cache when a comment update |
| 310 |
* |
| 311 |
* @param int $comment_id Comment ID |
| 312 |
* |
| 313 |
* @since 3.4.4 |
| 314 |
*/ |
| 315 |
public function purge_post_on_comment_update( $comment_id ) { |
| 316 |
$comment = get_comment( $comment_id ); |
| 317 |
$post_id = $comment->comment_post_ID; |
| 318 |
$post_url = get_permalink( $post_id ); |
| 319 |
delete_page_cache( $post_url, true ); |
| 320 |
|
| 321 |
/** |
| 322 |
* Fires after purging cache for a post that associated a comment |
| 323 |
* |
| 324 |
* @hook powered_cache_advanced_cache_purge_on_comment_update |
| 325 |
* |
| 326 |
* @param {int} $post_id Post ID. |
| 327 |
* @param {string} $post_url Post permalink. |
| 328 |
* @param {int} $comment_id Comment ID. |
| 329 |
* |
| 330 |
* @since 3.4.4 |
| 331 |
*/ |
| 332 |
do_action( 'powered_cache_advanced_cache_purge_on_comment_update', $post_id, $post_url, $comment_id ); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Purge site when switching to a new theme |
| 337 |
* |
| 338 |
* @since 2.0 |
| 339 |
*/ |
| 340 |
public function purge_on_switch_theme() { |
| 341 |
if ( $this->settings['async_cache_cleaning'] ) { |
| 342 |
$this->cache_purger->push_to_queue( [ 'call' => 'clean_site_cache_dir' ] ); |
| 343 |
$this->cache_purger->save()->dispatch(); |
| 344 |
} else { |
| 345 |
clean_site_cache_dir(); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Purge site cache when site updated (eg: archived, deleted etc...) |
| 351 |
* |
| 352 |
* @param \WP_Site $new_site New site object. |
| 353 |
* @param \WP_Site $old_site Old site object. |
| 354 |
* |
| 355 |
* @return void |
| 356 |
* @since 2.5.3 |
| 357 |
*/ |
| 358 |
public function purge_on_site_update( $new_site, $old_site ) { |
| 359 |
switch_to_blog( $old_site->id ); |
| 360 |
if ( $this->settings['async_cache_cleaning'] ) { |
| 361 |
$this->cache_purger->push_to_queue( [ 'call' => 'clean_site_cache_dir' ] ); |
| 362 |
$this->cache_purger->save()->dispatch(); |
| 363 |
} else { |
| 364 |
clean_site_cache_dir(); |
| 365 |
} |
| 366 |
restore_current_blog(); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Leave a cookie when commenting on a post as usual and don't show the cached page. |
| 371 |
* `comment_author_*` cookies are site-wide available. |
| 372 |
* Don't show cached results just for left a comment 5 months ago is not seems efficient here. |
| 373 |
* |
| 374 |
* @param object $comment Comment object |
| 375 |
* @param object $user User Object |
| 376 |
* |
| 377 |
* @since 1.0 |
| 378 |
*/ |
| 379 |
public function set_comment_cookie( $comment, $user ) { |
| 380 |
$post_id = $comment->comment_post_ID; |
| 381 |
$path = wp_parse_url( get_permalink( $post_id ), PHP_URL_PATH ); |
| 382 |
setcookie( 'powered_cache_commented_posts[' . $post_id . ']', $path, ( time() + DAY_IN_SECONDS * 30 ), $path ); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/** |
| 387 |
* Adds custom pages to post related urls |
| 388 |
* |
| 389 |
* @param array $urls The list of the urls |
| 390 |
* |
| 391 |
* @return array urls |
| 392 |
* @since 1.1 |
| 393 |
*/ |
| 394 |
public function powered_cache_post_related_urls( $urls ) { |
| 395 |
$settings = \PoweredCache\Utils\get_settings(); |
| 396 |
|
| 397 |
$additional_pages = $settings['purge_additional_pages']; |
| 398 |
|
| 399 |
if ( empty( $additional_pages ) ) { |
| 400 |
return $urls; |
| 401 |
} |
| 402 |
|
| 403 |
// we only need relative path |
| 404 |
$additional_pages = str_replace( site_url(), '', $additional_pages ); |
| 405 |
$additional_page_paths = explode( PHP_EOL, $additional_pages ); |
| 406 |
$additional_urls = array(); |
| 407 |
|
| 408 |
foreach ( $additional_page_paths as $page ) { |
| 409 |
$additional_urls[] = site_url( $page ); |
| 410 |
} |
| 411 |
|
| 412 |
$urls = array_merge( $urls, $additional_urls ); |
| 413 |
|
| 414 |
return $urls; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
/** |
| 419 |
* Get the list of rejected cookies |
| 420 |
* |
| 421 |
* @return mixed|void |
| 422 |
* @since 2.0 |
| 423 |
*/ |
| 424 |
public static function get_rejected_cookies() { |
| 425 |
$settings = \PoweredCache\Utils\get_settings(); |
| 426 |
$cookies = []; |
| 427 |
|
| 428 |
if ( ! empty( $settings['rejected_cookies'] ) ) { |
| 429 |
$cookies = preg_split( '#(\r\n|\r|\n)#', $settings['rejected_cookies'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 430 |
} |
| 431 |
|
| 432 |
$wp_cookies = [ |
| 433 |
'wp-postpass', |
| 434 |
'wordpressuser_', |
| 435 |
'wordpresspass_', |
| 436 |
'wordpress_sec_', |
| 437 |
'wordpress_logged_in_', |
| 438 |
'powered_cache_commented_posts', |
| 439 |
'comment_author_', |
| 440 |
'comment_author_email_', |
| 441 |
'comment_author_url_', |
| 442 |
]; |
| 443 |
|
| 444 |
if ( ! empty( $cookies ) ) { |
| 445 |
$wp_cookies = array_merge( $cookies, $wp_cookies ); |
| 446 |
} |
| 447 |
|
| 448 |
/** |
| 449 |
* Filter rejected cookie list. |
| 450 |
* |
| 451 |
* @hook powered_cache_rejected_cookies |
| 452 |
* |
| 453 |
* @param {array} $wp_cookies The rejected cookie list from the caching. |
| 454 |
* |
| 455 |
* @return {array} Rejected cookie list |
| 456 |
* @since 2.0 |
| 457 |
*/ |
| 458 |
return apply_filters( 'powered_cache_rejected_cookies', $wp_cookies ); |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Get the list of rejected cookies |
| 463 |
* |
| 464 |
* @return mixed|void |
| 465 |
* @since 2.0 |
| 466 |
*/ |
| 467 |
public static function get_vary_cookies() { |
| 468 |
$settings = \PoweredCache\Utils\get_settings(); |
| 469 |
$cookies = []; |
| 470 |
|
| 471 |
if ( ! empty( $settings['vary_cookies'] ) ) { |
| 472 |
$cookies = preg_split( '#(\r\n|\r|\n)#', $settings['vary_cookies'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 473 |
} |
| 474 |
|
| 475 |
/** |
| 476 |
* Filter vary cookie list. |
| 477 |
* |
| 478 |
* @hook powered_cache_vary_cookies |
| 479 |
* |
| 480 |
* @param {array} $cookies The varied cookie list, which allows to create separate cache based on the match. |
| 481 |
* |
| 482 |
* @return {array} Vary cookie list |
| 483 |
* @since 2.0 |
| 484 |
*/ |
| 485 |
return apply_filters( 'powered_cache_vary_cookies', $cookies ); |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Get the list of rejected referrers |
| 490 |
* |
| 491 |
* @return mixed|null |
| 492 |
* @since 3.6 |
| 493 |
*/ |
| 494 |
public static function get_rejected_referrers() { |
| 495 |
$settings = \PoweredCache\Utils\get_settings(); |
| 496 |
$rejected_referrers = []; |
| 497 |
|
| 498 |
if ( ! empty( $settings['rejected_referrers'] ) ) { |
| 499 |
$rejected_referrers = preg_split( '#(\r\n|\r|\n)#', $settings['rejected_referrers'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Filter rejected referrer list. |
| 504 |
* |
| 505 |
* @hook powered_cache_rejected_referrers |
| 506 |
* |
| 507 |
* @param {array} $rejected_referrers The referrer that will not see the cached page. |
| 508 |
* |
| 509 |
* @return {array} Rejected referrer list. |
| 510 |
* @since 3.6 |
| 511 |
*/ |
| 512 |
return apply_filters( 'powered_cache_rejected_referrers', $rejected_referrers ); |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Get the list of rejected user agents |
| 517 |
* |
| 518 |
* @return mixed|void |
| 519 |
* @since 2.0 |
| 520 |
*/ |
| 521 |
public static function get_rejected_user_agents() { |
| 522 |
$settings = \PoweredCache\Utils\get_settings(); |
| 523 |
$rejected_user_agents = []; |
| 524 |
|
| 525 |
if ( ! empty( $settings['rejected_user_agents'] ) ) { |
| 526 |
$rejected_user_agents = preg_split( '#(\r\n|\r|\n)#', $settings['rejected_user_agents'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 527 |
} |
| 528 |
|
| 529 |
$rejected_user_agents[] = 'facebookexternalhit'; |
| 530 |
|
| 531 |
/** |
| 532 |
* Filter rejected user agent list. |
| 533 |
* |
| 534 |
* @hook powered_cache_rejected_user_agents |
| 535 |
* |
| 536 |
* @param {array} $rejected_user_agents The user agents that will not see the cached page. |
| 537 |
* |
| 538 |
* @return {array} Rejected user agent list. |
| 539 |
* @since 2.0 |
| 540 |
*/ |
| 541 |
return apply_filters( 'powered_cache_rejected_user_agents', $rejected_user_agents ); |
| 542 |
} |
| 543 |
|
| 544 |
/** |
| 545 |
* Get the list of rejected uri that never get cached |
| 546 |
* |
| 547 |
* @return mixed|void |
| 548 |
* @since 2.0 |
| 549 |
*/ |
| 550 |
public static function get_rejected_uri() { |
| 551 |
$settings = \PoweredCache\Utils\get_settings(); |
| 552 |
$rejected_uri_list = []; |
| 553 |
|
| 554 |
if ( ! empty( $settings['rejected_uri'] ) ) { |
| 555 |
$rejected_uri_list = preg_split( '#(\r\n|\r|\n)#', $settings['rejected_uri'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* Filter rejected uri list |
| 560 |
* |
| 561 |
* @hook powered_cache_rejected_uri_list |
| 562 |
* |
| 563 |
* @param {array} $rejected_uri_list The list of rejected uri that never get cached. |
| 564 |
* |
| 565 |
* @return {array} New value |
| 566 |
* @since 2.0 |
| 567 |
*/ |
| 568 |
return apply_filters( 'powered_cache_rejected_uri_list', $rejected_uri_list ); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
/** |
| 573 |
* Get the allowed cache query parameters |
| 574 |
* |
| 575 |
* @return mixed|null |
| 576 |
* @since 3.0 |
| 577 |
*/ |
| 578 |
public static function get_cache_query_string() { |
| 579 |
$settings = \PoweredCache\Utils\get_settings(); |
| 580 |
$cache_query_strings = []; |
| 581 |
|
| 582 |
if ( ! empty( $settings['cache_query_strings'] ) ) { |
| 583 |
$cache_query_strings = preg_split( '#(\r\n|\r|\n)#', $settings['cache_query_strings'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 584 |
} |
| 585 |
|
| 586 |
$query_strings = [ |
| 587 |
'lang', |
| 588 |
]; |
| 589 |
|
| 590 |
if ( ! empty( $cache_query_strings ) ) { |
| 591 |
$cache_query_strings = array_merge( $query_strings, $cache_query_strings ); |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Filter accepted query strings. |
| 596 |
* |
| 597 |
* @hook powered_cache_cache_query_strings |
| 598 |
* |
| 599 |
* @param {array} $query_strings The list of query strings that will be cached based on their value |
| 600 |
* |
| 601 |
* @return {array} New value |
| 602 |
* @since 3.0 |
| 603 |
*/ |
| 604 |
return apply_filters( 'powered_cache_cache_query_strings', $cache_query_strings ); |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* These query strings will be ignored during the caching |
| 609 |
* |
| 610 |
* @return array |
| 611 |
* @since 3.0 deprecated |
| 612 |
* @deprecated Use `self::get_ignored_query_strings` instead |
| 613 |
*/ |
| 614 |
public static function get_accepted_query_strings() { |
| 615 |
_deprecated_function( '\PoweredCache\AdvancedCache::get_accepted_query_strings', '3.0', '\PoweredCache\AdvancedCache::get_ignored_query_strings' ); |
| 616 |
|
| 617 |
return self::get_ignored_query_strings(); |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* These query strings will be ignored during the caching |
| 622 |
* |
| 623 |
* @return mixed|void |
| 624 |
* @since 3.0 |
| 625 |
*/ |
| 626 |
public static function get_ignored_query_strings() { |
| 627 |
$settings = \PoweredCache\Utils\get_settings(); |
| 628 |
$ignored_query_strings = []; |
| 629 |
|
| 630 |
if ( ! empty( $settings['ignored_query_strings'] ) ) { |
| 631 |
$ignored_query_strings = preg_split( '#(\r\n|\r|\n)#', $settings['ignored_query_strings'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 632 |
} |
| 633 |
|
| 634 |
$query_strings = [ |
| 635 |
'fbclid', |
| 636 |
'fb_action_ids', |
| 637 |
'fb_action_types', |
| 638 |
'ref', |
| 639 |
'gclid', |
| 640 |
'fb_source', |
| 641 |
'utm_source', |
| 642 |
'utm_medium', |
| 643 |
'utm_campaign', |
| 644 |
'utm_term', |
| 645 |
'utm_content', |
| 646 |
'utm_expid', |
| 647 |
'_ga', |
| 648 |
'mc_cid', |
| 649 |
'mc_eid', |
| 650 |
'campaignid', |
| 651 |
'adgroupid', |
| 652 |
'adid', |
| 653 |
'age-verified', |
| 654 |
'usqp', |
| 655 |
'cn-reloaded', |
| 656 |
'ao_noptimize', |
| 657 |
]; |
| 658 |
|
| 659 |
if ( ! empty( $ignored_query_strings ) ) { |
| 660 |
$query_strings = array_merge( $query_strings, $ignored_query_strings ); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Filters ignored query strings. |
| 665 |
* |
| 666 |
* @hook powered_cache_accepted_query_strings |
| 667 |
* |
| 668 |
* @param {array} $query_strings The list of query strings will be ignored during the caching |
| 669 |
* |
| 670 |
* @return {array} New value |
| 671 |
* @since 2.0 |
| 672 |
* @depreacated since 3.0 |
| 673 |
*/ |
| 674 |
$query_strings = apply_filters( 'powered_cache_accepted_query_strings', $query_strings ); |
| 675 |
|
| 676 |
/** |
| 677 |
* Filters ignored query strings. |
| 678 |
* |
| 679 |
* @hook powered_cache_ignored_query_strings |
| 680 |
* |
| 681 |
* @param {array} $query_strings The list of query strings will be ignored during the caching |
| 682 |
* |
| 683 |
* @return {array} New value |
| 684 |
* @since 3.0 |
| 685 |
*/ |
| 686 |
$query_strings = apply_filters( 'powered_cache_ignored_query_strings', $query_strings ); |
| 687 |
|
| 688 |
return $query_strings; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
/** |
| 693 |
* Disable caching of the individual page when it marked as dont' cache |
| 694 |
* |
| 695 |
* @param bool $status Cache status |
| 696 |
* |
| 697 |
* @return bool |
| 698 |
*/ |
| 699 |
public function maybe_caching_disabled( $status ) { |
| 700 |
if ( is_single() ) { |
| 701 |
$is_cache_disabled = (bool) get_post_meta( get_the_ID(), POST_META_DISABLE_CACHE_KEY, true ); |
| 702 |
if ( $is_cache_disabled ) { |
| 703 |
return false; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
return $status; |
| 708 |
} |
| 709 |
|
| 710 |
/** |
| 711 |
* Maybe disable rewrite rules based on the settings |
| 712 |
* |
| 713 |
* @param bool $rewrite_status mod rewrite status |
| 714 |
* |
| 715 |
* @return bool |
| 716 |
* @since 2.0 |
| 717 |
*/ |
| 718 |
public function maybe_disable_mod_rewrite( $rewrite_status ) { |
| 719 |
$vary_cookies = self::get_vary_cookies(); |
| 720 |
if ( ! empty( $vary_cookies ) ) { |
| 721 |
$rewrite_status = false; |
| 722 |
} |
| 723 |
|
| 724 |
return $rewrite_status; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
/** |
| 729 |
* Purge cache when term change |
| 730 |
* |
| 731 |
* @param int $term_id Term ID |
| 732 |
* @param int $tt_id Term Taxonomy ID |
| 733 |
* @param string $taxonomy Taxonomy |
| 734 |
* |
| 735 |
* @since 3.4.2 |
| 736 |
*/ |
| 737 |
public function purge_on_term_change( $term_id, $tt_id, $taxonomy ) { |
| 738 |
$term_taxonomy = get_taxonomy( $taxonomy ); |
| 739 |
|
| 740 |
if ( ! $term_taxonomy->public ) { |
| 741 |
return; |
| 742 |
} |
| 743 |
|
| 744 |
$term_url = get_term_link( $term_id, $taxonomy ); |
| 745 |
|
| 746 |
if ( ! is_wp_error( $term_url ) ) { |
| 747 |
if ( $this->settings['async_cache_cleaning'] ) { |
| 748 |
$this->cache_purger->push_to_queue( |
| 749 |
[ |
| 750 |
'call' => 'delete_page_cache', |
| 751 |
'urls' => $term_url, |
| 752 |
] |
| 753 |
); |
| 754 |
$this->cache_purger->save()->dispatch(); |
| 755 |
} else { |
| 756 |
delete_page_cache( $term_url ); |
| 757 |
} |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
} |
| 762 |
|