-
Notifications
You must be signed in to change notification settings - Fork 19
/
Cdn_Fsd_CacheFlush.php
executable file
·235 lines (195 loc) · 6.01 KB
/
Cdn_Fsd_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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace W3TC;
/**
* CDN cache flusher
*/
class Cdn_Fsd_CacheFlush {
/**
* Array of urls to flush
*
* @var array
*/
private $queued_urls = array();
private $flush_all_requested = false;
/**
* Purges everything from CDNs that supports it
*/
static public function w3tc_flush_all( $extras = array() ) {
if ( isset( $extras['only'] ) && $extras['only'] != 'cdn' )
return;
$o = Dispatcher::component( 'Cdn_Fsd_CacheFlush' );
$o->flush_all_requested = true;
return true;
}
/**
* Purges cdn's post cache
*
* @param integer $post_id
* @return boolean
*/
static public function w3tc_flush_post( $post_id ) {
if ( !$post_id ) {
$post_id = Util_Environment::detect_post_id();
}
if ( !$post_id )
return false;
$config = Dispatcher::config();
$full_urls = array();
$post = null;
$terms = array();
$feeds = $config->get_array( 'pgcache.purge.feed.types' );
$limit_post_pages = $config->get_integer( 'pgcache.purge.postpages_limit' );
if ( $config->get_boolean( 'pgcache.purge.terms' ) || $config->get_boolean( 'varnish.pgcache.feed.terms' ) ) {
$taxonomies = get_post_taxonomies( $post_id );
$terms = wp_get_post_terms( $post_id, $taxonomies );
}
switch ( true ) {
case $config->get_boolean( 'pgcache.purge.author' ):
case $config->get_boolean( 'pgcache.purge.archive.daily' ):
case $config->get_boolean( 'pgcache.purge.archive.monthly' ):
case $config->get_boolean( 'pgcache.purge.archive.yearly' ):
case $config->get_boolean( 'pgcache.purge.feed.author' ):
$post = get_post( $post_id );
}
$front_page = get_option( 'show_on_front' );
/**
* Home (Frontpage) URL
*/
if ( ( $config->get_boolean( 'pgcache.purge.home' ) && $front_page == 'posts' )||
$config->get_boolean( 'pgcache.purge.front_page' ) ) {
$full_urls = array_merge( $full_urls,
Util_PageUrls::get_frontpage_urls( $limit_post_pages ) );
}
/**
* Home (Post page) URL
*/
if ( $config->get_boolean( 'pgcache.purge.home' ) && $front_page != 'posts' ) {
$full_urls = array_merge( $full_urls,
Util_PageUrls::get_postpage_urls( $limit_post_pages ) );
}
/**
* Post URL
*/
if ( $config->get_boolean( 'pgcache.purge.post' ) ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_post_urls( $post_id ) );
}
/**
* Post comments URLs
*/
if ( $config->get_boolean( 'pgcache.purge.comments' ) && function_exists( 'get_comments_pagenum_link' ) ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_post_comments_urls( $post_id ) );
}
/**
* Post author URLs
*/
if ( $config->get_boolean( 'pgcache.purge.author' ) && $post ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_post_author_urls( $post->post_author, $limit_post_pages ) );
}
/**
* Post terms URLs
*/
if ( $config->get_boolean( 'pgcache.purge.terms' ) ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_post_terms_urls( $terms, $limit_post_pages ) );
}
/**
* Daily archive URLs
*/
if ( $config->get_boolean( 'pgcache.purge.archive.daily' ) && $post ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_daily_archive_urls( $post, $limit_post_pages ) );
}
/**
* Monthly archive URLs
*/
if ( $config->get_boolean( 'pgcache.purge.archive.monthly' ) && $post ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_monthly_archive_urls( $post, $limit_post_pages ) );
}
/**
* Yearly archive URLs
*/
if ( $config->get_boolean( 'pgcache.purge.archive.yearly' ) && $post ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_yearly_archive_urls( $post, $limit_post_pages ) );
}
/**
* Feed URLs
*/
if ( $config->get_boolean( 'pgcache.purge.feed.blog' ) ) {
$full_urls = array_merge( $full_urls,
Util_PageUrls::get_feed_urls( $feeds ) );
}
if ( $config->get_boolean( 'pgcache.purge.feed.comments' ) ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_feed_comments_urls( $post_id, $feeds ) );
}
if ( $config->get_boolean( 'pgcache.purge.feed.author' ) && $post ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_feed_author_urls( $post->post_author, $feeds ) );
}
if ( $config->get_boolean( 'pgcache.purge.feed.terms' ) ) {
$full_urls = array_merge( $full_urls, Util_PageUrls::get_feed_terms_urls( $terms, $feeds ) );
}
/**
* Purge selected pages
*/
if ( $config->get_array( 'pgcache.purge.pages' ) ) {
$pages = $config->get_array( 'pgcache.purge.pages' );
$full_urls = array_merge( $full_urls,
Util_PageUrls::get_pages_urls( $pages ) );
}
/**
* Queue flush
*/
if ( count( $full_urls ) ) {
$o = Dispatcher::component( 'Cdn_Fsd_CacheFlush' );
foreach ( $full_urls as $url )
$o->queued_urls[$url] = '*';
}
return true;
}
/**
* Purge a single url
*
* @param unknown $url
*/
static public function w3tc_flush_url( $url ) {
$o = Dispatcher::component( 'Cdn_Fsd_CacheFlush' );
$o->queued_urls[$url] = '*';
return true;
}
/**
* Clears global and repeated urls
*/
static public function w3tc_flush_execute_delayed_operations( $actions_made ) {
$o = Dispatcher::component( 'Cdn_Fsd_CacheFlush' );
if ( $o->flush_all_requested ) {
$core = Dispatcher::component( 'Cdn_Fsd_Core' );
$engine = $core->get_engine();
try {
$engine->flush_all();
$actions_made[] = array( 'module' => 'cdn' );
} catch ( \Exception $ex ) {
$actions_made[] = array(
'module' => 'cdn',
'error' => $ex->getMessage()
);
}
$o->flush_all_requested = false;
$o->queued_urls = array();
} else {
$count = count( $o->queued_urls );
if ( $count > 0 ) {
$urls = array_keys( $o->queued_urls );
$core = Dispatcher::component( 'Cdn_Fsd_Core' );
$engine = $core->get_engine();
try {
$engine->flush_urls( $urls );
$actions_made[] = array( 'module' => 'cdn' );
} catch ( \Exception $ex ) {
$actions_made[] = array(
'module' => 'cdn',
'error' => $ex->getMessage()
);
}
$o->queued_urls = array();
}
}
return $actions_made;
}
}