diff --git a/inc/class-wp-revisions-control-bulk-actions.php b/inc/class-wp-revisions-control-bulk-actions.php index b8563ba4fa2322c113f685db9e696fdab12a2376..268d35f75be7781506369abcfbca27785b54143e 100644 --- a/inc/class-wp-revisions-control-bulk-actions.php +++ b/inc/class-wp-revisions-control-bulk-actions.php @@ -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 ); + } + } } diff --git a/inc/class-wp-revisions-control.php b/inc/class-wp-revisions-control.php index 2d556ad9ab9c35672ae16997c22d2817ddc8d0b0..5f6da1b2e35af5623d05a3303a83c94aa3f6461e 100644 --- a/inc/class-wp-revisions-control.php +++ b/inc/class-wp-revisions-control.php @@ -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. *