Nginx Helper - Version 2.1.0

Version Description

  • Add wildcard cache key deletion for device type cache purge. #203 - by pradeep910
  • Add filter rt_nginx_helper_purge_url to filter the URL to be purged. #182 - by todeveni
  • Add filter rt_nginx_helper_purge_cached_file to filter the cached file name. #182 - by todeveni
  • Add filter rt_nginx_helper_remote_purge_url to filter remote URL to be purged. #182 - by todeveni
  • Add action rt_nginx_helper_after_fastcgi_purge_all to fire after the FastCGI cache has been purged. #182 - by todeveni
  • Add action rt_nginx_helper_after_redis_purge_all to fire after the Redis cache has been purged. #182 - by todeveni
  • Add action rt_nginx_helper_purged_file to fire an action after deleting file from cache. #182 - by todeveni
  • Add action rt_nginx_helper_before_remote_purge_url to fire an action before purging remote URL. #182 - by todeveni
  • Add action rt_nginx_helper_after_remote_purge_url to fire an action after remote purge request. #182 - by todeveni
  • Fix issue with post purge on new comments. #175 - by jinschoi
  • Fix Nginx Timestamp being added to invalid content type. #200 - by thrijith
  • Handle filesize exception while truncating nginx.log file. #206 - by peterjanes
Download this release

Release Info

Developer thrijith
Plugin Icon 128x128 Nginx Helper
Version 2.1.0
Comparing to
See all releases

Code changes from version 2.0.3 to 2.1.0

admin/class-fastcgi-purger.php CHANGED
@@ -28,6 +28,15 @@ class FastCGI_Purger extends Purger {
28
 
29
  global $nginx_helper_admin;
30
 
 
 
 
 
 
 
 
 
 
31
  $this->log( '- Purging URL | ' . $url );
32
 
33
  $parse = wp_parse_url( $url );
@@ -166,6 +175,12 @@ class FastCGI_Purger extends Purger {
166
  $this->log( '* Purged Everything!' );
167
  $this->log( '* * * * *' );
168
 
 
 
 
 
 
 
169
  }
170
 
171
  }
28
 
29
  global $nginx_helper_admin;
30
 
31
+ /**
32
+ * Filters the URL to be purged.
33
+ *
34
+ * @since 2.1.0
35
+ *
36
+ * @param string $url URL to be purged.
37
+ */
38
+ $url = apply_filters( 'rt_nginx_helper_purge_url', $url );
39
+
40
  $this->log( '- Purging URL | ' . $url );
41
 
42
  $parse = wp_parse_url( $url );
175
  $this->log( '* Purged Everything!' );
176
  $this->log( '* * * * *' );
177
 
178
+ /**
179
+ * Fire an action after the FastCGI cache has been purged.
180
+ *
181
+ * @since 2.1.0
182
+ */
183
+ do_action( 'rt_nginx_helper_after_fastcgi_purge_all' );
184
  }
185
 
186
  }
admin/class-nginx-helper-admin.php CHANGED
@@ -427,10 +427,11 @@ class Nginx_Helper_Admin {
427
 
428
  foreach ( headers_list() as $header ) {
429
  list( $key, $value ) = explode( ':', $header, 2 );
430
- if ( 'Content-Type' === $key && strpos( trim( $value ), 'text/html' ) !== 0 ) {
 
431
  return;
432
  }
433
- if ( 'Content-Type' === $key ) {
434
  break;
435
  }
436
  }
427
 
428
  foreach ( headers_list() as $header ) {
429
  list( $key, $value ) = explode( ':', $header, 2 );
430
+ $key = strtolower( $key );
431
+ if ( 'content-type' === $key && strpos( trim( $value ), 'text/html' ) !== 0 ) {
432
  return;
433
  }
434
+ if ( 'content-type' === $key ) {
435
  break;
436
  }
437
  }
admin/class-phpredis-purger.php CHANGED
@@ -85,6 +85,12 @@ class PhpRedis_Purger extends Purger {
85
 
86
  $this->log( '* * * * *' );
87
 
 
 
 
 
 
 
88
  }
89
 
90
  /**
@@ -97,6 +103,15 @@ class PhpRedis_Purger extends Purger {
97
 
98
  global $nginx_helper_admin;
99
 
 
 
 
 
 
 
 
 
 
100
  $parse = wp_parse_url( $url );
101
 
102
  if ( ! isset( $parse['path'] ) ) {
@@ -105,12 +120,44 @@ class PhpRedis_Purger extends Purger {
105
 
106
  $prefix = $nginx_helper_admin->options['redis_prefix'];
107
  $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'];
108
- $is_purged = $this->delete_single_key( $_url_purge_base );
109
 
110
- if ( $is_purged ) {
111
- $this->log( '- Purged URL | ' . $url );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  } else {
113
- $this->log( '- Cache Not Found | ' . $url, 'ERROR' );
 
 
 
 
 
 
 
 
114
  }
115
 
116
  $this->log( '* * * * *' );
85
 
86
  $this->log( '* * * * *' );
87
 
88
+ /**
89
+ * Fire an action after the Redis cache has been purged.
90
+ *
91
+ * @since 2.1.0
92
+ */
93
+ do_action( 'rt_nginx_helper_after_redis_purge_all' );
94
  }
95
 
96
  /**
103
 
104
  global $nginx_helper_admin;
105
 
106
+ /**
107
+ * Filters the URL to be purged.
108
+ *
109
+ * @since 2.1.0
110
+ *
111
+ * @param string $url URL to be purged.
112
+ */
113
+ $url = apply_filters( 'rt_nginx_helper_purge_url', $url );
114
+
115
  $parse = wp_parse_url( $url );
116
 
117
  if ( ! isset( $parse['path'] ) ) {
120
 
121
  $prefix = $nginx_helper_admin->options['redis_prefix'];
122
  $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'];
 
123
 
124
+ /**
125
+ * To delete device type caches such as `<URL>--mobile`, `<URL>--desktop`, `<URL>--lowend`, etc.
126
+ * This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache.
127
+ *
128
+ * For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL.
129
+ * Add this filter in separate plugin or simply in theme's function.php file:
130
+ * ```
131
+ * add_filter( 'rt_nginx_helper_purge_url', function( $url ) {
132
+ * $url = $url . '--*';
133
+ * return $url;
134
+ * });
135
+ * ```
136
+ *
137
+ * Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted.
138
+ *
139
+ * @since 2.1.0
140
+ */
141
+ if ( strpos( $_url_purge_base, '*' ) === false ) {
142
+
143
+ $status = $this->delete_single_key( $_url_purge_base );
144
+
145
+ if ( $status ) {
146
+ $this->log( '- Purge URL | ' . $_url_purge_base );
147
+ } else {
148
+ $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' );
149
+ }
150
+
151
  } else {
152
+
153
+ $status = $this->delete_keys_by_wildcard( $_url_purge_base );
154
+
155
+ if ( $status ) {
156
+ $this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' );
157
+ } else {
158
+ $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' );
159
+ }
160
+
161
  }
162
 
163
  $this->log( '* * * * *' );
admin/class-predis-purger.php CHANGED
@@ -82,6 +82,12 @@ class Predis_Purger extends Purger {
82
 
83
  $this->log( '* * * * *' );
84
 
 
 
 
 
 
 
85
  }
86
 
87
  /**
@@ -94,6 +100,15 @@ class Predis_Purger extends Purger {
94
 
95
  global $nginx_helper_admin;
96
 
 
 
 
 
 
 
 
 
 
97
  $this->log( '- Purging URL | ' . $url );
98
 
99
  $parse = wp_parse_url( $url );
@@ -104,7 +119,45 @@ class Predis_Purger extends Purger {
104
 
105
  $prefix = $nginx_helper_admin->options['redis_prefix'];
106
  $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'];
107
- $this->delete_single_key( $_url_purge_base );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  }
110
 
82
 
83
  $this->log( '* * * * *' );
84
 
85
+ /**
86
+ * Fire an action after the Redis cache has been purged.
87
+ *
88
+ * @since 2.1.0
89
+ */
90
+ do_action( 'rt_nginx_helper_after_redis_purge_all' );
91
  }
92
 
93
  /**
100
 
101
  global $nginx_helper_admin;
102
 
103
+ /**
104
+ * Filters the URL to be purged.
105
+ *
106
+ * @since 2.1.0
107
+ *
108
+ * @param string $url URL to be purged.
109
+ */
110
+ $url = apply_filters( 'rt_nginx_helper_purge_url', $url );
111
+
112
  $this->log( '- Purging URL | ' . $url );
113
 
114
  $parse = wp_parse_url( $url );
119
 
120
  $prefix = $nginx_helper_admin->options['redis_prefix'];
121
  $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'];
122
+
123
+ /**
124
+ * To delete device type caches such as `<URL>--mobile`, `<URL>--desktop`, `<URL>--lowend`, etc.
125
+ * This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache.
126
+ *
127
+ * For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL.
128
+ * Add this filter in separate plugin or simply in theme's function.php file:
129
+ * ```
130
+ * add_filter( 'rt_nginx_helper_purge_url', function( $url ) {
131
+ * $url = $url . '--*';
132
+ * return $url;
133
+ * });
134
+ * ```
135
+ *
136
+ * Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted.
137
+ *
138
+ * @since 2.1.0
139
+ */
140
+ if ( strpos( $_url_purge_base, '*' ) === false ) {
141
+
142
+ $status = $this->delete_single_key( $_url_purge_base );
143
+
144
+ if ( $status ) {
145
+ $this->log( '- Purge URL | ' . $_url_purge_base );
146
+ } else {
147
+ $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' );
148
+ }
149
+
150
+ } else {
151
+
152
+ $status = $this->delete_keys_by_wildcard( $_url_purge_base );
153
+
154
+ if ( $status ) {
155
+ $this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' );
156
+ } else {
157
+ $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' );
158
+ }
159
+
160
+ }
161
 
162
  }
163
 
admin/class-purger.php CHANGED
@@ -96,7 +96,7 @@ abstract class Purger {
96
  switch ( $newstatus ) {
97
 
98
  case 'approved':
99
- if ( 1 === $nginx_helper_admin->options['purge_page_on_new_comment'] ) {
100
 
101
  $this->log( '* Comment ( ' . $_comment_id . ' ) approved. Post ( ' . $_post_id . ' ) purging...' );
102
  $this->log( '* * * * *' );
@@ -397,6 +397,15 @@ abstract class Purger {
397
  // Set path to cached file.
398
  $cached_file = $cache_path . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash;
399
 
 
 
 
 
 
 
 
 
 
400
  // Verify cached file exists.
401
  if ( ! file_exists( $cached_file ) ) {
402
 
@@ -408,6 +417,16 @@ abstract class Purger {
408
  // Delete the cached file.
409
  if ( unlink( $cached_file ) ) {
410
  $this->log( '- - ' . $url . ' *** PURGED ***' );
 
 
 
 
 
 
 
 
 
 
411
  } else {
412
  $this->log( '- - An error occurred deleting the cache file. Check the server logs for a PHP warning.', 'ERROR' );
413
  }
@@ -420,6 +439,23 @@ abstract class Purger {
420
  * @param string $url URL to do remote request.
421
  */
422
  protected function do_remote_get( $url ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
 
424
  $response = wp_remote_get( $url );
425
 
@@ -447,6 +483,15 @@ abstract class Purger {
447
 
448
  }
449
 
 
 
 
 
 
 
 
 
 
450
  }
451
 
452
  }
@@ -514,8 +559,16 @@ abstract class Purger {
514
 
515
  global $nginx_helper_admin;
516
 
 
 
 
 
517
  $nginx_asset_path = $nginx_helper_admin->functional_asset_path() . 'nginx.log';
518
 
 
 
 
 
519
  $max_size_allowed = ( is_numeric( $nginx_helper_admin->options['log_filesize'] ) ) ? $nginx_helper_admin->options['log_filesize'] * 1048576 : 5242880;
520
 
521
  $fileSize = filesize( $nginx_asset_path );
96
  switch ( $newstatus ) {
97
 
98
  case 'approved':
99
+ if ( 1 === (int)$nginx_helper_admin->options['purge_page_on_new_comment'] ) {
100
 
101
  $this->log( '* Comment ( ' . $_comment_id . ' ) approved. Post ( ' . $_post_id . ' ) purging...' );
102
  $this->log( '* * * * *' );
397
  // Set path to cached file.
398
  $cached_file = $cache_path . substr( $hash, -1 ) . '/' . substr( $hash, -3, 2 ) . '/' . $hash;
399
 
400
+ /**
401
+ * Filters the cached file name.
402
+ *
403
+ * @since 2.1.0
404
+ *
405
+ * @param string $cached_file Cached file name.
406
+ */
407
+ $cached_file = apply_filters( 'rt_nginx_helper_purge_cached_file', $cached_file );
408
+
409
  // Verify cached file exists.
410
  if ( ! file_exists( $cached_file ) ) {
411
 
417
  // Delete the cached file.
418
  if ( unlink( $cached_file ) ) {
419
  $this->log( '- - ' . $url . ' *** PURGED ***' );
420
+
421
+ /**
422
+ * Fire an action after deleting file from cache.
423
+ *
424
+ * @since 2.1.0
425
+ *
426
+ * @param string $url URL to be purged.
427
+ * @param string $cached_file Cached file name.
428
+ */
429
+ do_action( 'rt_nginx_helper_purged_file', $url, $cached_file );
430
  } else {
431
  $this->log( '- - An error occurred deleting the cache file. Check the server logs for a PHP warning.', 'ERROR' );
432
  }
439
  * @param string $url URL to do remote request.
440
  */
441
  protected function do_remote_get( $url ) {
442
+ /**
443
+ * Filters the URL to be purged.
444
+ *
445
+ * @since 2.1.0
446
+ *
447
+ * @param string $url URL to be purged.
448
+ */
449
+ $url = apply_filters( 'rt_nginx_helper_remote_purge_url', $url );
450
+
451
+ /**
452
+ * Fire an action before purging URL.
453
+ *
454
+ * @since 2.1.0
455
+ *
456
+ * @param string $url URL to be purged.
457
+ */
458
+ do_action( 'rt_nginx_helper_before_remote_purge_url', $url );
459
 
460
  $response = wp_remote_get( $url );
461
 
483
 
484
  }
485
 
486
+ /**
487
+ * Fire an action after remote purge request.
488
+ *
489
+ * @since 2.1.0
490
+ *
491
+ * @param string $url URL to be purged.
492
+ * @param array $response Array of results including HTTP headers.
493
+ */
494
+ do_action( 'rt_nginx_helper_after_remote_purge_url', $url, $response );
495
  }
496
 
497
  }
559
 
560
  global $nginx_helper_admin;
561
 
562
+ if ( ! $nginx_helper_admin->options['enable_log'] ) {
563
+ return;
564
+ }
565
+
566
  $nginx_asset_path = $nginx_helper_admin->functional_asset_path() . 'nginx.log';
567
 
568
+ if ( ! file_exists($nginx_asset_path) ) {
569
+ return;
570
+ }
571
+
572
  $max_size_allowed = ( is_numeric( $nginx_helper_admin->options['log_filesize'] ) ) ? $nginx_helper_admin->options['log_filesize'] * 1048576 : 5242880;
573
 
574
  $fileSize = filesize( $nginx_asset_path );
nginx-helper.php CHANGED
@@ -3,13 +3,13 @@
3
  * Plugin Name: Nginx Helper
4
  * Plugin URI: https://rtcamp.com/nginx-helper/
5
  * Description: Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does few more things.
6
- * Version: 2.0.3
7
  * Author: rtCamp
8
  * Author URI: https://rtcamp.com
9
  * Text Domain: nginx-helper
10
  * Domain Path: /languages
11
  * Requires at least: 3.0
12
- * Tested up to: 5.1
13
  *
14
  * @link https://rtcamp.com/nginx-helper/
15
  * @since 2.0.0
3
  * Plugin Name: Nginx Helper
4
  * Plugin URI: https://rtcamp.com/nginx-helper/
5
  * Description: Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does few more things.
6
+ * Version: 2.1.0
7
  * Author: rtCamp
8
  * Author URI: https://rtcamp.com
9
  * Text Domain: nginx-helper
10
  * Domain Path: /languages
11
  * Requires at least: 3.0
12
+ * Tested up to: 5.2.2
13
  *
14
  * @link https://rtcamp.com/nginx-helper/
15
  * @since 2.0.0
readme.txt CHANGED
@@ -5,8 +5,8 @@ Tags: nginx, cache, purge, nginx map, nginx cache, maps, fastcgi, proxy, redis,
5
  License: GPLv2 or later (of-course)
6
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
7
  Requires at least: 3.0
8
- Tested up to: 5.1
9
- Stable tag: 2.0.3
10
 
11
  Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things.
12
 
@@ -127,6 +127,20 @@ Please post your problem in [our free support forum](http://community.rtcamp.com
127
 
128
  == Changelog ==
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  = 2.0.3 =
131
  * Update article link for fastcgi cache purge. [#187](https://github.com/rtCamp/nginx-helper/pull/187) - by [gagan0123](https://github.com/gagan0123)
132
  * Fix map generation issue on `SUBDOMAIN_INSTALL`. [#189](https://github.com/rtCamp/nginx-helper/pull/189) - by [ChrisHardie](https://github.com/ChrisHardie)
@@ -381,6 +395,5 @@ Fix url escaping [#82](https://github.com/rtCamp/nginx-helper/pull/82) - by
381
 
382
  == Upgrade Notice ==
383
 
384
- = 2.0.3 =
385
- * Update article link for fastcgi cache purge. [#187](https://github.com/rtCamp/nginx-helper/pull/187) - by [gagan0123](https://github.com/gagan0123)
386
- * Fix map generation issue on `SUBDOMAIN_INSTALL`. [#189](https://github.com/rtCamp/nginx-helper/pull/189) - by [ChrisHardie](https://github.com/ChrisHardie)
5
  License: GPLv2 or later (of-course)
6
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
7
  Requires at least: 3.0
8
+ Tested up to: 5.2.2
9
+ Stable tag: 2.1.0
10
 
11
  Cleans nginx's fastcgi/proxy cache or redis-cache whenever a post is edited/published. Also does a few more things.
12
 
127
 
128
  == Changelog ==
129
 
130
+ = 2.1.0 =
131
+ * Add wildcard cache key deletion for device type cache purge. [#203](https://github.com/rtCamp/nginx-helper/pull/203) - by [pradeep910](https://github.com/pradeep910)
132
+ * Add filter `rt_nginx_helper_purge_url` to filter the URL to be purged. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
133
+ * Add filter `rt_nginx_helper_purge_cached_file` to filter the cached file name. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
134
+ * Add filter `rt_nginx_helper_remote_purge_url` to filter remote URL to be purged. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
135
+ * Add action `rt_nginx_helper_after_fastcgi_purge_all` to fire after the FastCGI cache has been purged. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
136
+ * Add action `rt_nginx_helper_after_redis_purge_all` to fire after the Redis cache has been purged. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
137
+ * Add action `rt_nginx_helper_purged_file` to fire an action after deleting file from cache. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
138
+ * Add action `rt_nginx_helper_before_remote_purge_url` to fire an action before purging remote URL. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
139
+ * Add action `rt_nginx_helper_after_remote_purge_url` to fire an action after remote purge request. [#182](https://github.com/rtCamp/nginx-helper/pull/182) - by [todeveni](https://github.com/todeveni)
140
+ * Fix issue with post purge on new comments. [#175](https://github.com/rtCamp/nginx-helper/pull/175) - by [jinschoi](https://github.com/jinschoi)
141
+ * Fix Nginx Timestamp being added to invalid content type. [#200](https://github.com/rtCamp/nginx-helper/pull/200) - by [thrijith](https://github.com/thrijith)
142
+ * Handle filesize exception while truncating nginx.log file. [#206](https://github.com/rtCamp/nginx-helper/pull/206) - by [peterjanes](https://github.com/peterjanes)
143
+
144
  = 2.0.3 =
145
  * Update article link for fastcgi cache purge. [#187](https://github.com/rtCamp/nginx-helper/pull/187) - by [gagan0123](https://github.com/gagan0123)
146
  * Fix map generation issue on `SUBDOMAIN_INSTALL`. [#189](https://github.com/rtCamp/nginx-helper/pull/189) - by [ChrisHardie](https://github.com/ChrisHardie)
395
 
396
  == Upgrade Notice ==
397
 
398
+ = 2.1.0 =
399
+ Nginx Helper 2.1.0, introduces new Hooks to extend cache purge, Adds support for wildcard cache key deletion for device type cache and bug fixes.