-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathCdnEngine_Mirror_StackPath.php
180 lines (154 loc) · 4.66 KB
/
CdnEngine_Mirror_StackPath.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* File: CdnEngine_Mirror_StackPath.php
*
* @package W3TC
*/
namespace W3TC;
/**
* Class CdnEngine_Mirror_StackPath
*/
class CdnEngine_Mirror_StackPath extends CdnEngine_Mirror {
/**
* Constructs the CdnEngine_Mirror_StackPath class.
*
* @param array $config {
* Configuration settings for the StackPath CDN engine. Keys include.
*
* @type string $authorization_key The authorization key for API access.
* @type string $alias The account alias.
* @type string $consumerkey The consumer key for API access.
* @type string $consumersecret The consumer secret for API access.
* @type int $zone_id The zone ID for the StackPath configuration.
* }
*
* @return void
*/
public function __construct( $config = array() ) {
$config = array_merge(
array(
'authorization_key' => '',
'alias' => '',
'consumerkey' => '',
'consumersecret' => '',
'zone_id' => 0,
),
$config
);
$split_keys = explode( '+', $config['authorization_key'] );
if ( 3 === count( $split_keys ) ) {
list( $config['alias'], $config['consumerkey'], $config['consumersecret'] ) = $split_keys;
}
parent::__construct( $config );
}
/**
* Purges a specific set of files from the StackPath CDN.
*
* @param array $files Array of files to be purged. Each file should have a 'remote_path' key.
* @param array $results Reference to an array where the purge results will be stored.
*
* @return bool True if the purge operation is successful, false otherwise.
*
* @throws \Exception If there is a failure during the API call to purge files.
*/
public function purge( $files, &$results ) {
if ( empty( $this->_config['authorization_key'] ) ) {
$results = $this->_get_results( $files, W3TC_CDN_RESULT_HALT, __( 'Empty Authorization Key.', 'w3-total-cache' ) );
return false;
}
if ( empty( $this->_config['alias'] ) ||
empty( $this->_config['consumerkey'] ) ||
empty( $this->_config['consumersecret'] ) ) {
$results = $this->_get_results( $files, W3TC_CDN_RESULT_HALT, __( 'Malformed Authorization Key.', 'w3-total-cache' ) );
return false;
}
$api = new Cdn_StackPath_Api(
$this->_config['alias'],
$this->_config['consumerkey'],
$this->_config['consumersecret']
);
$results = array();
try {
$zone_id = $this->_config['zone_id'];
if ( empty( $zone_id ) ) {
$results[] = $this->_get_result(
'',
'',
W3TC_CDN_RESULT_ERROR,
__( 'No zone defined', 'w3-total-cache' )
);
return ! $this->_is_error( $results );
}
$files_to_pass = array();
foreach ( $files as $file ) {
$files_to_pass[] = '/' . $file['remote_path'];
}
$params = array( 'files' => $files_to_pass );
$api->delete_site_cache( $zone_id, $params );
$results[] = $this->_get_result(
'',
'',
W3TC_CDN_RESULT_OK,
'OK'
);
} catch ( \Exception $e ) {
$results[] = $this->_get_result(
'',
'',
W3TC_CDN_RESULT_HALT,
__( 'Failure to pull zone: ', 'w3-total-cache' ) . $e->getMessage()
);
}
return ! $this->_is_error( $results );
}
/**
* Purges all cached files from the StackPath CDN zone.
*
* @param array $results Reference to an array where the purge results will be stored.
*
* @return bool True if the purge operation is successful, false otherwise.
*
* @throws \Exception If there is a failure during the API call to purge the zone.
*/
public function purge_all( &$results ) {
if ( empty( $this->_config['authorization_key'] ) ) {
$results = $this->_get_results(
array(),
W3TC_CDN_RESULT_HALT,
__( 'Empty Authorization Key.', 'w3-total-cache' )
);
return false;
}
if ( empty( $this->_config['alias'] ) || empty( $this->_config['consumerkey'] ) || empty( $this->_config['consumersecret'] ) ) {
$results = $this->_get_results(
array(),
W3TC_CDN_RESULT_HALT,
__( 'Malformed Authorization Key.', 'w3-total-cache' )
);
return false;
}
$api = new Cdn_StackPath_Api( $this->_config['alias'], $this->_config['consumerkey'], $this->_config['consumersecret'] );
$results = array();
try {
$zone_id = $this->_config['zone_id'];
if ( empty( $zone_id ) ) {
$results[] = $this->_get_result(
'',
'',
W3TC_CDN_RESULT_ERROR,
__( 'No zone defined', 'w3-total-cache' )
);
return ! $this->_is_error( $results );
}
$file_purge = $api->delete_site_cache( $zone_id );
} catch ( \Exception $e ) {
$results[] = $this->_get_result(
'',
'',
W3TC_CDN_RESULT_HALT,
__( 'Failure to pull zone: ', 'w3-total-cache' ) . $e->getMessage()
);
}
return ! $this->_is_error( $results );
}
}