Skip to content

Commit b6babf6

Browse files
Copilotswissspidy
andcommitted
Add wp post revision prune command
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent f812fc5 commit b6babf6

3 files changed

Lines changed: 274 additions & 0 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"post meta update",
117117
"post revision",
118118
"post revision diff",
119+
"post revision prune",
119120
"post revision restore",
120121
"post term",
121122
"post term add",

‎features/post-revision.feature‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,104 @@ Feature: Manage WordPress post revisions
149149

150150
When I run `wp post revision diff {REVISION_ID} --field=post_title`
151151
Then the return code should be 0
152+
153+
Scenario: Prune revisions keeping latest N
154+
When I run `wp post create --post_title='Prune Test' --post_content='Version 1' --porcelain`
155+
Then STDOUT should be a number
156+
And save STDOUT as {POST_ID}
157+
158+
When I run `wp post update {POST_ID} --post_content='Version 2'`
159+
When I run `wp post update {POST_ID} --post_content='Version 3'`
160+
When I run `wp post update {POST_ID} --post_content='Version 4'`
161+
When I run `wp post update {POST_ID} --post_content='Version 5'`
162+
163+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
164+
Then STDOUT should be:
165+
"""
166+
5
167+
"""
168+
169+
When I run `wp post revision prune {POST_ID} --latest=2 --yes`
170+
Then STDOUT should contain:
171+
"""
172+
Success: Deleted 3 revisions for post {POST_ID}.
173+
"""
174+
175+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
176+
Then STDOUT should be:
177+
"""
178+
2
179+
"""
180+
181+
Scenario: Prune revisions keeping earliest N
182+
When I run `wp post create --post_title='Prune Earliest Test' --post_content='Version 1' --porcelain`
183+
Then STDOUT should be a number
184+
And save STDOUT as {POST_ID}
185+
186+
When I run `wp post update {POST_ID} --post_content='Version 2'`
187+
When I run `wp post update {POST_ID} --post_content='Version 3'`
188+
When I run `wp post update {POST_ID} --post_content='Version 4'`
189+
190+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
191+
Then STDOUT should be:
192+
"""
193+
4
194+
"""
195+
196+
When I run `wp post revision prune {POST_ID} --earliest=2 --yes`
197+
Then STDOUT should contain:
198+
"""
199+
Success: Deleted 2 revisions for post {POST_ID}.
200+
"""
201+
202+
When I run `wp post list --post_type=revision --post_parent={POST_ID} --format=count`
203+
Then STDOUT should be:
204+
"""
205+
2
206+
"""
207+
208+
Scenario: Prune revisions for all posts
209+
When I run `wp post create --post_title='Post 1' --post_content='Content 1' --porcelain`
210+
Then save STDOUT as {POST_ID_1}
211+
212+
When I run `wp post update {POST_ID_1} --post_content='Update 1'`
213+
When I run `wp post update {POST_ID_1} --post_content='Update 2'`
214+
When I run `wp post update {POST_ID_1} --post_content='Update 3'`
215+
216+
When I run `wp post create --post_title='Post 2' --post_content='Content 2' --porcelain`
217+
Then save STDOUT as {POST_ID_2}
218+
219+
When I run `wp post update {POST_ID_2} --post_content='Update 1'`
220+
When I run `wp post update {POST_ID_2} --post_content='Update 2'`
221+
222+
When I run `wp post revision prune --latest=1 --yes`
223+
Then STDOUT should contain:
224+
"""
225+
Success: Deleted
226+
"""
227+
And STDOUT should contain:
228+
"""
229+
revisions across
230+
"""
231+
232+
Scenario: Prune with no flags should fail
233+
When I run `wp post create --post_title='Test' --post_content='Content' --porcelain`
234+
Then save STDOUT as {POST_ID}
235+
236+
When I try `wp post revision prune {POST_ID}`
237+
Then STDERR should contain:
238+
"""
239+
Error: Please specify either --latest or --earliest flag.
240+
"""
241+
And the return code should be 1
242+
243+
Scenario: Prune with both flags should fail
244+
When I run `wp post create --post_title='Test' --post_content='Content' --porcelain`
245+
Then save STDOUT as {POST_ID}
246+
247+
When I try `wp post revision prune {POST_ID} --latest=5 --earliest=5`
248+
Then STDERR should contain:
249+
"""
250+
Error: Cannot specify both --latest and --earliest flags.
251+
"""
252+
And the return code should be 1

‎src/Post_Revision_Command.php‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,176 @@ private function render_cli_diff( $diff ) {
245245
}
246246
}
247247
}
248+
249+
/**
250+
* Deletes old post revisions.
251+
*
252+
* ## OPTIONS
253+
*
254+
* [<post-id>...]
255+
* : One or more post IDs to prune revisions for. If not provided, prunes revisions for all posts.
256+
*
257+
* [--latest=<limit>]
258+
* : Keep only the latest N revisions per post. Older revisions will be deleted.
259+
*
260+
* [--earliest=<limit>]
261+
* : Keep only the earliest N revisions per post. Newer revisions will be deleted.
262+
*
263+
* [--yes]
264+
* : Skip confirmation prompt.
265+
*
266+
* ## EXAMPLES
267+
*
268+
* # Delete all but the latest 5 revisions for post 123
269+
* $ wp post revision prune 123 --latest=5
270+
* Success: Deleted 3 revisions for post 123.
271+
*
272+
* # Delete all but the latest 5 revisions for all posts
273+
* $ wp post revision prune --latest=5
274+
* Success: Deleted 150 revisions across 30 posts.
275+
*
276+
* # Delete all but the earliest 2 revisions for posts 123 and 456
277+
* $ wp post revision prune 123 456 --earliest=2
278+
* Success: Deleted 5 revisions for post 123.
279+
* Success: Deleted 3 revisions for post 456.
280+
*
281+
* @subcommand prune
282+
*/
283+
public function prune( $args, $assoc_args ) {
284+
$latest = Utils\get_flag_value( $assoc_args, 'latest', null );
285+
$earliest = Utils\get_flag_value( $assoc_args, 'earliest', null );
286+
287+
// Validate flags
288+
if ( null === $latest && null === $earliest ) {
289+
WP_CLI::error( 'Please specify either --latest or --earliest flag.' );
290+
}
291+
292+
if ( null !== $latest && null !== $earliest ) {
293+
WP_CLI::error( 'Cannot specify both --latest and --earliest flags.' );
294+
}
295+
296+
$limit = $latest ?? $earliest;
297+
$keep_latest = null !== $latest;
298+
299+
if ( ! is_numeric( $limit ) || (int) $limit < 1 ) {
300+
WP_CLI::error( 'Limit must be a positive integer.' );
301+
}
302+
303+
$limit = (int) $limit;
304+
305+
// Get posts to process
306+
if ( ! empty( $args ) ) {
307+
$post_ids = array_map( 'intval', $args );
308+
} else {
309+
// Get all posts that have revisions
310+
global $wpdb;
311+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
312+
$post_ids = $wpdb->get_col(
313+
"SELECT DISTINCT post_parent FROM {$wpdb->posts} WHERE post_type = 'revision' AND post_parent > 0"
314+
);
315+
$post_ids = array_map( 'intval', $post_ids );
316+
}
317+
318+
if ( empty( $post_ids ) ) {
319+
WP_CLI::warning( 'No posts found with revisions.' );
320+
return;
321+
}
322+
323+
// Confirm deletion if processing multiple posts without --yes flag
324+
if ( count( $post_ids ) > 1 && ! Utils\get_flag_value( $assoc_args, 'yes', false ) ) {
325+
WP_CLI::confirm(
326+
sprintf(
327+
'Are you sure you want to prune revisions for %d posts?',
328+
count( $post_ids )
329+
),
330+
$assoc_args
331+
);
332+
}
333+
334+
$total_deleted = 0;
335+
$posts_processed = 0;
336+
337+
foreach ( $post_ids as $post_id ) {
338+
$deleted = $this->prune_post_revisions( $post_id, $limit, $keep_latest );
339+
340+
if ( false === $deleted ) {
341+
WP_CLI::warning( "Post {$post_id} does not exist or has no revisions." );
342+
continue;
343+
}
344+
345+
if ( $deleted > 0 ) {
346+
++$posts_processed;
347+
$total_deleted += $deleted;
348+
WP_CLI::success( "Deleted {$deleted} revision" . ( $deleted > 1 ? 's' : '' ) . " for post {$post_id}." );
349+
} elseif ( count( $post_ids ) === 1 ) {
350+
WP_CLI::success( "No revisions to delete for post {$post_id}." );
351+
}
352+
}
353+
354+
if ( count( $post_ids ) > 1 ) {
355+
if ( $total_deleted > 0 ) {
356+
WP_CLI::success(
357+
sprintf(
358+
'Deleted %d revision%s across %d post%s.',
359+
$total_deleted,
360+
$total_deleted > 1 ? 's' : '',
361+
$posts_processed,
362+
$posts_processed > 1 ? 's' : ''
363+
)
364+
);
365+
} else {
366+
WP_CLI::success( 'No revisions to delete.' );
367+
}
368+
}
369+
}
370+
371+
/**
372+
* Prunes revisions for a single post.
373+
*
374+
* @param int $post_id The post ID.
375+
* @param int $limit Number of revisions to keep.
376+
* @param bool $keep_latest Whether to keep the latest revisions (true) or earliest (false).
377+
* @return int|false Number of revisions deleted, or false if post not found.
378+
*/
379+
private function prune_post_revisions( $post_id, $limit, $keep_latest ) {
380+
$post = get_post( $post_id );
381+
382+
if ( ! $post ) {
383+
return false;
384+
}
385+
386+
// Get all revisions for this post
387+
$revisions = wp_get_post_revisions( $post_id, [ 'order' => 'ASC' ] );
388+
389+
if ( empty( $revisions ) ) {
390+
return false;
391+
}
392+
393+
$revision_count = count( $revisions );
394+
395+
// If we have fewer or equal revisions than the limit, nothing to delete
396+
if ( $revision_count <= $limit ) {
397+
return 0;
398+
}
399+
400+
// Determine which revisions to delete
401+
$revisions_array = array_values( $revisions );
402+
403+
if ( $keep_latest ) {
404+
// Keep the latest N, delete the rest (from beginning)
405+
$to_delete = array_slice( $revisions_array, 0, $revision_count - $limit );
406+
} else {
407+
// Keep the earliest N, delete the rest (from end)
408+
$to_delete = array_slice( $revisions_array, $limit );
409+
}
410+
411+
$deleted = 0;
412+
foreach ( $to_delete as $revision ) {
413+
if ( $revision instanceof \WP_Post && wp_delete_post_revision( $revision->ID ) ) {
414+
++$deleted;
415+
}
416+
}
417+
418+
return $deleted;
419+
}
248420
}

0 commit comments

Comments
 (0)