Last active
February 28, 2019 14:14
-
-
Save 1naveengiri/f5cfd9f98065e1c7cf695f000fe15162 to your computer and use it in GitHub Desktop.
Clear all revision in you single site or multisite with WP-CLI Command. You can also keep few of them with the help of this command.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Clear Old Revisions | |
* Plugin URI: http://buddydevelopers.com | |
* Description: plugin to clear old revision data. | |
*/ | |
/** | |
* Class to clear old revision data. | |
*/ | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
class Clear_Revision_Command extends WP_CLI_Command { | |
/** | |
* Delete old revisions | |
* | |
* ## OPTIONS | |
* | |
* [<keep>] | |
* : Number of revisions to keep per post. Defaults to WP_POST_REVISIONS if it is an integer | |
* | |
* [--post_type=<post-type>] | |
* : Clean revisions for given post type(s). Default: any | |
* | |
* [--after-date=<yyyy-mm-dd>] | |
* : Clean revisions published on or after this date. Default: none. | |
* | |
* [--before-date=<yyyy-mm-dd>] | |
* : Clean revisions published on or before this date. Default: none. | |
* | |
* [--post_id=<post-id>] | |
* : Clean revisions for given post. | |
* | |
* [--hard] | |
* : Hard delete. Slower, uses wp_delete_post_revision(). | |
* | |
* [--dry-run] | |
* : Dry run, just a test, no actual cleaning done. | |
* | |
* ## EXAMPLES | |
* | |
* wp revisions clean | |
* wp revisions clean 5 | |
* wp revisions clean --post_id=2 | |
* wp revisions clean 5 --post_type=post,page | |
* wp revisions clean --after-date=2015-11-01 --before-date=2015-12-30 | |
* wp revisions clean --after-date=2015-11-01 --before-date=2015-12-30 --dry-run | |
*/ | |
public function clean( $args = array(), $assoc_args = array() ) { | |
global $wpdb; | |
if ( ! empty( $assoc_args['post_id'] ) ) { | |
$posts = array( $assoc_args['post_id'] ); | |
} else { | |
if ( empty( $assoc_args['post_type'] ) ) { | |
$post_types = $this->supports_revisions(); | |
} else { | |
$post_types = explode( ',', $assoc_args['post_type'] ); | |
} | |
$where = ''; | |
$post_type_where = array(); | |
foreach ( $post_types as $post_type ) { | |
$post_type_where[] = $wpdb->prepare( 'post_type = %s', $post_type ); | |
} | |
$where = ' AND (' . implode( ' OR ', $post_type_where ) . ')'; | |
if ( isset( $assoc_args['after-date'] ) && isset( $assoc_args['before-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND (post_date < %s AND post_date > %s)', $assoc_args['before-date'], $assoc_args['after-date'] ); | |
} else if ( isset( $assoc_args['after-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND post_date > %s', $assoc_args['after-date'] ); | |
} else if ( isset( $assoc_args['before-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND post_date < %s', $assoc_args['before-date'] ); | |
} | |
// get all IDs for posts in given post type(s). | |
$posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE 1=1 {$where}" ); | |
} | |
$total = count( $posts ); | |
$notify = \WP_CLI\Utils\make_progress_bar( sprintf( 'Cleaning revisions for %d post(s)', $total ), $total ); | |
if ( isset( $args[0] ) ) { | |
$keep = intval( $args[0] ); | |
} else if ( true === WP_POST_REVISIONS ) { | |
WP_CLI::error( 'WP_POST_REVISIONS is set to true (keeps all revisions). Please pass a number.' ); | |
} else { | |
$keep = WP_POST_REVISIONS; | |
} | |
$total_deleted = 0; | |
foreach ( $posts as $post_id ) { | |
$revisions = get_children( array( | |
'order' => 'DESC', | |
'orderby' => 'date ID', | |
'post_parent' => $post_id, | |
'post_type' => 'revision', | |
'post_status' => 'inherit', | |
// trust me on these. | |
'update_post_meta_cache' => false, | |
'update_post_term_cache' => false, | |
'fields' => 'ids', | |
) ); | |
if ( ! $revisions ) { | |
$notify->tick(); | |
continue; | |
} | |
$count = count( $revisions ); | |
if ( $count > $keep ) { | |
if ( $keep > 0 ) { | |
$revisions = array_slice( $revisions, $keep, null, true ); | |
} | |
$total_deleted += count( $revisions ); | |
if ( isset( $assoc_args['hard'] ) ) { | |
foreach ( $revisions as $id ) { | |
if ( empty( $assoc_args['dry-run'] ) ) { | |
wp_delete_post_revision( $id ); | |
} | |
} | |
} else { | |
$delete_ids = implode( ',', $revisions ); | |
if ( empty( $assoc_args['dry-run'] ) ) { | |
$wpdb->query( "DELETE FROM $wpdb->posts WHERE ID IN ($delete_ids)" ); | |
} | |
} | |
} | |
$wpdb->flush(); | |
$notify->tick(); | |
} | |
$notify->finish(); | |
if ( ! empty( $assoc_args['dry-run'] ) ) { | |
WP_CLI::success( sprintf( 'Dry Run: Will remove %d old revisions.', $total_deleted ) ); | |
} else { | |
WP_CLI::success( sprintf( 'Finished removing %d old revisions.', $total_deleted ) ); | |
} | |
} | |
/** | |
* List all revisions | |
* | |
* ## OPTIONS | |
* | |
* [--post_type=<post-type>] | |
* : List revisions for given post type(s). | |
* | |
* [--post_id=<post-id>] | |
* : List revisions for given post. Trumps --post_type. | |
* | |
* [--yes] | |
* : Answer yes to the confirmation message. | |
* | |
* ## EXAMPLES | |
* | |
* wp revisions list | |
* wp revisions list --post_id=2 | |
* wp revisions list --post_type=post,page | |
* | |
* @subcommand list | |
*/ | |
public function list_( $args = array(), $assoc_args = array() ) { | |
global $wpdb; | |
if ( ! empty( $assoc_args['post_id'] ) ) { | |
$revs = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE post_parent = %d", $assoc_args['post_id'] ) ); | |
} else if ( ! empty( $assoc_args['post_type'] ) ) { | |
$post_types = explode( ',', $assoc_args['post_type'] ); | |
$where = ''; | |
foreach ( $post_types as $post_type ) { | |
$where .= $wpdb->prepare( ' OR post_type = %s', $post_type ); | |
} | |
// get all IDs for posts in given post type(s). | |
$ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE 1=2 {$where}" ); | |
$post__in = implode( ',', $ids ); | |
// get revisions of those IDs. | |
$revs = $wpdb->get_results( "SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE post_type = 'revision' AND post_parent IN ({$post__in}) ORDER BY post_parent DESC" ); | |
} else { | |
$revs = $wpdb->get_results( "SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE post_type = 'revision' ORDER BY post_parent DESC" ); | |
} | |
$total = count( $revs ); | |
if ( $total > 100 ) { | |
WP_CLI::confirm( sprintf( 'List all %d revisions?', $total ), $assoc_args ); | |
} | |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'ID', 'post_parent', 'post_title' ), 'revisions' ); | |
$formatter->display_items( $revs ); | |
WP_CLI::success( sprintf( '%d revisions.', $total ) ); | |
} | |
/** | |
* Supports Revisions | |
* | |
* Get list of post types that support revisions | |
* | |
* @return array | |
*/ | |
private function supports_revisions() { | |
$supports_revisions = array(); | |
foreach ( get_post_types() as $post_type ) { | |
if ( post_type_supports( $post_type, 'revisions' ) ) { | |
$supports_revisions[] = $post_type; | |
} | |
} | |
return $supports_revisions; | |
} | |
} | |
WP_CLI::add_command( 'revisions', 'Clear_Revision_Command' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment