Improve cleanup of unknown or unregistered image sizes and add logging
More thorough removal of files with unknown or unregistered image sizes. Delete corresponding .webp images if they exist. Log deleted and generated file names.
Fixes #217
Hi there, thanks for your PR!
A couple of initial observations:
Deleting $intermediate_path . '.webp' is dangerous. There could be a legitimate, separate attachment which happens to have the same file name. We don't want to break that. I assume the EWWW plugin stores webp information in attachment meta too? That's how other plugins do it. Then we can just delete all files found in the metadata.
If that's not the case, we would need to make this behavior opt-in, as it's very risky.
Similarly, deleting any files not found in the database is also very risky. And it's not really related to thumbnail regeneration. This functionality is probably better kept in its own dedicated command. See #6 and https://github.com/DigitalTactics/wp-cli-mediadiff
Since that's a lot of changes overall, we would definitely require some tests to verify they work as intended.
The additional logging can be useful, but also a bit noisy. We should consider making them debug messages or so.
Aside: let's use the term allowlist instead of whitelist :)
EWWW doesn't save any webp info in the attachment metadata.
So when we've got pairs like image-120x120.jpg and image-120x120.jpg.webp, maybe we should double-check if both exist before deleting the webp version. Or even run a quick SQL query to see if the webp was uploaded directly - something like this one that checks for allowlisted thumbnails.
As for deleting files not found in the db - well, that’s exactly what "Removes files for unknown/unregistered image sizes" is meant to do using the command wp media regenerate --delete-unknown. This part of the code draws significant inspiration from Regenerate Thumbnails plugin.
Without proper logging, we’re just guessing what is going on. It might say thumbnails were regenerated, but behind the scenes… nothing actually happened.