Varnish HTTP Purge - Version 4.4.0

Version Description

  • March 2018
  • Improve translations
  • Massive improvements to the debugging tools
  • Allow for port to be added to host headers (props @davidbarratt)
  • WP-CLI for debugging

=

Download this release

Release Info

Developer Ipstenu
Plugin Icon 128x128 Varnish HTTP Purge
Version 4.4.0
Comparing to
See all releases

Code changes from version 4.3.1 to 4.4.0

Files changed (8) hide show
  1. changelog.txt +24 -0
  2. debug.php +518 -0
  3. readme.txt +21 -34
  4. status.php +334 -0
  5. uninstall.php +1 -1
  6. varnish-http-purge.php +33 -16
  7. varnish-status.php +0 -515
  8. wp-cli.php +145 -68
changelog.txt CHANGED
@@ -1,3 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  = 4.1.1 =
2
 
3
  * JSON - fixed overzealous json assumptions from causing errors
1
+ = 4.4.0 =
2
+ * March 2018
3
+ * Improve translations
4
+ * Massive improvements to the debugging tools
5
+ * Allow for port to be added to host headers (props @davidbarratt)
6
+ * WP-CLI for debugging
7
+
8
+ = 4.3.1 =
9
+ * 10 October 2017
10
+ * Copied a wrong line.
11
+
12
+ = 4.3.0 =
13
+ * 10 October 2017
14
+ * Add Varnish Flush for "this" page on front end
15
+ * Do not flush non-public taxonomies
16
+
17
+ = 4.2.0 =
18
+ * 30 August 2017
19
+ * More flexible support for custom cat/tag bases
20
+ * Added in support for custom taxonomies
21
+ * New function to generate the URLs, so it can be called by external plugins
22
+ * Move right now box to be called later, preventing double calls
23
+ * Extra check for if it's a URL, because some plugins are weird (props @danielkun)
24
+
25
  = 4.1.1 =
26
 
27
  * JSON - fixed overzealous json assumptions from causing errors
debug.php ADDED
@@ -0,0 +1,518 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ Copyright 2016-2018 Mika Epstein (email: ipstenu@halfelf.org)
4
+
5
+ This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
+
7
+ Varnish HTTP Purge is free software: you can redistribute it and/or modify
8
+ it under the terms of the Apache License 2.0 license.
9
+
10
+ Varnish HTTP Purge is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ */
14
+
15
+ if ( !defined( 'ABSPATH' ) ) die();
16
+
17
+ /**
18
+ * Varnish Debug
19
+ *
20
+ * @since 4.4
21
+ */
22
+
23
+ class VarnishDebug {
24
+
25
+ /**
26
+ * Remote Get Varnish URL
27
+ *
28
+ * @since 4.4.0
29
+ */
30
+ static function remote_get( $url = '' ) {
31
+
32
+ // Make sure it's not a stupid URL
33
+ $url = esc_url( $url );
34
+
35
+ $args = array(
36
+ 'headers' => array(
37
+ 'timeout' => 30,
38
+ 'redirection' => 10,
39
+ )
40
+ );
41
+
42
+ $response = wp_remote_get( $url, $args );
43
+
44
+ return $response;
45
+ }
46
+
47
+ /**
48
+ * Basic checks that should stop a scan
49
+ *
50
+ * @since 4.4.0
51
+ */
52
+ static function preflight( $response ) {
53
+
54
+ // Defaults
55
+ $preflight = true;
56
+ $message = __( 'Success', 'varnish-http-purge' );
57
+
58
+ if ( is_wp_error( $response ) ) {
59
+ $preflight = false;
60
+ $message = __( 'This request cannot be performed: ', 'varnish-http-purge' );
61
+ $message .= $response->get_error_message();
62
+ } elseif ( wp_remote_retrieve_response_code( $response ) == '404' ) {
63
+ $preflight = false;
64
+ $message = __( 'This URL is a 404. Please check your typing and try again.', 'varnish-http-purge' );
65
+ }
66
+
67
+ $return = array(
68
+ 'preflight' => $preflight,
69
+ 'message' => $message,
70
+ );
71
+
72
+ return $return;
73
+ }
74
+
75
+ /**
76
+ * Check for remote IP
77
+ *
78
+ * @since 4.4.0
79
+ */
80
+ static function remote_ip( $headers ) {
81
+
82
+ if ( isset( $headers['X-Forwarded-For'] ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
83
+ $remote_ip = $headers['X-Forwarded-For'];
84
+ } elseif ( isset( $headers['HTTP_X_FORWARDED_FOR'] ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )
85
+ ) {
86
+ $remote_ip = $headers['HTTP_X_FORWARDED_FOR'];
87
+ } elseif ( isset( $headers['Server'] ) && strpos( $headers['Server'] ,'cloudflare') !== false ) {
88
+ $remote_ip = 'cloudflare';
89
+ } else {
90
+ $remote_ip = false;
91
+ }
92
+
93
+ return $remote_ip;
94
+ }
95
+
96
+ /**
97
+ * Varnish
98
+ *
99
+ * Results on the Varnish calls
100
+ *
101
+ * @since 4.4.0
102
+ */
103
+ static function varnish_results( $headers = '' ) {
104
+
105
+ // Set the defaults
106
+ $return = array(
107
+ 'icon' => 'good',
108
+ 'message' => __( 'Varnish is running properly and caching is happening.', 'varnish-http-purge' ),
109
+ );
110
+
111
+ if ( $headers == '' ) {
112
+ $kronk = false;
113
+ } else {
114
+ $kronk = true;
115
+
116
+ // Check if the headers are set AND if the values are valid
117
+ $x_cachable = ( isset( $headers['X-Cacheable'] ) && strpos( $headers['X-Cacheable'] ,'YES') !== false )? true : false;
118
+ $x_varnish = ( isset( $headers['X-Varnish'] ) )? true : false;
119
+ $x_via = ( is_numeric( strpos( $headers['Via'], 'arnish' ) ) )? true : false;
120
+ $x_age = ( isset( $headers[ 'Age' ] ) && $headers[ 'Age' ] > 0 )? true : false;
121
+
122
+ // If this is TRUE it's NOT Cachable
123
+ $not_cachable = ( ( isset( $headers['X-Cacheable'] ) && strpos( $headers['X-Cacheable'] ,'NO') !== false ) || ( isset( $headers['Pragma'] ) && strpos( $headers['Pragma'] ,'no-cache') !== false ) || !$x_age )? true : false;
124
+ $cacheheaders_set = ( isset( $headers['X-Cacheable'] ) || isset( $headers['X-Varnish'] ) || isset( $headers['X-Cache'] ) || $x_via )? true : false;
125
+ }
126
+
127
+ if ( !$kronk ) {
128
+ $return['icon'] = 'bad';
129
+ $return['message'] = __( 'Something went very wrong with this request. Please try again.', 'varnish-http-purge' );
130
+ } elseif ( !$cacheheaders_set ) {
131
+ $return['icon'] = 'warning';
132
+ $return['message'] = __( 'We were unable find Varnish active for this domain. Please review the output below to understand why.', 'varnish-http-purge' );
133
+ } elseif ( !$not_cachable && ( $x_cachable || $x_varnish ) ) {
134
+ $return['icon'] = 'awesome';
135
+ $return['message'] = __( 'Varnish is running properly and caching is happening.', 'varnish-http-purge' );
136
+ } else {
137
+ $return['icon'] = 'warning';
138
+ $return['message'] = __( 'Varnish is running but is unable to cache your site. Please review the following output to diagnose the issue.', 'varnish-http-purge' );
139
+ }
140
+
141
+ return $return;
142
+ }
143
+
144
+ /**
145
+ * Remote IP
146
+ *
147
+ * Results on if we have a proxy going on and what that means
148
+ *
149
+ * @since 4.4.0
150
+ */
151
+ static function remote_ip_results( $remote_ip, $varniship ) {
152
+
153
+ // Set the defaults
154
+ $return = false;
155
+
156
+ if ( $remote_ip == false && !empty( $varniship) ) {
157
+ $return = array(
158
+ 'icon' => 'warning',
159
+ 'message' => sprintf( __( 'You have a Varnish IP set but a proxy like Cloudflare or Sucuri has not been detected. This is mostly harmless, but if you have issues with your cache not emptying when you make a post, you may need to <a href="%s">erase your Varnish IP</a>. Please check with your webhost or server admin before doing so.', 'varnish-http-purge' ), '#configure' )
160
+ );
161
+ } elseif ( $remote_ip !== false && $remote_ip !== $varniship ) {
162
+ $return = array(
163
+ 'icon' => 'warning',
164
+ 'message' => sprintf( __( 'You\'re using a Custom Varnish IP that doesn\'t appear to match your server IP address. If you\'re using multiple Varnish servers or IPv6, this is fine. Please make sure you\'ve <a href="%s">properly configured it</a> according to your webhost\'s specifications.', 'varnish-http-purge' ), '#configure' ),
165
+ );
166
+ } else {
167
+ $return = array(
168
+ 'icon' => 'awesome',
169
+ 'message' => __( 'Your server IP setup looks good.', 'varnish-http-purge' ),
170
+ );
171
+ }
172
+
173
+ return $return;
174
+ }
175
+
176
+ /**
177
+ * Server Details
178
+ *
179
+ * Includes nginx, hhvm, cloudflare, and more
180
+ *
181
+ * @since 4.4.0
182
+ */
183
+ static function server_results( $headers ) {
184
+
185
+ // Set the defaults
186
+ $return = array();
187
+
188
+ if ( isset( $headers['Server'] ) ) {
189
+ // nginx
190
+ if ( strpos( $varnish_headers['Server'] ,'nginx') !== false && strpos( $varnish_headers['Server'] ,'cloudflare') == false ) {
191
+ $return['nginx'] = array(
192
+ 'icon' => 'awkward',
193
+ 'message' => __( 'Your server is running nginx and Apache was expected. This may be fine, especially if you use a passthrough proxy, but keep it in mind.', 'varnish-http-purge' )
194
+ );
195
+ }
196
+
197
+ // Cloudflare
198
+ if ( strpos( $headers['Server'] ,'cloudflare') !== false ) {
199
+ $return['cloudflare'] = array(
200
+ 'icon' => 'warning',
201
+ 'message' => sprintf( __( 'CloudFlare has been detected. While this is generally fine, you may experience some cache oddities. Make sure you <a href="%s">configure WordPress for Cloudflare</a>.', 'varnish-http-purge' ), '#configure' ),
202
+ );
203
+ }
204
+
205
+ // HHVM: Note, WP is dropping support so ...
206
+ if ( isset( $headers['X-Powered-By'] ) && strpos( $headers['X-Powered-By'] ,'HHVM') !== false ) {
207
+ $return['hhvm'] = array(
208
+ 'icon' => 'awkward',
209
+ 'message' => __( 'You are running HHVM instead of PHP. While that is compatible with Varnish, you should consider PHP 7. WordPress will cease support for HHVM in 2018.', 'varnish-http-purge' ),
210
+ );
211
+ }
212
+
213
+ // Pagely
214
+ if ( strpos( $varnish_headers['Server'] ,'Pagely') !== false ) {
215
+ $return['pagely'] = array(
216
+ 'icon' => 'good',
217
+ 'message' => __( 'This site is hosted on Pagely.', 'varnish-http-purge' ),
218
+ );
219
+ }
220
+ }
221
+
222
+ if ( isset( $varnish_headers['X-hacker'] ) ) {
223
+ $return['wordpresscom'] = array(
224
+ 'icon' => 'bad',
225
+ 'message' => __( 'This site is hosted on WordPress.com, which is cool but, last we checked, they don\'t use Varnish.', 'varnish-http-purge' ),
226
+ );
227
+ }
228
+
229
+ if ( isset( $varnish_headers['X-Backend'] ) && strpos( $varnish_headers['X-Backend'] ,'wpaas_web_') !== false ) {
230
+ $return['godaddy'] = array(
231
+ 'icon' => 'good',
232
+ 'message' => __( 'This site is hosted on GoDaddy.', 'varnish-http-purge' ),
233
+ );
234
+ }
235
+
236
+ return $return;
237
+ }
238
+
239
+ /**
240
+ * GZIP
241
+ *
242
+ * Results on GZIP
243
+ *
244
+ * @since 4.4.0
245
+ */
246
+ static function gzip_results( $headers ) {
247
+
248
+ // Set the defaults
249
+ $return = false;
250
+
251
+ // GZip
252
+ if( strpos( $headers['Content-Encoding'] ,'gzip') !== false || ( isset( $headers['Vary'] ) && strpos( $headers['Vary'] ,'gzip' ) !== false ) ) {
253
+ $return = array(
254
+ 'icon' => 'good',
255
+ 'message' => __( 'Your site is compressing content and making the internet faster.', 'varnish-http-purge' ),
256
+ );
257
+ }
258
+
259
+ // Fastly
260
+ if ( strpos( $headers['Content-Encoding'] ,'Fastly') !== false ) {
261
+ $return = array(
262
+ 'icon' => 'good',
263
+ 'message' => sprintf( __( '<a href="%s">Fastly</a> is speeding up your site. Keep in mind, it may cache your CSS and images longer than Varnish does. Remember to empty all caches in all locations.', 'varnish-http-purge' ), esc_url( 'https://fastly.com' ) ),
264
+ );
265
+ }
266
+
267
+ return $return;
268
+ }
269
+
270
+ /**
271
+ * Cookies
272
+ *
273
+ * Cookies break Varnish. Sometimes.
274
+ *
275
+ * @since 4.4.0
276
+ */
277
+ static function cookie_results( $headers ) {
278
+
279
+ // Defaults
280
+ $return = $cookies = array();
281
+
282
+ // Set the default returns
283
+ $cookie_warning = array(
284
+ 'icon' => 'warning',
285
+ 'message' => __( 'Cookies have been detected on your site. This can cause Varnish to not properly cache unless it\'s configured specially to accommodate. Since it\'s impossible to cover all possible situations, please take the following alerts with a grain of salt. If you know certain cookies are safe on your server, this is fine. If you aren\'t sure, pass the details on to your webhost.', 'varnish-http-purge' ),
286
+ );
287
+ $default = array(
288
+ 'phpsessid' => array( 'icon' => 'bad', 'message' => __( 'A plugin or theme is setting a PHPSESSID cookie on every pageload. This makes Varnish not deliver cached pages.', 'varnish-http-purge' ) ),
289
+ 'edd_wp_session' => array( 'icon' => 'bad', 'message' => sprintf( __( '<a href="%s">Easy Digital Downloads</a> is being used with cookie sessions. This may cause your cache to misbehave. If you have issues, please set <code>define( "EDD_USE_PHP_SESSIONS", true );</code> in your <code>wp-config.php</code> file.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/easy-digital-downloads/') ) ),
290
+ 'edd_items_in_cart' => array( 'icon' => 'warning', 'message' => sprintf( __( '<a href="%s">Easy Digital Downloads</a> is putting down a shopping cart cookie on every page load. Make sure Varnish is set up to ignore that when it\'s empty.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/easy-digital-downloads/') ) ),
291
+ 'wfvt' => array( 'icon' => 'bad', 'message' => sprintf( __( '<a href="%s">Wordfence</a> is putting down cookies on every page load. Please check "Disable WordFence Cookies" under <a href="%s">General Wordfence Options</a> to resolve.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/wordfence/'), admin_url( 'admin.php?page=WordfenceOptions' ) ) ),
292
+ 'invite_anyone' => array( 'icon' => 'bad', 'message' => sprintf( __( '<a href="%s">Invite Anyone</a>, a plugin for BuddyPress, is putting down a cookie on every page load.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/invite-anyone/') ) ),
293
+ 'charitable_sessions' => array( 'icon' => 'bad', 'message' => sprintf( __( '<a href="%s">Charitable</a> is putting down a cookie on every page load. This has been fixed as of version 1.5.0, so please upgrade to the latest version.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/charitable/') ) ),
294
+ );
295
+
296
+ if ( isset( $headers['Set-Cookie'] ) ) {
297
+ // If cookies are an array, scan the whole thing. Otherwise, we can use strpos.
298
+ if ( is_array( $headers['Set-Cookie'] ) ) {
299
+ $cookies = array(
300
+ 'phpsessid' => in_array( 'PHPSESSID', $headers['Set-Cookie'], true ),
301
+ 'edd_wp_session' => in_array( 'edd_wp_session', $headers['Set-Cookie'], true ),
302
+ 'edd_items_in_cart' => in_array( 'edd_items_in_cart', $headers['Set-Cookie'], true ),
303
+ 'wfvt' => in_array( 'wfvt_', $headers['Set-Cookie'], true ),
304
+ 'invite_anyone' => in_array( 'invite-anyone', $headers['Set-Cookie'], true ),
305
+ 'charitable_sessions' => in_array( 'charitable_sessions', $headers['Set-Cookie'], true ),
306
+ );
307
+ } else {
308
+ $cookies = array(
309
+ 'phpsessid' => strpos( $headers['Set-Cookie'], 'PHPSESSID'),
310
+ 'edd_wp_session' => strpos( $headers['Set-Cookie'], 'edd_wp_session' ),
311
+ 'edd_items_in_cart' => strpos( $headers['Set-Cookie'], 'edd_items_in_cart' ),
312
+ 'wfvt' => strpos( $headers['Set-Cookie'], 'wfvt_' ),
313
+ 'invite_anyone' => strpos( $headers['Set-Cookie'], 'invite-anyone' ),
314
+ 'charitable_sessions' => strpos( $varnish_headers['Set-Cookie'], 'charitable_sessions' ),
315
+ );
316
+ }
317
+
318
+ foreach ( $cookies as $name => $value ) {
319
+ if ( $value !== false ) {
320
+ $return[ $name ] = array( 'icon' => $default[ $name]['icon'], 'message' => $default[ $name]['message'] );
321
+ }
322
+ }
323
+ }
324
+
325
+ // If there's data in the array, add a Cookie Monster Warning to the top
326
+ if ( !empty( $return ) ) array_unshift( $return, $cookie_warning );
327
+
328
+ return $return;
329
+ }
330
+
331
+
332
+ /**
333
+ * Cache
334
+ *
335
+ * Checking Age, Max Age, Cache Control, Pragma and more
336
+ *
337
+ * @since 4.4.0
338
+ */
339
+ static function cache_results( $headers ) {
340
+
341
+ $return = array();
342
+
343
+ // Cache Control
344
+ if ( isset( $headers['Cache-Control'] ) ) {
345
+
346
+ // No-Cache Set
347
+ if ( strpos( $headers['Cache-Control'], 'no-cache' ) !== false ) {
348
+ $return['no_cache'] = array(
349
+ 'icon' => 'bad',
350
+ 'message' => __( 'Something is setting the header Cache-Control to "no-cache" which means visitors will never get cached pages.', 'varnish-http-purge' ),
351
+ );
352
+ }
353
+
354
+ // Max-Age is 0
355
+ if ( strpos( $headers['Cache-Control'], 'max-age=0' ) !== false ) {
356
+ $return['max_age'] = array(
357
+ 'icon' => 'bad',
358
+ 'message' => __( 'Something is setting the header Cache-Control to "max-age=0" which means a page can be no older than 0 seconds before it needs to regenerate the cache.', 'varnish-http-purge' ),
359
+ );
360
+ }
361
+ }
362
+
363
+ // Age Headers
364
+ if ( !isset( $headers['Age'] ) ) {
365
+ $return['age'] = array(
366
+ 'icon' => 'bad',
367
+ 'message' => __( 'Your domain does not report an "Age" header, which means we can\'t tell if the page is actually serving from cache.', 'varnish-http-purge' ),
368
+ );
369
+ } elseif( $headers['Age'] <= 0 || $headers['Age'] == 0 ) {
370
+ $return['age'] = array(
371
+ 'icon' => 'warning',
372
+ 'message' => __( 'The "Age" header is set to less than 1, which means you checked right when Varnish cleared the cache for that url or Varnish is not serving cached content for that url. Check again but if it happens again, then either the URL is intentionally excluded from caching, or a theme or plugin is sending cache headers or cookies that instruct varnish not to cache.', 'varnish-http-purge' ),
373
+ );
374
+ } else {
375
+ $return['age'] = array(
376
+ 'icon' => 'good',
377
+ 'message' => __( 'Your site is returning proper "Age" headers.', 'varnish-http-purge' ),
378
+ );
379
+ }
380
+
381
+ // Pragma
382
+ if ( isset( $headers['Pragma'] ) && strpos( $headers['Pragma'] ,'no-cache') !== false ) {
383
+ $return['pragma'] = array(
384
+ 'icon' => 'bad',
385
+ 'message' => __( 'Something is setting the header Pragma to "no-cache" which means visitors will never get cached pages. This is usually done by plugins.', 'varnish-http-purge' ),
386
+ );
387
+ }
388
+
389
+ // X-Cache
390
+ if ( isset( $headers['X-Cache-Status'] ) && strpos( $headers['X-Cache-Status'] ,'MISS') !== false ) {
391
+ $return['X-Cache'] = array(
392
+ 'icon' => 'bad',
393
+ 'message' => __( 'X-Cache missed, which means it was not able to serve this page as cached. This may be resolved by rerunning the scan. If not, then a plugin or theme is forcing this setting.', 'varnish-http-purge' ),
394
+ );
395
+ }
396
+
397
+ // Mod-PageSpeed
398
+ if ( isset( $headers['X-Mod-Pagespeed'] ) ) {
399
+ if ( strpos( $headers['X-Cacheable'] , 'YES:Forced') !== false ) {
400
+ $return['mod_pagespeed'] = array(
401
+ 'icon' => 'good',
402
+ 'message' => __( 'Mod Pagespeed is active and configured to work properly with Varnish.', 'varnish-http-purge' ),
403
+ );
404
+ } else {
405
+ $return['mod_pagespeed'] = array(
406
+ 'icon' => 'bad',
407
+ 'message' => __( 'Mod Pagespeed is active but it looks like your caching headers may not be right. This may be a false negative if other parts of your site are overwriting headers. Fix all other errors listed, then come back to this. If you are still having errors, you will need to look into using htaccess or nginx to override the Pagespeed headers.', 'varnish-http-purge' ),
408
+ );
409
+ }
410
+ }
411
+
412
+ return $return;
413
+ }
414
+
415
+ /**
416
+ * Bad Actors
417
+ *
418
+ * Plugins and themes known to be problematic
419
+ *
420
+ * @since 4.4.0
421
+ */
422
+ static function bad_actors_results( ) {
423
+
424
+ $return = array();
425
+
426
+ $themes = array(
427
+ 'divi' => __( 'Divi themes use sessions in their headers for many of their themes. To check, change your theme and re-run this test. If this warning goes away, it\'s your theme.', 'varnish-http-purge' ),
428
+ 'enfold' => __( 'The Enfold theme uses sessions for every call of shortcodes in certain situations. To check, change your theme and re-run this test. If this warning goes away, it\'s your theme.', 'varnish-http-purge' ),
429
+ 'prophoto6' => __( 'Prophoto version 6 requires you to be on version 6.21.8 or higher to work properly with Varnish. Please make sure you site is up to date.', 'varnish-http-purge' ),
430
+ );
431
+
432
+ $plugins = array(
433
+ 'bad-behavior' => array(
434
+ 'path' => 'bad-behavior/bad-behavior.php',
435
+ 'message' => sprintf( __( '<a href="%s">Bad Behavior</a> may cause unexpected results with Varnish and not function properly.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/bad-behavior/' ),
436
+ ),
437
+ 'pie-register' => array(
438
+ 'path' => 'pie-register/pie-register.php',
439
+ 'message' => sprintf( __( '<a href="%s">Pie Register</a> sets output buffering in the header of every page load, which enforces sessions. There is no known fix at this time.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/pie-register/' ),
440
+ ),
441
+ 'quick-cache' => array(
442
+ 'path' => 'quick-cache/quick-cache.php',
443
+ 'message' => __( 'Quick Cache does not play well with Varnish.', 'varnish-http-purge' )
444
+ ),
445
+ 'simple-session-support' => array(
446
+ 'path' => 'simple-session-support/simple-session-support.php',
447
+ 'message' => sprintf( __( '<a href="%s">Simple Session Support</a> forces PHP Sessions. It\'s also no longer updated and not recommended for use.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/simple-session-support/' ),
448
+ ),
449
+ 'tweet-blender' => array(
450
+ 'path' => 'tweet-blender/tweet-blender.php',
451
+ 'message' => sprintf( __( '<a href="%s">Tweet Blender</a> conflicts with most server based caching. It also has not been updated since 2014.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/tweet-blender/' ),
452
+ ),
453
+ 'wp-cache' => array(
454
+ 'path' => 'wp-cache/wp-cache.php',
455
+ 'message' => sprintf( __( '<a href="%s">WP Cache</a> is not necessary when using a Varnish based caching, and can cause redundancy in caches, resulting in unexpected data load.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/wp-cache/' ),
456
+ ),
457
+ 'wp-file-cache' => array(
458
+ 'path' => 'wp-file-cache/file-cache.php',
459
+ 'message' => sprintf( __( '<a href="%s">WP Files Cache</a> is not necessary when using a Varnish based caching, and can cause redundancy in caches, resulting in unexpected data load.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/wp-file-cache/' ),
460
+ ),
461
+ 'wp-super-cache' => array(
462
+ 'path' => 'wp-super-cache/wp-cache.php',
463
+ 'message' => sprintf( __( '<a href="%s">WP Super Cache</a> is not necessary when using a Varnish based caching, and can cause redundancy in caches, resulting in unexpected data load.', 'varnish-http-purge' ), 'https://wordpress.org/plugins/wp-super-cache/' ),
464
+ ),
465
+ );
466
+
467
+ // Check all the themes. If one of the questionable ones are active, warn
468
+ foreach ( $themes as $theme => $message ) {
469
+ $my_theme = wp_get_theme( $theme );
470
+ if ( $my_theme->exists() ) {
471
+ $return[ $theme ] = array( 'icon' => 'warning', 'message' => $message );
472
+ }
473
+ }
474
+
475
+ // Check the plugins
476
+ foreach ( $plugins as $plugin => $data ) {
477
+ if ( is_plugin_active( $data['path'] ) ) {
478
+ $return[ $plugin ] = array( 'icon' => 'warning', 'message' => $data['message'] );
479
+ }
480
+ }
481
+
482
+ return $return;
483
+ }
484
+
485
+ /**
486
+ * Get all the results
487
+ *
488
+ * Collect everything, get all the data spit it out.
489
+ *
490
+ * @since 4.4.0
491
+ */
492
+ static function get_all_the_results( $headers, $remote_ip, $varniship ) {
493
+ $output = array();
494
+ $output['varnish'] = self::varnish_results( $headers );
495
+ $output['remote_ip'] = self::remote_ip_results( $remote_ip, $varniship );
496
+
497
+ // Server Results
498
+ $sever_results = self::server_results( $remote_ip, $varniship );
499
+ $output = array_merge( $output, $sever_results );
500
+
501
+ // Cache Results
502
+ $cache_results = self::cache_results( $headers );
503
+ $output = array_merge( $output, $cache_results );
504
+
505
+ // Cookies
506
+ $cookie_results = self::cookie_results( $headers );
507
+ $output = array_merge( $output, $cookie_results );
508
+
509
+ // Bad Actors (plugins and themes that don't play nicely with Varnish)
510
+ $bad_actors_results = self::bad_actors_results( $headers );
511
+ $output = array_merge( $output, $bad_actors_results );
512
+
513
+ return $output;
514
+ }
515
+
516
+ }
517
+
518
+ if ( class_exists( 'VarnishDebug' ) ) $varnish_debug = new VarnishDebug();
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: Ipstenu, mikeschroder, techpriester
3
  Tags: varnish, purge, cache
4
  Requires at least: 4.7
5
  Tested up to: 4.9
6
- Stable tag: 4.3.1
7
  Requires PHP: 5.6
8
 
9
  Automatically empty Varnish Cache when content on your site is modified.
@@ -29,10 +29,13 @@ In addition, your <em>entire</em> cache will be deleted on the following actions
29
 
30
  Plugins can hook into the purge actions as well, to filter their own events to trigger a purge.
31
 
32
- And if you're into WP-CLI, you can use that too: `wp varnish purge`
33
-
34
  On a multisite network using subfolders, only <strong>network admins</strong> can purge the main site. This is a security decision.
35
 
 
 
 
 
 
36
  = Requirements =
37
 
38
  * Pretty Permalinks enabled
@@ -44,21 +47,21 @@ On a multisite network using subfolders, only <strong>network admins</strong> ca
44
 
45
  If you have code patches, [pull requests are welcome](https://github.com/Ipstenu/varnish-http-purge).
46
 
47
- = How can I tell everything's working? =
48
 
49
- From your WordPress Dashboard, go to Tools -> Varnish Status. There a page will auto-scan your main plugin page and report back any issues found. This includes any known problematic plugins.
50
 
51
  = Does every WordPress plugin and theme work with Varnish? =
52
 
53
- No. Some of them have behaviour that causes Varnish not to cache. While I can't debug that for you, there is an "Is Varnish Working?" tool (see WP Admin -> Tools -> Varnish Status) that tries to detect most of the common issues and direct you to resolutions.
54
 
55
  = How can I debug my site? =
56
 
57
  Use the Varnish Status page. It will try and help you figure out what's wrong.
58
 
59
- = Will you fix my site if there's a conflict with a theme or plugin? =
60
 
61
- I'm sorry but I can't do that, I don't have that much availability. I will try to point you towards solving it on your own. Bear in mind, that may mean you have to decide if using a specific plugin or theme is worth an imperfect cache.
62
 
63
  = What version of Varnish is supported? =
64
 
@@ -66,21 +69,15 @@ This was built and tested on Varnish 3.x. While it is reported to work on 2.x an
66
 
67
  = Why doesn't every page cache get deleted when I make a new post? =
68
 
69
- Philosophy.
70
-
71
- There are many other plugins out there which will allow you to granularly select what pages should and should not be deleted on updates. With that in mind, the choice was made for decisions instead of options, and simplicity was the driving principle. The plugin decides what's best to delete on updates, and provides hooks for developers to use as needed.
72
 
73
  = Why doesn't my cache get deleted when I edit my theme? =
74
 
75
- If you activate a new theme, or use the customizer to edit your theme, it will delete your cache.
76
-
77
- If you edit theme (or plugin) files directly, WordPress cannot easily detect those changes, therefor the plugin will not delete the cache. In that situation, you will need to empty the cache manually.
78
 
79
  = How do I manually delete the whole cache? =
80
 
81
- Click the 'Empty Varnish Cache' button on the "Right Now" Dashboard (see the screenshot if you can't find it).
82
-
83
- There's also an "Empty Cache" button on the admin toolbar.
84
 
85
  = I don't see a button! =
86
 
@@ -151,26 +148,16 @@ Yes! [Full documentation can be found on Custom Filters in the wiki](https://git
151
  * Yes, I do work for DreamHost.
152
  * No, this plugin is not really official nor DreamHost Only
153
 
154
- This plugin is installed by default for _all_ DreamPress installs on DreamHost, and I maintain it for DreamHost, but it was not originally an official DH plugin which means I will continue to support all users to the best of my ability.
155
 
156
  == Changelog ==
157
 
158
- = 4.3.1 =
159
- * 10 October 2017
160
- * Copied a wrong line.
161
-
162
- = 4.3.0 =
163
- * 10 October 2017
164
- * Add Varnish Flush for "this" page on front end
165
- * Do not flush non-public taxonomies
166
-
167
- = 4.2.0 =
168
- * 30 August 2017
169
- * More flexible support for custom cat/tag bases
170
- * Added in support for custom taxonomies
171
- * New function to generate the URLs, so it can be called by external plugins
172
- * Move right now box to be called later, preventing double calls
173
- * Extra check for if it's a URL, because some plugins are weird (props @danielkun)
174
 
175
  == Screenshots ==
176
 
3
  Tags: varnish, purge, cache
4
  Requires at least: 4.7
5
  Tested up to: 4.9
6
+ Stable tag: 4.4.0
7
  Requires PHP: 5.6
8
 
9
  Automatically empty Varnish Cache when content on your site is modified.
29
 
30
  Plugins can hook into the purge actions as well, to filter their own events to trigger a purge.
31
 
 
 
32
  On a multisite network using subfolders, only <strong>network admins</strong> can purge the main site. This is a security decision.
33
 
34
+ = WP CLI =
35
+
36
+ * `wp varnish purge` - Flush the entire cache
37
+ * `wp varnish debug` - Help for debugging how well Varnish is (or isn't) working
38
+
39
  = Requirements =
40
 
41
  * Pretty Permalinks enabled
47
 
48
  If you have code patches, [pull requests are welcome](https://github.com/Ipstenu/varnish-http-purge).
49
 
50
+ = How can I tell if everything's working? =
51
 
52
+ From your WordPress Dashboard, go to Tools -> Varnish Status. There a page will auto-scan your front page and report back any issues found. This includes any known problematic plugins. You can use it to scan any URL on your domain (but ONLY on your own domain).
53
 
54
  = Does every WordPress plugin and theme work with Varnish? =
55
 
56
+ No. Some of them have behaviour that causes Varnish not to cache, either by accident or design.
57
 
58
  = How can I debug my site? =
59
 
60
  Use the Varnish Status page. It will try and help you figure out what's wrong.
61
 
62
+ = Will you fix my site? =
63
 
64
+ I don't have that much availability. I will try to point you towards solving it on your own. Bear in mind, that may mean you have to decide if using a specific plugin or theme is worth an imperfect cache.
65
 
66
  = What version of Varnish is supported? =
67
 
69
 
70
  = Why doesn't every page cache get deleted when I make a new post? =
71
 
72
+ Philosophy. There are many other plugins out there which will allow you to granularly select what pages should and should not be deleted on updates. With that in mind, the choice was made for decisions instead of options, and simplicity was the driving principle. The plugin decides what's best to delete on updates, and provides hooks for developers to use as needed.
 
 
73
 
74
  = Why doesn't my cache get deleted when I edit my theme? =
75
 
76
+ WordPress can only flush the cache automatically when you use the internal file editors. If you edit files manually then you will need to empty the cache manually.
 
 
77
 
78
  = How do I manually delete the whole cache? =
79
 
80
+ Click the 'Empty Varnish Cache' button on the "Right Now" Dashboard (see the screenshot if you can't find it). There's also an "Empty Cache" button on the admin toolbar, and you can use `wp varnish purge` from the command line.
 
 
81
 
82
  = I don't see a button! =
83
 
148
  * Yes, I do work for DreamHost.
149
  * No, this plugin is not really official nor DreamHost Only
150
 
151
+ This plugin is installed by default for _all_ DreamPress installs on DreamHost, and I maintain it for DreamHost, but it was not originally an official DreamHost plugin which means I will continue to support all users to the best of my ability.
152
 
153
  == Changelog ==
154
 
155
+ = 4.4.0 =
156
+ * March 2018
157
+ * Improve translations
158
+ * Massive improvements to the debugging tools
159
+ * Allow for port to be added to host headers (props @davidbarratt)
160
+ * WP-CLI for debugging
 
 
 
 
 
 
 
 
 
 
161
 
162
  == Screenshots ==
163
 
status.php ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ Copyright 2016-2018 Mika Epstein (email: ipstenu@halfelf.org)
4
+
5
+ This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
+
7
+ Varnish HTTP Purge is free software: you can redistribute it and/or modify
8
+ it under the terms of the Apache License 2.0 license.
9
+
10
+ Varnish HTTP Purge is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+ */
14
+
15
+ if ( !defined( 'ABSPATH' ) ) die();
16
+
17
+ /**
18
+ * Varnish Status Class
19
+ *
20
+ * @since 4.0
21
+ */
22
+
23
+ class VarnishStatus {
24
+
25
+ /**
26
+ * Construct
27
+ * Fires when class is constructed, adds init hook
28
+ *
29
+ * @since 4.0
30
+ */
31
+ function __construct() {
32
+ // Admin Init Hooks
33
+ add_action( 'admin_init', array( &$this, 'admin_init' ) );
34
+ // Admin Menu
35
+ add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
36
+ }
37
+
38
+ /**
39
+ * Admin init Callback
40
+ *
41
+ * @since 4.0
42
+ */
43
+ function admin_init() {
44
+ $this->register_settings_url();
45
+ if ( !is_multisite() ) $this->register_settings_ip();
46
+ }
47
+
48
+ /**
49
+ * Admin Menu Callback
50
+ *
51
+ * @since 4.0
52
+ */
53
+ function admin_menu() {
54
+ add_management_page( __( 'Is Varnish Working?', 'varnish-http-purge' ), __( 'Varnish Status', 'varnish-http-purge' ), 'manage_options', 'varnish-status', array( &$this, 'settings_page' ) );
55
+ }
56
+
57
+ /**
58
+ * Register Admin Settings URL
59
+ *
60
+ * @since 4.0
61
+ */
62
+ function register_settings_url() {
63
+ register_setting( 'varnish-http-purge-url', 'vhp_varnish_url', array( &$this, 'varnish_url_sanitize' ) );
64
+ add_settings_section( 'varnish-url-settings-section', __( 'Check Varnish Status', 'varnish-http-purge' ), array( &$this, 'options_callback_url'), 'varnish-url-settings' );
65
+ add_settings_field( 'varnish_url', __( 'Check A URL On Your Site:', 'varnish-http-purge' ), array( &$this, 'varnish_url_callback' ), 'varnish-url-settings', 'varnish-url-settings-section' );
66
+ }
67
+
68
+ /**
69
+ * Register Admin Settings IP
70
+ *
71
+ * @since 4.0.2
72
+ */
73
+ function register_settings_ip() {
74
+ register_setting( 'varnish-http-purge-ip', 'vhp_varnish_ip', array( &$this, 'varnish_ip_sanitize' ) );
75
+ add_settings_section( 'varnish-ip-settings-section', __('Configure Custom Varnish IP', 'varnish-http-purge'), array( &$this, 'options_callback_ip'), 'varnish-ip-settings' );
76
+ add_settings_field( 'varnish_ip', __('Set Varnish IP', 'varnish-http-purge'), array( &$this, 'varnish_ip_callback'), 'varnish-ip-settings', 'varnish-ip-settings-section' );
77
+ }
78
+
79
+ /**
80
+ * Options Callback - IP Address
81
+ *
82
+ * @since 4.0
83
+ */
84
+ function options_callback_ip() {
85
+ ?>
86
+ <p><a name="#configure"></a><?php _e( 'The majority of users will never need to look down here. However there are cases when a custom Varnish IP Address will need to be set, in order to tell the plugin to empty the cache in a specific location. If you\'re using a CDN like Cloudflare or a Firewall Proxy like Sucuri, you will want to set this.', 'varnish-http-purge' ); ?></p>
87
+ <p><?php _e( 'Your Varnish IP the IP address of the server where Varnish is installed. Your Varnish IP must be one of the IPs that Varnish is listening. If you use multiple IPs, or if you\'ve customized your ACLs, you\'ll need to pick one that doesn\'t conflict with your other settings. For example, if you have Varnish listening on a public and private IP, pick the private. On the other hand, if you told Varnish to listen on 0.0.0.0 (i.e. "listen on every interface you can") you would need to check what IP you set your purge ACL to allow (commonly 127.0.0.1 aka localhost), and use that (i.e. 127.0.0.1).', 'varnish-http-purge' ); ?></p>
88
+ <p><?php _e( 'If your webhost set up Varnish for you, as is the case with DreamPress or WP Engine, ask them for the specifics if they don\'t have it documented. I\'ve listed the ones I know about here, however you should still check with them if you\'re not sure.', 'varnish-http-purge' ); ?></p>
89
+ <p><strong><?php _e( 'If you aren\'t sure what to do, contact your webhost or server admin before making any changes.', 'varnish-http-purge' ); ?></strong></p>
90
+
91
+ <ul>
92
+ <li><?php _e( 'DreamHost - Go into the Panel and click on the DNS settings for the domain. The entry for <em>resolve-to.domain</em> (if set) will be your varnish server. If it\'s not set, then you don\'t need to worry about this at all. Example:', 'varnish-http-purge' ); ?> <code>resolve-to.www A 208.97.157.172</code></li>
93
+ </ul>
94
+ <?php
95
+ }
96
+
97
+ /**
98
+ * Varnish IP Callback
99
+ *
100
+ * @since 4.0
101
+ */
102
+ function varnish_ip_callback() {
103
+
104
+ $disabled = false;
105
+
106
+ if ( VHP_VARNISH_IP != false ) {
107
+ $disabled = true;
108
+ $varniship = VHP_VARNISH_IP;
109
+ } else {
110
+ $varniship = get_option( 'vhp_varnish_ip' );
111
+ }
112
+
113
+ ?>
114
+ <input type="text" id="vhp_varnish_ip" name="vhp_varnish_ip" value="<?php echo $varniship; ?>" size="25" <?php if ( $disabled == true ) { echo 'disabled'; } ?>/>
115
+ <label for="vhp_varnish_ip">
116
+ <?php
117
+ if ( $disabled == true ) {
118
+ _e( 'A Varnish IP has been defined in your wp-config, so it is not editable here.', 'varnish-http-purge' );
119
+ } else {
120
+ _e( 'Example:', 'varnish-http-purge' ); ?> <code>123.45.67.89</code><?php
121
+ }
122
+ ?>
123
+ </label>
124
+ <?php
125
+ }
126
+
127
+ /**
128
+ * Options Callback - URL Scanner
129
+ *
130
+ * @since 4.0
131
+ */
132
+ function options_callback_url() {
133
+
134
+ // Include the debug code
135
+ include( 'debug.php' );
136
+
137
+ ?><p><?php _e( 'While it is impossible to detect all possible conflicts, this status page performs a check of the most common issues that prevent Varnish from caching your site properly.', 'varnish-http-purge' ); ?></p>
138
+
139
+ <p><?php _e( 'This feature is provided to help you in debugging any conflicts on your own. When filing an issue with your web-host, we recommend you include the output in your ticket.', 'varnish-http-purge' ); ?></p>
140
+
141
+ <?php
142
+ // Set icons
143
+ $icons = array (
144
+ 'awesome' => '<span class="dashicons dashicons-heart" style="color:#008000;"></span>',
145
+ 'good' => '<span class="dashicons dashicons-thumbs-up" style="color:#008000;"></span>',
146
+ 'warning' => '<span class="dashicons dashicons-warning" style="color:#FF9933"></span>',
147
+ 'awkward' => '<span class="dashicons dashicons-flag" style="color:#FF9933;">',
148
+ 'bad' => '<span class="dashicons dashicons-thumbs-down" style="color:#990000;"></span>',
149
+ );
150
+
151
+ // Get the base URL to start
152
+ $url = esc_url( VarnishPurger::the_home_url() );
153
+ $varnishurl = get_option( 'vhp_varnish_url', $url );
154
+
155
+ // Get the response and headers
156
+ $remote_get = VarnishDebug::remote_get( $varnishurl );
157
+ $headers = wp_remote_retrieve_headers( $remote_get );
158
+
159
+ // Preflight checklist
160
+ $preflight = VarnishDebug::preflight( $remote_get );
161
+
162
+ // Check for Remote IP
163
+ $remote_ip = VarnishDebug::remote_ip( $headers );
164
+
165
+ // Get the Varnish IP
166
+ if ( VHP_VARNISH_IP != false ) {
167
+ $varniship = VHP_VARNISH_IP;
168
+ } else {
169
+ $varniship = get_option('vhp_varnish_ip');
170
+ }
171
+ ?>
172
+
173
+ <h4><?php printf( __( 'Results for %s', 'varnish-http-purge' ), $varnishurl ); ?></h4>
174
+
175
+ <table class="wp-list-table widefat fixed posts">
176
+ <?php
177
+
178
+ // If we failed the preflight checks, we fail.
179
+ if ( $preflight['preflight'] == false ) {
180
+ ?><tr>
181
+ <td width="40px"><?php echo $icons['bad']; ?></td>
182
+ <td><?php echo $preflight['message']; ?></td>
183
+ </tr><?php
184
+ } else {
185
+ // We passed the checks, let's get the data!
186
+
187
+ $output = VarnishDebug::get_all_the_results( $headers, $remote_ip, $varniship );
188
+
189
+ foreach ( $output as $item ) {
190
+ if ( $item !== false && is_array( $item ) ) {
191
+ ?><tr>
192
+ <td width="40px"><?php echo $icons[ $item['icon'] ]; ?></td>
193
+ <td><?php echo $item['message'] ?></td>
194
+ </tr><?php
195
+ }
196
+ }
197
+ }
198
+ ?>
199
+ </table>
200
+
201
+ <?php
202
+ if ( $preflight['preflight'] !== false ) {
203
+ ?>
204
+ <h4><?php _e( 'Technical Details', 'varnish-http-purge' ); ?></h4>
205
+ <table class="wp-list-table widefat fixed posts">
206
+ <?php
207
+ if ( !empty( $headers[0] ) ) {
208
+ echo '<tr><td width="200px">&nbsp;</td><td>' . $headers[0] . '</td></tr>';
209
+ }
210
+ foreach ( $headers as $header => $key ) {
211
+ if ( $header !== '0' ) {
212
+ if ( is_array( $key ) ) {
213
+ $content = print_r( $key, true );
214
+ } else {
215
+ $content = wp_kses_post( $key );
216
+ }
217
+
218
+ echo '<tr><td width="200px" style="text-align:right;">' . $header . ':</td><td>' . $content . '</td></tr>';
219
+ }
220
+ }
221
+ ?>
222
+ </table>
223
+ <?php
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Varnish URL Callback
229
+ *
230
+ * @since 4.0
231
+ */
232
+ function varnish_url_callback() {
233
+ $url = esc_url( VarnishPurger::the_home_url() );
234
+ $varnishurl = get_option( 'vhp_varnish_url', $url );
235
+ ?><input type="text" id="vhp_varnish_url" name="vhp_varnish_url" value="<?php echo $varnishurl; ?>" size="50" /><?php
236
+ }
237
+
238
+ /*
239
+ * Call settings page
240
+ *
241
+ * @since 4.0
242
+ */
243
+ function settings_page() {
244
+ ?>
245
+ <div class="wrap">
246
+
247
+ <h1><?php _e( 'Is Varnish Working?', 'varnish-http-purge' ); ?></h1>
248
+
249
+ <?php settings_errors(); ?>
250
+
251
+ <form action="options.php" method="POST" ><?php
252
+ settings_fields( 'varnish-http-purge-url' );
253
+ do_settings_sections( 'varnish-url-settings' );
254
+ submit_button( __( 'Check URL', 'varnish-http-purge' ), 'primary');
255
+ ?></form>
256
+
257
+ <form action="options.php" method="POST" ><?php
258
+ // Only available if _not_ multisite
259
+ if ( !is_multisite() ) {
260
+ settings_fields( 'varnish-http-purge-ip' );
261
+ do_settings_sections( 'varnish-ip-settings' );
262
+ submit_button( __( 'Save IP', 'varnish-http-purge' ), 'secondary');
263
+ }
264
+ ?></form>
265
+
266
+ </div>
267
+ <?php
268
+ }
269
+
270
+ /**
271
+ * Sanitization and validation for IP
272
+ *
273
+ * @param $input the input to be sanitized
274
+ * @since 4.0
275
+ */
276
+ function varnish_ip_sanitize( $input ) {
277
+
278
+ $output = '';
279
+ $set_message = __( 'You have entered an invalid IP address.', 'varnish-http-purge' );
280
+ $set_type = 'error';
281
+
282
+ if ( empty($input) ) {
283
+ return; // do nothing
284
+ } elseif ( filter_var( $input, FILTER_VALIDATE_IP) ) {
285
+ $set_message = 'IP Updated.';
286
+ $set_type = 'updated';
287
+ $output = filter_var( $input, FILTER_VALIDATE_IP);
288
+ }
289
+
290
+ add_settings_error( 'vhp_varnish_url', 'varnish-url', $set_message, $set_type );
291
+ return $output;
292
+ }
293
+
294
+ /**
295
+ * Sanitization and validation for URL
296
+ *
297
+ * @param $input the input to be sanitized
298
+ * @since 4.0
299
+ */
300
+ function varnish_url_sanitize( $input ) {
301
+
302
+ // Defaults
303
+ $output = esc_url( VarnishPurger::the_home_url() );
304
+ $set_type = 'error';
305
+
306
+ if ( !empty( $input ) ) {
307
+ $parsed_input = parse_url( $input );
308
+ if ( empty( $parsed_input['scheme'] ) ) {
309
+ $schema_input = 'http://';
310
+ if ( is_ssl() ) $schema_input = 'https://';
311
+ $input = $schema_input . ltrim( $input, '/' );
312
+ }
313
+ }
314
+
315
+ if ( empty( $input ) ) {
316
+ $set_message = __( 'You must enter a URL from your own domain to scan.', 'varnish-http-purge' );
317
+ } elseif ( !filter_var( $input, FILTER_VALIDATE_URL) ) {
318
+ $set_message = __( 'You have entered an invalid URL address.', 'varnish-http-purge' );
319
+ } elseif ( parse_url( $output, PHP_URL_HOST ) !== parse_url( $input, PHP_URL_HOST ) ) {
320
+ $set_message = __( 'You cannot scan URLs on other domains.', 'varnish-http-purge' );
321
+ } else {
322
+ $output = filter_var( $input, FILTER_VALIDATE_URL);
323
+ $set_message = __( 'Scanning New URL...', 'varnish-http-purge' );
324
+ $set_type = 'updated';
325
+ }
326
+
327
+ add_settings_error( 'vhp_varnish_url', 'varnish-url', $set_message, $set_type );
328
+ return $output;
329
+ }
330
+
331
+ }
332
+
333
+
334
+ $status = new VarnishStatus();
uninstall.php CHANGED
@@ -1,6 +1,6 @@
1
  <?php
2
  /**
3
- Copyright 2016-2017 Mika Epstein (email: ipstenu@halfelf.org)
4
 
5
  This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
 
1
  <?php
2
  /**
3
+ Copyright 2016-2018 Mika Epstein (email: ipstenu@halfelf.org)
4
 
5
  This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
 
varnish-http-purge.php CHANGED
@@ -3,14 +3,14 @@
3
  Plugin Name: Varnish HTTP Purge
4
  Plugin URI: https://halfelf.org/plugins/varnish-http-purge/
5
  Description: Automatically empty pages cached by Varnish when content on your site is modified.
6
- Version: 4.3.1
7
  Author: Mika Epstein
8
  Author URI: https://halfelf.org/
9
  License: http://www.apache.org/licenses/LICENSE-2.0
10
  Text Domain: varnish-http-purge
11
  Network: true
12
 
13
- Copyright 2013-2017: Mika A. Epstein (email: ipstenu@halfelf.org)
14
 
15
  Original Author: Leon Weidauer ( http:/www.lnwdr.de/ )
16
 
@@ -42,6 +42,7 @@ class VarnishPurger {
42
  */
43
  public function __construct( ) {
44
  defined( 'VHP_VARNISH_IP' ) || define( 'VHP_VARNISH_IP' , false );
 
45
  add_action( 'init', array( &$this, 'init' ) );
46
  add_action( 'admin_init', array( &$this, 'admin_init' ) );
47
  }
@@ -57,7 +58,7 @@ class VarnishPurger {
57
  // Add to 'right now'
58
  add_action( 'activity_box_end', array( $this, 'varnish_rightnow' ), 100 );
59
 
60
- // Failure: Pre WP 4.7
61
  if ( version_compare( get_bloginfo( 'version' ), '4.7', '<=' ) ) {
62
  deactivate_plugins( plugin_basename( __FILE__ ) );
63
  add_action( 'admin_notices' , array( $this, 'require_wp_version_notice'));
@@ -80,6 +81,10 @@ class VarnishPurger {
80
  public function init() {
81
  global $blog_id;
82
 
 
 
 
 
83
  // get my events
84
  $events = $this->getRegisterEvents();
85
  $noIDevents = $this->getNoIDEvents();
@@ -108,7 +113,7 @@ class VarnishPurger {
108
  if (
109
  ( isset( $_GET['vhp_flush_all'] ) && check_admin_referer( 'vhp-flush-all' ) ) ||
110
  ( isset( $_GET['vhp_flush_do'] ) && check_admin_referer( 'vhp-flush-do' ) ) ) {
111
- add_action( 'admin_notices' , array( $this, 'purgeMessage') );
112
  }
113
 
114
  // Add Admin Bar
@@ -170,7 +175,7 @@ class VarnishPurger {
170
  $args = array(
171
  array(
172
  'id' => 'purge-varnish-cache',
173
- 'title' => __( 'Varnish', 'varnish-http-purge' ),
174
  ),
175
  );
176
 
@@ -186,10 +191,10 @@ class VarnishPurger {
186
  $args[] = array(
187
  'parent' => 'purge-varnish-cache',
188
  'id' => 'purge-varnish-cache-all',
189
- 'title' => __( 'Empty Cache (All)', 'varnish-http-purge' ),
190
  'href' => wp_nonce_url( add_query_arg( 'vhp_flush_do', 'all' ), 'vhp-flush-do' ),
191
  'meta' => array(
192
- 'title' => __( 'Empty Cache (All)', 'varnish-http-purge' ),
193
  ),
194
  );
195
  }
@@ -202,10 +207,10 @@ class VarnishPurger {
202
  $args[] = array(
203
  'parent' => 'purge-varnish-cache',
204
  'id' => 'purge-varnish-cache-this',
205
- 'title' => __( 'Empty Cache (This Page)', 'varnish-http-purge' ),
206
  'href' => wp_nonce_url( add_query_arg( 'vhp_flush_do', $page_url . '/' ), 'vhp-flush-do' ),
207
  'meta' => array(
208
- 'title' => __( 'Empty Cache (This Page)', 'varnish-http-purge' ),
209
  ),
210
  );
211
  }
@@ -376,8 +381,20 @@ class VarnishPurger {
376
  $host = $p['host'];
377
  }
378
 
 
 
 
 
 
 
 
 
 
 
 
379
  $purgeme = $schema.$host.$path.$pregex;
380
 
 
381
  if ( !empty( $p['query'] ) && $p['query'] != 'vhp-regex' ) {
382
  $purgeme .= '?' . $p['query'];
383
  }
@@ -387,7 +404,7 @@ class VarnishPurger {
387
  *
388
  * @since 4.1
389
  */
390
- $headers = apply_filters( 'varnish_http_purge_headers', array( 'host' => $p['host'], 'X-Purge-Method' => $x_purge_method ) );
391
  $response = wp_remote_request( $purgeme, array( 'method' => 'PURGE', 'headers' => $headers ) );
392
 
393
  do_action( 'after_purge_url', $url, $purgeme, $response, $headers );
@@ -577,7 +594,7 @@ class VarnishPurger {
577
  array_push( $listofurls, get_permalink( get_option( 'page_for_posts' ) ) );
578
  }
579
  }
580
-
581
  } else {
582
  // We're not sure how we got here, but bail instead of processing anything else.
583
  return;
@@ -592,10 +609,10 @@ class VarnishPurger {
592
  }
593
  }
594
 
595
- // Filter to add or remove urls to the array of purged urls
596
- // @param array $purgeUrls the urls (paths) to be purged
597
- // @param int $postId the id of the new/edited post
598
- $this->purgeUrls = apply_filters( 'vhp_purge_urls', $this->purgeUrls, $postId );
599
  }
600
 
601
  }
@@ -615,4 +632,4 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) {
615
  *
616
  * @since 4.0
617
  */
618
- include_once( 'varnish-status.php' );
3
  Plugin Name: Varnish HTTP Purge
4
  Plugin URI: https://halfelf.org/plugins/varnish-http-purge/
5
  Description: Automatically empty pages cached by Varnish when content on your site is modified.
6
+ Version: 4.4.0
7
  Author: Mika Epstein
8
  Author URI: https://halfelf.org/
9
  License: http://www.apache.org/licenses/LICENSE-2.0
10
  Text Domain: varnish-http-purge
11
  Network: true
12
 
13
+ Copyright 2013-2018: Mika A. Epstein (email: ipstenu@halfelf.org)
14
 
15
  Original Author: Leon Weidauer ( http:/www.lnwdr.de/ )
16
 
42
  */
43
  public function __construct( ) {
44
  defined( 'VHP_VARNISH_IP' ) || define( 'VHP_VARNISH_IP' , false );
45
+ defined( 'VHP_DEBUG' ) || define( 'VHP_DEBUG', false );
46
  add_action( 'init', array( &$this, 'init' ) );
47
  add_action( 'admin_init', array( &$this, 'admin_init' ) );
48
  }
58
  // Add to 'right now'
59
  add_action( 'activity_box_end', array( $this, 'varnish_rightnow' ), 100 );
60
 
61
+ // Failure: Pre WP 4.7
62
  if ( version_compare( get_bloginfo( 'version' ), '4.7', '<=' ) ) {
63
  deactivate_plugins( plugin_basename( __FILE__ ) );
64
  add_action( 'admin_notices' , array( $this, 'require_wp_version_notice'));
81
  public function init() {
82
  global $blog_id;
83
 
84
+ // Cheap Dev Mode
85
+ // If VHP_DEBUG is true, throw down a session to 'break' caching
86
+ if ( VHP_DEBUG ) @session_start();
87
+
88
  // get my events
89
  $events = $this->getRegisterEvents();
90
  $noIDevents = $this->getNoIDEvents();
113
  if (
114
  ( isset( $_GET['vhp_flush_all'] ) && check_admin_referer( 'vhp-flush-all' ) ) ||
115
  ( isset( $_GET['vhp_flush_do'] ) && check_admin_referer( 'vhp-flush-do' ) ) ) {
116
+ add_action( 'admin_notices', array( $this, 'purgeMessage') );
117
  }
118
 
119
  // Add Admin Bar
175
  $args = array(
176
  array(
177
  'id' => 'purge-varnish-cache',
178
+ 'title' => __( 'Empty Cache', 'varnish-http-purge' ),
179
  ),
180
  );
181
 
191
  $args[] = array(
192
  'parent' => 'purge-varnish-cache',
193
  'id' => 'purge-varnish-cache-all',
194
+ 'title' => __( 'Entire Cache (All Pages)', 'varnish-http-purge' ),
195
  'href' => wp_nonce_url( add_query_arg( 'vhp_flush_do', 'all' ), 'vhp-flush-do' ),
196
  'meta' => array(
197
+ 'title' => __( 'Entire Cache (All Pages)', 'varnish-http-purge' ),
198
  ),
199
  );
200
  }
207
  $args[] = array(
208
  'parent' => 'purge-varnish-cache',
209
  'id' => 'purge-varnish-cache-this',
210
+ 'title' => __( 'This Page\'s Cache', 'varnish-http-purge' ),
211
  'href' => wp_nonce_url( add_query_arg( 'vhp_flush_do', $page_url . '/' ), 'vhp-flush-do' ),
212
  'meta' => array(
213
+ 'title' => __( 'This Page\'s Cache', 'varnish-http-purge' ),
214
  ),
215
  );
216
  }
381
  $host = $p['host'];
382
  }
383
 
384
+ /*
385
+ * Allow setting of ports in host name
386
+ * Credit: davidbarratt - https://github.com/Ipstenu/varnish-http-purge/pull/38/
387
+ * @since 4.4.0
388
+ */
389
+ $host_headers = $p['host'];
390
+ if ( isset( $p['port'] ) ) {
391
+ $host_headers .= ':' . $p['port'];
392
+ }
393
+
394
+ // Create path to purge
395
  $purgeme = $schema.$host.$path.$pregex;
396
 
397
+ // Check the queries...
398
  if ( !empty( $p['query'] ) && $p['query'] != 'vhp-regex' ) {
399
  $purgeme .= '?' . $p['query'];
400
  }
404
  *
405
  * @since 4.1
406
  */
407
+ $headers = apply_filters( 'varnish_http_purge_headers', array( 'host' => $host_headers, 'X-Purge-Method' => $x_purge_method ) );
408
  $response = wp_remote_request( $purgeme, array( 'method' => 'PURGE', 'headers' => $headers ) );
409
 
410
  do_action( 'after_purge_url', $url, $purgeme, $response, $headers );
594
  array_push( $listofurls, get_permalink( get_option( 'page_for_posts' ) ) );
595
  }
596
  }
597
+
598
  } else {
599
  // We're not sure how we got here, but bail instead of processing anything else.
600
  return;
609
  }
610
  }
611
 
612
+ // Filter to add or remove urls to the array of purged urls
613
+ // @param array $purgeUrls the urls (paths) to be purged
614
+ // @param int $postId the id of the new/edited post
615
+ $this->purgeUrls = apply_filters( 'vhp_purge_urls', $this->purgeUrls, $postId );
616
  }
617
 
618
  }
632
  *
633
  * @since 4.0
634
  */
635
+ include_once( 'status.php' );
varnish-status.php DELETED
@@ -1,515 +0,0 @@
1
- <?php
2
- /**
3
- Copyright 2016-2017 Mika Epstein (email: ipstenu@halfelf.org)
4
-
5
- This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
-
7
- Varnish HTTP Purge is free software: you can redistribute it and/or modify
8
- it under the terms of the Apache License 2.0 license.
9
-
10
- Varnish HTTP Purge is distributed in the hope that it will be useful,
11
- but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
- */
14
-
15
- if ( !defined( 'ABSPATH' ) ) {
16
- die();
17
- }
18
-
19
- /**
20
- * Varnish Status Class
21
- *
22
- * @since 4.0
23
- */
24
-
25
- class VarnishStatus {
26
-
27
- /**
28
- * Construct
29
- * Fires when class is constructed, adds init hook
30
- *
31
- * @since 4.0
32
- */
33
- function __construct() {
34
- // Admin Init Hooks
35
- add_action( 'admin_init', array( &$this, 'admin_init' ) );
36
- // Admin Menu
37
- add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
38
- }
39
-
40
- /**
41
- * Admin init Callback
42
- *
43
- * @since 4.0
44
- */
45
- function admin_init() {
46
- $this->register_settings_url();
47
- if ( !is_multisite() ) $this->register_settings_ip();
48
- }
49
-
50
- /**
51
- * Admin Menu Callback
52
- *
53
- * @since 4.0
54
- */
55
- function admin_menu() {
56
- add_management_page( __( 'Is Varnish Working?', 'varnish-http-purge' ), __( 'Varnish Status', 'varnish-http-purge' ), 'manage_options', 'varnish-status', array( &$this, 'settings_page' ) );
57
- }
58
-
59
- /**
60
- * Register Admin Settings URL
61
- *
62
- * @since 4.0
63
- */
64
- function register_settings_url() {
65
- register_setting( 'varnish-http-purge-url', 'vhp_varnish_url', array( &$this, 'varnish_url_sanitize' ) );
66
- add_settings_section( 'varnish-url-settings-section', __( 'Check Varnish Status', 'varnish-http-purge' ), array( &$this, 'options_callback_url'), 'varnish-url-settings' );
67
- add_settings_field( 'varnish_url', __( 'Check A URL On Your Site:', 'varnish-http-purge' ), array( &$this, 'varnish_url_callback' ), 'varnish-url-settings', 'varnish-url-settings-section' );
68
- }
69
-
70
- /**
71
- * Register Admin Settings IP
72
- *
73
- * @since 4.0.2
74
- */
75
- function register_settings_ip() {
76
- register_setting( 'varnish-http-purge-ip', 'vhp_varnish_ip', array( &$this, 'varnish_ip_sanitize' ) );
77
- add_settings_section( 'varnish-ip-settings-section', __('Configure Custom Varnish IP', 'varnish-http-purge'), array( &$this, 'options_callback_ip'), 'varnish-ip-settings' );
78
- add_settings_field( 'varnish_ip', __('Set Varnish IP', 'varnish-http-purge'), array( &$this, 'varnish_ip_callback'), 'varnish-ip-settings', 'varnish-ip-settings-section' );
79
- }
80
-
81
- /**
82
- * Options Callback - IP Address
83
- *
84
- * @since 4.0
85
- */
86
- function options_callback_ip() {
87
- ?>
88
- <p><a name="#configure"></a><?php _e( 'The majority of users will never need to look down here. However there are cases when a custom Varnish IP Address will need to be set, in order to tell the plugin to empty the cache in a specific location. If you\'re using a CDN like Cloudflare or a Firewall Proxy like Sucuri, you will want to set this.', 'varnish-http-purge' ); ?></p>
89
- <p><?php _e( 'Your Varnish IP the IP address of the server where Varnish is installed. Your Varnish IP must be one of the IPs that Varnish is listening. If you use multiple IPs, or if you\'ve customized your ACLs, you\'ll need to pick one that doesn\'t conflict with your other settings. For example, if you have Varnish listening on a public and private IP, pick the private. On the other hand, if you told Varnish to listen on 0.0.0.0 (i.e. "listen on every interface you can") you would need to check what IP you set your purge ACL to allow (commonly 127.0.0.1 aka localhost), and use that (i.e. 127.0.0.1).', 'varnish-http-purge' ); ?></p>
90
- <p><?php _e( 'If your webhost set up Varnish for you, as is the case with DreamPress or WP Engine, ask them for the specifics if they don\'t have it documented. I\'ve listed the ones I know about here, however you should still check with them if you\'re not sure.', 'varnish-http-purge' ); ?></p>
91
- <p><strong><?php _e( 'If you aren\'t sure what to do, contact your webhost or server admin before making any changes.', 'varnish-http-purge' ); ?></strong></p>
92
-
93
- <ul>
94
- <li><?php _e( 'DreamHost - Go into the Panel and click on the DNS settings for the domain. The entry for <em>resolve-to.domain</em> (if set) will be your varnish server. If it\'s not set, then you don\'t need to worry about this at all. Example:', 'varnish-http-purge' ); ?> <code>resolve-to.www A 208.97.157.172</code></li>
95
- </ul>
96
- <?php
97
- }
98
-
99
- /**
100
- * Varnish IP Callback
101
- *
102
- * @since 4.0
103
- */
104
- function varnish_ip_callback() {
105
-
106
- $disabled = false;
107
-
108
- if ( VHP_VARNISH_IP != false ) {
109
- $disabled = true;
110
- $varniship = VHP_VARNISH_IP;
111
- } else {
112
- $varniship = get_option( 'vhp_varnish_ip' );
113
- }
114
-
115
- ?>
116
- <input type="text" id="vhp_varnish_ip" name="vhp_varnish_ip" value="<?php echo $varniship; ?>" size="25" <?php if ( $disabled == true ) { echo 'disabled'; } ?>/>
117
- <label for="vhp_varnish_ip">
118
- <?php
119
- if ( $disabled == true ) {
120
- _e( 'A Varnish IP has been defined in your wp-config, so it is not editable here.', 'varnish-http-purge' );
121
- } else {
122
- _e( 'Example:', 'varnish-http-purge' ); ?> <code>123.45.67.89</code><?php
123
- }
124
- ?>
125
- </label>
126
- <?php
127
- }
128
-
129
- /**
130
- * Options Callback - URL Scanner
131
- *
132
- * @since 4.0
133
- */
134
- function options_callback_url() {
135
-
136
- ?><p><?php _e( 'While it is impossible to detect all possible conflicts, this status page performs a check of the most common issues that prevent Varnish from caching your site properly.', 'varnish-http-purge' ); ?></p>
137
-
138
- <p><?php _e( 'This feature is provided to help you in debugging any conflicts on your own. If filing an issue with your webhost, it can be useful to include the output in your ticket.', 'varnish-http-purge' ); ?></p>
139
-
140
- <?php
141
- $icon_awesome = '<span class="dashicons dashicons-heart" style="color:#008000;"></span>';
142
- $icon_good = '<span class="dashicons dashicons-thumbs-up" style="color:#008000;"></span>';
143
- $icon_warning = '<span class="dashicons dashicons-warning" style="color:#FF9933"></span>';
144
- $icon_awkward = '<span class="dashicons dashicons-flag" style="color:#FF9933;">';
145
- $icon_bad = '<span class="dashicons dashicons-thumbs-down" style="color:#990000;"></span>';
146
-
147
- $url = esc_url( VarnishPurger::the_home_url() );
148
- $varnishurl = get_option( 'vhp_varnish_url', $url );
149
-
150
- $args = array(
151
- 'headers' => array(
152
- 'timeout' => 30,
153
- 'redirection' => 10,
154
- )
155
- );
156
-
157
- $response = wp_remote_get( $varnishurl, $args );
158
- $headers = wp_remote_retrieve_headers( $response );
159
- $preflight = true;
160
-
161
- // Basic checks that should stop a scan
162
- if( is_wp_error($response) ) {
163
- $preflight = false;
164
- $failure_to_launch = __( 'This request cannot be performed: ', 'varnish-http-purge' );
165
- $failure_to_launch .= $response->get_error_message();
166
- }
167
- if( wp_remote_retrieve_response_code($response) == '404' ) {
168
- $preflight = false;
169
- $failure_to_launch = __( 'This URL is a 404. Please check your typing and try again.', 'varnish-http-purge' );
170
- }
171
-
172
- // Get the IP address so we can check things later
173
- if ( isset( $headers['X-Forwarded-For'] ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) {
174
- $remote_ip = $headers['X-Forwarded-For'];
175
- } elseif ( isset( $headers['HTTP_X_FORWARDED_FOR'] ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 )
176
- ) {
177
- $remote_ip = $headers['HTTP_X_FORWARDED_FOR'];
178
- } elseif ( strpos( $headers['Server'] ,'cloudflare') !== false ) {
179
- $remote_ip = 'cloudflare';
180
- } else {
181
- $remote_ip = false;
182
- }
183
-
184
- // Get the Varnish IP
185
- if ( VHP_VARNISH_IP != false ) {
186
- $varniship = VHP_VARNISH_IP;
187
- } else {
188
- $varniship = get_option('vhp_varnish_ip');
189
- }
190
- ?>
191
-
192
- <h4><?php printf( __( 'Results for %s', 'varnish-http-purge' ), $varnishurl ); ?></h4>
193
-
194
- <table class="wp-list-table widefat fixed posts">
195
- <?php
196
-
197
- if ( $preflight == false ) {
198
- ?><tr>
199
- <td width="40px"><?php echo $icon_bad; ?></td>
200
- <td><?php echo $failure_to_launch; ?></td>
201
- </tr><?php
202
- } else {
203
- /* Pre Flight Checks */
204
-
205
- // VARNISH
206
- if ( isset( $headers['x-cacheable'] ) && strpos( $headers['x-cacheable'] ,'YES') !== false ) {
207
- ?><tr>
208
- <td width="40px"><?php echo $icon_good; ?></td>
209
- <td><?php _e( 'Varnish is running properly and caching is happening.', 'varnish-http-purge' ); ?></td>
210
- </tr><?php
211
- } elseif (isset( $headers['x-cacheable'] ) && strpos( $headers['x-cacheable'] ,'NO') !== false ) {
212
- ?><tr>
213
- <td width="40px"><?php echo $icon_bad; ?></td>
214
- <td><?php _e( 'Varnish is running but is unable to cache your site.', 'varnish-http-purge' ); ?></td>
215
- </tr><?php
216
- } else {
217
- ?><tr>
218
- <td width="40px"><?php echo $icon_warning; ?></td>
219
- <td><?php _e( 'We cannot find Varnish active for this domain.', 'varnish-http-purge' ); ?></td>
220
- </tr><?php
221
- }
222
-
223
- // Remote IP (i.e. Yo Dawg, I heard you like proxies
224
- ?><tr><?php
225
- if ( $remote_ip == false && !empty( $varniship) ) {
226
- ?>
227
- <td width="40px"><?php echo $icon_warning; ?></td>
228
- <td><?php printf( __( 'You have a Varnish IP set but a proxy like Cloudflare or Sucuri has not been detected. This is mostly harmless, but if you have issues with your cache not emptying when you make a post, you may need to <a href="%s">erase your Varnish IP</a>. Please check with your webhost or server admin before doing so.', 'varnish-http-purge' ), '#configure' ); ?></td>
229
- <?php
230
- } elseif ( $remote_ip !== false && $remote_ip !== $varniship ) {
231
- ?>
232
- <td width="40px"><?php echo $icon_warning; ?></td>
233
- <td><?php printf( __( 'You\'re using a Custom Varnish IP that doesn\'t appear to match your server IP address. If you\'re using multiple Varnish servers or IPv6, this is fine. Please make sure you\'ve <a href="%s">properly configured it</a> according to your webhost\'s specifications.', 'varnish-http-purge' ), '#configure' ); ?></td>
234
- <?php
235
- } else {
236
- ?>
237
- <td width="40px"><?php echo $icon_good; ?></td>
238
- <td><?php printf( __( 'Your server IP setup looks good. If you are using a proxy (like Sucuri or Cloudflare or Fastly) please double check that configuration and make sure to add a <a href="%s">custom Varnish IP</a> if necessary.', 'varnish-http-purge' ), '#configure' ); ?></td>
239
- <?php
240
- }
241
- ?></tr><?php
242
-
243
- // Cloudflare
244
- if ( isset( $headers['Server'] ) && strpos( $headers['Server'] ,'cloudflare') !== false ) {
245
- ?><tr>
246
- <td><?php echo $icon_warning; ?></td>
247
- <td><?php printf( __( 'CloudFlare has been detected. While this is generally fine, you may experience some cache oddities. Make sure you <a href="%s">configure WordPress for Cloudflare</a>.', 'varnish-http-purge' ), '#configure' ); ?></td>
248
- </tr><?php
249
- }
250
-
251
- // HHVM
252
- if ( isset( $headers['X-Powered-By'] ) ) {
253
- if ( strpos( $headers['X-Powered-By'], 'HHVM' ) !== false ) {
254
- ?><tr>
255
- <td><?php echo $icon_good; ?></td>
256
- <td><?php _e( 'You are running HHVM instead of PHP. While that works fine, you should consider PHP 7. WordPress will be ceasing support for HHVM in 2018.', 'varnish-http-purge' ); ?></td>
257
- </tr><?php
258
- }
259
- }
260
-
261
- // GZIP
262
- if ( isset( $headers['Content-Encoding'] ) ) {
263
- // Regular gZIP
264
- if( strpos( $headers['Content-Encoding'] ,'gzip') !== false || ( isset( $headers['Vary'] ) && strpos( $headers['Vary'] ,'gzip' ) !== false ) ) {
265
- ?><tr>
266
- <td><?php echo $icon_good; ?></td>
267
- <td><?php _e( 'Your site is compressing content and making the internet faster.', 'varnish-http-purge' ); ?></td>
268
- </tr><?php
269
- }
270
-
271
- // Fastly
272
- if ( strpos( $headers['Content-Encoding'] ,'Fastly') !== false ) {
273
- ?><tr>
274
- <td><?php echo $icon_good; ?></td>
275
- <td><?php printf( __( '<a href="%s">Fastly</a> is speeding up your site. Keep in mind, it may cache your CSS and images longer than Varnish does. Remember to empty all caches.', 'varnish-http-purge' ), esc_url( 'https://fastly.com' ) ); ?></td>
276
- </tr><?php
277
- }
278
- }
279
-
280
- /* Things that breaks Varnish */
281
-
282
- // SET COOKIE
283
- if ( isset( $headers['Set-Cookie'] ) ) {
284
-
285
- // If cookies are an array, scan the whole thing. Otherwise, we can use strpos.
286
- if ( is_array( $headers['Set-Cookie'] ) ) {
287
- $cookie_phpsessid = in_array( 'PHPSESSID', $headers['Set-Cookie'], true );
288
- $cookie_edd_wp_session = in_array( 'edd_wp_session', $headers['Set-Cookie'], true );
289
- $cookie_edd_items_in_cart = in_array( 'edd_items_in_cart', $headers['Set-Cookie'], true );
290
- $cookie_wfvt = in_array( 'wfvt_', $headers['Set-Cookie'], true );
291
- $cookie_invite_anyone = in_array( 'invite-anyone', $headers['Set-Cookie'], true );
292
- } else {
293
- $cookie_phpsessid = strpos( $headers['Set-Cookie'] , 'PHPSESSID');
294
- $cookie_edd_wp_session = strpos( $headers['Set-Cookie'], 'edd_wp_session' );
295
- $cookie_edd_items_in_cart = strpos( $headers['Set-Cookie'], 'edd_items_in_cart' );
296
- $cookie_wfvt = strpos( $headers['Set-Cookie'], 'wfvt_' );
297
- $cookie_invite_anyone = strpos( $headers['Set-Cookie'], 'invite-anyone' );
298
- }
299
-
300
- if ( $cookie_phpsessid !== false ) {
301
- ?><tr>
302
- <td><?php echo $icon_bad; ?></td>
303
- <td><?php _e( 'A plugin or theme is setting a PHPSESSID cookie on every pageload. This makes Varnish not deliver cached pages.', 'varnish-http-purge' ); ?></td>
304
- </tr><?php
305
- }
306
- if ( $cookie_edd_wp_session !== false ) {
307
- ?><tr>
308
- <td><?php echo $icon_bad; ?></td>
309
- <td><?php printf( __( '<a href="%s">Easy Digital Downloads</a> is being used with cookie sessions. This may cause your cache to misbehave. If you have issues, please set <code>define( "EDD_USE_PHP_SESSIONS", true );</code> in your <code>wp-config.php</code> file.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/easy-digital-downloads/') ); ?></td>
310
- </tr><?php
311
- }
312
- if ( $cookie_edd_items_in_cart !== false ) {
313
- ?><tr>
314
- <td><?php echo $icon_warning; ?></td>
315
- <td><?php printf( __( '<a href="%s">Easy Digital Downloads</a> is putting down a shopping cart cookie on every page load. Make sure Varnish is set up to ignore that when it\'s empty.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/easy-digital-downloads/') ); ?></td>
316
- </tr><?php
317
- }
318
- if ( $cookie_wfvt !== false ) {
319
- ?><tr>
320
- <td><?php echo $icon_bad; ?></td>
321
- <td><?php printf( __( '<a href="%s">Wordfence</a> is putting down cookies on every page load. Please disable that in your options (available from version 4.0.4 and up).', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/wordfence/') ); ?></td>
322
- </tr><?php
323
- }
324
- if ( $cookie_invite_anyone !== false ) {
325
- ?><tr>
326
- <td><?php echo $icon_bad; ?></td>
327
- <td><?php printf( __( '<a href="%s">Invite Anyone</a>, a plugin for BuddyPress, is putting down a cookie on every page load. This will prevent Varnish from caching.', 'varnish-http-purge' ), esc_url('https://wordpress.org/plugins/invite-anyone/') ); ?></td>
328
- </tr><?php
329
- }
330
- }
331
-
332
- // AGE
333
- if( !isset($headers['Age']) ) {
334
- ?><tr>
335
- <td><?php echo $icon_bad; ?></td>
336
- <td><?php _e( 'Your domain does not report an "Age" header, which means we can\'t tell if the page is actually serving from cache.', 'varnish-http-purge' ); ?></td>
337
- </tr><?php
338
- } elseif( $headers['Age'] <= 0 || $headers['Age'] == 0 ) {
339
- if( !isset($headers['Cache-Control']) || strpos($headers['Cache-Control'], 'max-age') === FALSE ) {
340
- ?><tr>
341
- <td><?php echo $icon_warning; ?></td>
342
- <td><?php _e( 'The "Age" header is set to less than 1, which means you checked right when Varnish cleared the cache for that url or Varnish is not actually serving the content for that url from cache. Check again (refresh the page) but if it happens again, it could be one of the following reasons:', 'varnish-http-purge' ); ?>
343
- <ul style=\"text-align: left;\">
344
- <li><?php _e( 'That url is excluded from the cache on purpose in the Varnish vcl file (in which case everything is working.)', 'varnish-http-purge' ); ?></li>
345
- <li><?php _e( 'A theme or plugin is sending cache headers that are telling Varnish not to serve that content from cache. This means you will have to fix the cache headers the application is sending to Varnish. A lot of the time those headers are Cache-Control and/or Expires.', 'varnish-http-purge' ); ?></li>
346
- <li><?php _e( 'A theme or plugin is setting a session cookie, which can prevent Varnish from serving content from cache. You need to make it not send a session cookie for anonymous traffic. ', 'varnish-http-purge' ); ?></li>
347
- </ul>
348
- </td>
349
- </tr><?php
350
- }
351
- }
352
-
353
- // CACHE-CONTROL
354
- if ( isset( $headers['Cache-Control'] ) && strpos( $headers['Cache-Control'] ,'no-cache') !== false ) {
355
- ?><tr>
356
- <td><?php echo $icon_bad; ?></td>
357
- <td><?php _e( 'Something is setting the header Cache-Control to "no-cache" which means visitors will never get cached pages.', 'varnish-http-purge' ); ?></td>
358
- </tr><?php
359
- }
360
-
361
- // MAX AGE
362
- if ( isset( $headers['Cache-Control'] ) && strpos( $headers['Cache-Control'] ,'max-age=0') !== false ) {
363
- ?><tr>
364
- <td><?php echo $icon_bad; ?></td>
365
- <td><?php _e( 'Something is setting the header Cache-Control to "max-age=0" which means a page can be no older than 0 seconds before it needs to regenerate the cache.', 'varnish-http-purge' ); ?></td>
366
- </tr><?php
367
- }
368
-
369
- // PRAGMA
370
- if ( isset( $headers['Pragma'] ) && strpos( $headers['Pragma'] ,'no-cache') !== false ) {
371
- ?><tr>
372
- <td><?php echo $icon_bad; ?></td>
373
- <td><?php _e( 'Something is setting the header Pragma to "no-cache" which means visitors will never get cached pages.', 'varnish-http-purge' ); ?></td>
374
- </tr><?php
375
- }
376
-
377
- // X-CACHE (we're not running this)
378
- if ( isset( $headers['X-Cache-Status'] ) && strpos( $headers['X-Cache-Status'] ,'MISS') !== false ) {
379
- ?><tr>
380
- <td><?php echo $icon_bad; ?></td>
381
- <td><?php _e( 'X-Cache missed, which means it was not able to serve this page as cached.', 'varnish-http-purge' ); ?></td>
382
- </tr><?php
383
- }
384
-
385
- /* Server features */
386
-
387
- // PAGESPEED
388
- if ( isset( $headers['X-Mod-Pagespeed'] ) ) {
389
- if ( strpos( $headers['X-Cacheable'] , 'YES:Forced') !== false ) {
390
- ?><tr>
391
- <td><?php echo $icon_good; ?></td>
392
- <td><?php _e( 'Mod Pagespeed is active and working properly with Varnish.', 'varnish-http-purge' ); ?></td>
393
- </tr><?php
394
- } else {
395
- ?><tr>
396
- <td><?php echo $icon_bad; ?></td>
397
- <td><?php _e( 'Mod Pagespeed is active but it looks like your caching headers may not be right. This may be a false negative if other parts of your site are overwriting headers. Fix all other errors listed, then come back to this. If you are still having errors, you will need to look into using htaccess or nginx to override the Pagespeed headers.', 'varnish-http-purge' ); ?></td>
398
- </tr><?php
399
- }
400
- }
401
- }
402
- ?>
403
- </table>
404
- <?php
405
- }
406
-
407
- /**
408
- * Varnish URL Callback
409
- *
410
- * @since 4.0
411
- */
412
- function varnish_url_callback() {
413
- $url = esc_url( VarnishPurger::the_home_url() );
414
- $varnishurl = get_option( 'vhp_varnish_url', $url );
415
- ?><input type="text" id="vhp_varnish_url" name="vhp_varnish_url" value="<?php echo $varnishurl; ?>" size="50" /><?php
416
- }
417
-
418
- /*
419
- * Call settings page
420
- *
421
- * @since 4.0
422
- */
423
- function settings_page() {
424
- ?>
425
- <div class="wrap">
426
-
427
- <h1><?php _e( 'Is Varnish Working?', 'varnish-http-purge' ); ?></h1>
428
-
429
- <?php settings_errors(); ?>
430
-
431
- <form action="options.php" method="POST" ><?php
432
- settings_fields( 'varnish-http-purge-url' );
433
- do_settings_sections( 'varnish-url-settings' );
434
- submit_button( 'Check URL', 'primary');
435
- ?></form>
436
-
437
- <form action="options.php" method="POST" ><?php
438
- // Only available if _not_ multisite
439
- if ( !is_multisite() ) {
440
- settings_fields( 'varnish-http-purge-ip' );
441
- do_settings_sections( 'varnish-ip-settings' );
442
- submit_button( 'Save IP', 'secondary');
443
- }
444
- ?></form>
445
-
446
- </div>
447
- <?php
448
- }
449
-
450
- /**
451
- * Sanitization and validation for IP
452
- *
453
- * @param $input the input to be sanitized
454
- * @since 4.0
455
- */
456
- function varnish_ip_sanitize( $input ) {
457
-
458
- $output = '';
459
- $set_message = 'You have entered an invalid IP address.';
460
- $set_type = 'error';
461
-
462
- if ( empty($input) ) {
463
- return; // do nothing
464
- } elseif ( filter_var( $input, FILTER_VALIDATE_IP) ) {
465
- $set_message = 'IP Updated.';
466
- $set_type = 'updated';
467
- $output = filter_var( $input, FILTER_VALIDATE_IP);
468
- }
469
-
470
- add_settings_error( 'vhp_varnish_url', 'varnish-url', $set_message, $set_type );
471
- return $output;
472
- }
473
-
474
- /**
475
- * Sanitization and validation for URL
476
- *
477
- * @param $input the input to be sanitized
478
- * @since 4.0
479
- */
480
- function varnish_url_sanitize( $input ) {
481
-
482
- $baseurl_host = parse_url( esc_url( VarnishPurger::the_home_url() ), PHP_URL_HOST );
483
- $output = esc_url( VarnishPurger::the_home_url() );
484
- $set_type = 'error';
485
-
486
- if ( !empty($input) ) {
487
- $parsed_input = parse_url($input);
488
- if ( empty($parsed_input['scheme']) ) {
489
- $schema_input = 'http://';
490
- if ( is_ssl() ) {
491
- $schema_input = 'https://';
492
- }
493
- $input = $schema_input . ltrim($input, '/');
494
- }
495
- }
496
-
497
- if ( empty($input) ) {
498
- $set_message = 'You must enter a URL from your own domain to scan.';
499
- } elseif ( !filter_var( $input, FILTER_VALIDATE_URL) ) {
500
- $set_message = 'You have entered an invalid URL address.';
501
- } elseif ( $baseurl_host !== parse_url( $input, PHP_URL_HOST ) ) {
502
- $set_message = 'You cannot scan URLs on other domains.';
503
- } else {
504
- $output = filter_var( $input, FILTER_VALIDATE_URL);
505
- $set_message = 'Scanning New URL...';
506
- $set_type = 'updated';
507
- }
508
-
509
- add_settings_error( 'vhp_varnish_url', 'varnish-url', $set_message, $set_type );
510
- return $output;
511
- }
512
-
513
- }
514
-
515
- $status = new VarnishStatus();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
wp-cli.php CHANGED
@@ -1,6 +1,6 @@
1
  <?php
2
  /**
3
- Copyright 2015-2017 Mika Epstein (email: ipstenu@halfelf.org)
4
 
5
  This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
 
@@ -12,87 +12,164 @@
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
  */
14
 
15
- if ( !defined( 'ABSPATH' ) ) {
16
- die();
17
- }
18
 
19
  // Bail if WP-CLI is not present
20
  if ( !defined( 'WP_CLI' ) ) return;
21
 
22
  /**
23
- * Purges Varnish Cache
24
  */
25
- class WP_CLI_Varnish_Purge_Command extends WP_CLI_Command {
26
-
27
- private $wildcard = false;
28
-
29
- public function __construct() {
30
- $this->varnish_purge = new VarnishPurger();
31
- }
32
 
33
- /**
34
- * Forces a full Varnish Purge of the entire site (provided
35
- * regex is supported).
36
- *
37
- * ## EXAMPLES
38
- *
39
- * wp varnish purge
40
- *
41
- * wp varnish purge http://example.com/wp-content/themes/twentyeleventy/style.css
42
- *
43
- * wp varnish purge "/wp-content/themes/twentysixty/style.css"
44
- *
45
- * wp varnish purge http://example.com/wp-content/themes/ --wildcard
46
- *
47
- * wp varnish purge "/wp-content/themes/" --wildcard
48
- *
49
- */
50
-
51
- function purge( $args , $assoc_args ) {
52
 
53
- $wp_version = get_bloginfo( 'version' );
54
- $cli_version = WP_CLI_VERSION;
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- // Set the URL/path
57
- if ( !empty($args) ) { list( $url ) = $args; }
58
-
59
- // If wildcard is set, or the URL argument is empty
60
- // then treat this as a full purge
61
- $pregex = $wild = '';
62
- if ( isset( $assoc_args['wildcard'] ) || empty($url) ) {
63
- $pregex = '/?vhp-regex';
64
- $wild = ".*";
65
- }
66
-
67
- wp_create_nonce('vhp-flush-cli');
68
-
69
- // If the URL is not empty, sanitize. Else use home URL.
70
- if ( !empty( $url ) ) {
71
- $url = esc_url( $url );
72
 
73
- // If it's a regex, let's make sure we don't have //
74
- if ( isset( $assoc_args['wildcard'] ) ) $url = rtrim( $url, '/' );
75
- } else {
76
- $url = $this->varnish_purge->the_home_url();
77
- }
78
-
79
- if ( version_compare( $wp_version, '4.6', '>=' ) && ( version_compare( $cli_version, '0.25.0', '<' ) || version_compare( $cli_version, '0.25.0-alpha', 'eq' ) ) ) {
80
 
81
- WP_CLI::log( sprintf( 'This plugin does not work on WP 4.6 and up, unless WP-CLI is version 0.25.0 or greater. You\'re using WP-CLI %s and WordPress %s.', $cli_version, $wp_version ) );
82
- WP_CLI::log( 'To flush your cache, please run the following command:');
83
- WP_CLI::log( sprintf( '$ curl -X PURGE "%s"' , $url . $wild ) );
84
- WP_CLI::error( 'Varnish Cache must be purged manually.' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  }
86
-
87
- $this->varnish_purge->purgeUrl( $url . $pregex );
 
 
 
 
 
 
 
 
 
 
88
 
89
- if ( WP_DEBUG == true ) {
90
- WP_CLI::log( sprintf( 'Flushing URL: %s with params: %s.', $url, $pregex ) );
91
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
- WP_CLI::success( 'The Varnish cache was purged.' );
94
- }
95
 
 
 
 
 
 
 
96
  }
97
 
98
- WP_CLI::add_command( 'varnish', 'WP_CLI_Varnish_Purge_Command' );
1
  <?php
2
  /**
3
+ Copyright 2015-2018 Mika Epstein (email: ipstenu@halfelf.org)
4
 
5
  This file is part of Varnish HTTP Purge, a plugin for WordPress.
6
 
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
  */
14
 
15
+ if ( !defined( 'ABSPATH' ) ) die();
 
 
16
 
17
  // Bail if WP-CLI is not present
18
  if ( !defined( 'WP_CLI' ) ) return;
19
 
20
  /**
21
+ * WP-CLI Commands
22
  */
23
+
24
+ if ( !class_exists( 'WP_CLI_Varnish_Command' ) ) {
25
+ class WP_CLI_Varnish_Command extends WP_CLI_Command {
26
+
27
+ private $wildcard = false;
 
 
28
 
29
+ public function __construct() {
30
+ $this->varnish_purge = new VarnishPurger();
31
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
+ /**
34
+ * Forces a full Varnish Purge of the entire site (provided
35
+ * regex is supported). Alternately you can fluxh the cache
36
+ * for specific pages or folders (using the --wildcard param)
37
+ *
38
+ * ## EXAMPLES
39
+ *
40
+ * wp varnish purge
41
+ *
42
+ * wp varnish purge http://example.com/wp-content/themes/twentyeleventy/style.css
43
+ *
44
+ * wp varnish purge http://example.com/wp-content/themes/ --wildcard
45
+ *
46
+ */
47
 
48
+ function purge( $args, $assoc_args ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ $wp_version = get_bloginfo( 'version' );
51
+ $cli_version = WP_CLI_VERSION;
 
 
 
 
 
52
 
53
+ // Set the URL/path
54
+ if ( !empty($args) ) { list( $url ) = $args; }
55
+
56
+ // If wildcard is set, or the URL argument is empty
57
+ // then treat this as a full purge
58
+ $pregex = $wild = '';
59
+ if ( isset( $assoc_args['wildcard'] ) || empty($url) ) {
60
+ $pregex = '/?vhp-regex';
61
+ $wild = ".*";
62
+ }
63
+
64
+ wp_create_nonce('vhp-flush-cli');
65
+
66
+ // If the URL is not empty, sanitize. Else use home URL.
67
+ if ( !empty( $url ) ) {
68
+ $url = esc_url( $url );
69
+
70
+ // If it's a regex, let's make sure we don't have //
71
+ if ( isset( $assoc_args['wildcard'] ) ) $url = rtrim( $url, '/' );
72
+ } else {
73
+ $url = $this->varnish_purge->the_home_url();
74
+ }
75
+
76
+ if ( version_compare( $wp_version, '4.6', '>=' ) && ( version_compare( $cli_version, '0.25.0', '<' ) || version_compare( $cli_version, '0.25.0-alpha', 'eq' ) ) ) {
77
+
78
+ WP_CLI::log( sprintf( 'This plugin does not work on WP 4.6 and up, unless WP-CLI is version 0.25.0 or greater. You\'re using WP-CLI %s and WordPress %s.', $cli_version, $wp_version ) );
79
+ WP_CLI::log( 'To flush your cache, please run the following command:');
80
+ WP_CLI::log( sprintf( '$ curl -X PURGE "%s"' , $url . $wild ) );
81
+ WP_CLI::error( 'Varnish Cache must be purged manually.' );
82
+ }
83
+
84
+ $this->varnish_purge->purgeUrl( $url . $pregex );
85
+
86
+ if ( WP_DEBUG == true ) {
87
+ WP_CLI::log( sprintf( 'Flushing URL: %s with params: %s.', $url, $pregex ) );
88
+ }
89
+
90
+ WP_CLI::success( 'The Varnish cache was purged.' );
91
  }
92
+
93
+ /**
94
+ * Runs a debug check of the site to see if there are any known
95
+ * issues that would stop Varnish from caching.
96
+ *
97
+ * ## EXAMPLES
98
+ *
99
+ * wp varnish debug
100
+ *
101
+ * wp varnish debug http://example.com/wp-content/themes/twentyeleventy/style.css
102
+ *
103
+ */
104
 
105
+ function debug( $args, $assoc_args ) {
106
+
107
+ // Set the URL/path
108
+ if ( !empty($args) ) list( $url ) = $args;
109
+
110
+ $default_url = esc_url( $this->varnish_purge->the_home_url() );
111
+
112
+ if ( !empty( $url ) ) {
113
+ $parsed_input = parse_url($url);
114
+ if ( empty($parsed_input['scheme']) ) {
115
+ $schema_input = 'http://';
116
+ if ( is_ssl() ) $schema_input = 'https://';
117
+ $url = $schema_input . ltrim($url, '/');
118
+ }
119
+ } else {
120
+ $url = $default_url;
121
+ }
122
+
123
+ if ( empty( $url ) || parse_url( $default_url, PHP_URL_HOST ) !== parse_url( $url, PHP_URL_HOST ) ) {
124
+ WP_CLI::error( __( 'You must enter a URL from your own domain to scan.', 'varnish-http-purge' ) );
125
+ } elseif ( !filter_var( $url, FILTER_VALIDATE_URL) ) {
126
+ WP_CLI::error( __( 'You have entered an invalid URL address.', 'varnish-http-purge' ) );
127
+ } else {
128
+
129
+ // Include the debug code
130
+ include( 'debug.php' );
131
+
132
+ $varnishurl = get_option( 'vhp_varnish_url', $url );
133
+
134
+ // Get the response and headers
135
+ $remote_get = VarnishDebug::remote_get( $varnishurl );
136
+ $headers = wp_remote_retrieve_headers( $remote_get );
137
+
138
+ // Preflight checklist
139
+ $preflight = VarnishDebug::preflight( $remote_get );
140
+
141
+ // Check for Remote IP
142
+ $remote_ip = VarnishDebug::remote_ip( $headers );
143
+
144
+ // Get the Varnish IP
145
+ if ( VHP_VARNISH_IP != false ) {
146
+ $varniship = VHP_VARNISH_IP;
147
+ } else {
148
+ $varniship = get_option('vhp_varnish_ip');
149
+ }
150
+
151
+ if ( $preflight['preflight'] == false ) {
152
+ WP_CLI::error( $preflight['message'] );
153
+ } else {
154
+ $results = VarnishDebug::get_all_the_results( $headers, $remote_ip, $varniship );
155
+
156
+ // Generate array
157
+ foreach ( $results as $type => $content ) {
158
+ $items[] = array(
159
+ 'name' => $type,
160
+ 'status' => ucwords( $content['icon'] ),
161
+ 'message' => $content['message'],
162
+ );
163
+ }
164
 
165
+ $format = ( isset( $assoc_args['format'] ) )? $assoc_args['format'] : 'table';
 
166
 
167
+ // Output the data
168
+ WP_CLI\Utils\format_items( $format, $items, array( 'name', 'status', 'message' ) );
169
+ }
170
+ }
171
+ }
172
+ }
173
  }
174
 
175
+ WP_CLI::add_command( 'varnish', 'WP_CLI_Varnish_Command' );