forked from Automattic/vip-go-mu-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache-manager.php
257 lines (203 loc) · 7.77 KB
/
cache-manager.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/*
Plugin name: Cache Manager
Description: Automatically clears the Varnish cache when necessary
Author: Automattic
Author URI: http://automattic.com/
Version: 1.0
License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
class WPCOM_VIP_Cache_Manager {
private $ban_urls = array();
private $purge_urls = array();
private $site_cache_purged = false;
function __construct() {
// Execute the healthcheck as quickly as possible
if ( '/cache-healthcheck?' === $_SERVER['REQUEST_URI'] )
die( 'ok' );
add_action( 'init', array( $this, 'init' ) );
}
function init() {
if ( is_super_admin() && isset( $_GET['cm_purge_all'] ) && check_admin_referer( 'manual_purge' ) ) {
$this->purge_site_cache();
add_action( 'admin_notices' , array( $this, 'manual_purge_message' ) );
}
add_action( 'clean_post_cache', array( $this, 'queue_post_purge' ) );
add_action( 'switch_theme', array( $this, 'purge_site_cache' ) );
add_action( 'activity_box_end', array( $this, 'get_manual_purge_link' ), 100 );
add_action( 'shutdown', array( $this, 'execute_purges' ) );
}
function get_manual_purge_link() {
global $blog_id;
$url = wp_nonce_url( admin_url( '?cm_purge_all' ), 'manual_purge' );
$button = esc_html__( 'Press the button below to force a purge of your entire page cache.' );
$button .= '</p><p><span class="button"><a href="' . $url . '"><strong>';
$button .= esc_html__( 'Purge Page Cache' );
$button .= '</strong></a></span>';
$nobutton = esc_html__( 'You do not have permission to purge the cache for the whole site. Please contact your adminstrator.' );
if ( is_super_admin() ) {
echo "<p>$button</p>\n";
} else {
echo "<p>$nobutton</p>\n";
}
}
function manual_purge_message() {
echo "<div id='message' class='updated fade'><p><strong>".__('Varnish cache purged!', 'varnish-http-purge')."</strong></p></div>";
}
function curl_multi( $requests ) {
$curl_multi = curl_multi_init();
foreach ( $requests as $req ) {
// Purge HTTP
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, "http://{$req['ip']}{$req['uri']}" );
curl_setopt( $curl, CURLOPT_PORT, $req['port'] );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Host: {$req['host']}", "X-Forwarded-Proto: http" ) );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $req['method'] );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
curl_multi_add_handle( $curl_multi, $curl );
// Purge HTTPS
$curl = curl_init();
curl_setopt( $curl, CURLOPT_URL, "http://{$req['ip']}{$req['uri']}" );
curl_setopt( $curl, CURLOPT_PORT, $req['port'] );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Host: {$req['host']}", "X-Forwarded-Proto: https" ) );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $req['method'] );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_NOBODY, true );
curl_setopt( $curl, CURLOPT_HEADER, true );
curl_setopt( $curl, CURLOPT_TIMEOUT, 5 );
curl_multi_add_handle( $curl_multi, $curl );
}
$running = true;
while ( $running ) {
do {
$result = curl_multi_exec( $curl_multi, $running );
} while ( $result == CURLM_CALL_MULTI_PERFORM );
if ( $result != CURLM_OK )
error_log( 'curl_multi_exec() returned something different than CURLM_OK' );
curl_multi_select( $curl_multi, 0.2 );
}
while ( $completed = curl_multi_info_read( $curl_multi ) ) {
$info = curl_getinfo( $completed['handle'] );
if ( ! $info['http_code'] && curl_error( $completed['handle'] ) )
error_log( 'Error on: ' . $info['url'] . ' error: ' . curl_error( $completed['handle'] ) . "\n" );
if ( '200' != $info['http_code'] )
error_log( 'Request to ' . $info['url'] . ' returned HTTP code ' . $info['http_code'] . "\n" );
curl_multi_remove_handle( $curl_multi, $completed['handle'] );
}
curl_multi_close( $curl_multi );
}
function build_purge_request( $url, $method ) {
global $varnish_servers;
$requests = array();
if ( empty( $varnish_servers ) )
return $requests;
$parsed = parse_url( $url );
if ( empty( $parsed['host'] ) )
return $requests;
foreach ( $varnish_servers as $server ) {
$server = explode( ':', $server[0] );
$uri = '/';
if ( isset( $parsed['path'] ) )
$uri = $parsed['path'];
if ( isset( $parsed['query'] ) )
$uri .= $parsed['query'];
$requests[] = array(
'ip' => $server[0],
'port' => $server[1],
'host' => $parsed['host'],
'uri' => $uri,
'method' => $method
);
}
return $requests;
}
function execute_purges() {
$this->ban_urls = array_unique( $this->ban_urls );
$this->purge_urls = array_unique( $this->purge_urls );
if ( empty( $this->ban_urls ) && empty( $this->purge_urls ) )
return;
$requests = array();
foreach( (array) $this->ban_urls as $url )
$requests = array_merge( $requests, $this->build_purge_request( $url, 'BAN' ) );
foreach( (array) $this->purge_urls as $url )
$requests = array_merge( $requests, $this->build_purge_request( $url, 'PURGE' ) );
$this->ban_urls = $this->purge_urls = array();
if ( empty( $requests ) )
return;
return $this->curl_multi( $requests );
}
function purge_site_cache( $when = null ) {
if ( $this->site_cache_purged )
return;
$this->ban_urls[] = untrailingslashit( home_url() ) . '/.*';
$this->site_cache_purged = true;
return;
}
function queue_post_purge( $post_id ) {
if ( $this->site_cache_purged )
return;
if ( defined( 'WP_IMPORTING' ) ) {
$this->purge_site_cache();
return;
}
$post = get_post( $post_id );
if ( empty( $post ) ||
'revision' === $post->post_type ||
! in_array( get_post_status( $post_id ), array( 'publish', 'trash' ), true ) )
{
return;
}
$this->purge_urls[] = get_permalink( $post_id );
$this->purge_urls[] = trailingslashit( home_url() );
$categories = get_the_category( $post_id );
if ( $categories ) {
$category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : '/category';
$category_base = trailingslashit( $category_base );
foreach ( $categories as $cat )
$this->purge_urls[] = home_url( $category_base . $cat->slug . '/' );
}
$tags = get_the_tags( $post_id );
if ( $tags ) {
$tag_base = get_option( 'tag_base' ) ? get_option( 'tag_base' ) : '/tag';
$tag_base = trailingslashit( str_replace( '..', '', $tag_base ) );
foreach ( $tags as $tag )
$this->purge_urls[] = home_url( $tag_base . $tag->slug . '/' );
}
$feeds = array(
get_bloginfo('rdf_url'),
get_bloginfo('rss_url') ,
get_bloginfo('rss2_url'),
get_bloginfo('atom_url'),
get_bloginfo('comments_atom_url'),
get_bloginfo('comments_rss2_url'),
get_post_comments_feed_link( $post_id )
);
foreach ( $feeds as $feed )
$this->purge_urls[] = $feed;
/**
* Allows adding URLs to be PURGEd from cache when a given post ID is PURGEd
*
* Developers can hook this filter and check the post being purged in order
* to also purge related URLs, e.g. feeds.
*
* Related category archives, tag archives, generic feeds, etc, are already
* included to be purged (see code above).
*
* PLEASE NOTE: Your site benefits from the performance that our HTTP
* Reverse Proxy Caching provides, and purging URLs from that cache
* should be done cautiously. VIP may push back on use of this filter
* during initial code review and pre-deployment review where we
* see issues.
*
* @param array $this->purge_urls {
* An array of URLs for you to add to
* }
* @param type $post_id The ID of the post which is the primary reason for the purge
*/
$this->purge_urls = apply_filters( 'wpcom_vip_cache_purge_urls', $this->purge_urls, $post_id );
}
}
new WPCOM_VIP_Cache_Manager();