@@ -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