diff --git a/inc/Engine/Common/Cache/CacheInterface.php b/inc/Engine/Common/Cache/CacheInterface.php index 5daeb7b9dc..bdc8c95f6e 100644 --- a/inc/Engine/Common/Cache/CacheInterface.php +++ b/inc/Engine/Common/Cache/CacheInterface.php @@ -33,4 +33,13 @@ public function get_root_path(): string; * @return string */ public function generate_path( string $url ): string; + + /** + * Wipes the whole cache directory. + * + * @param array $preserve_dirs List of directories to be preserved. + * + * @return bool True on success and false on failure. + */ + public function full_clear( array $preserve_dirs = [] ): bool; } diff --git a/inc/Engine/Common/Cache/FilesystemCache.php b/inc/Engine/Common/Cache/FilesystemCache.php index 7096898174..d3c0d43478 100644 --- a/inc/Engine/Common/Cache/FilesystemCache.php +++ b/inc/Engine/Common/Cache/FilesystemCache.php @@ -103,6 +103,32 @@ public function clear() { return true; } + /** + * Clear the whole background-css directory. + * + * @param array $preserve_dirs List of directories to be preserved. + * + * @return bool True on success and false on failure. + */ + public function full_clear( array $preserve_dirs = [] ): bool { + $base_path = $this->get_base_path(); + if ( ! $this->filesystem->exists( $base_path ) ) { + return false; + } + + if ( ! empty( $preserve_dirs ) ) { + $preserve_dirs = array_map( + function ( $dir ) use ( $base_path ) { + return $base_path . $dir; + }, + $preserve_dirs + ); + } + + rocket_rrmdir( $base_path, $preserve_dirs, $this->filesystem ); + return true; + } + /** * Obtains multiple cache items by their unique keys. * @@ -226,11 +252,20 @@ public function is_accessible(): bool { } /** - * Get root path from the cache. + * Get root path from the cache (including current blog ID) with trailing slash. * * @return string */ public function get_root_path(): string { + return $this->get_base_path() . get_current_blog_id() . '/'; + } + + /** + * Get base path from the cache (/cache/background-css/) with trailing slash. + * + * @return string + */ + private function get_base_path(): string { return rtrim( _rocket_normalize_path( rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH' ) ) . $this->root_folder, '/' ) . '/'; } } diff --git a/inc/Engine/Common/ExtractCSS/ServiceProvider.php b/inc/Engine/Common/ExtractCSS/ServiceProvider.php index 0148a2f9cc..dea19f7be0 100644 --- a/inc/Engine/Common/ExtractCSS/ServiceProvider.php +++ b/inc/Engine/Common/ExtractCSS/ServiceProvider.php @@ -41,7 +41,7 @@ public function register(): void { * * @param string $root Background CSS cache folder. */ - $root = apply_filters( 'rocket_lazyload_css_cache_root', 'background-css' ); + $root = apply_filters( 'rocket_lazyload_css_cache_root', 'background-css/' ); $this->getContainer()->add( 'lazyload_css_cache', FilesystemCache::class ) ->addArgument( $root ); $this->getContainer()->addShared( 'common_extractcss_subscriber', Subscriber::class ); diff --git a/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php index 525608e7c1..cfc265e103 100644 --- a/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php @@ -34,6 +34,7 @@ public static function get_subscribed_events() { 'rocket_meta_boxes_fields' => [ 'add_meta_box', 8 ], 'admin_notices' => 'maybe_add_error_notice', 'rocket_safe_mode_reset_options' => 'add_option_safemode', + 'wp_rocket_upgrade' => [ 'clear_background_css_with_upgrade', 10, 2 ], ]; } @@ -79,4 +80,22 @@ public function add_option_safemode( array $options ) { $options['lazyload_css_bg_img'] = 0; return $options; } + + /** + * Upgrade callback. + * + * @param string $new_version Plugin new version. + * @param string $old_version Plugin old version. + * @return void + */ + public function clear_background_css_with_upgrade( $new_version, $old_version ) { + if ( empty( $old_version ) || version_compare( $old_version, '3.18', '>' ) ) { + return; + } + + $preserve_dirs = is_multisite() ? get_sites( [ 'fields' => 'ids' ] ) : [ get_current_blog_id() ]; + + // Completely clear background-css directory. + $this->cache->full_clear( $preserve_dirs ); + } } diff --git a/inc/Engine/Media/Lazyload/CSS/ServiceProvider.php b/inc/Engine/Media/Lazyload/CSS/ServiceProvider.php index 10f0189942..e0bbc28a15 100644 --- a/inc/Engine/Media/Lazyload/CSS/ServiceProvider.php +++ b/inc/Engine/Media/Lazyload/CSS/ServiceProvider.php @@ -24,7 +24,6 @@ class ServiceProvider extends AbstractServiceProvider { * @var array */ protected $provides = [ - 'lazyload_css_cache', 'lazyload_css_subscriber', ]; @@ -45,9 +44,6 @@ public function provides( string $id ): bool { * @return void */ public function register(): void { - $this->getContainer()->add( 'lazyload_css_cache', FilesystemCache::class ) - ->addArgument( apply_filters( 'rocket_lazyload_css_cache_root', 'background-css/' . get_current_blog_id() ) ); - $cache = $this->getContainer()->get( 'lazyload_css_cache' ); $this->getContainer()->add( 'lazyload_css_context', LazyloadCSSContext::class ) diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/clear.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/clear.php index 9c6d6cd7e4..da017b8f25 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/clear.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/clear.php @@ -7,7 +7,7 @@ ], 'expected' => [ 'output' => true, - 'path' => '/cache/background-css/' + 'path' => '/cache/background-css/1/' ] ], 'notExistsShouldReturnFalse' => [ @@ -17,7 +17,7 @@ ], 'expected' => [ 'output' => true, - 'path' => '/cache/background-css/' + 'path' => '/cache/background-css/1/' ] ], diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/delete.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/delete.php index 70d2d2ca1b..a550a680c4 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/delete.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/delete.php @@ -13,7 +13,7 @@ ] ], 'expected' => [ - 'path' => '/cache/background-css/example.org/blog/test/file.css', + 'path' => '/cache/background-css/1/example.org/blog/test/file.css', 'output' => true ] ], @@ -30,7 +30,7 @@ ] ], 'expected' => [ - 'path' => '/cache/background-css/example.org/blog/test', + 'path' => '/cache/background-css/1/example.org/blog/test', 'output' => true ] ], @@ -47,7 +47,7 @@ ] ], 'expected' => [ - 'path' => '/cache/background-css/example.org/blog/test/file.css', + 'path' => '/cache/background-css/1/example.org/blog/test/file.css', 'output' => false ] ], diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/deleteMultiple.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/deleteMultiple.php index 493fbdf28a..3ddf7ecc16 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/deleteMultiple.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/deleteMultiple.php @@ -19,12 +19,12 @@ ], ], 'is_dir' => [ - '/cache/background-css/example.org/blog/test/file.css' => false, - '/cache/background-css/example.org/blog/test' => true, + '/cache/background-css/1/example.org/blog/test/file.css' => false, + '/cache/background-css/1/example.org/blog/test' => true, ], 'exists' => [ - '/cache/background-css/example.org/blog/test/file.css' => true, - '/cache/background-css/example.org/blog/test' => true, + '/cache/background-css/1/example.org/blog/test/file.css' => true, + '/cache/background-css/1/example.org/blog/test' => true, ], ], 'expected' => [ @@ -50,11 +50,11 @@ ], ], 'is_dir' => [ - '/cache/background-css/example.org/blog/test/file.css' => false, + '/cache/background-css/1/example.org/blog/test/file.css' => false, ], 'exists' => [ - '/cache/background-css/example.org/blog/test/file.css' => true, - '/cache/background-css/example.org/blog/test' => false, + '/cache/background-css/1/example.org/blog/test/file.css' => true, + '/cache/background-css/1/example.org/blog/test' => false, ], ], 'expected' => [ diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/generateUrl.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/generateUrl.php index 5c023e121f..bbe2cd21c5 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/generateUrl.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/generateUrl.php @@ -19,8 +19,8 @@ ], 'expected' => [ 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', - 'output' => 'http://example.org/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', + 'output' => 'http://example.org/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'home_url' => 'http://example.org', ] ], @@ -43,8 +43,8 @@ ], 'expected' => [ 'url' => '/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', - 'output' => 'http://example.org/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', + 'output' => 'http://example.org/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'home_url' => 'http://example.org', ] ], @@ -67,8 +67,8 @@ ], 'expected' => [ 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', - 'output' => 'http://example.org/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', + 'output' => 'http://example.org/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'home_url' => 'http://example.org', ] ], diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/get.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/get.php index eaf7b77d96..396f93ca40 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/get.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/get.php @@ -15,7 +15,7 @@ ], 'expected' => [ 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'output' => 'content' ] ], @@ -34,7 +34,7 @@ ], 'expected' => [ 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'output' => null ] ] diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getMultiple.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getMultiple.php index f7e10bfb68..8794bf7fcf 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getMultiple.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getMultiple.php @@ -20,11 +20,11 @@ 'root' => '/var/html/wp-content/cache', 'home_url' => 'http://example.org', 'exists' => [ - '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css' => true, - '/var/html/wp-content/cache/background-css/example.org/blog/test/file2.css' => false, + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css' => true, + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file2.css' => false, ], 'content' => [ - '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css' => 'content' + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css' => 'content' ] ], 'expected' => [ diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getRootPath.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getRootPath.php index d25635979f..4d185f0b5e 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getRootPath.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/getRootPath.php @@ -4,7 +4,7 @@ 'config' => [ 'root' => '/var/html/wp-content/cache', ], - 'expected' => '/var/html/wp-content/cache/background-css/' + 'expected' => '/var/html/wp-content/cache/background-css/1/' ], ]; diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/has.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/has.php index 168bca8c81..610ebe2b8d 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/has.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/has.php @@ -13,7 +13,7 @@ ], 'expected' => [ 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'output' => true ] ], @@ -30,7 +30,7 @@ ], 'expected' => [ 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'output' => false ] ], diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/isAccessible.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/isAccessible.php index 0ae657da78..60d24c114a 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/isAccessible.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/isAccessible.php @@ -7,7 +7,7 @@ 'root' => '/var/html/wp-content/cache' ], 'expected' => [ - 'path' => '/var/html/wp-content/cache/background-css/', + 'path' => '/var/html/wp-content/cache/background-css/1/', 'output' => false ] ], @@ -19,7 +19,7 @@ 'root' => '/var/html/wp-content/cache' ], 'expected' => [ - 'path' => '/var/html/wp-content/cache/background-css/', + 'path' => '/var/html/wp-content/cache/background-css/1/', 'output' => true ] ], diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/set.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/set.php index f352ff2252..10d9ef868e 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/set.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/set.php @@ -17,7 +17,7 @@ 'expected' => [ 'content' => 'content', 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'output' => true, ] ], @@ -38,7 +38,7 @@ 'expected' => [ 'content' => 'content', 'url' => 'http://example.org/blog/test/file.css', - 'path' => '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css', + 'path' => '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css', 'output' => false, ] ], diff --git a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/setMultiple.php b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/setMultiple.php index 82ebcb1002..f064e16098 100644 --- a/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/setMultiple.php +++ b/tests/Fixtures/inc/Engine/Common/Cache/FilesystemCache/setMultiple.php @@ -21,11 +21,11 @@ 'home_url' => 'http://example.org', 'root' => '/var/html/wp-content/cache', 'saved' => [ - '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css' => [ + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css' => [ 'content' => 'content', 'output' => true, ], - '/var/html/wp-content/cache/background-css/example.org/blog/test/file2.css' => [ + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file2.css' => [ 'content' => 'content2', 'output' => true, ], @@ -56,11 +56,11 @@ 'home_url' => 'http://example.org', 'root' => '/var/html/wp-content/cache', 'saved' => [ - '/var/html/wp-content/cache/background-css/example.org/blog/test/file.css' => [ + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file.css' => [ 'content' => 'content', 'output' => true, ], - '/var/html/wp-content/cache/background-css/example.org/blog/test/file2.css' => [ + '/var/html/wp-content/cache/background-css/1/example.org/blog/test/file2.css' => [ 'content' => 'content2', 'output' => false, ], diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber/maybeAddErrorNoticeIntegration.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber/maybeAddErrorNoticeIntegration.php index 2fe6167cdf..f728883d6d 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber/maybeAddErrorNoticeIntegration.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Admin/Subscriber/maybeAddErrorNoticeIntegration.php @@ -7,7 +7,7 @@ WP Rocket cannot configure itself due to missing writing permissions.
Affected file/folder: -vfs://public/wp-content/cache/background-css/ +vfs://public/wp-content/cache/background-css/1/
Troubleshoot: How to make system files writeable diff --git a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php index 7bf875ada2..9a9e903221 100644 --- a/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php +++ b/tests/Fixtures/inc/Engine/Media/Lazyload/CSS/Subscriber/integration/HTML/html_excluded.php @@ -7,9 +7,9 @@ HTML 5 Boilerplate - + - +