-
Erick Hitter authoredErick Hitter authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
class-move-to-trash.php 2.46 KiB
<?php
/**
* Offload "Move to Trash"
*
* @package Bulk_Actions_Cron_Offload
*/
namespace Automattic\WP\Bulk_Actions_Cron_Offload;
/**
* Class Move_To_Trash
*/
class Move_To_Trash {
/**
* Common hooks and such
*/
use Bulk_Actions;
/**
* Class constants
*/
const ACTION = 'trash';
const ADMIN_NOTICE_KEY = 'bulk_actions_cron_offload_move_to_trash';
/**
* 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 ) ) {
$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_actions_cron_offload_move_to_trash_request_completed', $results, $vars );
} else {