wp-rocket
wp-rocket copied to clipboard
rocket_clean_post() - The whole cache is cleared under certain conditions
Before submitting an issue please check that you’ve completed the following steps:
- Made sure you’re on the latest version ✅
- Used the search feature to ensure that the bug hasn’t been reported before ✅
Describe the bug
When the cache of a post is cleared, we are using rocket_clean_post() which calls rocket_get_purge_urls() to get related posts whose cache also needs to be cleared.
If one of them doesn't contain a path, then the $entry will be just $dir, and that will lead to the clearance of the whole cache folder:
https://github.com/wp-media/wp-rocket/blob/e49355167e75f1d3cf51ee65c429b75f3e28682e/inc/functions/files.php#L538-L551
This issue occurred on a customer's website where, for security reasons, they were using the following to disable author archive pages:
add_filter('author_link', function() { return '#'; }, 99);
So, here: https://github.com/wp-media/wp-rocket/blob/e49355167e75f1d3cf51ee65c429b75f3e28682e/inc/common/purge.php#L134 that resulted in:
$purge_urls[] = '#';
and finally, the whole cache was cleared every time the rocket_clean_post() run.
To Reproduce
Steps to reproduce the behavior:
- Hardcode the
#as a value here. - Run
rocket_clean_post()to clear the cache of a post. - Monitor the
/cache/wp-rocket/folder.
Expected behavior
Clear only the cache of related posts/archive pages.
Additional context
This is an edge case, but it's causing unnecessary cache clearing, and it is hard to troubleshoot. It's necessary to safeguard the process.
Could be taken into consideration when #2549 will be tackled.
Related ticket: https://secure.helpscout.net/conversation/1305116799/201131?folderId=273766
Backlog Grooming (for WP Media dev team use only)
- [ ] Reproduce the problem
- [ ] Identify the root cause
- [ ] Scope a solution
- [ ] Estimate the effort
A similar case: https://secure.helpscout.net/conversation/1421714596/238324?folderId=2135277
Here the issue comes from the fact that the author's URL is altered, for security reasons, and the homepage is returned here: https://github.com/wp-media/wp-rocket/blob/8b355a3b75ac201b64806a9e2f7f1bec3b371303/inc/common/purge.php#L134
This results in the whole cache being cleared every time a post's cache is cleared.
The WordPress filter used is author_link.
Another case where rocket_clean_posts() clears the whole cache: https://secure.helpscout.net/conversation/1657430071/300538/
(Is a multisite, all sites are cleared)
Another case where rocket_clean_posts() clears the whole cache: https://secure.helpscout.net/conversation/1997887310/365868/
It is happening when updating an Elementor's template.
@mostafa-hisham wasn't able to reproduce the issue so I checked it.
The case which led me to create this issue is handled by the following change which happened a while back: https://github.com/wp-media/wp-rocket/blob/22240ef297b873c476277b88ae8fea8e00eca452/inc/functions/files.php#L533
Now, if a URL doesn't have a host, e.g. it has only a #, it won't be processed so the specific issue won't take place.
@piotrbak Should we keep this issue open to add cases where rocket_clean_post() unexpectedly clears the whole cache?
Another case where rocket_clean_posts() clears the whole cache when editing Elementor templates: https://secure.helpscout.net/conversation/2027023981/372593
@piotrbak any update on this one?
@DahmaniAdame As per Vasilis, the issue described in the initial ticket is not valid anymore: https://github.com/wp-media/wp-rocket/issues/3246#issuecomment-1283460493
In that case it'll be better to close the issue and redo finding the root cause
@piotrbak I'm closing this one. Check #5916.