Varnish HTTP Purge - Version 4.1

Version Description

  • JSON / REST API Support
  • Fix for Varnish Status Page on MAMP (props @jeremyclarke)
  • Filter for purge headers (props @ocean90)
  • Disallow people from editing the Varnish IP on Multisite
  • Drop support for pre 4.7 because of JSON / REST API
  • Support flushing cache for private pages

=

Download this release

Release Info

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

Code changes from version 4.0.3 to 4.1

Files changed (4) hide show
  1. changelog.txt +28 -0
  2. readme.txt +56 -62
  3. varnish-http-purge.php +183 -111
  4. varnish-status.php +36 -24
changelog.txt CHANGED
@@ -1,3 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  = 3.9.3 =
2
  * Update Documentation and Readme
3
 
1
+ = 4.0.3 =
2
+ * Better explanation when Cloudflare gets in the way of DNS
3
+ * Rename flush button for people who don't speak Varnish
4
+
5
+ = 4.0.2 =
6
+ * Support for WP-CLI commands and PHP > 5.5 (though please upgrade, props @robertpeake)
7
+ * Better information regarding Cloudflare
8
+ * Better failure on domains for scanner
9
+ * Better IP detection
10
+ * Break apart settings for faster saving
11
+
12
+ = 4.0.1 =
13
+ * Fix typo (on -> one)
14
+ * Correct permissions on Multisite (props @phh - resolves #27 #28)
15
+ * Correct weird merge error (props @phh - resolves #25 #26)
16
+ * Fix formatting in Changelog
17
+
18
+ = 4.0 =
19
+ * Added Varnish Status Page - Tools -> Varnish Status (includes basic scanning etc)
20
+ * Allow filter for `home_url()`
21
+ * Update readme with list of filters.
22
+ * Added wp-cli commands to flush specific URLs and wildcards
23
+ * Requires wp-cli 0.25+ to work [3315](https://github.com/wp-cli/wp-cli/issues/3315) for WP 4.6+
24
+ * Update `purgePost()` to validate page_for_posts ([Props JeremyClarke](https://github.com/Ipstenu/varnish-http-purge/pull/20))
25
+ * Add check for AMP ([Props JeremyClarke](https://wordpress.org/support/topic/varnish-http-purge-doesnt-update-amp-urls-on-post-update/))
26
+ * Purge 'default' AMP URL as well
27
+ * Cleanup on Uninstall
28
+
29
  = 3.9.3 =
30
  * Update Documentation and Readme
31
 
readme.txt CHANGED
@@ -1,24 +1,25 @@
1
  === Varnish HTTP Purge ===
2
  Contributors: Ipstenu, mikeschroder, techpriester
3
  Tags: varnish, purge, cache
4
- Requires at least: 4.0
5
- Tested up to: 4.7
6
- Stable tag: 4.0.3
7
 
8
- Automatically purge Varnish Cache when content on your site is modified.
9
 
10
  == Description ==
11
- Varnish HTTP Purge sends a PURGE request to the URL of a page or post every time it it modified. This occurs when editing, publishing, commenting or deleting an item, and when changing themes.
12
 
13
- <a href="https://www.varnish-cache.org/">Varnish</a> is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. This plugin <em>does not</em> install Varnish for you, nor does it configure Varnish for WordPress. It's expected you already did that on your own.
14
 
15
- Not all pages are purged every time, depending on your Varnish configuration. When a post, page, or custom post type is edited, or a new comment is added, <em>only</em> the following pages will purge:
 
 
16
 
17
  * The front page
18
  * The post/page edited
19
  * Any categories or tags associated with the page
20
 
21
- In addition, your entire cache will be purged on the following actions:
22
 
23
  * Changing themes
24
  * Press the 'Purge Varnish Cache' button on the dashboard
@@ -28,14 +29,15 @@ Plugins can hook into the purge actions as well, to filter their own events to t
28
 
29
  And if you're into WP-CLI, you can use that too: `wp varnish purge`
30
 
31
- Please note: On a multisite network using subfolders, only <strong>network admins</strong> can purge the main site.
32
 
33
  == Installation ==
34
  No WordPress configuration needed.
35
 
36
- When used on Multisite, the plugin is Network Activatable Only.
37
 
38
  = Requirements =
 
39
  * Pretty Permalinks enabled
40
  * Varnish 3.x or higher
41
 
@@ -47,51 +49,57 @@ If you have code patches, [pull requests are welcome](https://github.com/Ipstenu
47
 
48
  = How can I tell everything's working? =
49
 
50
- This can be difficult, since it's a silent plugin. That is you turn it on and walk away. The easiest way to tell would be view a page as a non-logged-in user. I recommend a private browsing window. Then, as a logged in user in another window, edit an existing post (make a small change). Refresh the view in your private window. If the change is there, then everything's working!
51
 
52
  = Does every WordPress plugin and theme work with Varnish? =
53
 
54
- No. Some of them have behavior that causes Varnish not to cache. While debugging that is outside the scope of this plugin, 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.
55
 
56
  = How can I debug my site? =
57
 
58
- 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.
 
 
 
 
59
 
60
  = What version of Varnish is supported? =
61
 
62
  This was built and tested on Varnish 3.x. While it is reported to work on 2.x and 4.x, it is only supported on v3 at this time.
63
 
64
- = Why doesn't every page flush when I make a new post? =
65
 
66
- The only pages that should purge are the post's page, the front page, categories, and tags. The reason why is a little philosophical.
67
 
68
- When building out this plugin, there were a couple pathways on how best to handle purging caches and they boiled down to two: Decisions (the plugin purges what it purges when it purges) and Options (you decide what to purge, when and why). It's entirely possible to make this plugin purge everything, every time a 'trigger' happens, have it purge some things, or have it be so you can pick that purges.
69
 
70
- In the interests of design, we decided that the KISS principle was key. Since you can configure your Varnish to always purge all pages recursively (i.e. purging http://example.com/ would purge all pages below it), if that's a requirement, you can set it yourself. There are also other Varnish plugins that allow for more granular control (including W3 Total Cache), however this plugin will not be gaining a whole bunch of options to handle that.
71
 
72
- = Why doesn't my cache purge when I edit my theme? =
73
 
74
- Because the plugin only purges your <em>content</em> when you edit it. That means if you edit a page/post, or someone leaves a comment, it'll change. Otherwise, you have to purge the whole cache. The plugin will do this for you if you activate a new theme, but not when you edit your current theme's files.
75
 
76
- If you use Jetpack's CSS editor, it will purge the whole cache for your site on save.
77
 
78
- = How do I manually purge the whole cache? =
79
 
80
- Click the 'Purge Varnish Cache' button on the "Right Now" Dashboard (see the screenshot if you can't find it).
81
 
82
- There's also a "Purge Varnish" button on the admin toolbar.
 
 
83
 
84
  = I don't see a button! =
85
 
86
- If you're on a Multisite Network and you're on the primary site in the network, only the <em>network</em> admins can purge that site
87
 
88
- On a subfolder network if you flush the site at `example.com`, then everything under that (like `example.com/site1` and `example.com/siten` and everything else) would also get flushed. That means that a purge on the main site purges the entire network.
89
 
90
- In order to mitigate the destructive nature of that power, only the network admins can purge everything on the main site of a subfolder network.
91
 
92
  = Why is nothing caching when I use PageSpeed? =
93
 
94
- PageSpeed likes to put in Caching headers to say <em>not</em> to cache. To fix this, you need to put this in your .htaccess section for PageSpeed: `ModPagespeedModifyCachingHeaders off`
95
 
96
  If you're using nginx, it's `pagespeed ModifyCachingHeaders off;`
97
 
@@ -101,7 +109,7 @@ Yes, but you'll need to make some additional changes (see "Why aren't my changes
101
 
102
  = Why aren't my changes showing when I use CloudFlare or another proxy? =
103
 
104
- When you use CloudFlare or any other similar service, you've got a proxy in front of the Varnish proxy. In general this isn't a bad thing. The problem arises when the DNS shenanigans send the purge request to your domain name. When you've got an additional proxy like CloudFlare, you don't want the request to go to the proxy, you want it to go to Varnish server.
105
 
106
  On single-site, you can edit this via the Tools -> Varnish Status page. On Multisite, you'll need to add the following to your wp-config.php file:
107
 
@@ -124,7 +132,7 @@ Your Varnish IP must be one of the IPs that Varnish is listening on. If you use
124
  If your webhost set up Varnish for you, you may need to 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.
125
 
126
  <ul>
127
- <li><strong>DreamHost</strong> - If you're using DreamPress and Cloudflare, go into the Panel and click on the DNS settings for the domain. The entry for <em>resolve-to.domain</em> is your varnish server: `resolve-to.www A 208.97.157.172` -- If you're <em>NOT</em> using Cloudflare, you don't need it, but it's just your normal IP.</li>
128
  </ul>
129
 
130
  = What if I have multiple varnish IPs? =
@@ -133,17 +141,17 @@ Multiple IPs are not supported at this time.
133
 
134
  I have a major issue with writing code I don't use, which means that since I'm only using one IP right now, I don't want to be on the ball for supporting multiple IPs. I don't even have a place to test it, which is just insane to attempt to code if you think about it. Yes, I could accept pull requests, but that means everyone's at some other person's discretion. So no, I won't be doing that at this time.
135
 
136
- = Why don't my gzip'd pages flush? =
137
 
138
  Make sure your Varnish VCL is configured correctly to purge all the right pages. This is normally an issue with Varnish 2, which is not supported by this plugin.
139
 
140
- = Why isn't the whole cache purge working? =
141
 
142
  The plugin sends a PURGE command of <code>/.*</code> and `X-Purge-Method` in the header with a value of regex. If your Varnish server doesn't doesn't understand the wildcard, you can configure it to check for the header.
143
 
144
- = How can I debug? =
145
 
146
- If `WP_DEBUG` is on and you're not seeing any errors, you'll have to jump into the command line.
147
 
148
  To see every request made to varnish, use this:
149
  `varnishncsa -F "%m %U"`
@@ -189,11 +197,13 @@ All of these VCLs work with this plugin.
189
 
190
  Yes!
191
 
 
192
  * `vhp_home_url` - Change the home URL (default is `home_url()`)
193
  * `vhp_purge_urls` - Add additional URLs to what will be purged
 
 
194
  * `varnish_http_purge_events` - Add a specific event to trigger a page purge
195
  * `varnish_http_purge_events_full` - Add a specific event to trigger a full site purge
196
- * `varnish_http_purge_schema` - Allows you to change the schema (default is http)
197
 
198
  I strongly urge you to use the last one with caution. If you trigger a full site purge too often, you'll obviate the usefulness of caching!
199
 
@@ -205,37 +215,21 @@ If you don't have a post ID and you still need this, add it to *both* `varnish_h
205
 
206
  = Hey, don't you work at DreamHost? Is this Official or DreamHost only? =
207
 
208
- Yes I do, and yes and no. 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.
 
 
 
209
 
210
  == Changelog ==
211
 
212
- = 4.0.3 =
213
- * Better explanation when Cloudflare gets in the way of DNS
214
- * Rename flush button for people who don't speak Varnish
215
-
216
- = 4.0.2 =
217
- * Support for WP-CLI commands and PHP > 5.5 (though please upgrade, props @robertpeake)
218
- * Better information regarding Cloudflare
219
- * Better failure on domains for scanner
220
- * Better IP detection
221
- * Break apart settings for faster saving
222
-
223
- = 4.0.1 =
224
- * Fix typo (on -> one)
225
- * Correct permissions on Multisite (props @phh - resolves #27 #28)
226
- * Correct weird merge error (props @phh - resolves #25 #26)
227
- * Fix formatting in Changelog
228
-
229
- = 4.0 =
230
- * Added Varnish Status Page - Tools -> Varnish Status (includes basic scanning etc)
231
- * Allow filter for `home_url()`
232
- * Update readme with list of filters.
233
- * Added wp-cli commands to flush specific URLs and wildcards
234
- * Requires wp-cli 0.25+ to work [3315](https://github.com/wp-cli/wp-cli/issues/3315) for WP 4.6+
235
- * Update `purgePost()` to validate page_for_posts ([Props JeremyClarke](https://github.com/Ipstenu/varnish-http-purge/pull/20))
236
- * Add check for AMP ([Props JeremyClarke](https://wordpress.org/support/topic/varnish-http-purge-doesnt-update-amp-urls-on-post-update/))
237
- * Purge 'default' AMP URL as well
238
- * Cleanup on Uninstall
239
 
240
  == Screenshots ==
241
 
1
  === Varnish HTTP Purge ===
2
  Contributors: Ipstenu, mikeschroder, techpriester
3
  Tags: varnish, purge, cache
4
+ Requires at least: 4.7
5
+ Tested up to: 4.8
6
+ Stable tag: 4.1
7
 
8
+ Automatically empty a Varnish Cache when content on your site is modified.
9
 
10
  == Description ==
 
11
 
12
+ Varnish HTTP Purge sends a PURGE request to delete the cached data of a page or post every time it it modified. This happens when updating, publishing, commenting on, or deleting an post, and when changing themes.
13
 
14
+ <a href="https://www.varnish-cache.org/">Varnish</a> is a web application accelerator also known as a caching HTTP reverse proxy. You install it in front of any server that speaks HTTP and configure it to cache the contents. This plugin <em>does not</em> install Varnish for you, nor will it configure Varnish for WordPress. It's expected you already did that on your own.
15
+
16
+ Not all page caches are deleted every time, depending on your Varnish configuration. For example, when a post, page, or custom post type is edited, or a new comment is added, <em>only</em> the following pages will purge:
17
 
18
  * The front page
19
  * The post/page edited
20
  * Any categories or tags associated with the page
21
 
22
+ In addition, your <em>entire</em> cache will be deleted on the following actions:
23
 
24
  * Changing themes
25
  * Press the 'Purge Varnish Cache' button on the dashboard
29
 
30
  And if you're into WP-CLI, you can use that too: `wp varnish purge`
31
 
32
+ Please note: On a multisite network using subfolders, only <strong>network admins</strong> can purge the main site. This is a security decision.
33
 
34
  == Installation ==
35
  No WordPress configuration needed.
36
 
37
+ When used on Multisite, the plugin is only able to be activated Network Wide.
38
 
39
  = Requirements =
40
+
41
  * Pretty Permalinks enabled
42
  * Varnish 3.x or higher
43
 
49
 
50
  = How can I tell everything's working? =
51
 
52
+ 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.
53
 
54
  = Does every WordPress plugin and theme work with Varnish? =
55
 
56
+ No. Some of them have behavior 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.
57
 
58
  = How can I debug my site? =
59
 
60
+ Use the Varnish Status page. It'll try and help you figure out what's wrong.
61
+
62
+ = Will you fix my site if there's a conflict with a theme or plugin? =
63
+
64
+ I wish I could. I don't have that much free time. What I can do is try and point you to how you can fix it yourself. 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
 
68
  This was built and tested on Varnish 3.x. While it is reported to work on 2.x and 4.x, it is only supported on v3 at this time.
69
 
70
+ = Why doesn't every page cache get deleted when I make a new post? =
71
 
72
+ The only cache pages that should be deleted are the post's page, the front page, categories, and tags. The reason why is a little philosophical.
73
 
74
+ When building out this plugin, there were a couple pathways on how best to handle purging caches and they boiled down to two: Decisions (the plugin deletes what it deletes when it deems it appropriate) and Options (you decide what to delete, when and why). It's entirely possible to make this plugin delete everything in a cache, every time a 'trigger' happens, have it only delete prt of the cache, or have it be so you can pick what gets deleted.
75
 
76
+ It was decided that the KISS principle was key. Since you can configure Varnish itself to always delete all pages recursively (i.e. purging `http://example.com/` would purge all pages below it), I felt it prudent to allow you to make that choice yourself in Varnish, not the plugin. There are also other plugins that allow for more granular control of Varnish and deleting caches (including W3 Total Cache).
77
 
78
+ In the interest of simplicity, this plugin will not be gaining a whole bunch of options to handle that.
79
 
80
+ = Why doesn't my cache get deleted when I edit my theme? =
81
 
82
+ Because the plugin only deletes caches of your <em>content</em> when you edit it. That means if you edit a page/post, or someone leaves a comment, it'll delete the impacted cached content. Otherwise, you have to delete the whole cache. The plugin will do this for you if you activate a new theme, but not when you edit your current theme's files, and not when you use customizer to change your widgets etc.
83
 
84
+ However... If you use the CSS editor in customizer, it will empty the whole cache for your site on publish.
85
 
86
+ = How do I manually delete the whole cache? =
87
 
88
+ Click the 'Empty Varnish Cache' button on the "Right Now" Dashboard (see the screenshot if you can't find it).
89
+
90
+ There's also an "Empty Cache" button on the admin toolbar.
91
 
92
  = I don't see a button! =
93
 
94
+ If you're on a Multisite Network and you're on the primary site in the network, only the <em>network</em> admins can empty the cache for that site
95
 
96
+ On a subfolder network if you empty the cache for `example.com`, then everything under that (like `example.com/site1` and `example.com/site99` and everything in between) would also get flushed. That means that deleting the cache on the main site deletes the cache for the entire network. Which would really suck.
97
 
98
+ In order to mitigate the destructive nature of that power, only the network admins can empty the cache of the main site of a subfolder network.
99
 
100
  = Why is nothing caching when I use PageSpeed? =
101
 
102
+ PageSpeed likes to put in Caching headers to say <em>not</em> to cache. To fix this, you need to put this in your `.htaccess` section for PageSpeed: `ModPagespeedModifyCachingHeaders off`
103
 
104
  If you're using nginx, it's `pagespeed ModifyCachingHeaders off;`
105
 
109
 
110
  = Why aren't my changes showing when I use CloudFlare or another proxy? =
111
 
112
+ When you use CloudFlare or any other similar service, you've got a proxy in front of the Varnish proxy. In general this isn't a bad thing, though it can introduce some network latency (that means your site may run slower because it has to go through multiple layers to get to the content). The problem arises when the DNS shenanigans send the purge request to your domain name. When you've got an additional proxy like CloudFlare, you don't want the request to go to the proxy, you want it to go to Varnish server.
113
 
114
  On single-site, you can edit this via the Tools -> Varnish Status page. On Multisite, you'll need to add the following to your wp-config.php file:
115
 
132
  If your webhost set up Varnish for you, you may need to 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.
133
 
134
  <ul>
135
+ <li><strong>DreamHost</strong> - If you're using DreamPress and Cloudflare, go into the Panel and click on the DNS settings for the domain. The entry for <em>resolve-to.domain</em> is your varnish server: `resolve-to.www A 208.97.157.172` -- If you're <em>NOT</em> using Cloudflare, you don't need it; it's just your normal IP.</li>
136
  </ul>
137
 
138
  = What if I have multiple varnish IPs? =
141
 
142
  I have a major issue with writing code I don't use, which means that since I'm only using one IP right now, I don't want to be on the ball for supporting multiple IPs. I don't even have a place to test it, which is just insane to attempt to code if you think about it. Yes, I could accept pull requests, but that means everyone's at some other person's discretion. So no, I won't be doing that at this time.
143
 
144
+ = Why don't my gzip'd pages get deleted? =
145
 
146
  Make sure your Varnish VCL is configured correctly to purge all the right pages. This is normally an issue with Varnish 2, which is not supported by this plugin.
147
 
148
+ = Why isn't the whole cache deletion working? =
149
 
150
  The plugin sends a PURGE command of <code>/.*</code> and `X-Purge-Method` in the header with a value of regex. If your Varnish server doesn't doesn't understand the wildcard, you can configure it to check for the header.
151
 
152
+ = How can I see what the plugin is sending to Varnish? =
153
 
154
+ Danger! Here be dragons! If you're command line savvy, you can monitor the interactions between the plugin and Varnish. This can help you understand what's not working quite right, but it can very confusing.
155
 
156
  To see every request made to varnish, use this:
157
  `varnishncsa -F "%m %U"`
197
 
198
  Yes!
199
 
200
+
201
  * `vhp_home_url` - Change the home URL (default is `home_url()`)
202
  * `vhp_purge_urls` - Add additional URLs to what will be purged
203
+ * `varnish_http_purge_headers` - Allows you to change the HTTP headers to send with a PURGE request.
204
+ * `varnish_http_purge_schema` - Allows you to change the schema (default is http)
205
  * `varnish_http_purge_events` - Add a specific event to trigger a page purge
206
  * `varnish_http_purge_events_full` - Add a specific event to trigger a full site purge
 
207
 
208
  I strongly urge you to use the last one with caution. If you trigger a full site purge too often, you'll obviate the usefulness of caching!
209
 
215
 
216
  = Hey, don't you work at DreamHost? Is this Official or DreamHost only? =
217
 
218
+ * Yes, I do work for DreamHost.
219
+ * No, this plugin is not really official nor DreamHost Only
220
+
221
+ 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.
222
 
223
  == Changelog ==
224
 
225
+ = 4.1 =
226
+
227
+ * JSON / REST API Support
228
+ * Fix for Varnish Status Page on MAMP (props @jeremyclarke)
229
+ * Filter for purge headers (props @ocean90)
230
+ * Disallow people from editing the Varnish IP on Multisite
231
+ * Drop support for pre 4.7 because of JSON / REST API
232
+ * Support flushing cache for private pages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
  == Screenshots ==
235
 
varnish-http-purge.php CHANGED
@@ -2,15 +2,15 @@
2
  /*
3
  Plugin Name: Varnish HTTP Purge
4
  Plugin URI: https://halfelf.org/plugins/varnish-http-purge/
5
- Description: Automatically purge Varnish Cache when content on your site is modified.
6
- Version: 4.0.3
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-2016: Mika A. Epstein (email: ipstenu@halfelf.org)
14
 
15
  Original Author: Leon Weidauer ( http:/www.lnwdr.de/ )
16
 
@@ -41,25 +41,42 @@ class VarnishPurger {
41
  * @access public
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( 'activity_box_end', array( $this, 'varnish_rightnow' ), 100 );
47
  }
48
 
49
  /**
50
- * Plugin Init
51
  *
52
- * @since 1.0
53
  * @access public
54
  */
55
- public function init() {
56
- global $blog_id;
 
 
 
 
 
 
57
 
58
  // Warning: No Pretty Permalinks!
59
- if ( '' == get_option( 'permalink_structure' ) && current_user_can('manage_options') ) {
60
- add_action( 'admin_notices' , array( $this, 'prettyPermalinksMessage'));
61
  return;
62
  }
 
 
 
 
 
 
 
 
 
 
63
 
64
  // get my events
65
  $events = $this->getRegisterEvents();
@@ -76,28 +93,28 @@ class VarnishPurger {
76
  foreach ( $events as $event) {
77
  if ( in_array($event, $noIDevents ) ) {
78
  // These events have no post ID and, thus, will perform a full purge
79
- add_action( $event, array($this, 'purgeNoID') );
80
  } else {
81
- add_action( $event, array($this, 'purgePost'), 10, 2 );
82
  }
83
  }
84
  }
85
 
86
- add_action( 'shutdown', array($this, 'executePurge') );
87
 
88
  // Success: Admin notice when purging
89
- if ( isset($_GET['vhp_flush_all']) && check_admin_referer('vhp-flush-all') ) {
90
  add_action( 'admin_notices' , array( $this, 'purgeMessage'));
91
  }
92
 
93
  // Checking user permissions for who can and cannot use the admin button
94
  if (
95
  // SingleSite - admins can always purge
96
- ( !is_multisite() && current_user_can('activate_plugins') ) ||
97
  // Multisite - Network Admin can always purge
98
- current_user_can('manage_network') ||
99
  // Multisite - Site admins can purge UNLESS it's a subfolder install and we're on site #1
100
- ( is_multisite() && current_user_can('activate_plugins') && ( SUBDOMAIN_INSTALL || ( !SUBDOMAIN_INSTALL && ( BLOG_ID_CURRENT_SITE != $blog_id ) ) ) )
101
  ) {
102
  add_action( 'admin_bar_menu', array( $this, 'varnish_rightnow_adminbar' ), 100 );
103
  }
@@ -111,19 +128,29 @@ class VarnishPurger {
111
  * @since 2.0
112
  */
113
  function purgeMessage() {
114
- echo "<div id='message' class='notice notice-success fade is-dismissible'><p><strong>".__('Varnish cache purged!', 'varnish-http-purge')."</strong></p></div>";
115
  }
116
 
117
  /**
118
- * Permalinks Message
119
- * Explains you need Pretty Permalinks on to use this plugin
120
  *
121
  * @since 2.0
122
  */
123
- function prettyPermalinksMessage() {
124
  echo "<div id='message' class='error'><p>" . sprintf( __( 'Varnish HTTP Purge requires you to use custom permalinks. Please go to the <a href="%1$s">Permalinks Options Page</a> to configure them.', 'varnish-http-purge' ), admin_url( 'options-permalink.php' ) ) . "</p></div>";
125
  }
126
 
 
 
 
 
 
 
 
 
 
 
127
  /**
128
  * The Home URL
129
  * Get the Home URL and allow it to be filterable
@@ -142,13 +169,13 @@ class VarnishPurger {
142
  *
143
  * @since 2.0
144
  */
145
- function varnish_rightnow_adminbar($admin_bar){
146
  $admin_bar->add_menu( array(
147
  'id' => 'purge-varnish-cache-all',
148
- 'title' => __('Empty Cache','varnish-http-purge'),
149
  'href' => wp_nonce_url( add_query_arg('vhp_flush_all', 1), 'vhp-flush-all'),
150
  'meta' => array(
151
- 'title' => __('Empty Cache','varnish-http-purge'),
152
  ),
153
  ));
154
  }
@@ -161,21 +188,21 @@ class VarnishPurger {
161
  */
162
  function varnish_rightnow() {
163
  global $blog_id;
164
- $url = wp_nonce_url(add_query_arg('vhp_flush_all', 1), 'vhp-flush-all');
165
- $intro = sprintf( __('<a href="%1$s">Varnish HTTP Purge</a> automatically purges your posts when published or updated. Sometimes you need a manual flush.', 'varnish-http-purge' ), 'http://wordpress.org/plugins/varnish-http-purge/' );
166
- $button = __('Press the button below to force it to empty your entire Varnish cache.', 'varnish-http-purge' );
167
  $button .= '</p><p><span class="button"><a href="'.$url.'"><strong>';
168
- $button .= __('Empty Cache', 'varnish-http-purge' );
169
  $button .= '</strong></a></span>';
170
- $nobutton = __('You do not have permission to purge the cache for the whole site. Please contact your administrator.', 'varnish-http-purge' );
171
 
172
  if (
173
  // SingleSite - admins can always purge
174
- ( !is_multisite() && current_user_can('activate_plugins') ) ||
175
  // Multisite - Network Admin can always purge
176
- current_user_can('manage_network') ||
177
  // Multisite - Site admins can purge UNLESS it's a subfolder install and we're on site #1
178
- ( is_multisite() && current_user_can('activate_plugins') && ( SUBDOMAIN_INSTALL || ( !SUBDOMAIN_INSTALL && ( BLOG_ID_CURRENT_SITE != $blog_id ) ) ) )
179
  ) {
180
  $text = $intro.' '.$button;
181
  } else {
@@ -241,13 +268,13 @@ class VarnishPurger {
241
  public function executePurge() {
242
  $purgeUrls = array_unique( $this->purgeUrls );
243
 
244
- if ( empty($purgeUrls) ) {
245
- if ( isset($_GET['vhp_flush_all']) && check_admin_referer('vhp-flush-all') ) {
246
- $this->purgeUrl( $this->the_home_url() .'/?vhp-regex' );
247
  }
248
  } else {
249
- foreach($purgeUrls as $url) {
250
- $this->purgeUrl($url);
251
  }
252
  }
253
  }
@@ -260,29 +287,30 @@ class VarnishPurger {
260
  * @param array $url the url to be purged
261
  * @access protected
262
  */
263
- public function purgeUrl($url) {
264
- $p = parse_url($url);
 
 
 
 
265
 
266
  if ( isset($p['query']) && ( $p['query'] == 'vhp-regex' ) ) {
267
  $pregex = '.*';
268
- $varnish_x_purgemethod = 'regex';
269
- } else {
270
- $pregex = '';
271
- $varnish_x_purgemethod = 'default';
272
  }
273
 
274
  // Build a varniship
275
  if ( VHP_VARNISH_IP != false ) {
276
  $varniship = VHP_VARNISH_IP;
277
  } else {
278
- $varniship = get_option('vhp_varnish_ip');
279
  }
280
- $varniship = apply_filters('vhp_varnish_ip', $varniship);
281
 
282
- if (isset($p['path'] ) ) {
 
 
283
  $path = $p['path'];
284
- } else {
285
- $path = '';
286
  }
287
 
288
  /**
@@ -292,13 +320,11 @@ class VarnishPurger {
292
  * varnish_http_purge_schema()
293
  *
294
  * @since 3.7.3
295
- *
296
  */
297
-
298
  $schema = apply_filters( 'varnish_http_purge_schema', 'http://' );
299
 
300
  // If we made varniship, let it sail
301
- if ( isset($varniship) && $varniship != null ) {
302
  $host = $varniship;
303
  } else {
304
  $host = $p['host'];
@@ -306,15 +332,22 @@ class VarnishPurger {
306
 
307
  $purgeme = $schema.$host.$path.$pregex;
308
 
309
- if (!empty($p['query']) && $p['query'] != 'vhp-regex') {
310
  $purgeme .= '?' . $p['query'];
311
  }
312
 
 
 
 
 
 
 
 
313
  // Cleanup CURL functions to be wp_remote_request and thus better
314
  // http://wordpress.org/support/topic/incompatability-with-editorial-calendar-plugin
315
- $response = wp_remote_request($purgeme, array('method' => 'PURGE', 'headers' => array( 'host' => $p['host'], 'X-Purge-Method' => $varnish_x_purgemethod ) ) );
316
 
317
- do_action('after_purge_url', $url, $purgeme, $response);
318
  }
319
 
320
  /**
@@ -322,17 +355,17 @@ class VarnishPurger {
322
  * Flush the whole cache
323
  *
324
  * @since 3.9
325
- * @access private
326
  */
327
  public function purgeNoID( $postId ) {
328
  $listofurls = array();
329
 
330
- array_push($listofurls, $this->the_home_url().'/?vhp-regex' );
331
 
332
  // Now flush all the URLs we've collected provided the array isn't empty
333
- if ( !empty($listofurls) ) {
334
- foreach ($listofurls as $url) {
335
- array_push($this->purgeUrls, $url ) ;
336
  }
337
  }
338
  }
@@ -347,94 +380,133 @@ class VarnishPurger {
347
  */
348
  public function purgePost( $postId ) {
349
 
 
 
 
 
350
  // If this is a valid post we want to purge the post,
351
  // the home page and any associated tags and categories
352
-
353
- $validPostStatus = array("publish", "trash");
354
- $thisPostStatus = get_post_status($postId);
 
 
 
 
 
 
 
 
 
355
 
356
  // array to collect all our URLs
357
  $listofurls = array();
358
 
359
- if( get_permalink($postId) == true && in_array($thisPostStatus, $validPostStatus) ) {
360
- // If this is a post with a permalink AND it's published or trashed,
361
- // we're going to add a ton of things to flush.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
 
 
 
 
 
 
 
 
 
 
 
363
  // Category purge based on Donnacha's work in WP Super Cache
364
- $categories = get_the_category($postId);
365
  if ( $categories ) {
366
- foreach ($categories as $cat) {
367
- array_push($listofurls, get_category_link( $cat->term_id ) );
 
 
 
368
  }
369
  }
370
  // Tag purge based on Donnacha's work in WP Super Cache
371
- $tags = get_the_tags($postId);
372
  if ( $tags ) {
373
- foreach ($tags as $tag) {
374
- array_push($listofurls, get_tag_link( $tag->term_id ) );
 
 
 
375
  }
376
  }
377
-
378
  // Author URL
379
- array_push($listofurls,
380
- get_author_posts_url( get_post_field( 'post_author', $postId ) ),
381
- get_author_feed_link( get_post_field( 'post_author', $postId ) )
 
 
382
  );
383
 
384
  // Archives and their feeds
385
- $archiveurls = array();
386
- if ( get_post_type_archive_link( get_post_type( $postId ) ) == true ) {
387
- array_push($listofurls,
388
  get_post_type_archive_link( get_post_type( $postId ) ),
389
  get_post_type_archive_feed_link( get_post_type( $postId ) )
 
390
  );
391
  }
392
-
393
- // Post URL
394
- array_push( $listofurls, get_permalink($postId) );
395
-
396
- // Also clean URL for trashed post.
397
- if ( $thisPostStatus == "trash" ) {
398
- $trashpost = get_permalink($postId);
399
- $trashpost = str_replace("__trashed", "", $trashpost);
400
- array_push( $listofurls, $trashpost, $trashpost.'feed/' );
401
- }
402
-
403
- // Add in AMP permalink if Automattic's AMP is installed
404
- if ( function_exists('amp_get_permalink') ) {
405
- array_push( $listofurls, amp_get_permalink($postId) );
406
- }
407
-
408
- // Regular AMP url for posts
409
- array_push( $listofurls, get_permalink($postId).'amp/' );
410
 
411
  // Feeds
412
- array_push($listofurls,
413
- get_bloginfo_rss('rdf_url') ,
414
- get_bloginfo_rss('rss_url') ,
415
- get_bloginfo_rss('rss2_url'),
416
- get_bloginfo_rss('atom_url'),
417
- get_bloginfo_rss('comments_rss2_url'),
418
- get_post_comments_feed_link($postId)
419
  );
420
 
421
- // Home Page and (if used) posts page
422
- array_push( $listofurls, $this->the_home_url().'/' );
423
- if ( get_option('show_on_front') == 'page' ) {
 
 
 
424
  // Ensure we have a page_for_posts setting to avoid empty URL
425
  if ( get_option('page_for_posts') ) {
426
- array_push( $listofurls, get_permalink( get_option('page_for_posts') ) );
427
  }
428
  }
 
429
  } else {
430
  // We're not sure how we got here, but bail instead of processing anything else.
431
  return;
432
  }
433
 
434
  // Now flush all the URLs we've collected provided the array isn't empty
435
- if ( !empty($listofurls) ) {
436
- foreach ($listofurls as $url) {
437
- array_push($this->purgeUrls, $url ) ;
 
 
438
  }
439
  }
440
 
@@ -453,7 +525,7 @@ $purger = new VarnishPurger();
453
  *
454
  * @since 3.8
455
  */
456
- if ( defined('WP_CLI') && WP_CLI ) {
457
  include( 'wp-cli.php' );
458
  }
459
 
2
  /*
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.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
 
41
  * @access public
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
  add_action( 'activity_box_end', array( $this, 'varnish_rightnow' ), 100 );
48
  }
49
 
50
  /**
51
+ * Admin Init
52
  *
53
+ * @since 4.1
54
  * @access public
55
  */
56
+ public function admin_init() {
57
+
58
+ // Failure: Pre WP 4.7
59
+ if ( version_compare( get_bloginfo( 'version' ), '4.7', '<=' ) ) {
60
+ deactivate_plugins( plugin_basename( __FILE__ ) );
61
+ add_action( 'admin_notices' , array( $this, 'require_wp_version_notice'));
62
+ return;
63
+ }
64
 
65
  // Warning: No Pretty Permalinks!
66
+ if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) ) {
67
+ add_action( 'admin_notices' , array( $this, 'require_pretty_permalinks_notice' ) );
68
  return;
69
  }
70
+ }
71
+
72
+ /**
73
+ * Plugin Init
74
+ *
75
+ * @since 1.0
76
+ * @access public
77
+ */
78
+ public function init() {
79
+ global $blog_id;
80
 
81
  // get my events
82
  $events = $this->getRegisterEvents();
93
  foreach ( $events as $event) {
94
  if ( in_array($event, $noIDevents ) ) {
95
  // These events have no post ID and, thus, will perform a full purge
96
+ add_action( $event, array( $this, 'purgeNoID' ) );
97
  } else {
98
+ add_action( $event, array( $this, 'purgePost' ), 10, 2 );
99
  }
100
  }
101
  }
102
 
103
+ add_action( 'shutdown', array( $this, 'executePurge' ) );
104
 
105
  // Success: Admin notice when purging
106
+ if ( isset( $_GET['vhp_flush_all'] ) && check_admin_referer( 'vhp-flush-all' ) ) {
107
  add_action( 'admin_notices' , array( $this, 'purgeMessage'));
108
  }
109
 
110
  // Checking user permissions for who can and cannot use the admin button
111
  if (
112
  // SingleSite - admins can always purge
113
+ ( !is_multisite() && current_user_can( 'activate_plugins' ) ) ||
114
  // Multisite - Network Admin can always purge
115
+ current_user_can( 'manage_network' ) ||
116
  // Multisite - Site admins can purge UNLESS it's a subfolder install and we're on site #1
117
+ ( is_multisite() && current_user_can( 'activate_plugins' ) && ( SUBDOMAIN_INSTALL || ( !SUBDOMAIN_INSTALL && ( BLOG_ID_CURRENT_SITE != $blog_id ) ) ) )
118
  ) {
119
  add_action( 'admin_bar_menu', array( $this, 'varnish_rightnow_adminbar' ), 100 );
120
  }
128
  * @since 2.0
129
  */
130
  function purgeMessage() {
131
+ echo "<div id='message' class='notice notice-success fade is-dismissible'><p><strong>".__('Varnish cache emptied!', 'varnish-http-purge')."</strong></p></div>";
132
  }
133
 
134
  /**
135
+ * Require: Pretty Permalinks Message
136
+ * Explains you need Pretty Permalinks enabled to use this plugin
137
  *
138
  * @since 2.0
139
  */
140
+ function require_pretty_permalinks_notice() {
141
  echo "<div id='message' class='error'><p>" . sprintf( __( 'Varnish HTTP Purge requires you to use custom permalinks. Please go to the <a href="%1$s">Permalinks Options Page</a> to configure them.', 'varnish-http-purge' ), admin_url( 'options-permalink.php' ) ) . "</p></div>";
142
  }
143
 
144
+ /**
145
+ * Require: WP Version Message
146
+ * Explains you need WordPress 4.7+ to use this plugin
147
+ *
148
+ * @since 4.1
149
+ */
150
+ function require_wp_version_notice() {
151
+ echo "<div id='message' class='error'><p>" . sprintf( __( 'Varnish HTTP Purge requires WordPress 4.7 or greater. Please <a href="%1$s">upgrade WordPress</a>.', 'varnish-http-purge' ), admin_url( 'update-core.php' ) ) . "</p></div>";
152
+ }
153
+
154
  /**
155
  * The Home URL
156
  * Get the Home URL and allow it to be filterable
169
  *
170
  * @since 2.0
171
  */
172
+ function varnish_rightnow_adminbar( $admin_bar ){
173
  $admin_bar->add_menu( array(
174
  'id' => 'purge-varnish-cache-all',
175
+ 'title' => __( 'Empty Cache', 'varnish-http-purge' ),
176
  'href' => wp_nonce_url( add_query_arg('vhp_flush_all', 1), 'vhp-flush-all'),
177
  'meta' => array(
178
+ 'title' => __( 'Empty Cache', 'varnish-http-purge' ),
179
  ),
180
  ));
181
  }
188
  */
189
  function varnish_rightnow() {
190
  global $blog_id;
191
+ $url = wp_nonce_url( add_query_arg( 'vhp_flush_all', 1 ), 'vhp-flush-all' );
192
+ $intro = sprintf( __( '<a href="%1$s">Varnish HTTP Purge</a> automatically deletes your cached posts when published or updated. When making major site changes, such as with a new theme, plugins, or widgets, you may need to manually empty the cache.', 'varnish-http-purge' ), 'http://wordpress.org/plugins/varnish-http-purge/' );
193
+ $button = __( 'Press the button below to force it to empty your entire Varnish cache.', 'varnish-http-purge' );
194
  $button .= '</p><p><span class="button"><a href="'.$url.'"><strong>';
195
+ $button .= __( 'Empty Cache', 'varnish-http-purge' );
196
  $button .= '</strong></a></span>';
197
+ $nobutton = __( 'You do not have permission to empty the Varnish cache for the whole site. Please contact your administrator.', 'varnish-http-purge' );
198
 
199
  if (
200
  // SingleSite - admins can always purge
201
+ ( !is_multisite() && current_user_can( 'activate_plugins' ) ) ||
202
  // Multisite - Network Admin can always purge
203
+ current_user_can( 'manage_network' ) ||
204
  // Multisite - Site admins can purge UNLESS it's a subfolder install and we're on site #1
205
+ ( is_multisite() && current_user_can( 'activate_plugins' ) && ( SUBDOMAIN_INSTALL || ( !SUBDOMAIN_INSTALL && ( BLOG_ID_CURRENT_SITE != $blog_id ) ) ) )
206
  ) {
207
  $text = $intro.' '.$button;
208
  } else {
268
  public function executePurge() {
269
  $purgeUrls = array_unique( $this->purgeUrls );
270
 
271
+ if ( empty( $purgeUrls ) ) {
272
+ if ( isset( $_GET['vhp_flush_all'] ) && check_admin_referer( 'vhp-flush-all' ) ) {
273
+ $this->purgeUrl( $this->the_home_url() . '/?vhp-regex' );
274
  }
275
  } else {
276
+ foreach( $purgeUrls as $url ) {
277
+ $this->purgeUrl( $url );
278
  }
279
  }
280
  }
287
  * @param array $url the url to be purged
288
  * @access protected
289
  */
290
+ public function purgeUrl( $url ) {
291
+ $p = parse_url( $url );
292
+
293
+ // Determine if we're using regex to flush all pages or not
294
+ $pregex = '';
295
+ $x_purge_method = 'default';
296
 
297
  if ( isset($p['query']) && ( $p['query'] == 'vhp-regex' ) ) {
298
  $pregex = '.*';
299
+ $x_purge_method = 'regex';
 
 
 
300
  }
301
 
302
  // Build a varniship
303
  if ( VHP_VARNISH_IP != false ) {
304
  $varniship = VHP_VARNISH_IP;
305
  } else {
306
+ $varniship = get_option( 'vhp_varnish_ip' );
307
  }
308
+ $varniship = apply_filters( 'vhp_varnish_ip' , $varniship );
309
 
310
+ // Determine the path
311
+ $path = '';
312
+ if ( isset( $p['path'] ) ) {
313
  $path = $p['path'];
 
 
314
  }
315
 
316
  /**
320
  * varnish_http_purge_schema()
321
  *
322
  * @since 3.7.3
 
323
  */
 
324
  $schema = apply_filters( 'varnish_http_purge_schema', 'http://' );
325
 
326
  // If we made varniship, let it sail
327
+ if ( isset( $varniship ) && $varniship != null ) {
328
  $host = $varniship;
329
  } else {
330
  $host = $p['host'];
332
 
333
  $purgeme = $schema.$host.$path.$pregex;
334
 
335
+ if ( !empty( $p['query'] ) && $p['query'] != 'vhp-regex' ) {
336
  $purgeme .= '?' . $p['query'];
337
  }
338
 
339
+ /**
340
+ * Filters the HTTP headers to send with a PURGE request.
341
+ *
342
+ * @since 4.1
343
+ */
344
+ $headers = apply_filters( 'varnish_http_purge_headers', array( 'host' => $p['host'], 'X-Purge-Method' => $x_purge_method ) );
345
+
346
  // Cleanup CURL functions to be wp_remote_request and thus better
347
  // http://wordpress.org/support/topic/incompatability-with-editorial-calendar-plugin
348
+ $response = wp_remote_request( $purgeme, array( 'method' => 'PURGE', 'headers' => $headers ) );
349
 
350
+ do_action( 'after_purge_url', $url, $purgeme, $response, $headers );
351
  }
352
 
353
  /**
355
  * Flush the whole cache
356
  *
357
  * @since 3.9
358
+ * @access public
359
  */
360
  public function purgeNoID( $postId ) {
361
  $listofurls = array();
362
 
363
+ array_push( $listofurls, $this->the_home_url() . '/?vhp-regex' );
364
 
365
  // Now flush all the URLs we've collected provided the array isn't empty
366
+ if ( !empty( $listofurls ) ) {
367
+ foreach ( $listofurls as $url ) {
368
+ array_push( $this->purgeUrls, $url ) ;
369
  }
370
  }
371
  }
380
  */
381
  public function purgePost( $postId ) {
382
 
383
+ // Future Me: You may need this if you figure out how to use an array
384
+ // further down with versions of WP and their json versions.
385
+ // global $wp_version;
386
+
387
  // If this is a valid post we want to purge the post,
388
  // the home page and any associated tags and categories
389
+ $valid_post_status = array( 'publish', 'private', 'trash' );
390
+ $this_post_status = get_post_status( $postId );
391
+
392
+ // Not all post types are created equal
393
+ $invalid_post_type = array( 'nav_menu_item', 'revision' );
394
+ $noarchive_post_type = array( 'post', 'page' );
395
+ $this_post_type = get_post_type( $postId );
396
+
397
+ // Determine the route for the rest API
398
+ // This will need to be revisted if WP updates the version.
399
+ // Future me: Consider an array? 4.7-4.7.3 use v2, and then adapt from there?
400
+ $rest_api_route = 'wp/v2';
401
 
402
  // array to collect all our URLs
403
  $listofurls = array();
404
 
405
+ // Verify we have a permalink and that we're a valid post status and a not an invalid post type
406
+ if( get_permalink( $postId ) == true && in_array( $this_post_status, $valid_post_status ) && !in_array( $this_post_type, $invalid_post_type ) ) {
407
+
408
+ // Post URL
409
+ array_push( $listofurls, get_permalink( $postId ) );
410
+
411
+ // JSON API Permalink for the post based on type
412
+ // We only want to do this if the rest_base exists
413
+ // But we apparently have to force it for posts and pages (seriously?)
414
+ $post_type_object = get_post_type_object( $postId );
415
+ if ( isset( $post_type_object->rest_base ) ) {
416
+ $rest_permalink = get_rest_url() . $rest_api_route . '/' . $post_type_object->rest_base . '/' . $postId . '/';
417
+ } elseif ( $this_post_type == 'post' ) {
418
+ $rest_permalink = get_rest_url() . $rest_api_route . '/posts/' . $postId . '/';
419
+ } elseif ( $this_post_type == 'page' ) {
420
+ $rest_permalink = get_rest_url() . $rest_api_route . '/pages/' . $postId . '/';
421
+ }
422
+ array_push( $listofurls, $rest_permalink );
423
+
424
+ // Add in AMP permalink if Automattic's AMP is installed
425
+ if ( function_exists( 'amp_get_permalink' ) ) {
426
+ array_push( $listofurls, amp_get_permalink( $postId ) );
427
+ }
428
 
429
+ // Regular AMP url for posts
430
+ array_push( $listofurls, get_permalink( $postId ) . 'amp/' );
431
+
432
+ // Also clean URL for trashed post.
433
+ if ( $this_post_status == 'trash' ) {
434
+ $trashpost = get_permalink( $postId );
435
+ $trashpost = str_replace( '__trashed', '', $trashpost );
436
+ array_push( $listofurls, $trashpost, $trashpost . 'feed/' );
437
+ }
438
+
439
  // Category purge based on Donnacha's work in WP Super Cache
440
+ $categories = get_the_category( $postId) ;
441
  if ( $categories ) {
442
+ foreach ( $categories as $cat ) {
443
+ array_push( $listofurls,
444
+ get_category_link( $cat->term_id ),
445
+ get_rest_url() . $rest_api_route . '/categories/' . $cat->term_id . '/'
446
+ );
447
  }
448
  }
449
  // Tag purge based on Donnacha's work in WP Super Cache
450
+ $tags = get_the_tags( $postId );
451
  if ( $tags ) {
452
+ foreach ( $tags as $tag ) {
453
+ array_push( $listofurls,
454
+ get_tag_link( $tag->term_id ),
455
+ get_rest_url() . $rest_api_route . '/tags/' . $tag->term_id . '/'
456
+ );
457
  }
458
  }
459
+
460
  // Author URL
461
+ $author_id = get_post_field( 'post_author', $postId );
462
+ array_push( $listofurls,
463
+ get_author_posts_url( $author_id ),
464
+ get_author_feed_link( $author_id ),
465
+ get_rest_url() . $rest_api_route . '/users/' . $author_id . '/'
466
  );
467
 
468
  // Archives and their feeds
469
+ if ( $this_post_type && !in_array( $this_post_type, $noarchive_post_type ) ) {
470
+ array_push( $listofurls,
 
471
  get_post_type_archive_link( get_post_type( $postId ) ),
472
  get_post_type_archive_feed_link( get_post_type( $postId ) )
473
+ // Need to add in JSON?
474
  );
475
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
 
477
  // Feeds
478
+ array_push( $listofurls,
479
+ get_bloginfo_rss( 'rdf_url' ),
480
+ get_bloginfo_rss( 'rss_url' ),
481
+ get_bloginfo_rss( 'rss2_url' ),
482
+ get_bloginfo_rss( 'atom_url' ),
483
+ get_bloginfo_rss( 'comments_rss2_url' ),
484
+ get_post_comments_feed_link( $postId )
485
  );
486
 
487
+ // Home Pages and (if used) posts page
488
+ array_push( $listofurls,
489
+ get_rest_url(),
490
+ $this->the_home_url() . '/'
491
+ );
492
+ if ( get_option( 'show_on_front' ) == 'page' ) {
493
  // Ensure we have a page_for_posts setting to avoid empty URL
494
  if ( get_option('page_for_posts') ) {
495
+ array_push( $listofurls, get_permalink( get_option( 'page_for_posts' ) ) );
496
  }
497
  }
498
+
499
  } else {
500
  // We're not sure how we got here, but bail instead of processing anything else.
501
  return;
502
  }
503
 
504
  // Now flush all the URLs we've collected provided the array isn't empty
505
+ // And make sure each URL only gets purged once, eh?
506
+ if ( !empty( $listofurls ) ) {
507
+ $purgeurls = array_unique( $listofurls, SORT_REGULAR );
508
+ foreach ( $purgeurls as $url ) {
509
+ array_push( $this->purgeUrls, $url ) ;
510
  }
511
  }
512
 
525
  *
526
  * @since 3.8
527
  */
528
+ if ( defined( 'WP_CLI' ) && WP_CLI ) {
529
  include( 'wp-cli.php' );
530
  }
531
 
varnish-status.php CHANGED
@@ -9,10 +9,9 @@
9
  Varnish HTTP Purge is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
-
13
  */
14
 
15
- if (!defined('ABSPATH')) {
16
  die();
17
  }
18
 
@@ -43,9 +42,8 @@ class VarnishStatus {
43
  * @since 4.0
44
  */
45
  function admin_init() {
46
- // Register Settings
47
- $this->register_settings_url();
48
- $this->register_settings_ip();
49
  }
50
 
51
  /**
@@ -64,7 +62,6 @@ class VarnishStatus {
64
  */
65
  function register_settings_url() {
66
  register_setting( 'varnish-http-purge-url', 'vhp_varnish_url', array( &$this, 'varnish_url_sanitize' ) );
67
-
68
  add_settings_section( 'varnish-url-settings-section', __('Check Varnish Status', 'varnish-http-purge'), array( &$this, 'options_callback_url'), 'varnish-url-settings' );
69
  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' );
70
  }
@@ -76,7 +73,6 @@ class VarnishStatus {
76
  */
77
  function register_settings_ip() {
78
  register_setting( 'varnish-http-purge-ip', 'vhp_varnish_ip', array( &$this, 'varnish_ip_sanitize' ) );
79
-
80
  add_settings_section( 'varnish-ip-settings-section', __('Configure Custom Varnish IP', 'varnish-http-purge'), array( &$this, 'options_callback_ip'), 'varnish-ip-settings' );
81
  add_settings_field( 'varnish_ip', __('Set Varnish IP', 'varnish-http-purge'), array( &$this, 'varnish_ip_callback'), 'varnish-ip-settings', 'varnish-ip-settings-section' );
82
  }
@@ -89,13 +85,13 @@ class VarnishStatus {
89
  function options_callback_ip() {
90
  ?>
91
  <p><a name="#configure"></a><?php _e('The majority of users will never need to so much as 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>
92
- <p><?php _e('Your Varnish IP is just the IP address of the server where Varnish is installed. Your Varnish IP must be one of the IPs that Varnish is listening on. 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>
93
- <p><?php _e('If your webhost set up Varnish for you, you may need to 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>
 
94
 
95
  <ul>
96
  <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 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>
97
  </ul>
98
-
99
  <?php
100
  }
101
 
@@ -114,6 +110,7 @@ class VarnishStatus {
114
  } else {
115
  $varniship = get_option('vhp_varnish_ip');
116
  }
 
117
  ?>
118
  <input type="text" id="vhp_varnish_ip" name="vhp_varnish_ip" value="<?php echo $varniship; ?>" size="25" <?php if ( $disabled == true ) { echo 'disabled'; } ?>/>
119
  <label for="vhp_varnish_ip">
@@ -125,7 +122,7 @@ class VarnishStatus {
125
  }
126
  ?>
127
  </label>
128
- <?php
129
  }
130
 
131
  /**
@@ -224,24 +221,24 @@ class VarnishStatus {
224
  ?><tr><?php
225
  if ( $remote_ip == false && !empty( $varniship) ) {
226
  ?>
227
- <td width="40px"><?php echo $icon_bad; ?></td>
228
- <td><?php printf( __( 'You have a Varnish IP set but you don\'t appear to be using a proxy like Cloudflare or Sucuri. Or at least we don\'t detect a proxy IP like we expected. You may need to <a href="%s">erase your Varnish IP</a> if you have issues with caches not emptying.', 'varnish-http-purge' ), '#configure' ); ?></td>
229
  <?php
230
  } elseif ( $remote_ip !== false && $remote_ip !== $varniship ) {
231
  ?>
232
- <td width="40px"><?php echo $icon_bad; ?></td>
233
- <td><?php printf( __( 'You\'re using a Custom Varnish IP that doesn\'t appear to match your server IP address. 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 sever IP setup looks just fine. 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> .', 'varnish-http-purge' ), '#configure' ); ?></td>
239
  <?php
240
  }
241
  ?></tr><?php
242
 
243
  // Cloudflare
244
- if ( strpos( $headers['Server'] ,'cloudflare') !== false ) {
245
  ?><tr>
246
  <td><?php echo $icon_warning; ?></td>
247
  <td><?php printf( __( 'Because CloudFlare is running, you may experience some cache oddities. Make sure you <a href="%s">configure WordPress for Cloudflare</a>.', 'varnish-http-purge' ), '#configure' ); ?></td>
@@ -281,32 +278,47 @@ class VarnishStatus {
281
 
282
  // SET COOKIE
283
  if ( isset( $headers['Set-Cookie'] ) ) {
284
-
285
- if ( strpos( $headers['Set-Cookie'] , 'PHPSESSID') !== false ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  ?><tr>
287
  <td><?php echo $icon_bad; ?></td>
288
  <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>
289
  </tr><?php
290
  }
291
- if ( strpos( $headers['Set-Cookie'], 'edd_wp_session' ) !== false ) {
292
  ?><tr>
293
  <td><?php echo $icon_bad; ?></td>
294
  <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>
295
  </tr><?php
296
  }
297
- if ( strpos( $headers['Set-Cookie'], 'edd_items_in_cart' ) !== false ) {
298
  ?><tr>
299
  <td><?php echo $icon_warning; ?></td>
300
  <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>
301
  </tr><?php
302
  }
303
- if ( strpos( $headers['Set-Cookie'], 'wfvt_' ) !== false ) {
304
  ?><tr>
305
  <td><?php echo $icon_bad; ?></td>
306
  <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>
307
  </tr><?php
308
  }
309
- if ( strpos( $headers['Set-Cookie'], 'invite-anyone' ) !== false ) {
310
  ?><tr>
311
  <td><?php echo $icon_bad; ?></td>
312
  <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>
@@ -497,4 +509,4 @@ class VarnishStatus {
497
 
498
  }
499
 
500
- $status = new VarnishStatus();
9
  Varnish HTTP Purge is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
  */
13
 
14
+ if ( !defined('ABSPATH') ) {
15
  die();
16
  }
17
 
42
  * @since 4.0
43
  */
44
  function admin_init() {
45
+ $this->register_settings_url();
46
+ if ( !is_multisite() ) $this->register_settings_ip();
 
47
  }
48
 
49
  /**
62
  */
63
  function register_settings_url() {
64
  register_setting( 'varnish-http-purge-url', 'vhp_varnish_url', array( &$this, 'varnish_url_sanitize' ) );
 
65
  add_settings_section( 'varnish-url-settings-section', __('Check Varnish Status', 'varnish-http-purge'), array( &$this, 'options_callback_url'), 'varnish-url-settings' );
66
  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' );
67
  }
73
  */
74
  function register_settings_ip() {
75
  register_setting( 'varnish-http-purge-ip', 'vhp_varnish_ip', array( &$this, 'varnish_ip_sanitize' ) );
 
76
  add_settings_section( 'varnish-ip-settings-section', __('Configure Custom Varnish IP', 'varnish-http-purge'), array( &$this, 'options_callback_ip'), 'varnish-ip-settings' );
77
  add_settings_field( 'varnish_ip', __('Set Varnish IP', 'varnish-http-purge'), array( &$this, 'varnish_ip_callback'), 'varnish-ip-settings', 'varnish-ip-settings-section' );
78
  }
85
  function options_callback_ip() {
86
  ?>
87
  <p><a name="#configure"></a><?php _e('The majority of users will never need to so much as 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>
88
+ <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>
89
+ <p><?php _e('If your webhost set up Varnish for you, as is the case with DreamPress or WP Engine, you may need to 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>
90
+ <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>
91
 
92
  <ul>
93
  <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 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>
94
  </ul>
 
95
  <?php
96
  }
97
 
110
  } else {
111
  $varniship = get_option('vhp_varnish_ip');
112
  }
113
+
114
  ?>
115
  <input type="text" id="vhp_varnish_ip" name="vhp_varnish_ip" value="<?php echo $varniship; ?>" size="25" <?php if ( $disabled == true ) { echo 'disabled'; } ?>/>
116
  <label for="vhp_varnish_ip">
122
  }
123
  ?>
124
  </label>
125
+ <?php
126
  }
127
 
128
  /**
221
  ?><tr><?php
222
  if ( $remote_ip == false && !empty( $varniship) ) {
223
  ?>
224
+ <td width="40px"><?php echo $icon_warning; ?></td>
225
+ <td><?php printf( __( 'You have a Varnish IP set but a proxy like Cloudflare or Sucuri has not been detected. This is usually fine, 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>
226
  <?php
227
  } elseif ( $remote_ip !== false && $remote_ip !== $varniship ) {
228
  ?>
229
+ <td width="40px"><?php echo $icon_warning; ?></td>
230
+ <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>
231
  <?php
232
  } else {
233
  ?>
234
  <td width="40px"><?php echo $icon_good; ?></td>
235
+ <td><?php printf( __( 'Your sever 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>
236
  <?php
237
  }
238
  ?></tr><?php
239
 
240
  // Cloudflare
241
+ if ( isset( $headers['Server'] ) && strpos( $headers['Server'] ,'cloudflare') !== false ) {
242
  ?><tr>
243
  <td><?php echo $icon_warning; ?></td>
244
  <td><?php printf( __( 'Because CloudFlare is running, you may experience some cache oddities. Make sure you <a href="%s">configure WordPress for Cloudflare</a>.', 'varnish-http-purge' ), '#configure' ); ?></td>
278
 
279
  // SET COOKIE
280
  if ( isset( $headers['Set-Cookie'] ) ) {
281
+
282
+ // If cookies are an array, scan the whole thing. Otherwise, we can use strpos.
283
+ if ( is_array( $headers['Set-Cookie'] ) ) {
284
+ $cookie_phpsessid = in_array( 'PHPSESSID', $headers['Set-Cookie'], true );
285
+ $cookie_edd_wp_session = in_array( 'edd_wp_session', $headers['Set-Cookie'], true );
286
+ $cookie_edd_items_in_cart = in_array( 'edd_items_in_cart', $headers['Set-Cookie'], true );
287
+ $cookie_wfvt = in_array( 'wfvt_', $headers['Set-Cookie'], true );
288
+ $cookie_invite_anyone = in_array( 'invite-anyone', $headers['Set-Cookie'], true );
289
+ } else {
290
+ $cookie_phpsessid = strpos( $headers['Set-Cookie'] , 'PHPSESSID');
291
+ $cookie_edd_wp_session = strpos( $headers['Set-Cookie'], 'edd_wp_session' );
292
+ $cookie_edd_items_in_cart = strpos( $headers['Set-Cookie'], 'edd_items_in_cart' );
293
+ $cookie_wfvt = strpos( $headers['Set-Cookie'], 'wfvt_' );
294
+ $cookie_invite_anyone = strpos( $headers['Set-Cookie'], 'invite-anyone' );
295
+ }
296
+
297
+ if ( $cookie_phpsessid !== false ) {
298
  ?><tr>
299
  <td><?php echo $icon_bad; ?></td>
300
  <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>
301
  </tr><?php
302
  }
303
+ if ( $cookie_edd_wp_session !== false ) {
304
  ?><tr>
305
  <td><?php echo $icon_bad; ?></td>
306
  <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>
307
  </tr><?php
308
  }
309
+ if ( $cookie_edd_items_in_cart !== false ) {
310
  ?><tr>
311
  <td><?php echo $icon_warning; ?></td>
312
  <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>
313
  </tr><?php
314
  }
315
+ if ( $cookie_wfvt !== false ) {
316
  ?><tr>
317
  <td><?php echo $icon_bad; ?></td>
318
  <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>
319
  </tr><?php
320
  }
321
+ if ( $cookie_invite_anyone !== false ) {
322
  ?><tr>
323
  <td><?php echo $icon_bad; ?></td>
324
  <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>
509
 
510
  }
511
 
512
+ $status = new VarnishStatus();