Reputation: 252
I'm building a platform that allows members to create textbooks, courses, classrooms, and "labs". Is there a way to allow only certain members to store more than, say, 3 revisions?
I thought that maybe I could do a check like:
if(!has_cap('store_unlimited_revisions')) define('WP_REVISIONS', 3);
Where everyone is limited to 3, except for those with 'store_unlimited_revisions'
. However, if there are 10 revisions already and you define 3, then only the latest 3 are saved and the rest removed when you create a new revision.
Would this work? If a person with the role (A) saves an instant before a person without the role (B), would person A lose their revisions because person B was the last person to define the revision limit?
If not, is there some other way to remove the oldest revision above 3 upon save?
Upvotes: 0
Views: 106
Reputation: 4849
Re your first "would this work OK?" question - to be honest I'm not familiar with how that constant is used, nor specifically how/when revisions are purged based on that constant, so I can't answer that sorry. Experiment with it yourself, I guess.
Re the "other way", here's how I'd do it. Something like this will pull out all revisions for a given post ID:
SELECT
ID
FROM
wp_posts
WHERE
post_parent = {$id_of_post_being_saved}
and
post_type = 'revision'
and
post_name like '{$id_of_post_being_saved}-revision%'
ORDER BY
id
(post_name like '{$id_of_post_being_saved}-revision%'
included in the where clause to exclude eg autosaves).
Then could (back in PHP) establish if the count of revisions returned exceeds 3, and sort on post_name
(eg "425-revision", "425-revision-2", "425-revision-3" etc for revisions of post id 425) or on post_date
to get the "oldest" revision (whatever logic meets your needs, basically - either way I'd recommend some research/experimentation to confirm that logic will actually isolate the revision you're after, though, as I'm pulling this out of my head without having done this myself in practice!)
Now, this code I have used in practice, to delete ALL revisions on all posts (ie doing a clean-up of ALL old drafts prior to release to production on WP-based brochure-ware sites):
DELETE
a,b,c
FROM
wp_posts a
LEFT JOIN wp_term_relationships b
ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c
ON (a.ID = c.post_id)
WHERE
a.post_type = 'revision'
But now that you have the post ID
of the specific post's specific revision that you want to knock on the head (from the first query), add the following to the above where clause to do that:
and a.id = {$post_id_to_delete}
NB This could all be implemented in one clever SQL statement, eg by using a self join between the first result set and grouped max of the first result set, and then deleting based on that... but personally I'd handle the "smarts" in PHP so that you can more easily adjust the logic (eg the older than 3 revisions rule) based on parameters available in WP/PHP etc.
Upvotes: 1