-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathCdn_CacheFlush.php
102 lines (91 loc) · 2.64 KB
/
Cdn_CacheFlush.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* File: Cdn_CacheFlush.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class Cdn_CacheFlush
*
* CDN cache purge object
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
*/
class Cdn_CacheFlush {
/**
* Advanced cache config
*
* @var Config
*/
private $_config = null;
/**
* Array of urls to flush
*
* @var array
*/
private $flush_operation_requested = false;
/**
* Constructor for the Cdn_CacheFlush class.
*
* Initializes the configuration by fetching it from the Dispatcher. This constructor sets up the necessary
* configuration needed for the class to function correctly.
*
* @return void
*/
public function __construct() {
$this->_config = Dispatcher::config();
}
/**
* Purges all cached content.
*
* This method triggers a purge of all cached content. It sets a flag indicating that a flush operation has been
* requested, and returns true to confirm that the operation was initiated.
*
* @return bool True if the purge operation is initiated successfully.
*/
public function purge_all() {
$this->flush_operation_requested = true;
return true;
}
/**
* Purges the cache for a specific URL.
*
* This method purges the cache for the provided URL. It parses the URL, constructs the appropriate CDN path,
* and triggers the cache purge operation through the CDN service.
*
* @param string $url The URL whose cache needs to be purged.
*
* @return void
*/
public function purge_url( $url ) {
$common = Dispatcher::component( 'Cdn_Core' );
$results = array();
$files = array();
$parsed = wp_parse_url( $url );
$local_site_path = isset( $parsed['path'] ) ? ltrim( $parsed['path'], '/' ) : '';
$remote_path = $common->uri_to_cdn_uri( $local_site_path );
$files[] = $common->build_file_descriptor( $local_site_path, $remote_path );
$this->_flushed_urls[] = $url;
$common->purge( $files, $results );
}
/**
* Performs cleanup actions after a post purge operation.
*
* This method is responsible for performing any necessary cleanup after a cache purge operation. It checks
* whether a flush operation has been requested, triggers a full CDN cache purge if necessary, and resets the
* relevant flags.
*
* @return int The number of items that were processed during the cleanup.
*/
public function purge_post_cleanup() {
if ( $this->flush_operation_requested ) {
$common = Dispatcher::component( 'Cdn_Core' );
$results = array();
$common->purge_all( $results );
$count = 999;
$this->flush_operation_requested = false;
}
return $count;
}
}