| @@ -6,13 +6,15 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace PoweredCache; |
| 9 | 9 | |
| 10 | +use PoweredCache\Async\CachePurger; | |
| 10 | 11 | use const PoweredCache\Constants\POST_META_DISABLE_CACHE_KEY; |
| 11 | 12 | use function PoweredCache\Utils\clean_page_cache_dir; |
| 12 | 13 | use function PoweredCache\Utils\clean_site_cache_dir; |
| 13 | 14 | use function PoweredCache\Utils\delete_page_cache; |
| 14 | 15 | use function PoweredCache\Utils\get_post_related_urls; |
| 16 | +use const PoweredCache\Constants\PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT; | |
| 15 | 17 | |
| 16 | 18 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | 19 | exit; // Exit if accessed directly. |
| 18 | 20 | } |
| @@ -24,8 +26,23 @@ | ||
| 24 | 26 | */ |
| 25 | 27 | class AdvancedCache { |
| 26 | 28 | |
| 27 | 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 | + /** | |
| 28 | 45 | * Return an instance of the current class |
| 29 | 46 | * |
| 30 | 47 | * @return AdvancedCache |
| 31 | 48 | * @since 1.0 |
| @@ -47,26 +64,31 @@ | ||
| 47 | 64 | * |
| 48 | 65 | * @since 1.0 |
| 49 | 66 | */ |
| 50 | 67 | public function setup() { |
| 51 | - $settings = \PoweredCache\Utils\get_settings(); | |
| 68 | + $this->settings = \PoweredCache\Utils\get_settings(); | |
| 52 | 69 | |
| 53 | - if ( ! $settings['enable_page_cache'] ) { | |
| 70 | + if ( ! $this->settings['enable_page_cache'] ) { | |
| 54 | 71 | return; |
| 55 | 72 | } |
| 56 | 73 | |
| 74 | + $this->cache_purger = CachePurger::factory(); | |
| 75 | + | |
| 57 | 76 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ) ); |
| 58 | 77 | add_action( 'admin_post_powered_cache_purge_page_cache', array( $this, 'purge_page_cache' ) ); |
| 59 | 78 | add_action( 'admin_post_powered_cache_purge_page_cache_network', array( $this, 'purge_page_cache_network_wide' ) ); |
| 60 | - add_action( 'pre_post_update', array( $this, 'purge_on_post_update' ), 10, 1 ); | |
| 61 | - add_action( 'save_post', array( $this, 'purge_on_post_update' ), 10, 1 ); | |
| 62 | - add_action( 'wp_trash_post', array( $this, 'purge_on_post_update' ), 10, 1 ); | |
| 79 | + add_action( 'transition_post_status', array( $this, 'purge_on_post_update' ), 9999, 3 ); | |
| 63 | 80 | add_action( 'switch_theme', array( $this, 'purge_on_switch_theme' ) ); |
| 64 | - add_action( 'wp_set_comment_status', array( $this, 'purge_post_on_comment_status_change' ), 10, 2 ); | |
| 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' ) ); | |
| 65 | 83 | add_action( 'set_comment_cookies', array( $this, 'set_comment_cookie' ), 10, 2 ); |
| 66 | 84 | add_filter( 'powered_cache_post_related_urls', array( $this, 'powered_cache_post_related_urls' ) ); |
| 67 | 85 | add_filter( 'powered_cache_page_cache_enable', array( $this, 'maybe_caching_disabled' ) ); |
| 68 | 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 ); | |
| 69 | 91 | } |
| 70 | 92 | |
| 71 | 93 | /** |
| 72 | 94 | * Add purge button on admin bar |
| @@ -95,8 +117,19 @@ | ||
| 95 | 117 | 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_page_cache' ), 'powered_cache_purge_page_cache' ), |
| 96 | 118 | 'parent' => 'powered-cache', |
| 97 | 119 | ) |
| 98 | 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 | + } | |
| 99 | 132 | } |
| 100 | 133 | } |
| 101 | 134 | |
| 102 | 135 | |
| @@ -105,20 +138,28 @@ | ||
| 105 | 138 | * |
| 106 | 139 | * @since 2.0 |
| 107 | 140 | */ |
| 108 | 141 | public function purge_page_cache_network_wide() { |
| 109 | - if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_purge_page_cache_network' ) ) { | |
| 142 | + $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ) : ''; | |
| 143 | + if ( ! wp_verify_nonce( $nonce, 'powered_cache_purge_page_cache_network' ) ) { | |
| 110 | 144 | wp_nonce_ays( '' ); |
| 111 | 145 | } |
| 112 | 146 | |
| 113 | 147 | if ( current_user_can( 'manage_network' ) ) { |
| 114 | - clean_page_cache_dir(); | |
| 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 | + } | |
| 115 | 154 | $redirect_url = add_query_arg( 'pc_action', 'flush_page_cache_network', wp_get_referer() ); |
| 116 | 155 | } else { |
| 117 | 156 | $redirect_url = add_query_arg( 'pc_action', 'flush_page_cache_network_err_permission', wp_get_referer() ); |
| 118 | 157 | } |
| 119 | 158 | |
| 120 | - wp_safe_redirect( $redirect_url ); | |
| 159 | + delete_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); | |
| 160 | + | |
| 161 | + wp_safe_redirect( esc_url_raw( $redirect_url ) ); | |
| 121 | 162 | exit; |
| 122 | 163 | } |
| 123 | 164 | |
| 124 | 165 | /** |
| @@ -127,54 +168,107 @@ | ||
| 127 | 168 | * @since 1.0 |
| 128 | 169 | * @since 1.1 clean site dir instead of root page caching dir |
| 129 | 170 | */ |
| 130 | 171 | public function purge_page_cache() { |
| 131 | - if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_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 | |
| 132 | 173 | wp_nonce_ays( '' ); |
| 133 | 174 | } |
| 134 | 175 | |
| 135 | 176 | if ( current_user_can( 'manage_options' ) ) { |
| 136 | - clean_site_cache_dir(); | |
| 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 | + | |
| 137 | 186 | $redirect_url = add_query_arg( 'pc_action', 'flush_page_cache', wp_get_referer() ); |
| 138 | 187 | } else { |
| 139 | 188 | $redirect_url = add_query_arg( 'pc_action', 'flush_page_cache_err_permission', wp_get_referer() ); |
| 140 | 189 | } |
| 141 | 190 | |
| 142 | - wp_safe_redirect( $redirect_url ); | |
| 191 | + delete_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ); | |
| 192 | + | |
| 193 | + wp_safe_redirect( esc_url_raw( $redirect_url ) ); | |
| 143 | 194 | exit; |
| 144 | 195 | } |
| 145 | 196 | |
| 146 | 197 | |
| 147 | 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 | + /** | |
| 148 | 216 | * Purge post related page cache |
| 149 | 217 | * |
| 150 | 218 | * @param int $post_id Post ID |
| 151 | 219 | * |
| 152 | - * @since 1.0 | |
| 220 | + * @since 3.4 | |
| 153 | 221 | */ |
| 154 | - public function purge_on_post_update( $post_id ) { | |
| 155 | - $current_post_status = get_post_status( $post_id ); | |
| 222 | + public function purge_post( $post_id ) { | |
| 223 | + $current_post = get_post( $post_id ); | |
| 156 | 224 | |
| 157 | - $urls = array(); | |
| 225 | + if ( ! is_a( $current_post, 'WP_Post' ) ) { | |
| 226 | + return; | |
| 227 | + } | |
| 158 | 228 | |
| 159 | - if ( in_array( $current_post_status, array( 'publish', 'trash' ), true ) ) { | |
| 160 | - $urls = get_post_related_urls( $post_id ); | |
| 229 | + if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) { | |
| 230 | + return; | |
| 231 | + } | |
| 161 | 232 | |
| 162 | - /** | |
| 163 | - * Page cache purge urls. | |
| 164 | - * | |
| 165 | - * @hook powered_cache_advanced_cache_purge_urls | |
| 166 | - * | |
| 167 | - * @param {array} $urls The list of URLs that will purged | |
| 168 | - * @param {int} $post_id Post ID. | |
| 169 | - * | |
| 170 | - * @return {array} New value | |
| 171 | - * @since 1.0 | |
| 172 | - */ | |
| 173 | - $urls = apply_filters( 'powered_cache_advanced_cache_purge_urls', $urls, $post_id ); | |
| 233 | + if ( ! is_post_type_viewable( $current_post->post_type ) ) { | |
| 234 | + return; | |
| 235 | + } | |
| 174 | 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 { | |
| 175 | 267 | foreach ( $urls as $url ) { |
| 176 | - delete_page_cache( $url ); | |
| 268 | + if ( delete_page_cache( $url ) ) { | |
| 269 | + $deleted_urls[] = $url; | |
| 270 | + } | |
| 177 | 271 | } |
| 178 | 272 | } |
| 179 | 273 | |
| 180 | 274 | /** |
| @@ -182,39 +276,61 @@ | ||
| 182 | 276 | * |
| 183 | 277 | * @hook powered_cache_advanced_cache_purge_post |
| 184 | 278 | * |
| 185 | 279 | * @param {int} $post_id The Post ID. |
| 186 | - * @param {array} $urls The list of related urls with the updated post. | |
| 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 | |
| 187 | 282 | * |
| 188 | 283 | * @since 1.0 |
| 189 | 284 | */ |
| 190 | - do_action( 'powered_cache_advanced_cache_purge_post', $post_id, $urls ); | |
| 285 | + do_action( 'powered_cache_advanced_cache_purge_post', $post_id, $deleted_urls, $urls ); | |
| 191 | 286 | } |
| 192 | 287 | |
| 193 | 288 | /** |
| 194 | - * Delete page cache when post update | |
| 289 | + * Purge post cache when comment status change | |
| 195 | 290 | * |
| 196 | 291 | * @param int $comment_id Comment ID |
| 197 | - * @param string $comment_status Status of the comment | |
| 292 | + * @param string $comment_status Comment status | |
| 198 | 293 | * |
| 199 | - * @since 1.0 | |
| 294 | + * @return void | |
| 295 | + * @deprecated since 3.4.4 | |
| 200 | 296 | */ |
| 201 | 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 ); | |
| 202 | 301 | $comment = get_comment( $comment_id ); |
| 203 | 302 | $post_id = $comment->comment_post_ID; |
| 204 | 303 | $post_url = get_permalink( $post_id ); |
| 205 | - delete_page_cache( $post_url ); | |
| 206 | 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 | + | |
| 207 | 321 | /** |
| 208 | 322 | * Fires after purging cache for a post that associated a comment |
| 209 | 323 | * |
| 324 | + * @hook powered_cache_advanced_cache_purge_on_comment_update | |
| 325 | + * | |
| 210 | 326 | * @param {int} $post_id Post ID. |
| 211 | 327 | * @param {string} $post_url Post permalink. |
| 212 | 328 | * @param {int} $comment_id Comment ID. |
| 213 | 329 | * |
| 214 | - * @since 2.0 | |
| 330 | + * @since 3.4.4 | |
| 215 | 331 | */ |
| 216 | - do_action( 'powered_cache_advanced_cache_purge_on_comment_status_change', $post_id, $post_url, $comment_id ); | |
| 332 | + do_action( 'powered_cache_advanced_cache_purge_on_comment_update', $post_id, $post_url, $comment_id ); | |
| 217 | 333 | } |
| 218 | 334 | |
| 219 | 335 | /** |
| 220 | 336 | * Purge site when switching to a new theme |
| @@ -221,12 +337,37 @@ | ||
| 221 | 337 | * |
| 222 | 338 | * @since 2.0 |
| 223 | 339 | */ |
| 224 | 340 | public function purge_on_switch_theme() { |
| 225 | - clean_site_cache_dir(); | |
| 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 | + } | |
| 226 | 347 | } |
| 227 | 348 | |
| 228 | 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 | + /** | |
| 229 | 370 | * Leave a cookie when commenting on a post as usual and don't show the cached page. |
| 230 | 371 | * `comment_author_*` cookies are site-wide available. |
| 231 | 372 | * Don't show cached results just for left a comment 5 months ago is not seems efficient here. |
| 232 | 373 | * |
| @@ -287,9 +428,19 @@ | ||
| 287 | 428 | if ( ! empty( $settings['rejected_cookies'] ) ) { |
| 288 | 429 | $cookies = preg_split( '#(\r\n|\r|\n)#', $settings['rejected_cookies'], - 1, PREG_SPLIT_NO_EMPTY ); |
| 289 | 430 | } |
| 290 | 431 | |
| 291 | - $wp_cookies = [ 'wp-postpass', 'wordpressuser_', 'wordpresspass_', 'wordpress_sec_', 'wordpress_logged_in_', 'powered_cache_commented_posts' ]; | |
| 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 | + ]; | |
| 292 | 443 | |
| 293 | 444 | if ( ! empty( $cookies ) ) { |
| 294 | 445 | $wp_cookies = array_merge( $cookies, $wp_cookies ); |
| 295 | 446 | } |
| @@ -333,9 +484,35 @@ | ||
| 333 | 484 | */ |
| 334 | 485 | return apply_filters( 'powered_cache_vary_cookies', $cookies ); |
| 335 | 486 | } |
| 336 | 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 = []; | |
| 337 | 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 | + | |
| 338 | 515 | /** |
| 339 | 516 | * Get the list of rejected user agents |
| 340 | 517 | * |
| 341 | 518 | * @return mixed|void |
| @@ -364,9 +541,9 @@ | ||
| 364 | 541 | return apply_filters( 'powered_cache_rejected_user_agents', $rejected_user_agents ); |
| 365 | 542 | } |
| 366 | 543 | |
| 367 | 544 | /** |
| 368 | - * Get the list of rejected uri that nerver get cached | |
| 545 | + * Get the list of rejected uri that never get cached | |
| 369 | 546 | * |
| 370 | 547 | * @return mixed|void |
| 371 | 548 | * @since 2.0 |
| 372 | 549 | */ |
| @@ -382,9 +559,9 @@ | ||
| 382 | 559 | * Filter rejected uri list |
| 383 | 560 | * |
| 384 | 561 | * @hook powered_cache_rejected_uri_list |
| 385 | 562 | * |
| 386 | - * @param {array} $rejected_uri_list The list of rejected uri that nerver get cached. | |
| 563 | + * @param {array} $rejected_uri_list The list of rejected uri that never get cached. | |
| 387 | 564 | * |
| 388 | 565 | * @return {array} New value |
| 389 | 566 | * @since 2.0 |
| 390 | 567 | */ |
| @@ -392,18 +569,67 @@ | ||
| 392 | 569 | } |
| 393 | 570 | |
| 394 | 571 | |
| 395 | 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 | + /** | |
| 396 | 608 | * These query strings will be ignored during the caching |
| 397 | 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 | + * | |
| 398 | 623 | * @return mixed|void |
| 624 | + * @since 3.0 | |
| 399 | 625 | */ |
| 400 | - public static function get_accepted_query_strings() { | |
| 401 | - $settings = \PoweredCache\Utils\get_settings(); | |
| 402 | - $accepted_query_strings = []; | |
| 626 | + public static function get_ignored_query_strings() { | |
| 627 | + $settings = \PoweredCache\Utils\get_settings(); | |
| 628 | + $ignored_query_strings = []; | |
| 403 | 629 | |
| 404 | - if ( ! empty( $settings['accepted_query_strings'] ) ) { | |
| 405 | - $accepted_query_strings = preg_split( '#(\r\n|\r|\n)#', $settings['accepted_query_strings'], - 1, PREG_SPLIT_NO_EMPTY ); | |
| 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 ); | |
| 406 | 632 | } |
| 407 | 633 | |
| 408 | 634 | $query_strings = [ |
| 409 | 635 | 'fbclid', |
| @@ -427,26 +653,40 @@ | ||
| 427 | 653 | 'age-verified', |
| 428 | 654 | 'usqp', |
| 429 | 655 | 'cn-reloaded', |
| 430 | 656 | 'ao_noptimize', |
| 431 | - 'lang', | |
| 432 | 657 | ]; |
| 433 | 658 | |
| 434 | - if ( ! empty( $accepted_query_strings ) ) { | |
| 435 | - $query_strings = array_merge( $query_strings, $accepted_query_strings ); | |
| 659 | + if ( ! empty( $ignored_query_strings ) ) { | |
| 660 | + $query_strings = array_merge( $query_strings, $ignored_query_strings ); | |
| 436 | 661 | } |
| 437 | 662 | |
| 438 | 663 | /** |
| 439 | - * Filter accepted query strings. | |
| 664 | + * Filters ignored query strings. | |
| 440 | 665 | * |
| 441 | - * @hook powered_cache_accepted_query_strings | |
| 666 | + * @hook powered_cache_accepted_query_strings | |
| 442 | 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 | + * | |
| 443 | 681 | * @param {array} $query_strings The list of query strings will be ignored during the caching |
| 444 | 682 | * |
| 445 | 683 | * @return {array} New value |
| 446 | - * @since 2.0 | |
| 684 | + * @since 3.0 | |
| 447 | 685 | */ |
| 448 | - return apply_filters( 'powered_cache_accepted_query_strings', $query_strings ); | |
| 686 | + $query_strings = apply_filters( 'powered_cache_ignored_query_strings', $query_strings ); | |
| 687 | + | |
| 688 | + return $query_strings; | |
| 449 | 689 | } |
| 450 | 690 | |
| 451 | 691 | |
| 452 | 692 | /** |
| @@ -483,6 +723,39 @@ | ||
| 483 | 723 | |
| 484 | 724 | return $rewrite_status; |
| 485 | 725 | } |
| 486 | 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 | + | |
| 487 | 761 | } |
| 488 | - | |