Skip to content
Snippets Groups Projects
Commit a8a2d1ea authored by Erick Hitter's avatar Erick Hitter
Browse files

Handle scheduling delete request and processing it

parent 4fbce4cb
No related branches found
No related tags found
No related merge requests found
......@@ -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 );
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment