From 783961839dc1123265750b00ddef387f215c5815 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sat, 27 Mar 2021 22:35:59 -0700 Subject: [PATCH] REST endpoint for purging excess revisions --- inc/class-wp-revisions-control.php | 72 ++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/inc/class-wp-revisions-control.php b/inc/class-wp-revisions-control.php index ad51242..4dc419c 100644 --- a/inc/class-wp-revisions-control.php +++ b/inc/class-wp-revisions-control.php @@ -71,6 +71,13 @@ class WP_Revisions_Control { */ private $meta_key_limit = '_wp_rev_ctl_limit'; + /** + * Name of action used to clean up post's revisions via cron. + * + * @var string + */ + private $cron_action = 'wp_revisions_control_cron_purge'; + /** * Silence is golden! */ @@ -121,6 +128,7 @@ class WP_Revisions_Control { add_action( 'rest_api_init', array( $this, 'action_rest_api_init' ) ); add_filter( 'is_protected_meta', array( $this, 'filter_is_protected_meta' ), 10, 2 ); add_action( 'enqueue_block_editor_assets', array( $this, 'action_enqueue_block_editor_assets' ) ); + add_action( $this->cron_action, array( $this, 'do_purge_excess' ) ); } /** @@ -279,8 +287,8 @@ class WP_Revisions_Control { /** * Override Core's revisions metabox. * - * @param string $post_type Post type. - * @param \WP_Post $post Post object. + * @param string $post_type Post type. + * @param WP_Post $post Post object. */ public function action_add_meta_boxes( $post_type, $post ) { if ( @@ -522,7 +530,7 @@ class WP_Revisions_Control { */ /** - * Register meta for Gutenberg UI. + * Register REST API components for Gutenberg UI. */ public function action_rest_api_init() { foreach ( array_keys( $this->get_post_types() ) as $post_type ) { @@ -542,6 +550,64 @@ class WP_Revisions_Control { ) ); } + + register_rest_route( + 'wp-revisions-control/v1', + 'schedule/(?P<id>[\d]+)', + array( + 'methods' => 'PUT', + 'callback' => array( $this, 'rest_api_schedule_purge' ), + 'permission_callback' => array( $this, 'rest_api_permission_callback' ), + 'args' => array( + 'id' => array( + 'required' => true, + 'type' => 'integer', + 'validate_callback' => array( $this, 'rest_api_validate_id' ), + ), + ), + ) + ); + } + + /** + * Permissions callback for REST endpoint. + * + * @param WP_REST_Request $request Request object. + * @return bool + */ + public function rest_api_permission_callback( $request ) { + return current_user_can( + 'edit_post', + $request->get_param( 'id' ) + ); + } + + /** + * Validate post ID. + * + * @param int $input Post ID. + * @return bool + */ + public function rest_api_validate_id( $input ) { + return is_numeric( $input ); + } + + /** + * Schedule cleanup of post's excess revisions. + * + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response + */ + public function rest_api_schedule_purge( $request ) { + $result = wp_schedule_single_event( + time() + 3, + $this->cron_action, + [ + $request->get_param( 'id' ), + ] + ); + + return rest_ensure_response( $result ); } /** -- GitLab