From 2918b73915c6a853f04595c6a9f0eca20fecb7c0 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 13 Jun 2023 10:19:07 +1000 Subject: [PATCH] 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. --- admin/class-fastcgi-purger.php | 51 ++++++++++++++++++- .../partials/nginx-helper-general-options.php | 35 ++++++++++++- readme.txt | 2 +- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/admin/class-fastcgi-purger.php b/admin/class-fastcgi-purger.php index 687d8625..ed30e30b 100644 --- a/admin/class-fastcgi-purger.php +++ b/admin/class-fastcgi-purger.php @@ -69,6 +69,10 @@ public function purge_url( $url, $feed = true ) { case 'get_request': // Go to default case. + + case 'get_request_torden': + // Go to default case. + default: $_url_purge_base = $this->purge_base_url() . $parse['path']; $_url_purge = $_url_purge_base; @@ -137,6 +141,10 @@ public function custom_purge_urls() { case 'get_request': // Go to default case. + + case 'get_request_torden': + // Go to default case. + default: $_url_purge_base = $this->purge_base_url(); @@ -166,7 +174,48 @@ public function custom_purge_urls() { */ public function purge_all() { - $this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false ); + global $nginx_helper_admin; + + switch ( $nginx_helper_admin->options['purge_method'] ) { + + case 'get_request_torden': + $site = get_site_url(); + $find = [ 'http://', 'https://' ]; + $replace = ''; + $host = str_replace( $find, $replace, $site); + + if ( is_ssl() ) { + + $purgeurl = $site . '/purgeall' ; + $curl = curl_init( $purgeurl ); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE" ); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_RESOLVE, array( $host . ":443:127.0.0.1" )); + + } else { + + $curl = curl_init( "http://127.0.0.1/purgeall" ); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Host:' . $host )); + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PURGE" ); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); + + } + + $response = curl_exec($curl); + if ($response === false) + $this->log( curl_errno($curl) .': '. curl_error($curl) ); + curl_close($curl); + break; + + case 'get_request': + // Go to default case. + + default: + $this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false ); + break; + } + $this->log( '* * * * *' ); $this->log( '* Purged Everything!' ); $this->log( '* * * * *' ); diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index 87f2e32d..615725ce 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -181,7 +181,40 @@ sprintf( // translators: %s Nginx cache purge module link. __( 'Uses the %s module.', 'nginx-helper' ), - 'ngx_cache_purge' + 'ngx_cache_purge (FRiCKLE)' + ), + array( + 'strong' => array(), + 'a' => array( + 'href' => array(), + ), + ) + ); + ?> + + +
+