Commit 6060b5e5 authored by Erick Hitter's avatar Erick Hitter

Introduce method to purge excess revisions

parent 8d885579
......@@ -16,6 +16,13 @@ class WP_Revisions_Control_Bulk_Actions {
*/
protected $post_types;
/**
* Base for bulk action names.
*
* @var string
*/
protected $action_base = 'wp_rev_ctl_bulk_';
/**
* Custom bulk actions.
*
......@@ -43,11 +50,10 @@ class WP_Revisions_Control_Bulk_Actions {
* Register custom actions.
*/
protected function register_actions() {
$actions = array();
$action_base = 'wp_rev_ctl_bulk_';
$actions = array();
$actions[ $action_base . 'purge_excess' ] = __( 'Purge excess revisions', 'wp_revisions_control' );
$actions[ $action_base . 'purge_all' ] = __( 'Purge ALL revisions', 'wp_revisions_control' );
$actions[ $this->action_base . 'purge_excess' ] = __( 'Purge excess revisions', 'wp_revisions_control' );
$actions[ $this->action_base . 'purge_all' ] = __( 'Purge ALL revisions', 'wp_revisions_control' );
$this->actions = $actions;
}
......@@ -74,6 +80,7 @@ class WP_Revisions_Control_Bulk_Actions {
add_filter( 'bulk_actions-' . $screen->id, array( $this, 'add_actions' ) );
add_filter( 'handle_bulk_actions-' . $screen->id, array( $this, 'handle_action' ), 10, 3 );
// TODO: messages.
}
/**
......@@ -99,7 +106,44 @@ class WP_Revisions_Control_Bulk_Actions {
return $redirect_to;
}
$action = str_replace( $this->action_base, '', $action );
switch ( $action ) {
case 'purge_all':
$this->purge_all( $ids );
break;
case 'purge_excess':
$this->purge_excess( $ids );
break;
default:
break;
}
// TODO: implement and add a query string to trigger a message.
return $redirect_to;
}
/**
* Remove all revisions from the given IDs.
*
* @param array $ids Object IDs.
*/
protected function purge_all( $ids ) {
foreach ( $ids as $id ) {
WP_Revisions_Control::get_instance()->do_purge_all( $id );
}
}
/**
* Remove excess revisions from the given IDs.
*
* @param array $ids Object IDs.
*/
protected function purge_excess( $ids ) {
foreach ( $ids as $id ) {
WP_Revisions_Control::get_instance()->do_purge_excess( $id );
}
}
}
......@@ -410,6 +410,50 @@ class WP_Revisions_Control {
return $response;
}
/**
* Remove any revisions in excess of a post's limit.
*
* @param int $post_id Post ID to purge of excess revisions.
* @return array
*/
public function do_purge_excess( $post_id ) {
$response = array(
'count' => 0,
);
$to_keep = wp_revisions_to_keep( get_post( $post_id ) );
if ( $to_keep < 0 ) {
$response['success'] = __(
'No revisions to remove.',
'wp_revisions_control'
);
return $response;
}
$revisions = wp_get_post_revisions( $post_id );
$starting_count = count( $revisions );
if ( $starting_count <= $to_keep ) {
$response['success'] = __(
'No revisions to remove.',
'wp_revisions_control'
);
return $response;
}
$to_remove = array_slice( $revisions, $to_keep, null, true );
$response['count'] = count( $to_remove );
foreach ( $to_remove as $revision ) {
wp_delete_post_revision( $revision->ID );
}
return $response;
}
/**
* Sanitize and store post-specifiy revisions quantity.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment