Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Multisite Cache Deletion Issue with lazyload background images feature #7226

Merged
merged 10 commits into from
Jan 29, 2025
9 changes: 9 additions & 0 deletions inc/Engine/Common/Cache/CacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
37 changes: 36 additions & 1 deletion inc/Engine/Common/Cache/FilesystemCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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, '/' ) . '/';
}
}
2 changes: 1 addition & 1 deletion inc/Engine/Common/ExtractCSS/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
19 changes: 19 additions & 0 deletions inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'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 ],
];
}

Expand Down Expand Up @@ -79,4 +80,22 @@
$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 ) {

Check warning on line 91 in inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/Lazyload/CSS/Admin/Subscriber.php#L91

Avoid unused parameters such as '$new_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 );
}
}
4 changes: 0 additions & 4 deletions inc/Engine/Media/Lazyload/CSS/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ServiceProvider extends AbstractServiceProvider {
* @var array
*/
protected $provides = [
'lazyload_css_cache',
'lazyload_css_subscriber',
];

Expand All @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
'expected' => [
'output' => true,
'path' => '/cache/background-css/'
'path' => '/cache/background-css/1/'
]
],
'notExistsShouldReturnFalse' => [
Expand All @@ -17,7 +17,7 @@
],
'expected' => [
'output' => true,
'path' => '/cache/background-css/'
'path' => '/cache/background-css/1/'
]
],

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],
Expand All @@ -30,7 +30,7 @@
]
],
'expected' => [
'path' => '/cache/background-css/example.org/blog/test',
'path' => '/cache/background-css/1/example.org/blog/test',
'output' => true
]
],
Expand All @@ -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
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
],
Expand All @@ -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',
]
],
Expand All @@ -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',
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
],
Expand All @@ -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
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
],

];
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],
Expand All @@ -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
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
],
Expand All @@ -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
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
],
Expand All @@ -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,
]
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
Expand Down Expand Up @@ -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,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
WP Rocket</strong>
cannot configure itself due to missing writing permissions.<br>
Affected file/folder:<code>
vfs://public/wp-content/cache/background-css/</code>
vfs://public/wp-content/cache/background-css/1/</code>
<br>
Troubleshoot:<a href="https://docs.wp-rocket.me/article/626-how-to-make-system-files-htaccess-wp-config-writeable/?utm_source=wp_plugin&utm_medium=wp_rocket" target="_blank">
How to make system files writeable</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<title>
HTML 5 Boilerplate</title>
<!-- external CSS background images here min and non-min file -->
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/example.org/wp-content/rocket-test-data/styles/lazyload_css_background_images.css?test=1&wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/1/example.org/wp-content/rocket-test-data/styles/lazyload_css_background_images.css?test=1&wpr_t=17895120">
<link rel="stylesheet" href="/wp-content/rocket-test-data/styles/excluded.css">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/lazyload_css_background_images.min.css?wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/1/new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/lazyload_css_background_images.min.css?wpr_t=17895120">
<link rel="stylesheet" href="https://new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/no_background.css">
<!-- CSS loaded by js, probably won't be processed -->
<script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<!-- external CSS background images here min and non-min file -->
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/example.org/wp-content/rocket-test-data/styles/lazyload_css_background_images.css?test=1&wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/example.org/wp-content/rocket-test-data/styles/excluded.css?wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/lazyload_css_background_images.min.css?wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/1/example.org/wp-content/rocket-test-data/styles/lazyload_css_background_images.css?test=1&wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/1/example.org/wp-content/rocket-test-data/styles/excluded.css?wpr_t=17895120">
<link rel="stylesheet" href="http://example.org/wp-content/cache/background-css/1/new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/lazyload_css_background_images.min.css?wpr_t=17895120">
<link rel="stylesheet" href="https://new.rocketlabsqa.ovh/wp-content/rocket-test-data/styles/no_background.css">

<!-- CSS loaded by js, probably won't be processed -->
Expand Down
Loading
Loading