Skip to content

Commit 2918b73

Browse files
committed
Add support for the torden branch of ngx_cache_purge
The torden branch has support for `purge_all` with the following directive added to the nginx configuration: ``` location = /purgeall { fastcgi_pass unix:/dev/null; fastcgi_cache WORDPRESS; fastcgi_cache_purge PURGE purge_all from 127.0.0.1; } ``` This makes it possible to purge the cache on systems where Nginx is running as a different user to the PHP process. Note, the `unix:/dev/null` in the above example is not a placeholder, the module needs `fastcgi_pass` set to something valid or it will not function, even though it never uses the value provided.
1 parent 23f7b70 commit 2918b73

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

admin/class-fastcgi-purger.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function purge_url( $url, $feed = true ) {
6969

7070
case 'get_request':
7171
// Go to default case.
72+
73+
case 'get_request_torden':
74+
// Go to default case.
75+
7276
default:
7377
$_url_purge_base = $this->purge_base_url() . $parse['path'];
7478
$_url_purge = $_url_purge_base;
@@ -137,6 +141,10 @@ public function custom_purge_urls() {
137141

138142
case 'get_request':
139143
// Go to default case.
144+
145+
case 'get_request_torden':
146+
// Go to default case.
147+
140148
default:
141149
$_url_purge_base = $this->purge_base_url();
142150

@@ -166,7 +174,48 @@ public function custom_purge_urls() {
166174
*/
167175
public function purge_all() {
168176

169-
$this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false );
177+
global $nginx_helper_admin;
178+
179+
switch ( $nginx_helper_admin->options['purge_method'] ) {
180+
181+
case 'get_request_torden':
182+
$site = get_site_url();
183+
$find = [ 'http://', 'https://' ];
184+
$replace = '';
185+
$host = str_replace( $find, $replace, $site);
186+
187+
if ( is_ssl() ) {
188+
189+
$purgeurl = $site . '/purgeall' ;
190+
$curl = curl_init( $purgeurl );
191+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE" );
192+
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
193+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
194+
curl_setopt($curl, CURLOPT_RESOLVE, array( $host . ":443:127.0.0.1" ));
195+
196+
} else {
197+
198+
$curl = curl_init( "http://127.0.0.1/purgeall" );
199+
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host:' . $host ));
200+
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE" );
201+
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
202+
203+
}
204+
205+
$response = curl_exec($curl);
206+
if ($response === false)
207+
$this->log( curl_errno($curl) .': '. curl_error($curl) );
208+
curl_close($curl);
209+
break;
210+
211+
case 'get_request':
212+
// Go to default case.
213+
214+
default:
215+
$this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false );
216+
break;
217+
}
218+
170219
$this->log( '* * * * *' );
171220
$this->log( '* Purged Everything!' );
172221
$this->log( '* * * * *' );

admin/partials/nginx-helper-general-options.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,40 @@
181181
sprintf(
182182
// translators: %s Nginx cache purge module link.
183183
__( 'Uses the %s module.', 'nginx-helper' ),
184-
'<strong><a href="https://github.com/FRiCKLE/ngx_cache_purge">ngx_cache_purge</a></strong>'
184+
'<strong><a href="https://github.com/FRiCKLE/ngx_cache_purge">ngx_cache_purge (FRiCKLE)</a></strong>'
185+
),
186+
array(
187+
'strong' => array(),
188+
'a' => array(
189+
'href' => array(),
190+
),
191+
)
192+
);
193+
?>
194+
</small>
195+
</label>
196+
<br />
197+
<label for="purge_method_get_request_torden">
198+
<input type="radio" value="get_request_torden" id="purge_method_get_request_torden" name="purge_method" <?php checked( $nginx_helper_settings['purge_method'], 'get_request_torden' ); ?>>
199+
&nbsp;
200+
<?php
201+
echo wp_kses(
202+
sprintf(
203+
'%1$s <strong>PURGE/url</strong> %2$s',
204+
esc_html__( 'Using a GET request to', 'nginx-helper' ),
205+
esc_html__( '(Supports torden\'s `purge_all` method)', 'nginx-helper' )
206+
),
207+
array( 'strong' => array() )
208+
);
209+
?>
210+
<br />
211+
<small>
212+
<?php
213+
echo wp_kses(
214+
sprintf(
215+
// translators: %s Nginx cache purge module link.
216+
__( 'Uses the %s module.', 'nginx-helper' ),
217+
'<strong><a href="https://github.com/torden/ngx_cache_purge">ngx_cache_purge (torden)</a></strong>'
185218
),
186219
array(
187220
'strong' => array(),

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
=== Nginx Helper ===
2-
Contributors: rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint, iam404, rittesh.patel, vishalkakadiya, BhargavBhandari90, vincent-lu, murrayjbrown, bryant1410, 1gor, matt-h, pySilver, johan-chassaing, dotsam, sanketio, petenelson, nathanielks, rigagoogoo, dslatten, jinschoi, kelin1003, vaishuagola27, rahulsprajapati, Joel-James, utkarshpatel, gsayed786, shashwatmittal, sudhiryadav, thrijith, stayallive, jaredwsmith, abhijitrakas, umeshnevase, sid177, souptik, arafatkn, subscriptiongroup
2+
Contributors: rtcamp, rahul286, saurabhshukla, manishsongirkar36, faishal, desaiuditd, darren-slatten, jk3us, daankortenbach, telofy, pjv, llonchj, jinnko, weskoop, bcole808, gungeekatx, rohanveer, chandrapatel, gagan0123, ravanh, michaelbeil, samedwards, niwreg, entr, nuvoPoint, iam404, rittesh.patel, vishalkakadiya, BhargavBhandari90, vincent-lu, murrayjbrown, bryant1410, 1gor, matt-h, pySilver, johan-chassaing, dotsam, sanketio, petenelson, nathanielks, rigagoogoo, dslatten, jinschoi, kelin1003, vaishuagola27, rahulsprajapati, Joel-James, utkarshpatel, gsayed786, shashwatmittal, sudhiryadav, thrijith, stayallive, jaredwsmith, abhijitrakas, umeshnevase, sid177, souptik, arafatkn, subscriptiongroup, gnif
33
Donate Link: http://rt.cx/eedonate/
44
Tags: nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks
55
License: GPLv2 or later (of-course)

0 commit comments

Comments
 (0)