Cache Enabler – WordPress Cache - Version 1.4.8

Version Description

  • Update WebP URL conversion (#116)
  • Update WP-CLI clear subcommand messages (#111)
  • Update WP-CLI clear subcommand for multisite networks (#111)
  • Fix cache clearing for installations in a subdirectory
  • Fix advanced cache settings recognition for installations in a subdirectory
  • Fix file permissions requirement notice
Download this release

Release Info

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

Code changes from version 1.4.7 to 1.4.8

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.4.7
10
  */
11
 
12
  /*
@@ -87,6 +87,4 @@ function cache_autoload( $class ) {
87
  // load the WP-CLI command
88
  if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WP_CLI' ) ) {
89
  require_once CE_DIR . '/inc/cache_enabler_cli.class.php';
90
-
91
- WP_CLI::add_command( 'cache-enabler', 'Cache_Enabler_CLI' );
92
  }
6
  Author: KeyCDN
7
  Author URI: https://www.keycdn.com
8
  License: GPLv2 or later
9
+ Version: 1.4.8
10
  */
11
 
12
  /*
87
  // load the WP-CLI command
88
  if ( defined( 'WP_CLI' ) && WP_CLI && class_exists( 'WP_CLI' ) ) {
89
  require_once CE_DIR . '/inc/cache_enabler_cli.class.php';
 
 
90
  }
inc/cache_enabler.class.php CHANGED
@@ -778,7 +778,7 @@ final class Cache_Enabler {
778
  * get blog domain
779
  *
780
  * @since 1.4.0
781
- * @change 1.4.0
782
  *
783
  * @return string $domain current blog domain
784
  */
@@ -786,7 +786,7 @@ final class Cache_Enabler {
786
  public static function get_blog_domain() {
787
 
788
  // get current blog domain
789
- $domain = parse_url( get_site_url(), PHP_URL_HOST );
790
 
791
  // check if empty when creating new site in network
792
  if ( is_multisite() && empty( $domain ) ) {
@@ -1224,18 +1224,13 @@ final class Cache_Enabler {
1224
  * clear page cache by post ID
1225
  *
1226
  * @since 1.0.0
1227
- * @change 1.4.7
1228
  *
1229
  * @param integer|string $post_id post ID
1230
  */
1231
 
1232
  public static function clear_page_cache_by_post_id( $post_id ) {
1233
 
1234
- // check if post ID is empty
1235
- if ( empty( $post_id ) ) {
1236
- return;
1237
- }
1238
-
1239
  // validate integer
1240
  if ( ! is_int( $post_id ) ) {
1241
  // if string try to convert to integer
@@ -1255,21 +1250,21 @@ final class Cache_Enabler {
1255
  * clear page cache by URL
1256
  *
1257
  * @since 1.0.0
1258
- * @change 1.4.7
1259
  *
1260
- * @param string $clear_url full or relative URL of a page
1261
- * @param string $clear_type clear all specific `page` variants or the entire `dir`
1262
  */
1263
 
1264
  public static function clear_page_cache_by_url( $clear_url, $clear_type = 'page' ) {
1265
 
1266
- // check if clear URL is empty
1267
- if ( empty( $clear_url ) ) {
1268
  return;
1269
  }
1270
 
1271
- // validate string
1272
- if ( ! is_string( $clear_url ) ) {
1273
  return;
1274
  }
1275
 
@@ -1285,13 +1280,21 @@ final class Cache_Enabler {
1285
  * clear home page cache
1286
  *
1287
  * @since 1.0.7
1288
- * @change 1.4.7
 
 
1289
  */
1290
 
1291
- public static function clear_home_page_cache() {
 
 
 
 
 
 
1292
 
1293
  // clear home page cache
1294
- self::clear_page_cache_by_url( get_site_url() );
1295
 
1296
  // clear home page cache post hook
1297
  do_action( 'ce_action_home_page_cache_cleared' );
@@ -1302,7 +1305,7 @@ final class Cache_Enabler {
1302
  * clear blog ID cache
1303
  *
1304
  * @since 1.4.0
1305
- * @change 1.4.7
1306
  *
1307
  * @param integer|string $blog_id blog ID
1308
  */
@@ -1324,8 +1327,13 @@ final class Cache_Enabler {
1324
  }
1325
  }
1326
 
 
 
 
 
 
1327
  // set clear URL
1328
- $clear_url = get_site_url( $blog_id );
1329
 
1330
  // network with subdomain configuration
1331
  if ( is_subdomain_install() ) {
@@ -1340,10 +1348,8 @@ final class Cache_Enabler {
1340
  if ( $blog_path === '/' ) {
1341
  // get blog paths
1342
  $blog_paths = self::_get_blog_paths();
1343
-
1344
  // get blog domain
1345
  $blog_domain = self::get_blog_domain();
1346
-
1347
  // glob path
1348
  $glob_path = CE_CACHE_DIR . '/' . $blog_domain;
1349
 
@@ -1359,7 +1365,7 @@ final class Cache_Enabler {
1359
  }
1360
 
1361
  // clear home page cache
1362
- self::clear_home_page_cache();
1363
  // subsite
1364
  } else {
1365
  // clear subsite cache
@@ -1954,7 +1960,7 @@ final class Cache_Enabler {
1954
  '<code>755</code>',
1955
  '<code>wp-content/cache</code>',
1956
  sprintf(
1957
- '<a href="%s" target="_blank"></a>',
1958
  'https://wordpress.org/support/article/changing-file-permissions/',
1959
  esc_html__( 'file permissions', 'cache-enabler' )
1960
  )
778
  * get blog domain
779
  *
780
  * @since 1.4.0
781
+ * @change 1.4.8
782
  *
783
  * @return string $domain current blog domain
784
  */
786
  public static function get_blog_domain() {
787
 
788
  // get current blog domain
789
+ $domain = parse_url( get_home_url(), PHP_URL_HOST );
790
 
791
  // check if empty when creating new site in network
792
  if ( is_multisite() && empty( $domain ) ) {
1224
  * clear page cache by post ID
1225
  *
1226
  * @since 1.0.0
1227
+ * @change 1.4.8
1228
  *
1229
  * @param integer|string $post_id post ID
1230
  */
1231
 
1232
  public static function clear_page_cache_by_post_id( $post_id ) {
1233
 
 
 
 
 
 
1234
  // validate integer
1235
  if ( ! is_int( $post_id ) ) {
1236
  // if string try to convert to integer
1250
  * clear page cache by URL
1251
  *
1252
  * @since 1.0.0
1253
+ * @change 1.4.8
1254
  *
1255
+ * @param string $clear_url full URL of a cached page
1256
+ * @param string $clear_type clear all specific cached `page` variants or the entire `dir`
1257
  */
1258
 
1259
  public static function clear_page_cache_by_url( $clear_url, $clear_type = 'page' ) {
1260
 
1261
+ // validate string
1262
+ if ( ! is_string( $clear_url ) ) {
1263
  return;
1264
  }
1265
 
1266
+ // validate URL
1267
+ if ( ! filter_var( $clear_url, FILTER_VALIDATE_URL ) ) {
1268
  return;
1269
  }
1270
 
1280
  * clear home page cache
1281
  *
1282
  * @since 1.0.7
1283
+ * @change 1.4.8
1284
+ *
1285
+ * @param integer $blog_id blog ID
1286
  */
1287
 
1288
+ public static function clear_home_page_cache( $blog_id = null ) {
1289
+
1290
+ // set blog ID if given, get current site otherwise
1291
+ $blog_id = ( $blog_id ) ? $blog_id : get_current_blog_id();
1292
+
1293
+ // get home page URL
1294
+ $home_page_url = get_home_url( $blog_id );
1295
 
1296
  // clear home page cache
1297
+ self::clear_page_cache_by_url( $home_page_url );
1298
 
1299
  // clear home page cache post hook
1300
  do_action( 'ce_action_home_page_cache_cleared' );
1305
  * clear blog ID cache
1306
  *
1307
  * @since 1.4.0
1308
+ * @change 1.4.8
1309
  *
1310
  * @param integer|string $blog_id blog ID
1311
  */
1327
  }
1328
  }
1329
 
1330
+ // check if blog ID exists
1331
+ if ( ! in_array( $blog_id, self::_get_blog_ids() ) ) {
1332
+ return;
1333
+ }
1334
+
1335
  // set clear URL
1336
+ $clear_url = get_home_url( $blog_id );
1337
 
1338
  // network with subdomain configuration
1339
  if ( is_subdomain_install() ) {
1348
  if ( $blog_path === '/' ) {
1349
  // get blog paths
1350
  $blog_paths = self::_get_blog_paths();
 
1351
  // get blog domain
1352
  $blog_domain = self::get_blog_domain();
 
1353
  // glob path
1354
  $glob_path = CE_CACHE_DIR . '/' . $blog_domain;
1355
 
1365
  }
1366
 
1367
  // clear home page cache
1368
+ self::clear_home_page_cache( $blog_id );
1369
  // subsite
1370
  } else {
1371
  // clear subsite cache
1960
  '<code>755</code>',
1961
  '<code>wp-content/cache</code>',
1962
  sprintf(
1963
+ '<a href="%s" target="_blank">%s</a>',
1964
  'https://wordpress.org/support/article/changing-file-permissions/',
1965
  esc_html__( 'file permissions', 'cache-enabler' )
1966
  )
inc/cache_enabler_cli.class.php CHANGED
@@ -6,7 +6,7 @@ defined( 'ABSPATH' ) || exit;
6
 
7
 
8
  /**
9
- * Interact with Cache Enabler
10
  *
11
  * @since 1.3.5
12
  */
@@ -25,16 +25,26 @@ class Cache_Enabler_CLI {
25
  * [--urls=<url>]
26
  * : Clear the cache for the given URL(s). Separate multiple URLs with commas.
27
  *
 
 
 
28
  * ## EXAMPLES
29
  *
30
- * # Clear all page caches
31
- * wp cache-enabler clear
 
 
 
 
 
32
  *
33
- * # Clear the cache for object IDs 1, 2, and 3
34
- * wp cache-enabler clear --ids=1,2,3
 
35
  *
36
- * # Clear the cache for a particular URL
37
- * wp cache-enabler clear --urls=https://example.com/about-us
 
38
  *
39
  * @alias clear
40
  */
@@ -44,22 +54,49 @@ class Cache_Enabler_CLI {
44
  $assoc_args = wp_parse_args(
45
  $assoc_args,
46
  array(
47
- 'ids' => '',
48
- 'urls' => '',
 
49
  )
50
  );
51
 
52
- // clear everything if we are not given IDs and/or URLs
53
- if ( empty( $assoc_args['ids'] ) && empty( $assoc_args['urls'] ) ) {
54
  Cache_Enabler::clear_total_cache();
55
 
56
- return WP_CLI::success( esc_html__( 'The page cache has been cleared.', 'cache-enabler' ) );
57
  }
58
 
59
- // clear specific IDs and/or URLs
60
- array_map( 'Cache_Enabler::clear_page_cache_by_post_id', explode( ',', $assoc_args['ids'] ) );
61
- array_map( 'Cache_Enabler::clear_page_cache_by_url', explode( ',', $assoc_args['urls'] ) );
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- WP_CLI::success( 'The requested caches have been cleared.', 'cache-enabler' );
 
 
 
 
 
 
 
 
 
 
 
 
64
  }
65
  }
 
 
 
6
 
7
 
8
  /**
9
+ * Interact with Cache Enabler.
10
  *
11
  * @since 1.3.5
12
  */
25
  * [--urls=<url>]
26
  * : Clear the cache for the given URL(s). Separate multiple URLs with commas.
27
  *
28
+ * [--sites=<site>]
29
+ * : Clear the cache for the given blog ID(s). Separate multiple blog IDs with commas.
30
+ *
31
  * ## EXAMPLES
32
  *
33
+ * # Clear all pages cache.
34
+ * $ wp cache-enabler clear
35
+ * Success: Cache cleared.
36
+ *
37
+ * # Clear the page cache for post IDs 1, 2, and 3.
38
+ * $ wp cache-enabler clear --ids=1,2,3
39
+ * Success: Pages cache cleared.
40
  *
41
+ * # Clear the page cache for a particular URL.
42
+ * $ wp cache-enabler clear --urls=https://example.com/about-us
43
+ * Success: Page cache cleared.
44
  *
45
+ * # Clear all pages cache for sites with blog IDs 1, 2, and 3.
46
+ * $ wp cache-enabler clear --sites=1,2,3
47
+ * Success: Sites cache cleared.
48
  *
49
  * @alias clear
50
  */
54
  $assoc_args = wp_parse_args(
55
  $assoc_args,
56
  array(
57
+ 'ids' => '',
58
+ 'urls' => '',
59
+ 'sites' => '',
60
  )
61
  );
62
 
63
+ // clear complete cache if no associative arguments are given
64
+ if ( empty( $assoc_args['ids'] ) && empty( $assoc_args['urls'] ) && empty( $assoc_args['sites'] ) ) {
65
  Cache_Enabler::clear_total_cache();
66
 
67
+ return WP_CLI::success( ( is_multisite() && is_plugin_active_for_network( CE_BASE ) ) ? esc_html__( 'Network cache cleared.', 'cache-enabler' ) : esc_html__( 'Cache cleared.', 'cache-enabler' ) );
68
  }
69
 
70
+ // clear page(s) cache by post ID(s) and/or URL(s)
71
+ if ( ! empty( $assoc_args['ids'] ) || ! empty( $assoc_args['urls'] ) ) {
72
+ array_map( 'Cache_Enabler::clear_page_cache_by_post_id', explode( ',', $assoc_args['ids'] ) );
73
+ array_map( 'Cache_Enabler::clear_page_cache_by_url', explode( ',', $assoc_args['urls'] ) );
74
+
75
+ // check if there is more than one ID and/or URL
76
+ $separators = substr_count( $assoc_args['ids'], ',' ) + substr_count( $assoc_args['urls'], ',' );
77
+
78
+ if ( $separators > 0 ) {
79
+ return WP_CLI::success( esc_html__( 'Pages cache cleared.', 'cache-enabler' ) );
80
+ } else {
81
+ return WP_CLI::success( esc_html__( 'Page cache cleared.', 'cache-enabler' ) );
82
+ }
83
+ }
84
 
85
+ // clear pages cache by blog ID(s)
86
+ if ( ! empty( $assoc_args['sites'] ) ) {
87
+ array_map( 'Cache_Enabler::clear_blog_id_cache', explode( ',', $assoc_args['sites'] ) );
88
+
89
+ // check if there is more than one site
90
+ $separators = substr_count( $assoc_args['sites'], ',' );
91
+
92
+ if ( $separators > 0 ) {
93
+ return WP_CLI::success( esc_html__( 'Sites cache cleared.', 'cache-enabler' ) );
94
+ } else {
95
+ return WP_CLI::success( esc_html__( 'Site cache cleared.', 'cache-enabler' ) );
96
+ }
97
+ }
98
  }
99
  }
100
+
101
+ // add WP-CLI command for Cache Enabler
102
+ WP_CLI::add_command( 'cache-enabler', 'Cache_Enabler_CLI' );
inc/cache_enabler_disk.class.php CHANGED
@@ -235,7 +235,7 @@ final class Cache_Enabler_Disk {
235
  * create files
236
  *
237
  * @since 1.0.0
238
- * @change 1.4.0
239
  *
240
  * @param string $data HTML content
241
  */
@@ -264,7 +264,7 @@ final class Cache_Enabler_Disk {
264
  // create webp supported files
265
  if ( $options['webp'] ) {
266
  // magic regex rule
267
- $regex_rule = '#(?<=(?:(ref|src|set)=[\"\']))(?:[^\"\']+)(\.png|\.jp[e]?g)(?:[^\"\']+)?(?=[\"\')])#';
268
 
269
  // call the webp converter callback
270
  $converted_data = apply_filters( 'cache_enabler_disk_webp_converted_data', preg_replace_callback( $regex_rule, 'self::_convert_webp', $data ) );
@@ -418,33 +418,34 @@ final class Cache_Enabler_Disk {
418
  * get cached file path
419
  *
420
  * @since 1.0.0
421
- * @change 1.4.7
422
  *
423
- * @param string $path URI or permalink
424
- * @return string $diff path to cached file
425
  */
426
 
427
- private static function _file_path( $path = null ) {
428
 
429
- $path = sprintf(
430
  '%s%s%s%s',
431
  CE_CACHE_DIR,
432
  DIRECTORY_SEPARATOR,
433
  parse_url(
434
- ( $path ) ? get_site_url() : 'http://' . strtolower( $_SERVER['HTTP_HOST'] ),
435
  PHP_URL_HOST
436
  ),
437
  parse_url(
438
- ( $path ) ? $path : $_SERVER['REQUEST_URI'],
439
  PHP_URL_PATH
440
  )
441
  );
442
 
443
- if ( is_file( $path ) ) {
444
- wp_die( 'Path is not valid.' );
 
445
  }
446
 
447
- return trailingslashit( $path );
448
  }
449
 
450
 
@@ -527,7 +528,7 @@ final class Cache_Enabler_Disk {
527
  * get settings file
528
  *
529
  * @since 1.4.0
530
- * @change 1.4.0
531
  *
532
  * @return string settings file path
533
  */
@@ -537,14 +538,14 @@ final class Cache_Enabler_Disk {
537
  // network with subdirectory configuration
538
  if ( is_multisite() && ! is_subdomain_install() ) {
539
  // get blog path
540
- $path = trim( get_blog_details()->path, '/' );
541
  // check if subsite
542
- if ( ! empty( $path ) ) {
543
- $path = '-' . $path;
544
  }
545
  // single site, network subdirectory main site, or any network subdomain site
546
  } else {
547
- $path = '';
548
  }
549
 
550
  // get settings file
@@ -552,7 +553,7 @@ final class Cache_Enabler_Disk {
552
  '%s-%s%s.json',
553
  WP_CONTENT_DIR . '/plugins/cache-enabler/settings/cache-enabler-advcache',
554
  Cache_Enabler::get_blog_domain(),
555
- $path
556
  );
557
 
558
  return $settings_file;
@@ -693,119 +694,70 @@ final class Cache_Enabler_Disk {
693
 
694
 
695
  /**
696
- * convert to webp
697
  *
698
- * @since 1.0.1
699
- * @change 1.1.1
700
  *
701
- * @return string converted HTML file
 
702
  */
703
 
704
- private static function _convert_webp( $asset ) {
705
 
706
- if ( $asset[1] === 'src' ) {
707
- return self::_convert_webp_src( $asset[0] );
708
- } elseif ( $asset[1] === 'ref' ) {
709
- return self::_convert_webp_src( $asset[0] );
710
- } elseif ( $asset[1] === 'set' ) {
711
- return self::_convert_webp_srcset( $asset[0] );
712
- }
713
 
714
- return $asset[0];
715
  }
716
 
717
 
718
  /**
719
- * convert src to webp source
720
  *
721
  * @since 1.0.1
722
- * @change 1.1.0
723
  *
724
- * @return string converted src webp source
 
725
  */
726
 
727
- private static function _convert_webp_src( $src ) {
728
-
729
- $upload_dir = wp_upload_dir();
730
- $src_url = parse_url( $upload_dir['baseurl'] );
731
- $upload_path = $src_url['path'];
732
 
733
- if ( strpos( $src, $upload_path ) !== false ) {
 
734
 
735
- $src_webp = str_replace( '.jpg', '.webp', $src );
736
- $src_webp = str_replace( '.jpeg', '.webp', $src_webp );
737
- $src_webp = str_replace( '.png', '.webp', $src_webp );
 
 
 
 
 
 
738
 
739
- $parts = explode( $upload_path, $src_webp );
740
- $relative_path = $parts[1];
741
-
742
- // check if relative path is not empty and file exists
743
- if ( ! empty( $relative_path ) && file_exists( $upload_dir['basedir'] . $relative_path ) ) {
744
- return $src_webp;
745
- } else {
746
- // try appended webp extension
747
- $src_webp_appended = $src . '.webp';
748
- $parts_appended = explode( $upload_path, $src_webp_appended );
749
- $relative_path_appended = $parts_appended[1];
750
-
751
- // check if relative path is not empty and file exists
752
- if ( ! empty( $relative_path_appended ) && file_exists( $upload_dir['basedir'] . $relative_path_appended ) ) {
753
- return $src_webp_appended;
754
- }
755
- }
756
- }
757
-
758
- return $src;
759
- }
760
-
761
-
762
- /**
763
- * convert srcset to webp source
764
- *
765
- * @since 1.0.8
766
- * @change 1.1.0
767
- *
768
- * @return string converted srcset webp source
769
- */
770
-
771
- private static function _convert_webp_srcset( $srcset ) {
772
-
773
- $sizes = explode( ', ', $srcset );
774
- $upload_dir = wp_upload_dir();
775
- $src_url = parse_url( $upload_dir['baseurl'] );
776
- $upload_path = $src_url['path'];
777
-
778
- for ( $i = 0; $i < count( $sizes ); $i++ ) {
779
- if ( strpos( $sizes[ $i ], $upload_path ) !== false ) {
780
- $src_webp = str_replace( '.jpg', '.webp', $sizes[ $i ] );
781
- $src_webp = str_replace( '.jpeg', '.webp', $src_webp );
782
- $src_webp = str_replace( '.png', '.webp', $src_webp );
783
-
784
- $size_parts = explode( ' ', $src_webp );
785
- $parts = explode( $upload_path, $size_parts[0] );
786
- $relative_path = $parts[1];
787
-
788
- // check if relative path is not empty and file exists
789
- if ( ! empty( $relative_path ) && file_exists( $upload_dir['basedir'] . $relative_path ) ) {
790
- $sizes[ $i ] = $src_webp;
791
  } else {
792
- // try appended webp extension
793
- $size_parts_appended = explode( ' ', $sizes[ $i ] );
794
- $src_webp_appended = $size_parts_appended[0] . '.webp';
795
- $parts_appended = explode( $upload_path, $src_webp_appended );
796
- $relative_path_appended = $parts_appended[1];
797
- $src_webp_appended = $src_webp_appended . ' '. $size_parts_appended[1];
798
-
799
- // check if relative path is not empty and file exists
800
- if ( ! empty( $relative_path_appended ) && file_exists( $upload_dir['basedir'] . $relative_path_appended ) ) {
801
- $sizes[ $i ] = $src_webp_appended;
802
  }
803
  }
804
  }
805
- }
806
 
807
- $srcset = implode( ', ', $sizes );
808
 
809
- return $srcset;
 
810
  }
811
  }
235
  * create files
236
  *
237
  * @since 1.0.0
238
+ * @change 1.4.8
239
  *
240
  * @param string $data HTML content
241
  */
264
  // create webp supported files
265
  if ( $options['webp'] ) {
266
  // magic regex rule
267
+ $regex_rule = '#(?:(?:(src|srcset|data-[^=]+)\s*=|(url)\()\s*[\'\"]?\s*)\K(?:[^\?\"\'\s>]+)(?:\.jpe?g|\.png)(?:\s\d+w[^\"\'>]*)?(?=\/?[\"\'\s\)>])(?=[^<{]*(?:\)[^<{]*\}|>))#i';
268
 
269
  // call the webp converter callback
270
  $converted_data = apply_filters( 'cache_enabler_disk_webp_converted_data', preg_replace_callback( $regex_rule, 'self::_convert_webp', $data ) );
418
  * get cached file path
419
  *
420
  * @since 1.0.0
421
+ * @change 1.4.8
422
  *
423
+ * @param string $url full URL of a cached page
424
+ * @return string path to cached file
425
  */
426
 
427
+ private static function _file_path( $url = null ) {
428
 
429
+ $file_path = sprintf(
430
  '%s%s%s%s',
431
  CE_CACHE_DIR,
432
  DIRECTORY_SEPARATOR,
433
  parse_url(
434
+ ( $url ) ? $url : 'http://' . strtolower( $_SERVER['HTTP_HOST'] ),
435
  PHP_URL_HOST
436
  ),
437
  parse_url(
438
+ ( $url ) ? $url : $_SERVER['REQUEST_URI'],
439
  PHP_URL_PATH
440
  )
441
  );
442
 
443
+ if ( is_file( $file_path ) ) {
444
+ header( $_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found', true, 404 );
445
+ exit;
446
  }
447
 
448
+ return trailingslashit( $file_path );
449
  }
450
 
451
 
528
  * get settings file
529
  *
530
  * @since 1.4.0
531
+ * @change 1.4.8
532
  *
533
  * @return string settings file path
534
  */
538
  // network with subdirectory configuration
539
  if ( is_multisite() && ! is_subdomain_install() ) {
540
  // get blog path
541
+ $blog_path = trim( get_blog_details()->path, '/' );
542
  // check if subsite
543
+ if ( ! empty( $blog_path ) ) {
544
+ $blog_path = '-' . $blog_path;
545
  }
546
  // single site, network subdirectory main site, or any network subdomain site
547
  } else {
548
+ $blog_path = '';
549
  }
550
 
551
  // get settings file
553
  '%s-%s%s.json',
554
  WP_CONTENT_DIR . '/plugins/cache-enabler/settings/cache-enabler-advcache',
555
  Cache_Enabler::get_blog_domain(),
556
+ $blog_path
557
  );
558
 
559
  return $settings_file;
694
 
695
 
696
  /**
697
+ * get image path
698
  *
699
+ * @since 1.4.8
700
+ * @change 1.4.8
701
  *
702
+ * @param string $image_url full or relative URL with or without intrinsic width
703
+ * @return string $image_path path to image
704
  */
705
 
706
+ private static function _image_path( $image_url ) {
707
 
708
+ // in case image has intrinsic width
709
+ $image_parts = explode( ' ', $image_url );
710
+ $image_url = $image_parts[0];
711
+ $image_path = ABSPATH . ltrim( parse_url( $image_url, PHP_URL_PATH ), '/' );
 
 
 
712
 
713
+ return $image_path;
714
  }
715
 
716
 
717
  /**
718
+ * convert image URL to WebP
719
  *
720
  * @since 1.0.1
721
+ * @change 1.4.8
722
  *
723
+ * @param array $matches pattern matches from parsed HTML file
724
+ * @return string $conversion converted image URL(s) to WebP if applicable, default URL(s) otherwise
725
  */
726
 
727
+ private static function _convert_webp( $matches ) {
 
 
 
 
728
 
729
+ $full_match = strtolower( $matches[0] );
730
+ $image_count = substr_count( $full_match, '.png' ) + substr_count( $full_match, '.jpg' ) + substr_count( $full_match, '.jpeg' );
731
 
732
+ if ( $image_count > 0 ) {
733
+ $image_urls = explode( ',', $full_match );
734
+ foreach ( $image_urls as &$image_url ) {
735
+ // remove spaces if there are any
736
+ $image_url = trim( $image_url, ' ' );
737
+ // append .webp extension
738
+ $image_url_webp = preg_replace( '/(\.jpe?g|\.png)/', '$1.webp', $image_url );
739
+ // get WebP image path
740
+ $image_path_webp = self::_image_path( $image_url_webp );
741
 
742
+ // check if WebP image exists
743
+ if ( is_file( $image_path_webp ) ) {
744
+ $image_url = $image_url_webp;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
745
  } else {
746
+ // remove default extension
747
+ $image_url_webp = preg_replace( '/(\.jpe?g|\.png)/', '', $image_url_webp );
748
+ // get WebP image path
749
+ $image_path_webp = self::_image_path( $image_url_webp );
750
+
751
+ // check if WebP image exists
752
+ if ( is_file( $image_path_webp ) ) {
753
+ $image_url = $image_url_webp;
 
 
754
  }
755
  }
756
  }
 
757
 
758
+ $conversion = implode( ', ', $image_urls );
759
 
760
+ return $conversion;
761
+ }
762
  }
763
  }
readme.txt CHANGED
@@ -47,14 +47,17 @@ When combined with Optimus, the WordPress Cache Enabler allows you to easily del
47
 
48
  = WP-CLI =
49
 
50
- * Clear all page caches
51
- `wp cache-enabler clear`
52
 
53
- * Clear the cache for object IDs 1, 2, and 3
54
- `wp cache-enabler clear --ids=1,2,3`
55
 
56
- * Clear the cache for a particular URL
57
- `wp cache-enabler clear --urls=https://example.com/about-us`
 
 
 
58
 
59
 
60
  = Website =
@@ -81,11 +84,19 @@ When combined with Optimus, the WordPress Cache Enabler allows you to easily del
81
 
82
  == Changelog ==
83
 
 
 
 
 
 
 
 
 
84
  = 1.4.7 =
85
  * Update getting wp-config.php if one level above installation (#106)
86
  * Add clear types for strict cache clearing (#110)
87
  * Fix advanced cache settings recognition for subdirectory multisite networks
88
- * Fix WP-CLI clear command for post IDs (#110)
89
  * Fix scheme-based caching for NGINX/PHP-FPM (#109)
90
  * Fix trailing slash handling
91
 
47
 
48
  = WP-CLI =
49
 
50
+ * Clear all pages cache.
51
+ `wp cache-enabler clear`
52
 
53
+ * Clear the page cache for post IDs 1, 2, and 3.
54
+ `wp cache-enabler clear --ids=1,2,3`
55
 
56
+ * Clear the page cache for a particular URL.
57
+ `wp cache-enabler clear --urls=https://example.com/about-us`
58
+
59
+ * Clear all pages cache for sites with blog IDs 1, 2, and 3.
60
+ `wp cache-enabler clear --sites=1,2,3`
61
 
62
 
63
  = Website =
84
 
85
  == Changelog ==
86
 
87
+ = 1.4.8 =
88
+ * Update WebP URL conversion (#116)
89
+ * Update WP-CLI clear subcommand messages (#111)
90
+ * Update WP-CLI clear subcommand for multisite networks (#111)
91
+ * Fix cache clearing for installations in a subdirectory
92
+ * Fix advanced cache settings recognition for installations in a subdirectory
93
+ * Fix file permissions requirement notice
94
+
95
  = 1.4.7 =
96
  * Update getting wp-config.php if one level above installation (#106)
97
  * Add clear types for strict cache clearing (#110)
98
  * Fix advanced cache settings recognition for subdirectory multisite networks
99
+ * Fix WP-CLI clear subcommand for post IDs (#110)
100
  * Fix scheme-based caching for NGINX/PHP-FPM (#109)
101
  * Fix trailing slash handling
102