diff --git a/includes/class-move-to-trash.php b/includes/class-move-to-trash.php index abc31a1da0548749f128428568a4c43a5f2a32b2..32b03f0ce16404687daea912db5243a70e59a8f9 100644 --- a/includes/class-move-to-trash.php +++ b/includes/class-move-to-trash.php @@ -21,15 +21,69 @@ class Move_To_Trash { */ public static function register_hooks() { add_action( Main::build_hook( 'trash' ), array( __CLASS__, 'process' ) ); + add_action( self::CRON_EVENT, array( __CLASS__, 'process_via_cron' ) ); } /** - * Handle a request to delete all trashed items for a given post type + * Handle a request to move some posts to the trash * * @param object $vars Bulk-request variables. */ public static function process( $vars ) { - error_log( var_export( $vars, true ) ); + wp_schedule_single_event( time(), self::CRON_EVENT, array( $vars ) ); + + Main::do_admin_redirect( self::ADMIN_NOTICE_KEY, true ); + } + + /** + * Cron callback to move requested items to trash + * + * @param object $vars Bulk-request variables. + */ + public static function process_via_cron( $vars ) { + $count = 0; + + if ( is_array( $vars->posts ) && ! empty( $vars->posts ) ) { + require_once ABSPATH . '/wp-admin/includes/post.php'; + + $trashed = array(); + $locked = array(); + $auth_error = array(); + $error = array(); + + foreach ( $vars->posts as $post_id ) { + // Can the user trash this post? + if ( ! user_can( $vars->user_id, 'delete_post', $post_id ) ) { + $auth_error[] = $post_id; + continue; + } + + // Post is locked by someone, so leave it alone. + if ( false !== wp_check_post_lock( $post_id ) ) { + $locked[] = $post_id; + continue; + } + + // Try trashing. + $post_trashed = wp_trash_post( $post_id ); + if ( $post_trashed ) { + $trashed[] = $post_id; + } else { + $error[] = $post_id; + } + + // Take a break periodically. + if ( 0 === $count++ % 50 ) { + stop_the_insanity(); + sleep( 3 ); + } + } + + $results = compact( 'trashed', 'locked', 'auth_error', 'error' ); + do_action( 'bulk_edit_cron_offload_move_to_trash_request_completed', $results, $vars ); + } else { + do_action( 'bulk_edit_cron_offload_move_to_trash_request_no_posts', $vars->posts, $vars ); + } } }