Open remkus opened 8 years ago
The users do not need to set how often the WP Cron runs. The WP Cron should just keep firing at regular intervals until all of the images are gone, then stop.
Sure, but this setting was referring to starting that general cron job at certain intervals
But are you sure you want to just delete images on a regular basis? I'm not sure what use case anyone would have for that.
Yes. If you’re using this plugin to periodically clean up your media library for example when you have a lot of imported posts for example or a lot of posts not reaching a published status.
A settings page which lets you choose which types of images it deletes? Could select based on post-type, taxonomy, featured image. (Since taxonomies, user images and the likes can have (featured) images as well)
Post-types make sense, and have been implemented for this.
Taxonomies and other things don't really make sense to me here though, as they can't have featured images or attachments. You can upload an attachment, and set that attachment as a piece of taxonomy meta, but it is not directly attached to the taxonomy per se, unlike posts.
Well, how are we going to prevent deleting an image that is connected to a term then?
I don't think that is possible. We'd need to know in what way the image was connected with the term. WordPress has no built-in way to know that. Each plugin/theme implements it however they feel like it, so we could only add support for specific known plugins/themes.
For my Unique Headers plugin for example, it stores an attachment ID for a taxonomy term. But it's just an arbitarily chosen taxonomy meta key I used. Unless I've missed something, there's no way of actually attaching an image to a taxonomy.
Hmm, I guess there's no way of getting the attachment ID and do a sort of reverse lookup in the database where that is stored, right?
If a post has the concept status and there are images attached but not (yet) set as featured image(s) then those images should be untouched
I've set it to only delete attachments from things which are set to "publish". This ensures things like auto-saves, pending posts etc are not deleted too. If you think this should be specific to draft posts, please let me know and I'll refactor it a little.
We could go through every bit of taxonomy meta and do a reverse lookup, but we'd only be guessing that the bit of data we were looking at was actually an attachment ID. It could be coincidentally a piece of data about something else.
For example, a taxonomy meta with a persons year of birth in say 1980, could result in the attachment with ID of 1980 being deleted.
I actually wrote the plugin to iterate through every post, and just delete the images which are attached to it but not the featured image. So there isn't any risk that the current plugin would inadvertently start deleting images it shouldn't do. It'll only delete stuff which is actually attached to a post. Deleting everything could result in some weird problems. Stuff like custom header images for example would get deleted, which would likely not be the desired result.
only delete attachments from things which are set to "publish
That's fine, thanks.
Have a settings page – under the Media Menu – where you can set a cron for how often this should fire. Daily, weekly, monthly intervals come to mind
The BackupWP plugin has exactly that settings page (tab, really) that perhaps you could use for inspiration for this feature.
It'll only delete stuff which is actually attached to a post
Perfect.
Have a settings page – under the Media Menu – where you can set a cron for how often this should fire. Daily, weekly, monthly intervals come to mind
The current setup fires a WP Cron task every 30 seconds (I chose that number since it's what many hosts set as their maximum execution time). This means that it is continously attempting to delete stuff. If there is nothing to delete, it just doesn't bother doing anything. I think this makes the need for a Cron setting unneeded.
I'm not entirely sure what the use-case is for this though, so perhaps I'm misunderstanding your intention here though.
Since the task is run every 30 seconds, we can set the number of things to iterate through to a very low level to keep the total load on the system quite low. It currently checks a maximum of five posts each time.
This means that it is continuously attempting to delete stuff.
OK, that potentially sounds like a solid solution as well. Will need to have it out in the open and see how it goes.
I've modified it so that instead of processing them in batches of posts, it just keeps looping over and over again until there are no posts left to process, then it finishes the WP Cron task. If it runs out of time, then it'll wait for the next WP Cron task to fire and start back at the point it stopped at previously.
To reduce any sudden load on the system, I've introduced a 100 ms sleep time between each post.
Cool. That should do the trick and keep resources low.
This plugin should: