Process Bulk Actions (callable)

array key: process_bulk_actions

redirects and exits on execution

The process_bulk_actions callable executes on list table construction and is designed to use query string values based on action and action2 to invoke execution. If action and action2 are not set to a value used for processing this callable should do nothing. Like link actions bulk actions are commonly used to delete or put things in the trash. The process_bulk_actions callable should redirect after processing and usually some values, such as pagination or view should be specified on redirect. The bulk action generally uses checkbox array cb[] during processing and redirects when action or action2 are set.

function saturn_tables_cars_process_bulk_actions() {

    global $wpdb;

    if (isset($_GET['action']) && ($_GET['action'] == 'bulk_trash') || (isset($_GET['action2']) && ($_GET['action2'] == 'bulk_trash' ))) {      

        $trash_ids = esc_sql( $_GET['cb'] );

        $count =  0;        
        foreach ($trash_ids as $id) {
            $affected = $wpdb->update( 'saturn_tables_cars',  array('status' => 1), array('id' => $id),  array('%d'),  array('%d') );
            $count += $affected;
        }

        parse_str($_SERVER['QUERY_STRING'], $query_string);
        unset($query_string['action'],$query_string['action2']);
        $query_string['bulk'] = "trash";
        $query_string['count'] = $count;
        $redirect = admin_url() . "?" . http_build_query($query_string);
        wp_redirect( $redirect );       
        exit;

    } elseif (isset($_GET['action']) && ($_GET['action'] == 'bulk_delete') || (isset($_GET['action2']) && ($_GET['action2'] == 'bulk_delete' ))) {

        $delete_ids = esc_sql( $_GET['cb'] );

        $count =  0;        
        foreach ($delete_ids as $id) {
            $affected = $wpdb->delete( 'saturn_tables_cars', array('id' => $id),  array('id' => '%d'));;
            $count += $affected;
        }

        parse_str($_SERVER['QUERY_STRING'], $query_string);
        unset($query_string['action'],$query_string['action2']);
        $query_string['bulk'] = "delete";
        $query_string['status'] = "trash";
        $query_string['count'] = $count;
        $redirect = admin_url() . "?" . http_build_query($query_string);
        wp_redirect( $redirect );       
        exit;
    }
}