Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPCS warning and error with refactoring #158

Merged
merged 8 commits into from
Oct 19, 2018
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Nginx Helper #
**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

**Tags:** nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks
**Requires at least:** 3.0
**Tested up to:** 4.9.5
**Stable tag:** 1.9.11
**License:** GPLv2 or later (of-course)
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
**Donate Link:** http://rtcamp.com/donate/
**Tags:** nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis, redis-cache, rewrite, permalinks
**Requires at least:** 3.0
**Tested up to:** 4.9.8
**Stable tag:** 1.9.12
**License:** GPLv2 or later (of-course)
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html
**Donate Link:** http://rtcamp.com/donate/

Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things.

Expand Down Expand Up @@ -94,6 +94,21 @@ To purge a page immediately, follow these instructions:
* Needless to say, this won't work, if you have a page or taxonomy called 'purge'.


### FAQ - Nginx Redis Cache ###

**Q. Can I override the redis hostname, port and prefix?**

Yes, you can force override the redis hostname, port or prefix by defining constant in wp-config.php. For example:

```
define( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME', '10.0.0.1' );

define( 'RT_WP_NGINX_HELPER_REDIS_PORT', '6000' );

define( 'RT_WP_NGINX_HELPER_REDIS_PREFIX', 'page-cache:' );
```


### FAQ - Nginx Map ###

**Q. My multisite already uses `WPMU_ACCEL_REDIRECT`. Do I still need Nginx Map?**
Expand All @@ -119,6 +134,9 @@ Please post your problem in [our free support forum](http://community.rtcamp.com

## Changelog ##

### 1.9.12 ###
* Allow override Redis host/port/prefix by defining constant in wp-config.php [#152](https://github.com/rtCamp/nginx-helper/pull/152) - by [vincent-lu](https://github.com/vincent-lu)

### 1.9.11 ###
* Fixed issue where permalinks without trailing slash does not purging [#124](https://github.com/rtCamp/nginx-helper/issues/124) - by Patrick
* Check whether role exist or not before removing capability. [#134](https://github.com/rtCamp/nginx-helper/pull/134) - by [1gor](https://github.com/1gor)
Expand Down Expand Up @@ -349,6 +367,5 @@ Fix url escaping [#82](https://github.com/rtCamp/nginx-helper/pull/82) - by

## Upgrade Notice ##

### 1.9.11 ###
* Fixed issue where permalinks without trailing slash does not purging [#124](https://github.com/rtCamp/nginx-helper/issues/124) - by Patrick
* Check whether role exist or not before removing capability. [#134](https://github.com/rtCamp/nginx-helper/pull/134) - by [1gor](https://github.com/1gor)
### 1.9.12 ###
* Allow override Redis host/port/prefix by defining constant in wp-config.php [#152](https://github.com/rtCamp/nginx-helper/pull/152) - by [vincent-lu](https://github.com/vincent-lu)
93 changes: 69 additions & 24 deletions admin/class-fastcgi-purger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* The admin-specific functionality of the plugin.
*
Expand All @@ -19,108 +18,154 @@
*/
class FastCGI_Purger extends Purger {

public function purgeUrl( $url, $feed = true ) {
/**
* Function to purge url.
*
* @param string $url URL.
* @param bool $feed Weather it is feed or not.
*/
public function purge_url( $url, $feed = true ) {

global $nginx_helper_admin;

$this->log( '- Purging URL | ' . $url );

$parse = parse_url( $url );
$parse = wp_parse_url( $url );

if ( ! isset( $parse['path'] ) ) {
$parse['path'] = '';
}

switch ( $nginx_helper_admin->options['purge_method'] ) {

case 'unlink_files':
$_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . $parse['path'];
$_url_purge = $_url_purge_base;

if ( isset( $parse['query'] ) && $parse['query'] != '' ) {
if ( isset( $parse['query'] ) && $parse['query'] !== '' ) {
$_url_purge .= '?' . $parse['query'];
}

$this->_delete_cache_file_for( $_url_purge );
$this->delete_cache_file_for( $_url_purge );

if ( $feed ) {

$feed_url = rtrim( $_url_purge_base, '/' ) . '/feed/';
$this->_delete_cache_file_for( $feed_url );
$this->_delete_cache_file_for( $feed_url . 'atom/' );
$this->_delete_cache_file_for( $feed_url . 'rdf/' );
$this->delete_cache_file_for( $feed_url );
$this->delete_cache_file_for( $feed_url . 'atom/' );
$this->delete_cache_file_for( $feed_url . 'rdf/' );

}
break;

case 'get_request':
// Go to default case
// Go to default case.
default:
$_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . '/purge' . $parse['path'];
$_url_purge = $_url_purge_base;

if ( isset( $parse['query'] ) && '' != $parse['query'] ) {
if ( isset( $parse['query'] ) && '' !== $parse['query'] ) {
$_url_purge .= '?' . $parse['query'];
}

$this->_do_remote_get( $_url_purge );
$this->do_remote_get( $_url_purge );

if ( $feed ) {

$feed_url = rtrim( $_url_purge_base, '/' ) . '/feed/';
$this->_do_remote_get( $feed_url );
$this->_do_remote_get( $feed_url . 'atom/' );
$this->_do_remote_get( $feed_url . 'rdf/' );
$this->do_remote_get( $feed_url );
$this->do_remote_get( $feed_url . 'atom/' );
$this->do_remote_get( $feed_url . 'rdf/' );

}
break;

}

}

public function customPurgeUrls() {
/**
* Function to custom purge urls.
*/
public function custom_purge_urls() {

global $nginx_helper_admin;

$parse = parse_url( site_url() );
$parse = wp_parse_url( site_url() );

$purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ?
explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array();

// Allow plugins/themes to modify/extend urls. Pass urls array in first parameter, second says if wildcards are allowed
/**
* Allow plugins/themes to modify/extend urls.
*
* @param array $purge_urls URLs which needs to be purged.
* @param bool $wildcard If wildcard in url is allowed or not. default false.
*/
$purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, false );

switch ( $nginx_helper_admin->options['purge_method'] ) {

case 'unlink_files':
$_url_purge_base = $parse['scheme'] . '://' . $parse['host'];

if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) {

foreach ( $purge_urls as $purge_url ) {

$purge_url = trim( $purge_url );

if ( strpos( $purge_url, '*' ) === false ) {

$purge_url = $_url_purge_base . $purge_url;
$this->log( '- Purging URL | ' . $url );
$this->_delete_cache_file_for( $purge_url );
$this->log( '- Purging URL | ' . $purge_url );
$this->delete_cache_file_for( $purge_url );

}

}

}
break;

case 'get_request':
// Go to default case
// Go to default case.
default:
$_url_purge_base = $parse['scheme'] . '://' . $parse['host'] . '/purge';

if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) {

foreach ( $purge_urls as $purge_url ) {

$purge_url = trim( $purge_url );

if ( strpos( $purge_url, '*' ) === false ) {

$purge_url = $_url_purge_base . $purge_url;
$this->log( '- Purging URL | ' . $url );
$this->_do_remote_get( $purge_url );
$this->log( '- Purging URL | ' . $purge_url );
$this->do_remote_get( $purge_url );

}

}

}
break;

}

}

public function purgeAll() {
$this->unlinkRecursive( RT_WP_NGINX_HELPER_CACHE_PATH, false );
/**
* Purge everything.
*/
public function purge_all() {

$this->unlink_recursive( RT_WP_NGINX_HELPER_CACHE_PATH, false );
$this->log( '* * * * *' );
$this->log( '* Purged Everything!' );
$this->log( '* * * * *' );

}

}
Loading