Skip to content

Commit c3bb78e

Browse files
committed
enh: ensure single url cache is bust when a single path is invalidated
1 parent ae1492e commit c3bb78e

3 files changed

Lines changed: 59 additions & 8 deletions

File tree

inc/compatibilities/aruba_hsc.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,29 @@ public function should_load_early() {
4040
/**
4141
* Clear cache for Aruba Hispeed Cache.
4242
*
43+
* @param string|bool $location The location to clear the cache for. If true, clear the cache globally. If a string, clear the cache for a particular url.
4344
* @return void
4445
*/
45-
public function add_clear_cache_action() {
46+
public function add_clear_cache_action( $location ) {
4647
if ( ! class_exists( '\ArubaSPA\HiSpeedCache\Purger\WpPurger' ) || ! defined( 'AHSC_PURGER' ) ) {
4748
return;
4849
}
4950

51+
// Initialize the purger.
5052
$purge = new \ArubaSPA\HiSpeedCache\Purger\WpPurger();
5153
$purge->setPurger( AHSC_PURGER );
52-
$purge->purgeAll();
54+
55+
// Purge all cache when no location is provided.
56+
if ( $location === true && method_exists( $purge, 'purgeAll' ) ) {
57+
$purge->purgeAll();
58+
return;
59+
}
60+
61+
// Purge single URL based on the location parameter.
62+
if ( ! method_exists( $purge, 'purgeUrl' ) ) {
63+
return;
64+
}
65+
66+
$purge->purgeUrl( $location );
5367
}
5468
}

inc/compatibilities/cache_enabler.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,29 @@ public function register() {
2626

2727
add_filter( 'cache_enabler_page_contents_before_store', [ Optml_Main::instance()->manager, 'replace_content' ], PHP_INT_MAX, 1 );
2828

29-
add_action( 'optml_settings_updated', [ $this, 'add_clear_cache_action' ] );
29+
add_action(
30+
'optml_settings_updated',
31+
function () {
32+
do_action( 'cache_enabler_clear_site_cache' );
33+
}
34+
);
35+
3036
add_action( 'optml_clear_cache', [ $this, 'add_clear_cache_action' ] );
3137
}
3238

3339
/**
34-
* Clear cache for Cache Enabler.
40+
* Clear cache for Super Page Cache for Cloudflare.
3541
*
42+
* @param string|bool $location The location to clear the cache for. If true, clear the cache globally. If a string, clear the cache for a particular url.
3643
* @return void
3744
*/
38-
public function add_clear_cache_action() {
39-
do_action( 'cache_enabler_clear_site_cache' );
45+
public function add_clear_cache_action( $location ) {
46+
if ( $location === true ) {
47+
do_action( 'cache_enabler_clear_site_cache' );
48+
return;
49+
}
50+
51+
do_action( 'cache_enabler_clear_page_cache_by_url', $location );
4052
}
4153

4254
/**

inc/compatibilities/hummingbird.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,34 @@ public function should_load_early() {
4040
/**
4141
* Clear cache for Hummingbird.
4242
*
43+
* @param string|bool $location The location to clear the cache for. If true, clear the cache globally. If a string, clear the cache for a particular url.
4344
* @return void
4445
*/
45-
public function add_clear_cache_action() {
46-
do_action( 'wphb_clear_page_cache' );
46+
public function add_clear_cache_action( $location ) {
47+
if ( ! class_exists( '\Hummingbird\Core\Utils' ) || ! method_exists( '\Hummingbird\Core\Utils', 'get_module' ) ) {
48+
return;
49+
}
50+
51+
$page_cache = \Hummingbird\Core\Utils::get_module( 'page_cache' );
52+
53+
if ( ! $page_cache ) {
54+
return;
55+
}
56+
57+
// Clear all cache
58+
if ( true === $location ) {
59+
$page_cache->clear_cache();
60+
return;
61+
}
62+
63+
// Clear specific URL
64+
if ( ! is_string( $location ) || empty( $location ) ) {
65+
return;
66+
}
67+
68+
$url_path = wp_parse_url( $location, PHP_URL_PATH );
69+
if ( $url_path ) {
70+
$page_cache->clear_cache( trailingslashit( $url_path ), true );
71+
}
4772
}
4873
}

0 commit comments

Comments
 (0)