Cache Enabler – WordPress Cache - Version 1.2.1

Version Description

  • Minor fixes
Download this release

Release Info

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

Code changes from version 1.2.0 to 1.2.1

advanced-cache.php CHANGED
@@ -2,36 +2,36 @@
2
 
3
  // check if request method is GET
4
  if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || $_SERVER['REQUEST_METHOD'] != 'GET' ) {
5
- return false;
6
  }
7
 
8
  // check if request with query strings
9
  if ( ! empty($_GET) && ! isset( $_GET['utm_source'], $_GET['utm_medium'], $_GET['utm_campaign'] ) ) {
10
- return false;
11
  }
12
 
13
  // check cookie values
14
  if ( !empty($_COOKIE) ) {
15
- foreach ( $_COOKIE as $k => $v) {
16
- if ( preg_match('/^(wp-postpass|wordpress_logged_in|comment_author)_/', $k) ) {
17
- return false;
18
- }
19
- }
20
  }
21
 
22
  // base path
23
  $path = sprintf(
24
- '%s%s%s%s',
25
- WP_CONTENT_DIR . '/cache/cache-enabler',
26
- DIRECTORY_SEPARATOR,
27
- parse_url(
28
- 'http://' .strtolower($_SERVER['HTTP_HOST']),
29
- PHP_URL_HOST
30
- ),
31
- parse_url(
32
- $_SERVER['REQUEST_URI'],
33
- PHP_URL_PATH
34
- )
35
  );
36
 
37
  // add trailing slash
@@ -45,50 +45,52 @@ $path_webp_gzip = $path . 'index-webp.html.gz';
45
 
46
  if ( is_readable( $path_html ) ) {
47
 
48
- // set cache handler header
49
- header('x-cache-handler: wp');
50
 
51
- // get if-modified request headers
52
- if ( function_exists( 'apache_request_headers' ) ) {
53
- $headers = apache_request_headers();
54
- $http_if_modified_since = ( isset( $headers[ 'If-Modified-Since' ] ) ) ? $headers[ 'If-Modified-Since' ] : '';
55
- $http_accept = ( isset( $headers[ 'Accept' ] ) ) ? $headers[ 'Accept' ] : '';
56
- $http_accept_encoding = ( isset( $headers[ 'Accept-Encoding' ] ) ) ? $headers[ 'Accept-Encoding' ] : '';
57
- } else {
58
- $http_if_modified_since = ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '';
59
- $http_accept = ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '';
60
- $http_accept_encoding = ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] : '';
61
- }
62
 
63
- // check modified since with cached file and return 304 if no difference
64
- if ( $http_if_modified_since && ( strtotime( $http_if_modified_since ) == filemtime( $path_html ) ) ) {
65
- header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 );
66
- exit;
67
- }
68
 
69
- // check webp and deliver gzip webp file if support
70
- if ( $http_accept && ( strpos($http_accept, 'webp') !== false ) ) {
71
- if ( is_readable( $path_webp_gzip ) ) {
72
- header('Content-Encoding: gzip');
73
- readfile( $path_webp_gzip );
74
- exit;
75
- } elseif ( is_readable( $path_webp_html ) ) {
76
- readfile( $path_webp_html );
77
- exit;
78
- }
79
- }
80
 
81
- // check encoding and deliver gzip file if support
82
- if ( $http_accept_encoding && ( strpos($http_accept_encoding, 'gzip') !== false ) && is_readable( $path_gzip ) ) {
83
- header('Content-Encoding: gzip');
84
- readfile( $path_gzip );
85
- exit;
86
- }
 
 
 
 
 
87
 
88
- // deliver cached file (default)
89
- readfile( $path_html );
90
- exit;
 
 
 
 
 
 
 
91
 
92
  } else {
93
- return false;
94
  }
2
 
3
  // check if request method is GET
4
  if ( ! isset( $_SERVER['REQUEST_METHOD'] ) || $_SERVER['REQUEST_METHOD'] != 'GET' ) {
5
+ return false;
6
  }
7
 
8
  // check if request with query strings
9
  if ( ! empty($_GET) && ! isset( $_GET['utm_source'], $_GET['utm_medium'], $_GET['utm_campaign'] ) ) {
10
+ return false;
11
  }
12
 
13
  // check cookie values
14
  if ( !empty($_COOKIE) ) {
15
+ foreach ( $_COOKIE as $k => $v) {
16
+ if ( preg_match('/^(wp-postpass|wordpress_logged_in|comment_author)_/', $k) ) {
17
+ return false;
18
+ }
19
+ }
20
  }
21
 
22
  // base path
23
  $path = sprintf(
24
+ '%s%s%s%s',
25
+ WP_CONTENT_DIR . '/cache/cache-enabler',
26
+ DIRECTORY_SEPARATOR,
27
+ parse_url(
28
+ 'http://' .strtolower($_SERVER['HTTP_HOST']),
29
+ PHP_URL_HOST
30
+ ),
31
+ parse_url(
32
+ $_SERVER['REQUEST_URI'],
33
+ PHP_URL_PATH
34
+ )
35
  );
36
 
37
  // add trailing slash
45
 
46
  if ( is_readable( $path_html ) ) {
47
 
48
+ // set cache handler header
49
+ header('x-cache-handler: wp');
50
 
51
+ // get if-modified request headers
52
+ if ( function_exists( 'apache_request_headers' ) ) {
53
+ $headers = apache_request_headers();
54
+ $http_if_modified_since = ( isset( $headers[ 'If-Modified-Since' ] ) ) ? $headers[ 'If-Modified-Since' ] : '';
55
+ $http_accept = ( isset( $headers[ 'Accept' ] ) ) ? $headers[ 'Accept' ] : '';
56
+ $http_accept_encoding = ( isset( $headers[ 'Accept-Encoding' ] ) ) ? $headers[ 'Accept-Encoding' ] : '';
57
+ } else {
58
+ $http_if_modified_since = ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '';
59
+ $http_accept = ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '';
60
+ $http_accept_encoding = ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] : '';
61
+ }
62
 
63
+ // check modified since with cached file and return 304 if no difference
64
+ if ( $http_if_modified_since && ( strtotime( $http_if_modified_since ) == filemtime( $path_html ) ) ) {
65
+ header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 );
66
+ exit;
67
+ }
68
 
69
+ header( 'Last-Modified: ' . gmdate("D, d M Y H:i:s",filemtime( $path_html )).' GMT' );
 
 
 
 
 
 
 
 
 
 
70
 
71
+ // check webp and deliver gzip webp file if support
72
+ if ( $http_accept && ( strpos($http_accept, 'webp') !== false ) ) {
73
+ if ( is_readable( $path_webp_gzip ) ) {
74
+ header('Content-Encoding: gzip');
75
+ readfile( $path_webp_gzip );
76
+ exit;
77
+ } elseif ( is_readable( $path_webp_html ) ) {
78
+ readfile( $path_webp_html );
79
+ exit;
80
+ }
81
+ }
82
 
83
+ // check encoding and deliver gzip file if support
84
+ if ( $http_accept_encoding && ( strpos($http_accept_encoding, 'gzip') !== false ) && is_readable( $path_gzip ) ) {
85
+ header('Content-Encoding: gzip');
86
+ readfile( $path_gzip );
87
+ exit;
88
+ }
89
+
90
+ // deliver cached file (default)
91
+ readfile( $path_html );
92
+ exit;
93
 
94
  } else {
95
+ return false;
96
  }
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.2.0
10
  */
11
 
12
  /*
@@ -42,32 +42,32 @@ define('CE_MIN_WP', '4.1');
42
 
43
  // hooks
44
  add_action(
45
- 'plugins_loaded',
46
- array(
47
- 'Cache_Enabler',
48
- 'instance'
49
- )
50
  );
51
  register_activation_hook(
52
- __FILE__,
53
- array(
54
- 'Cache_Enabler',
55
- 'on_activation'
56
- )
57
  );
58
  register_deactivation_hook(
59
- __FILE__,
60
- array(
61
- 'Cache_Enabler',
62
- 'on_deactivation'
63
- )
64
  );
65
  register_uninstall_hook(
66
- __FILE__,
67
- array(
68
- 'Cache_Enabler',
69
- 'on_uninstall'
70
- )
71
  );
72
 
73
 
@@ -76,13 +76,13 @@ spl_autoload_register('cache_autoload');
76
 
77
  // autoload function
78
  function cache_autoload($class) {
79
- if ( in_array($class, array('Cache_Enabler', 'Cache_Enabler_Disk')) ) {
80
- require_once(
81
- sprintf(
82
- '%s/inc/%s.class.php',
83
- CE_DIR,
84
- strtolower($class)
85
- )
86
- );
87
- }
88
  }
6
  Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  License: GPLv2 or later
9
+ Version: 1.2.1
10
  */
11
 
12
  /*
42
 
43
  // hooks
44
  add_action(
45
+ 'plugins_loaded',
46
+ array(
47
+ 'Cache_Enabler',
48
+ 'instance'
49
+ )
50
  );
51
  register_activation_hook(
52
+ __FILE__,
53
+ array(
54
+ 'Cache_Enabler',
55
+ 'on_activation'
56
+ )
57
  );
58
  register_deactivation_hook(
59
+ __FILE__,
60
+ array(
61
+ 'Cache_Enabler',
62
+ 'on_deactivation'
63
+ )
64
  );
65
  register_uninstall_hook(
66
+ __FILE__,
67
+ array(
68
+ 'Cache_Enabler',
69
+ 'on_uninstall'
70
+ )
71
  );
72
 
73
 
76
 
77
  // autoload function
78
  function cache_autoload($class) {
79
+ if ( in_array($class, array('Cache_Enabler', 'Cache_Enabler_Disk')) ) {
80
+ require_once(
81
+ sprintf(
82
+ '%s/inc/%s.class.php',
83
+ CE_DIR,
84
+ strtolower($class)
85
+ )
86
+ );
87
+ }
88
  }
inc/cache_enabler.class.php CHANGED
@@ -381,7 +381,7 @@ final class Cache_Enabler {
381
  private static function _install_backend() {
382
 
383
  add_option(
384
- 'cache',
385
  array()
386
  );
387
 
@@ -504,7 +504,7 @@ final class Cache_Enabler {
504
  private static function _uninstall_backend() {
505
 
506
  // delete options
507
- delete_option('cache');
508
 
509
  // clear cache
510
  self::clear_total_cache(true);
@@ -550,15 +550,25 @@ final class Cache_Enabler {
550
  * get options
551
  *
552
  * @since 1.0.0
553
- * @change 1.1.0
554
  *
555
  * @return array options array
556
  */
557
 
558
  private static function _get_options() {
559
 
 
 
 
 
 
 
 
 
 
 
560
  return wp_parse_args(
561
- get_option('cache'),
562
  array(
563
  'expires' => 0,
564
  'new_post' => 0,
@@ -1717,7 +1727,7 @@ final class Cache_Enabler {
1717
 
1718
  register_setting(
1719
  'cache-enabler',
1720
- 'cache',
1721
  array(
1722
  __CLASS__,
1723
  'validate_settings'
381
  private static function _install_backend() {
382
 
383
  add_option(
384
+ 'cache-enabler',
385
  array()
386
  );
387
 
504
  private static function _uninstall_backend() {
505
 
506
  // delete options
507
+ delete_option('cache-enabler');
508
 
509
  // clear cache
510
  self::clear_total_cache(true);
550
  * get options
551
  *
552
  * @since 1.0.0
553
+ * @change 1.2.1
554
  *
555
  * @return array options array
556
  */
557
 
558
  private static function _get_options() {
559
 
560
+ // decom
561
+ $ce_leg = get_option('cache');
562
+ if (!empty($ce_leg)) {
563
+ delete_option('cache');
564
+ add_option(
565
+ 'cache-enabler',
566
+ $ce_leg
567
+ );
568
+ }
569
+
570
  return wp_parse_args(
571
+ get_option('cache-enabler'),
572
  array(
573
  'expires' => 0,
574
  'new_post' => 0,
1727
 
1728
  register_setting(
1729
  'cache-enabler',
1730
+ 'cache-enabler',
1731
  array(
1732
  __CLASS__,
1733
  'validate_settings'
inc/cache_enabler_disk.class.php CHANGED
@@ -14,627 +14,627 @@ defined('ABSPATH') OR exit;
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
- *
35
- * @since 1.0.0
36
- * @change 1.0.0
37
- *
38
- * @return boolean true if installed
39
- */
40
-
41
- public static function is_permalink() {
42
- return get_option('permalink_structure');
43
- }
44
-
45
-
46
- /**
47
- * store asset
48
- *
49
- * @since 1.0.0
50
- * @change 1.0.0
51
- *
52
- * @param string $data content of the asset
53
- */
54
-
55
- public static function store_asset($data) {
56
-
57
- // check if empty
58
- if ( empty($data) ) {
59
- wp_die('Asset is empty.');
60
- }
61
-
62
- // save asset
63
- self::_create_files(
64
- $data
65
- );
66
-
67
- }
68
-
69
-
70
- /**
71
- * check asset
72
- *
73
- * @since 1.0.0
74
- * @change 1.0.0
75
- *
76
- * @return boolean true if asset exists
77
- */
78
-
79
- public static function check_asset() {
80
- return is_readable(
81
- self::_file_html()
82
- );
83
- }
84
-
85
-
86
- /**
87
- * check expiry
88
- *
89
- * @since 1.0.1
90
- * @change 1.0.1
91
- *
92
- * @return boolean true if asset expired
93
- */
94
-
95
- public static function check_expiry() {
96
-
97
- // cache enabler options
98
- $options = Cache_Enabler::$options;
99
-
100
- // check if expires is active
101
- if ($options['expires'] == 0) {
102
- return false;
103
- }
104
-
105
- $now = time();
106
- $expires_seconds = 3600*$options['expires'];
107
-
108
- // check if asset has expired
109
- if ( ( filemtime(self::_file_html()) + $expires_seconds ) <= $now ) {
110
- return true;
111
- }
112
-
113
- return false;
114
-
115
- }
116
-
117
-
118
- /**
119
- * delete asset
120
- *
121
- * @since 1.0.0
122
- * @change 1.0.0
123
- *
124
- * @param string $url url of cached asset
125
- */
126
-
127
- public static function delete_asset($url) {
128
-
129
- // check if url empty
130
- if ( empty($url) ) {
131
- wp_die('URL is empty.');
132
- }
133
-
134
- // delete
135
- self::_clear_dir(
136
- self::_file_path($url)
137
- );
138
- }
139
-
140
-
141
- /**
142
- * clear cache
143
- *
144
- * @since 1.0.0
145
- * @change 1.0.0
146
- */
147
-
148
- public static function clear_cache() {
149
- self::_clear_dir(
150
- CE_CACHE_DIR
151
- );
152
- }
153
-
154
-
155
- /**
156
- * clear home cache
157
- *
158
- * @since 1.0.7
159
- * @change 1.0.9
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.9
183
- */
184
-
185
- public static function get_asset() {
186
-
187
- // set cache handler header
188
- header('x-cache-handler: php');
189
-
190
- // get if-modified request headers
191
- if ( function_exists( 'apache_request_headers' ) ) {
192
- $headers = apache_request_headers();
193
- $http_if_modified_since = ( isset( $headers[ 'If-Modified-Since' ] ) ) ? $headers[ 'If-Modified-Since' ] : '';
194
- $http_accept = ( isset( $headers[ 'Accept' ] ) ) ? $headers[ 'Accept' ] : '';
195
- $http_accept_encoding = ( isset( $headers[ 'Accept-Encoding' ] ) ) ? $headers[ 'Accept-Encoding' ] : '';
196
- } else {
197
- $http_if_modified_since = ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '';
198
- $http_accept = ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '';
199
- $http_accept_encoding = ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] : '';
200
- }
201
-
202
- // check modified since with cached file and return 304 if no difference
203
- if ( $http_if_modified_since && ( strtotime( $http_if_modified_since ) == filemtime( self::_file_html() ) ) ) {
204
- header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 );
205
- exit;
206
- }
207
-
208
- // check webp and deliver gzip webp file if support
209
- if ( $http_accept && ( strpos($http_accept, 'webp') !== false ) ) {
210
- if ( is_readable( self::_file_webp_gzip() ) ) {
211
- header('Content-Encoding: gzip');
212
- readfile( self::_file_webp_gzip() );
213
- exit;
214
- } elseif ( is_readable( self::_file_webp_html() ) ) {
215
- readfile( self::_file_webp_html() );
216
- exit;
217
- }
218
- }
219
-
220
- // check encoding and deliver gzip file if support
221
- if ( $http_accept_encoding && ( strpos($http_accept_encoding, 'gzip') !== false ) && is_readable( self::_file_gzip() ) ) {
222
- header('Content-Encoding: gzip');
223
- readfile( self::_file_gzip() );
224
- exit;
225
- }
226
-
227
- // deliver cached file (default)
228
- readfile( self::_file_html() );
229
- exit;
230
- }
231
-
232
-
233
- /**
234
- * create signature
235
- *
236
- * @since 1.0.0
237
- * @change 1.0.0
238
- *
239
- * @return string signature
240
- */
241
-
242
- private static function _cache_signatur() {
243
- return sprintf(
244
- "\n\n<!-- %s @ %s",
245
- 'Cache Enabler by KeyCDN',
246
- date_i18n(
247
- 'd.m.Y H:i:s',
248
- current_time('timestamp')
249
- )
250
- );
251
- }
252
-
253
-
254
- /**
255
- * create files
256
- *
257
- * @since 1.0.0
258
- * @change 1.1.1
259
- *
260
- * @param string $data html content
261
- */
262
-
263
- private static function _create_files($data) {
264
-
265
- // create folder
266
- if ( ! wp_mkdir_p( self::_file_path() ) ) {
267
- wp_die('Unable to create directory.');
268
- }
269
-
270
- // get base signature
271
- $cache_signature = self::_cache_signatur();
272
-
273
- // cache enabler options
274
- $options = Cache_Enabler::$options;
275
-
276
- // create files
277
- self::_create_file( self::_file_html(), $data.$cache_signature." (html) -->" );
278
-
279
- // create pre-compressed file
280
- if ($options['compress']) {
281
- self::_create_file( self::_file_gzip(), gzencode($data.$cache_signature." (html gzip) -->", 9) );
282
- }
283
-
284
- // create webp supported files
285
- if ($options['webp']) {
286
- // magic regex rule
287
- $regex_rule = '#(?<=(?:(ref|src|set)=[\"\']))(?:http[s]?[^\"\']+)(\.png|\.jp[e]?g)(?:[^\"\']+)?(?=[\"\')])#';
288
-
289
- // call the webp converter callback
290
- $converted_data = preg_replace_callback($regex_rule,'self::_convert_webp',$data);
291
-
292
- self::_create_file( self::_file_webp_html(), $converted_data.$cache_signature." (webp) -->" );
293
-
294
- // create pre-compressed file
295
- if ($options['compress']) {
296
- self::_create_file( self::_file_webp_gzip(), gzencode($converted_data.$cache_signature." (webp gzip) -->", 9) );
297
- }
298
- }
299
-
300
- }
301
-
302
-
303
- /**
304
- * create file
305
- *
306
- * @since 1.0.0
307
- * @change 1.0.0
308
- *
309
- * @param string $file file path
310
- * @param string $data content of the html
311
- */
312
-
313
- private static function _create_file($file, $data) {
314
-
315
- // open file handler
316
- if ( ! $handle = @fopen($file, 'wb') ) {
317
- wp_die('Can not write to file.');
318
- }
319
-
320
- // write
321
- @fwrite($handle, $data);
322
- fclose($handle);
323
- clearstatcache();
324
-
325
- // set permissions
326
- $stat = @stat( dirname($file) );
327
- $perms = $stat['mode'] & 0007777;
328
- $perms = $perms & 0000666;
329
- @chmod($file, $perms);
330
- clearstatcache();
331
- }
332
-
333
-
334
- /**
335
- * clear directory
336
- *
337
- * @since 1.0.0
338
- * @change 1.0.0
339
- *
340
- * @param string $dir directory
341
- */
342
-
343
- private static function _clear_dir($dir) {
344
-
345
- // remove slashes
346
- $dir = untrailingslashit($dir);
347
-
348
- // check if dir
349
- if ( ! is_dir($dir) ) {
350
- return;
351
- }
352
-
353
- // get dir data
354
- $objects = array_diff(
355
- scandir($dir),
356
- array('..', '.')
357
- );
358
-
359
- if ( empty($objects) ) {
360
- return;
361
- }
362
-
363
- foreach ( $objects as $object ) {
364
- // full path
365
- $object = $dir. DIRECTORY_SEPARATOR .$object;
366
-
367
- // check if directory
368
- if ( is_dir($object) ) {
369
- self::_clear_dir($object);
370
- } else {
371
- unlink($object);
372
- }
373
- }
374
-
375
- // delete
376
- @rmdir($dir);
377
-
378
- // clears file status cache
379
- clearstatcache();
380
- }
381
-
382
-
383
- /**
384
- * get cache size
385
- *
386
- * @since 1.0.0
387
- * @change 1.0.0
388
- *
389
- * @param string $dir folder path
390
- * @return mixed $size size in bytes
391
- */
392
-
393
- public static function cache_size($dir = '.') {
394
-
395
- // check if not dir
396
- if ( ! is_dir($dir) ) {
397
- return;
398
- }
399
-
400
- // get dir data
401
- $objects = array_diff(
402
- scandir($dir),
403
- array('..', '.')
404
- );
405
-
406
- if ( empty($objects) ) {
407
- return;
408
- }
409
-
410
- $size = 0;
411
-
412
- foreach ( $objects as $object ) {
413
- // full path
414
- $object = $dir. DIRECTORY_SEPARATOR .$object;
415
-
416
- // check if dir
417
- if ( is_dir($object) ) {
418
- $size += self::cache_size($object);
419
- } else {
420
- $size += filesize($object);
421
- }
422
- }
423
-
424
- return $size;
425
- }
426
-
427
-
428
- /**
429
- * cache path
430
- *
431
- * @since 1.0.0
432
- * @change 1.1.0
433
- *
434
- * @param string $path uri or permlink
435
- * @return string $diff path to cached asset
436
- */
437
-
438
- private static function _file_path($path = NULL) {
439
-
440
- $path = sprintf(
441
- '%s%s%s%s',
442
- CE_CACHE_DIR,
443
- DIRECTORY_SEPARATOR,
444
- parse_url(
445
- 'http://' .strtolower($_SERVER['HTTP_HOST']),
446
- PHP_URL_HOST
447
- ),
448
- parse_url(
449
- ( $path ? $path : $_SERVER['REQUEST_URI'] ),
450
- PHP_URL_PATH
451
- )
452
- );
453
-
454
- if ( is_file($path) > 0 ) {
455
- wp_die('Path is not valid.');
456
- }
457
-
458
- return trailingslashit($path);
459
- }
460
-
461
-
462
- /**
463
- * get file path
464
- *
465
- * @since 1.0.0
466
- * @change 1.0.7
467
- *
468
- * @return string path to the html file
469
- */
470
-
471
- private static function _file_html() {
472
- return self::_file_path(). self::FILE_HTML;
473
- }
474
-
475
-
476
- /**
477
- * get gzip file path
478
- *
479
- * @since 1.0.1
480
- * @change 1.0.7
481
- *
482
- * @return string path to the gzipped html file
483
- */
484
-
485
- private static function _file_gzip() {
486
- return self::_file_path(). self::FILE_GZIP;
487
- }
488
-
489
-
490
- /**
491
- * get webp file path
492
- *
493
- * @since 1.0.7
494
- * @change 1.0.7
495
- *
496
- * @return string path to the webp html file
497
- */
498
-
499
- private static function _file_webp_html() {
500
- return self::_file_path(). self::FILE_WEBP_HTML;
501
- }
502
-
503
-
504
- /**
505
- * get gzip webp file path
506
- *
507
- * @since 1.0.1
508
- * @change 1.0.7
509
- *
510
- * @return string path to the webp gzipped html file
511
- */
512
-
513
- private static function _file_webp_gzip() {
514
- return self::_file_path(). self::FILE_WEBP_GZIP;
515
- }
516
-
517
-
518
- /**
519
- * convert to webp
520
- *
521
- * @since 1.0.1
522
- * @change 1.1.1
523
- *
524
- * @return string converted HTML file
525
- */
526
-
527
- private static function _convert_webp($asset) {
528
-
529
- if ($asset[1] == 'src') {
530
- return self::_convert_webp_src($asset[0]);
531
- } elseif ($asset[1] == 'ref') {
532
- return self::_convert_webp_src($asset[0]);
533
- } elseif ($asset[1] == 'set') {
534
- return self::_convert_webp_srcset($asset[0]);
535
- }
536
-
537
- return $asset[0];
538
-
539
- }
540
-
541
-
542
- /**
543
- * convert src to webp source
544
- *
545
- * @since 1.0.1
546
- * @change 1.1.0
547
- *
548
- * @return string converted src webp source
549
- */
550
-
551
- private static function _convert_webp_src($src) {
552
- $upload_dir = wp_upload_dir();
553
- $src_url = parse_url($upload_dir['baseurl']);
554
- $upload_path = $src_url['path'];
555
-
556
- if ( strpos($src, $upload_path) !== false ) {
557
-
558
- $src_webp = str_replace('.jpg', '.webp', $src);
559
- $src_webp = str_replace('.jpeg', '.webp', $src_webp);
560
- $src_webp = str_replace('.png', '.webp', $src_webp);
561
-
562
- $parts = explode($upload_path, $src_webp);
563
- $relative_path = $parts[1];
564
-
565
- // check if relative path is not empty and file exists
566
- if ( !empty($relative_path) && file_exists($upload_dir['basedir'].$relative_path) ) {
567
- return $src_webp;
568
- } else {
569
- // try appended webp extension
570
- $src_webp_appended = $src.'.webp';
571
- $parts_appended = explode($upload_path, $src_webp_appended);
572
- $relative_path_appended = $parts_appended[1];
573
-
574
- // check if relative path is not empty and file exists
575
- if ( !empty($relative_path_appended) && file_exists($upload_dir['basedir'].$relative_path_appended) ) {
576
- return $src_webp_appended;
577
- }
578
- }
579
-
580
- }
581
-
582
- return $src;
583
- }
584
-
585
-
586
- /**
587
- * convert srcset to webp source
588
- *
589
- * @since 1.0.8
590
- * @change 1.1.0
591
- *
592
- * @return string converted srcset webp source
593
- */
594
-
595
- private static function _convert_webp_srcset($srcset) {
596
-
597
- $sizes = explode(', ', $srcset);
598
- $upload_dir = wp_upload_dir();
599
- $src_url = parse_url($upload_dir['baseurl']);
600
- $upload_path = $src_url['path'];
601
-
602
- for ($i=0; $i<count($sizes); $i++) {
603
-
604
- if ( strpos($sizes[$i], $upload_path) !== false ) {
605
-
606
- $src_webp = str_replace('.jpg', '.webp', $sizes[$i]);
607
- $src_webp = str_replace('.jpeg', '.webp', $src_webp);
608
- $src_webp = str_replace('.png', '.webp', $src_webp);
609
-
610
- $size_parts = explode(' ', $src_webp);
611
- $parts = explode($upload_path, $size_parts[0]);
612
- $relative_path = $parts[1];
613
-
614
- // check if relative path is not empty and file exists
615
- if ( !empty($relative_path) && file_exists($upload_dir['basedir'].$relative_path) ) {
616
- $sizes[$i] = $src_webp;
617
- } else {
618
- // try appended webp extension
619
- $size_parts_appended = explode(' ', $sizes[$i]);
620
- $src_webp_appended = $size_parts_appended[0].'.webp';
621
- $parts_appended = explode($upload_path, $src_webp_appended);
622
- $relative_path_appended = $parts_appended[1];
623
- $src_webp_appended = $src_webp_appended.' '.$size_parts_appended[1];
624
 
625
- // check if relative path is not empty and file exists
626
- if ( !empty($relative_path_appended) && file_exists($upload_dir['basedir'].$relative_path_appended) ) {
627
- $sizes[$i] = $src_webp_appended;
628
- }
629
- }
630
-
631
- }
632
 
633
- }
634
 
635
- $srcset = implode(', ', $sizes);
636
 
637
- return $srcset;
638
- }
639
 
640
  }
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
+ *
35
+ * @since 1.0.0
36
+ * @change 1.0.0
37
+ *
38
+ * @return boolean true if installed
39
+ */
40
+
41
+ public static function is_permalink() {
42
+ return get_option('permalink_structure');
43
+ }
44
+
45
+
46
+ /**
47
+ * store asset
48
+ *
49
+ * @since 1.0.0
50
+ * @change 1.0.0
51
+ *
52
+ * @param string $data content of the asset
53
+ */
54
+
55
+ public static function store_asset($data) {
56
+
57
+ // check if empty
58
+ if ( empty($data) ) {
59
+ wp_die('Asset is empty.');
60
+ }
61
+
62
+ // save asset
63
+ self::_create_files(
64
+ $data
65
+ );
66
+
67
+ }
68
+
69
+
70
+ /**
71
+ * check asset
72
+ *
73
+ * @since 1.0.0
74
+ * @change 1.0.0
75
+ *
76
+ * @return boolean true if asset exists
77
+ */
78
+
79
+ public static function check_asset() {
80
+ return is_readable(
81
+ self::_file_html()
82
+ );
83
+ }
84
+
85
+
86
+ /**
87
+ * check expiry
88
+ *
89
+ * @since 1.0.1
90
+ * @change 1.0.1
91
+ *
92
+ * @return boolean true if asset expired
93
+ */
94
+
95
+ public static function check_expiry() {
96
+
97
+ // cache enabler options
98
+ $options = Cache_Enabler::$options;
99
+
100
+ // check if expires is active
101
+ if ($options['expires'] == 0) {
102
+ return false;
103
+ }
104
+
105
+ $now = time();
106
+ $expires_seconds = 3600*$options['expires'];
107
+
108
+ // check if asset has expired
109
+ if ( ( filemtime(self::_file_html()) + $expires_seconds ) <= $now ) {
110
+ return true;
111
+ }
112
+
113
+ return false;
114
+
115
+ }
116
+
117
+
118
+ /**
119
+ * delete asset
120
+ *
121
+ * @since 1.0.0
122
+ * @change 1.0.0
123
+ *
124
+ * @param string $url url of cached asset
125
+ */
126
+
127
+ public static function delete_asset($url) {
128
+
129
+ // check if url empty
130
+ if ( empty($url) ) {
131
+ wp_die('URL is empty.');
132
+ }
133
+
134
+ // delete
135
+ self::_clear_dir(
136
+ self::_file_path($url)
137
+ );
138
+ }
139
+
140
+
141
+ /**
142
+ * clear cache
143
+ *
144
+ * @since 1.0.0
145
+ * @change 1.0.0
146
+ */
147
+
148
+ public static function clear_cache() {
149
+ self::_clear_dir(
150
+ CE_CACHE_DIR
151
+ );
152
+ }
153
+
154
+
155
+ /**
156
+ * clear home cache
157
+ *
158
+ * @since 1.0.7
159
+ * @change 1.0.9
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.9
183
+ */
184
+
185
+ public static function get_asset() {
186
+
187
+ // set cache handler header
188
+ header('x-cache-handler: php');
189
+
190
+ // get if-modified request headers
191
+ if ( function_exists( 'apache_request_headers' ) ) {
192
+ $headers = apache_request_headers();
193
+ $http_if_modified_since = ( isset( $headers[ 'If-Modified-Since' ] ) ) ? $headers[ 'If-Modified-Since' ] : '';
194
+ $http_accept = ( isset( $headers[ 'Accept' ] ) ) ? $headers[ 'Accept' ] : '';
195
+ $http_accept_encoding = ( isset( $headers[ 'Accept-Encoding' ] ) ) ? $headers[ 'Accept-Encoding' ] : '';
196
+ } else {
197
+ $http_if_modified_since = ( isset( $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] ) ) ? $_SERVER[ 'HTTP_IF_MODIFIED_SINCE' ] : '';
198
+ $http_accept = ( isset( $_SERVER[ 'HTTP_ACCEPT' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT' ] : '';
199
+ $http_accept_encoding = ( isset( $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] ) ) ? $_SERVER[ 'HTTP_ACCEPT_ENCODING' ] : '';
200
+ }
201
+
202
+ // check modified since with cached file and return 304 if no difference
203
+ if ( $http_if_modified_since && ( strtotime( $http_if_modified_since ) == filemtime( self::_file_html() ) ) ) {
204
+ header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified', true, 304 );
205
+ exit;
206
+ }
207
+
208
+ // check webp and deliver gzip webp file if support
209
+ if ( $http_accept && ( strpos($http_accept, 'webp') !== false ) ) {
210
+ if ( is_readable( self::_file_webp_gzip() ) ) {
211
+ header('Content-Encoding: gzip');
212
+ readfile( self::_file_webp_gzip() );
213
+ exit;
214
+ } elseif ( is_readable( self::_file_webp_html() ) ) {
215
+ readfile( self::_file_webp_html() );
216
+ exit;
217
+ }
218
+ }
219
+
220
+ // check encoding and deliver gzip file if support
221
+ if ( $http_accept_encoding && ( strpos($http_accept_encoding, 'gzip') !== false ) && is_readable( self::_file_gzip() ) ) {
222
+ header('Content-Encoding: gzip');
223
+ readfile( self::_file_gzip() );
224
+ exit;
225
+ }
226
+
227
+ // deliver cached file (default)
228
+ readfile( self::_file_html() );
229
+ exit;
230
+ }
231
+
232
+
233
+ /**
234
+ * create signature
235
+ *
236
+ * @since 1.0.0
237
+ * @change 1.0.0
238
+ *
239
+ * @return string signature
240
+ */
241
+
242
+ private static function _cache_signatur() {
243
+ return sprintf(
244
+ "\n\n<!-- %s @ %s",
245
+ 'Cache Enabler by KeyCDN',
246
+ date_i18n(
247
+ 'd.m.Y H:i:s',
248
+ current_time('timestamp')
249
+ )
250
+ );
251
+ }
252
+
253
+
254
+ /**
255
+ * create files
256
+ *
257
+ * @since 1.0.0
258
+ * @change 1.1.1
259
+ *
260
+ * @param string $data html content
261
+ */
262
+
263
+ private static function _create_files($data) {
264
+
265
+ // create folder
266
+ if ( ! wp_mkdir_p( self::_file_path() ) ) {
267
+ wp_die('Unable to create directory.');
268
+ }
269
+
270
+ // get base signature
271
+ $cache_signature = self::_cache_signatur();
272
+
273
+ // cache enabler options
274
+ $options = Cache_Enabler::$options;
275
+
276
+ // create files
277
+ self::_create_file( self::_file_html(), $data.$cache_signature." (html) -->" );
278
+
279
+ // create pre-compressed file
280
+ if ($options['compress']) {
281
+ self::_create_file( self::_file_gzip(), gzencode($data.$cache_signature." (html gzip) -->", 9) );
282
+ }
283
+
284
+ // create webp supported files
285
+ if ($options['webp']) {
286
+ // magic regex rule
287
+ $regex_rule = '#(?<=(?:(ref|src|set)=[\"\']))(?:http[s]?[^\"\']+)(\.png|\.jp[e]?g)(?:[^\"\']+)?(?=[\"\')])#';
288
+
289
+ // call the webp converter callback
290
+ $converted_data = preg_replace_callback($regex_rule,'self::_convert_webp',$data);
291
+
292
+ self::_create_file( self::_file_webp_html(), $converted_data.$cache_signature." (webp) -->" );
293
+
294
+ // create pre-compressed file
295
+ if ($options['compress']) {
296
+ self::_create_file( self::_file_webp_gzip(), gzencode($converted_data.$cache_signature." (webp gzip) -->", 9) );
297
+ }
298
+ }
299
+
300
+ }
301
+
302
+
303
+ /**
304
+ * create file
305
+ *
306
+ * @since 1.0.0
307
+ * @change 1.0.0
308
+ *
309
+ * @param string $file file path
310
+ * @param string $data content of the html
311
+ */
312
+
313
+ private static function _create_file($file, $data) {
314
+
315
+ // open file handler
316
+ if ( ! $handle = @fopen($file, 'wb') ) {
317
+ wp_die('Can not write to file.');
318
+ }
319
+
320
+ // write
321
+ @fwrite($handle, $data);
322
+ fclose($handle);
323
+ clearstatcache();
324
+
325
+ // set permissions
326
+ $stat = @stat( dirname($file) );
327
+ $perms = $stat['mode'] & 0007777;
328
+ $perms = $perms & 0000666;
329
+ @chmod($file, $perms);
330
+ clearstatcache();
331
+ }
332
+
333
+
334
+ /**
335
+ * clear directory
336
+ *
337
+ * @since 1.0.0
338
+ * @change 1.0.0
339
+ *
340
+ * @param string $dir directory
341
+ */
342
+
343
+ private static function _clear_dir($dir) {
344
+
345
+ // remove slashes
346
+ $dir = untrailingslashit($dir);
347
+
348
+ // check if dir
349
+ if ( ! is_dir($dir) ) {
350
+ return;
351
+ }
352
+
353
+ // get dir data
354
+ $objects = array_diff(
355
+ scandir($dir),
356
+ array('..', '.')
357
+ );
358
+
359
+ if ( empty($objects) ) {
360
+ return;
361
+ }
362
+
363
+ foreach ( $objects as $object ) {
364
+ // full path
365
+ $object = $dir. DIRECTORY_SEPARATOR .$object;
366
+
367
+ // check if directory
368
+ if ( is_dir($object) ) {
369
+ self::_clear_dir($object);
370
+ } else {
371
+ unlink($object);
372
+ }
373
+ }
374
+
375
+ // delete
376
+ @rmdir($dir);
377
+
378
+ // clears file status cache
379
+ clearstatcache();
380
+ }
381
+
382
+
383
+ /**
384
+ * get cache size
385
+ *
386
+ * @since 1.0.0
387
+ * @change 1.0.0
388
+ *
389
+ * @param string $dir folder path
390
+ * @return mixed $size size in bytes
391
+ */
392
+
393
+ public static function cache_size($dir = '.') {
394
+
395
+ // check if not dir
396
+ if ( ! is_dir($dir) ) {
397
+ return;
398
+ }
399
+
400
+ // get dir data
401
+ $objects = array_diff(
402
+ scandir($dir),
403
+ array('..', '.')
404
+ );
405
+
406
+ if ( empty($objects) ) {
407
+ return;
408
+ }
409
+
410
+ $size = 0;
411
+
412
+ foreach ( $objects as $object ) {
413
+ // full path
414
+ $object = $dir. DIRECTORY_SEPARATOR .$object;
415
+
416
+ // check if dir
417
+ if ( is_dir($object) ) {
418
+ $size += self::cache_size($object);
419
+ } else {
420
+ $size += filesize($object);
421
+ }
422
+ }
423
+
424
+ return $size;
425
+ }
426
+
427
+
428
+ /**
429
+ * cache path
430
+ *
431
+ * @since 1.0.0
432
+ * @change 1.1.0
433
+ *
434
+ * @param string $path uri or permlink
435
+ * @return string $diff path to cached asset
436
+ */
437
+
438
+ private static function _file_path($path = NULL) {
439
+
440
+ $path = sprintf(
441
+ '%s%s%s%s',
442
+ CE_CACHE_DIR,
443
+ DIRECTORY_SEPARATOR,
444
+ parse_url(
445
+ 'http://' .strtolower($_SERVER['HTTP_HOST']),
446
+ PHP_URL_HOST
447
+ ),
448
+ parse_url(
449
+ ( $path ? $path : $_SERVER['REQUEST_URI'] ),
450
+ PHP_URL_PATH
451
+ )
452
+ );
453
+
454
+ if ( is_file($path) > 0 ) {
455
+ wp_die('Path is not valid.');
456
+ }
457
+
458
+ return trailingslashit($path);
459
+ }
460
+
461
+
462
+ /**
463
+ * get file path
464
+ *
465
+ * @since 1.0.0
466
+ * @change 1.0.7
467
+ *
468
+ * @return string path to the html file
469
+ */
470
+
471
+ private static function _file_html() {
472
+ return self::_file_path(). self::FILE_HTML;
473
+ }
474
+
475
+
476
+ /**
477
+ * get gzip file path
478
+ *
479
+ * @since 1.0.1
480
+ * @change 1.0.7
481
+ *
482
+ * @return string path to the gzipped html file
483
+ */
484
+
485
+ private static function _file_gzip() {
486
+ return self::_file_path(). self::FILE_GZIP;
487
+ }
488
+
489
+
490
+ /**
491
+ * get webp file path
492
+ *
493
+ * @since 1.0.7
494
+ * @change 1.0.7
495
+ *
496
+ * @return string path to the webp html file
497
+ */
498
+
499
+ private static function _file_webp_html() {
500
+ return self::_file_path(). self::FILE_WEBP_HTML;
501
+ }
502
+
503
+
504
+ /**
505
+ * get gzip webp file path
506
+ *
507
+ * @since 1.0.1
508
+ * @change 1.0.7
509
+ *
510
+ * @return string path to the webp gzipped html file
511
+ */
512
+
513
+ private static function _file_webp_gzip() {
514
+ return self::_file_path(). self::FILE_WEBP_GZIP;
515
+ }
516
+
517
+
518
+ /**
519
+ * convert to webp
520
+ *
521
+ * @since 1.0.1
522
+ * @change 1.1.1
523
+ *
524
+ * @return string converted HTML file
525
+ */
526
+
527
+ private static function _convert_webp($asset) {
528
+
529
+ if ($asset[1] == 'src') {
530
+ return self::_convert_webp_src($asset[0]);
531
+ } elseif ($asset[1] == 'ref') {
532
+ return self::_convert_webp_src($asset[0]);
533
+ } elseif ($asset[1] == 'set') {
534
+ return self::_convert_webp_srcset($asset[0]);
535
+ }
536
+
537
+ return $asset[0];
538
+
539
+ }
540
+
541
+
542
+ /**
543
+ * convert src to webp source
544
+ *
545
+ * @since 1.0.1
546
+ * @change 1.1.0
547
+ *
548
+ * @return string converted src webp source
549
+ */
550
+
551
+ private static function _convert_webp_src($src) {
552
+ $upload_dir = wp_upload_dir();
553
+ $src_url = parse_url($upload_dir['baseurl']);
554
+ $upload_path = $src_url['path'];
555
+
556
+ if ( strpos($src, $upload_path) !== false ) {
557
+
558
+ $src_webp = str_replace('.jpg', '.webp', $src);
559
+ $src_webp = str_replace('.jpeg', '.webp', $src_webp);
560
+ $src_webp = str_replace('.png', '.webp', $src_webp);
561
+
562
+ $parts = explode($upload_path, $src_webp);
563
+ $relative_path = $parts[1];
564
+
565
+ // check if relative path is not empty and file exists
566
+ if ( !empty($relative_path) && file_exists($upload_dir['basedir'].$relative_path) ) {
567
+ return $src_webp;
568
+ } else {
569
+ // try appended webp extension
570
+ $src_webp_appended = $src.'.webp';
571
+ $parts_appended = explode($upload_path, $src_webp_appended);
572
+ $relative_path_appended = $parts_appended[1];
573
+
574
+ // check if relative path is not empty and file exists
575
+ if ( !empty($relative_path_appended) && file_exists($upload_dir['basedir'].$relative_path_appended) ) {
576
+ return $src_webp_appended;
577
+ }
578
+ }
579
+
580
+ }
581
+
582
+ return $src;
583
+ }
584
+
585
+
586
+ /**
587
+ * convert srcset to webp source
588
+ *
589
+ * @since 1.0.8
590
+ * @change 1.1.0
591
+ *
592
+ * @return string converted srcset webp source
593
+ */
594
+
595
+ private static function _convert_webp_srcset($srcset) {
596
+
597
+ $sizes = explode(', ', $srcset);
598
+ $upload_dir = wp_upload_dir();
599
+ $src_url = parse_url($upload_dir['baseurl']);
600
+ $upload_path = $src_url['path'];
601
+
602
+ for ($i=0; $i<count($sizes); $i++) {
603
+
604
+ if ( strpos($sizes[$i], $upload_path) !== false ) {
605
+
606
+ $src_webp = str_replace('.jpg', '.webp', $sizes[$i]);
607
+ $src_webp = str_replace('.jpeg', '.webp', $src_webp);
608
+ $src_webp = str_replace('.png', '.webp', $src_webp);
609
+
610
+ $size_parts = explode(' ', $src_webp);
611
+ $parts = explode($upload_path, $size_parts[0]);
612
+ $relative_path = $parts[1];
613
+
614
+ // check if relative path is not empty and file exists
615
+ if ( !empty($relative_path) && file_exists($upload_dir['basedir'].$relative_path) ) {
616
+ $sizes[$i] = $src_webp;
617
+ } else {
618
+ // try appended webp extension
619
+ $size_parts_appended = explode(' ', $sizes[$i]);
620
+ $src_webp_appended = $size_parts_appended[0].'.webp';
621
+ $parts_appended = explode($upload_path, $src_webp_appended);
622
+ $relative_path_appended = $parts_appended[1];
623
+ $src_webp_appended = $src_webp_appended.' '.$size_parts_appended[1];
624
 
625
+ // check if relative path is not empty and file exists
626
+ if ( !empty($relative_path_appended) && file_exists($upload_dir['basedir'].$relative_path_appended) ) {
627
+ $sizes[$i] = $src_webp_appended;
628
+ }
629
+ }
630
+
631
+ }
632
 
633
+ }
634
 
635
+ $srcset = implode(', ', $sizes);
636
 
637
+ return $srcset;
638
+ }
639
 
640
  }
readme.txt CHANGED
@@ -64,6 +64,9 @@ This WordPress cache plugin is partially based on Cachify developed by [Sergej M
64
 
65
  == Changelog ==
66
 
 
 
 
67
  = 1.2.0 =
68
  * Added advanced cache feature
69
  * Clear cache if reply to a comment in WP admin
64
 
65
  == Changelog ==
66
 
67
+ = 1.2.1 =
68
+ * Minor fixes
69
+
70
  = 1.2.0 =
71
  * Added advanced cache feature
72
  * Clear cache if reply to a comment in WP admin