Cache Enabler – WordPress Cache - Version 1.0.7

Version Description

  • Added cache behavior option for new posts
  • Improved metainformation of the signature
  • Optimized cache handling for nginx
Download this release

Release Info

Developer keycdn
Plugin Icon 128x128 Cache Enabler – WordPress Cache
Version 1.0.7
Comparing to
See all releases

Code changes from version 1.0.6 to 1.0.7

cache-enabler.php CHANGED
@@ -6,7 +6,7 @@ Description: Simple and fast WordPress disk caching plugin.
6
  Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  License: GPLv2 or later
9
- Version: 1.0.6
10
  */
11
 
12
  /*
6
  Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  License: GPLv2 or later
9
+ Version: 1.0.7
10
  */
11
 
12
  /*
inc/cache_enabler.class.php CHANGED
@@ -63,7 +63,7 @@ final class Cache_Enabler {
63
  * constructor
64
  *
65
  * @since 1.0.0
66
- * @change 1.0.1
67
  *
68
  * @param void
69
  * @return void
@@ -86,7 +86,7 @@ final class Cache_Enabler {
86
 
87
  // clear cache hooks
88
  add_action(
89
- 'ce_remove_post_cache',
90
  array(
91
  __CLASS__,
92
  'clear_page_cache_by_post_id'
@@ -464,7 +464,7 @@ final class Cache_Enabler {
464
  * get options
465
  *
466
  * @since 1.0.0
467
- * @change 1.0.0
468
  *
469
  * @return array options array
470
  */
@@ -475,7 +475,7 @@ final class Cache_Enabler {
475
  get_option('cache'),
476
  array(
477
  'expires' => 0,
478
- 'if_loggedin' => 1,
479
  'new_comment' => 0,
480
  'webp' => 0,
481
  'excl_ids' => '',
@@ -895,7 +895,7 @@ final class Cache_Enabler {
895
  * delete post type cache on post updates
896
  *
897
  * @since 1.0.0
898
- * @change 1.0.0
899
  *
900
  * @param integer $post_ID Post ID
901
  */
@@ -914,7 +914,14 @@ final class Cache_Enabler {
914
 
915
  // purge cache if clean post on update
916
  if ( ! isset($_POST['_clear_post_cache_on_update']) ) {
917
- return self::clear_total_cache();
 
 
 
 
 
 
 
918
  }
919
 
920
  // validate nonce
@@ -995,6 +1002,26 @@ final class Cache_Enabler {
995
  }
996
 
997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
  /**
999
  * explode on comma
1000
  *
@@ -1072,7 +1099,7 @@ final class Cache_Enabler {
1072
  * check to bypass the cache
1073
  *
1074
  * @since 1.0.0
1075
- * @change 1.0.6
1076
  *
1077
  * @return boolean true if exception
1078
  *
@@ -1081,7 +1108,7 @@ final class Cache_Enabler {
1081
 
1082
  private static function _bypass_cache() {
1083
 
1084
- // skip cache hook
1085
  if ( apply_filters('bypass_cache', false) ) {
1086
  return true;
1087
  }
@@ -1105,12 +1132,12 @@ final class Cache_Enabler {
1105
  }
1106
 
1107
  // Request with query strings
1108
- if ( ! empty($_GET) && get_option('permalink_structure') ) {
1109
  return true;
1110
  }
1111
 
1112
  // if logged in
1113
- if ( $options['if_loggedin'] && self::_is_logged_in() ) {
1114
  return true;
1115
  }
1116
 
@@ -1488,7 +1515,7 @@ final class Cache_Enabler {
1488
  * validate settings
1489
  *
1490
  * @since 1.0.0
1491
- * @change 1.0.1
1492
  *
1493
  * @param array $data array form data
1494
  * @return array array form data valid
@@ -1506,7 +1533,7 @@ final class Cache_Enabler {
1506
 
1507
  return array(
1508
  'expires' => (int)$data['expires'],
1509
- 'if_loggedin' => (int)(!empty($data['if_loggedin'])),
1510
  'new_comment' => (int)(!empty($data['new_comment'])),
1511
  'webp' => (int)(!empty($data['webp'])),
1512
  'excl_ids' => (string)sanitize_text_field(@$data['excl_ids']),
@@ -1519,7 +1546,7 @@ final class Cache_Enabler {
1519
  * settings page
1520
  *
1521
  * @since 1.0.0
1522
- * @change 1.0.1
1523
  */
1524
 
1525
  public static function settings_page() { ?>
@@ -1557,9 +1584,9 @@ final class Cache_Enabler {
1557
  </th>
1558
  <td>
1559
  <fieldset>
1560
- <label for="cache_if_loggedin">
1561
- <input type="checkbox" name="cache[if_loggedin]" id="cache_if_loggedin" value="1" <?php checked('1', $options['if_loggedin']); ?> />
1562
- <?php _e("Disable caching if logged in.", "cache") ?>
1563
  </label>
1564
 
1565
  <br />
63
  * constructor
64
  *
65
  * @since 1.0.0
66
+ * @change 1.0.7
67
  *
68
  * @param void
69
  * @return void
86
 
87
  // clear cache hooks
88
  add_action(
89
+ 'ce_clear_post_cache',
90
  array(
91
  __CLASS__,
92
  'clear_page_cache_by_post_id'
464
  * get options
465
  *
466
  * @since 1.0.0
467
+ * @change 1.0.7
468
  *
469
  * @return array options array
470
  */
475
  get_option('cache'),
476
  array(
477
  'expires' => 0,
478
+ 'new_post' => 0,
479
  'new_comment' => 0,
480
  'webp' => 0,
481
  'excl_ids' => '',
895
  * delete post type cache on post updates
896
  *
897
  * @since 1.0.0
898
+ * @change 1.0.7
899
  *
900
  * @param integer $post_ID Post ID
901
  */
914
 
915
  // purge cache if clean post on update
916
  if ( ! isset($_POST['_clear_post_cache_on_update']) ) {
917
+
918
+ // clear complete cache if option enabled
919
+ if ( self::$options['new_post'] ) {
920
+ return self::clear_total_cache();
921
+ } else {
922
+ return self::clear_home_page_cache();
923
+ }
924
+
925
  }
926
 
927
  // validate nonce
1002
  }
1003
 
1004
 
1005
+ /**
1006
+ * clear home page cache
1007
+ *
1008
+ * @since 1.0.7
1009
+ * @change 1.0.7
1010
+ *
1011
+ */
1012
+
1013
+ public static function clear_home_page_cache() {
1014
+
1015
+ call_user_func(
1016
+ array(
1017
+ self::$disk,
1018
+ 'clear_home'
1019
+ )
1020
+ );
1021
+
1022
+ }
1023
+
1024
+
1025
  /**
1026
  * explode on comma
1027
  *
1099
  * check to bypass the cache
1100
  *
1101
  * @since 1.0.0
1102
+ * @change 1.0.7
1103
  *
1104
  * @return boolean true if exception
1105
  *
1108
 
1109
  private static function _bypass_cache() {
1110
 
1111
+ // bypass cache hook
1112
  if ( apply_filters('bypass_cache', false) ) {
1113
  return true;
1114
  }
1132
  }
1133
 
1134
  // Request with query strings
1135
+ if ( ! empty($_GET) && ! isset( $_GET['utm_source'], $_GET['utm_medium'], $_GET['utm_campaign'] ) && get_option('permalink_structure') ) {
1136
  return true;
1137
  }
1138
 
1139
  // if logged in
1140
+ if ( self::_is_logged_in() ) {
1141
  return true;
1142
  }
1143
 
1515
  * validate settings
1516
  *
1517
  * @since 1.0.0
1518
+ * @change 1.0.7
1519
  *
1520
  * @param array $data array form data
1521
  * @return array array form data valid
1533
 
1534
  return array(
1535
  'expires' => (int)$data['expires'],
1536
+ 'new_post' => (int)(!empty($data['new_post'])),
1537
  'new_comment' => (int)(!empty($data['new_comment'])),
1538
  'webp' => (int)(!empty($data['webp'])),
1539
  'excl_ids' => (string)sanitize_text_field(@$data['excl_ids']),
1546
  * settings page
1547
  *
1548
  * @since 1.0.0
1549
+ * @change 1.0.7
1550
  */
1551
 
1552
  public static function settings_page() { ?>
1584
  </th>
1585
  <td>
1586
  <fieldset>
1587
+ <label for="cache_new_post">
1588
+ <input type="checkbox" name="cache[new_post]" id="cache_new_post" value="1" <?php checked('1', $options['new_post']); ?> />
1589
+ <?php _e("Clear the complete cache if a new post has been published (instead of only the home page cache).", "cache") ?>
1590
  </label>
1591
 
1592
  <br />
inc/cache_enabler_disk.class.php CHANGED
@@ -14,6 +14,21 @@ defined('ABSPATH') OR exit;
14
  final class Cache_Enabler_Disk {
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  /**
18
  * permalink check
19
  *
@@ -46,7 +61,7 @@ final class Cache_Enabler_Disk {
46
 
47
  // save asset
48
  self::_create_files(
49
- $data . self::_cache_signatur()
50
  );
51
 
52
  }
@@ -137,11 +152,34 @@ final class Cache_Enabler_Disk {
137
  }
138
 
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  /**
141
  * get asset
142
  *
143
  * @since 1.0.0
144
- * @change 1.0.3
145
  */
146
 
147
  public static function get_asset() {
@@ -151,9 +189,11 @@ final class Cache_Enabler_Disk {
151
  $headers = apache_request_headers();
152
  $http_if_modified_since = ( isset( $headers[ 'If-Modified-Since' ] ) ) ? $headers[ 'If-Modified-Since' ] : '';
153
  $http_accept = ( isset( $headers[ 'Accept' ] ) ) ? $headers[ 'Accept' ] : '';
 
154
  } else {
155
  $http_if_modified_since = ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '';
156
  $http_accept = ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '';
 
157
  }
158
 
159
  // check modified since with cached file and return 304 if no difference
@@ -162,14 +202,21 @@ final class Cache_Enabler_Disk {
162
  exit;
163
  }
164
 
165
- // check webp support
166
- if ( $http_accept && ( strpos($http_accept, 'webp') !== false ) && is_readable( self::_file_webp() ) ) {
167
  header('Content-Encoding: gzip');
168
- readfile( self::_file_webp() );
169
  exit;
170
  }
171
 
172
- // deliver cached file
 
 
 
 
 
 
 
173
  readfile( self::_file_html() );
174
  exit;
175
  }
@@ -186,7 +233,7 @@ final class Cache_Enabler_Disk {
186
 
187
  private static function _cache_signatur() {
188
  return sprintf(
189
- "\n\n<!-- %s @ %s -->",
190
  'Cache Enabler by KeyCDN',
191
  date_i18n(
192
  'd.m.Y H:i:s',
@@ -212,16 +259,21 @@ final class Cache_Enabler_Disk {
212
  wp_die('Unable to create directory.');
213
  }
214
 
 
 
 
215
  // create files
216
- self::_create_file( self::_file_html(), $data );
217
- self::_create_file( self::_file_gzip(), gzencode($data."\n<!-- (gzip) -->", 9) );
218
 
219
  // cache enabler options
220
  $options = Cache_Enabler::$options;
221
 
222
  // create webp supported files
223
  if ($options['webp']) {
224
- self::_create_file( self::_file_webp(), gzencode(self::_convert_webp($data)."\n<!-- (webp) -->", 9) );
 
 
225
  }
226
 
227
  }
@@ -390,13 +442,13 @@ final class Cache_Enabler_Disk {
390
  * get file path
391
  *
392
  * @since 1.0.0
393
- * @change 1.0.0
394
  *
395
  * @return string path to the html file
396
  */
397
 
398
  private static function _file_html() {
399
- return self::_file_path(). 'index.html';
400
  }
401
 
402
 
@@ -404,27 +456,41 @@ final class Cache_Enabler_Disk {
404
  * get gzip file path
405
  *
406
  * @since 1.0.1
407
- * @change 1.0.1
408
  *
409
  * @return string path to the gzipped html file
410
  */
411
 
412
  private static function _file_gzip() {
413
- return self::_file_path(). 'index.html.gz';
414
  }
415
 
416
 
417
  /**
418
  * get webp file path
419
  *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420
  * @since 1.0.1
421
- * @change 1.0.4
422
  *
423
  * @return string path to the webp gzipped html file
424
  */
425
 
426
- private static function _file_webp() {
427
- return self::_file_path(). 'index-webp.html.gz';
428
  }
429
 
430
 
14
  final class Cache_Enabler_Disk {
15
 
16
 
17
+ /**
18
+ * cached filename settings
19
+ *
20
+ * @since 1.0.7
21
+ * @change 1.0.7
22
+ *
23
+ * @var string
24
+ */
25
+
26
+ const FILE_HTML = 'index.html';
27
+ const FILE_GZIP = 'index.html.gz';
28
+ const FILE_WEBP_HTML = 'index-webp.html';
29
+ const FILE_WEBP_GZIP = 'index-webp.html.gz';
30
+
31
+
32
  /**
33
  * permalink check
34
  *
61
 
62
  // save asset
63
  self::_create_files(
64
+ $data
65
  );
66
 
67
  }
152
  }
153
 
154
 
155
+ /**
156
+ * clear home cache
157
+ *
158
+ * @since 1.0.7
159
+ * @change 1.0.7
160
+ */
161
+
162
+ public static function clear_home() {
163
+ $path = sprintf(
164
+ '%s%s%s%s',
165
+ CE_CACHE_DIR,
166
+ DIRECTORY_SEPARATOR,
167
+ preg_replace('#^https?://#', '', get_option('siteurl')),
168
+ DIRECTORY_SEPARATOR
169
+ );
170
+
171
+ unlink($path.self::FILE_HTML);
172
+ unlink($path.self::FILE_GZIP);
173
+ unlink($path.self::FILE_WEBP_HTML);
174
+ unlink($path.self::FILE_WEBP_GZIP);
175
+ }
176
+
177
+
178
  /**
179
  * get asset
180
  *
181
  * @since 1.0.0
182
+ * @change 1.0.7
183
  */
184
 
185
  public static function get_asset() {
189
  $headers = apache_request_headers();
190
  $http_if_modified_since = ( isset( $headers[ 'If-Modified-Since' ] ) ) ? $headers[ 'If-Modified-Since' ] : '';
191
  $http_accept = ( isset( $headers[ 'Accept' ] ) ) ? $headers[ 'Accept' ] : '';
192
+ $http_accept_encoding = ( isset( $headers[ 'Accept-Encoding' ] ) ) ? $headers[ 'Accept-Encoding' ] : '';
193
  } else {
194
  $http_if_modified_since = ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '';
195
  $http_accept = ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '';
196
+ $http_accept_encoding = ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] : '';
197
  }
198
 
199
  // check modified since with cached file and return 304 if no difference
202
  exit;
203
  }
204
 
205
+ // check webp and deliver gzip webp file if support
206
+ if ( $http_accept && ( strpos($http_accept, 'webp') !== false ) && is_readable( self::_file_webp_gzip() ) ) {
207
  header('Content-Encoding: gzip');
208
+ readfile( self::_file_webp_gzip() );
209
  exit;
210
  }
211
 
212
+ // check encoding and deliver gzip file if support
213
+ if ( $http_accept_encoding && ( strpos($http_accept_encoding, 'gzip') !== false ) ) {
214
+ header('Content-Encoding: gzip');
215
+ readfile( self::_file_gzip() );
216
+ exit;
217
+ }
218
+
219
+ // deliver cached file (default)
220
  readfile( self::_file_html() );
221
  exit;
222
  }
233
 
234
  private static function _cache_signatur() {
235
  return sprintf(
236
+ "\n\n<!-- %s @ %s",
237
  'Cache Enabler by KeyCDN',
238
  date_i18n(
239
  'd.m.Y H:i:s',
259
  wp_die('Unable to create directory.');
260
  }
261
 
262
+ // get base signature
263
+ $cache_signature = self::_cache_signatur();
264
+
265
  // create files
266
+ self::_create_file( self::_file_html(), $data.$cache_signature." (html) -->" );
267
+ self::_create_file( self::_file_gzip(), gzencode($data.$cache_signature." (html gzip) -->", 9) );
268
 
269
  // cache enabler options
270
  $options = Cache_Enabler::$options;
271
 
272
  // create webp supported files
273
  if ($options['webp']) {
274
+ $converted_data = self::_convert_webp($data);
275
+ self::_create_file( self::_file_webp_html(), $converted_data.$cache_signature." (webp) -->" );
276
+ self::_create_file( self::_file_webp_gzip(), gzencode($converted_data.$cache_signature." (webp gzip) -->", 9) );
277
  }
278
 
279
  }
442
  * get file path
443
  *
444
  * @since 1.0.0
445
+ * @change 1.0.7
446
  *
447
  * @return string path to the html file
448
  */
449
 
450
  private static function _file_html() {
451
+ return self::_file_path(). self::FILE_HTML;
452
  }
453
 
454
 
456
  * get gzip file path
457
  *
458
  * @since 1.0.1
459
+ * @change 1.0.7
460
  *
461
  * @return string path to the gzipped html file
462
  */
463
 
464
  private static function _file_gzip() {
465
+ return self::_file_path(). self::FILE_GZIP;
466
  }
467
 
468
 
469
  /**
470
  * get webp file path
471
  *
472
+ * @since 1.0.7
473
+ * @change 1.0.7
474
+ *
475
+ * @return string path to the webp html file
476
+ */
477
+
478
+ private static function _file_webp_html() {
479
+ return self::_file_path(). self::FILE_WEBP_HTML;
480
+ }
481
+
482
+
483
+ /**
484
+ * get gzip webp file path
485
+ *
486
  * @since 1.0.1
487
+ * @change 1.0.7
488
  *
489
  * @return string path to the webp gzipped html file
490
  */
491
 
492
+ private static function _file_webp_gzip() {
493
+ return self::_file_path(). self::FILE_WEBP_GZIP;
494
  }
495
 
496
 
readme.txt CHANGED
@@ -57,6 +57,11 @@ This plugin is partially based on the Cachify plugin developed by [Sergej Mülle
57
 
58
  == Changelog ==
59
 
 
 
 
 
 
60
  = 1.0.6 =
61
  * Fixed query string related caching issue
62
 
57
 
58
  == Changelog ==
59
 
60
+ = 1.0.7 =
61
+ * Added cache behavior option for new posts
62
+ * Improved metainformation of the signature
63
+ * Optimized cache handling for nginx
64
+
65
  = 1.0.6 =
66
  * Fixed query string related caching issue
67