/**
  * Filter tickets by assigned staff
  *
  * @since 3.3
  *
  * @param WP_Query $wp_query
  *
  * @return void
  */
 public function filter_staff($wp_query)
 {
     global $pagenow;
     if ('edit.php' !== $pagenow || !isset($_GET['post_type']) || 'ticket' !== $_GET['post_type']) {
         return;
     }
     if (!$wp_query->is_main_query()) {
         return;
     }
     if (!isset($_GET['staff'])) {
         return;
     }
     $staff_id = (int) $_GET['staff'];
     $agent = new WPAS_Member_Agent($staff_id);
     if (!$agent->is_agent()) {
         return;
     }
     $meta_query = $wp_query->get('meta_query');
     if (!is_array($meta_query)) {
         $meta_query = (array) $meta_query;
     }
     $meta_query[] = array('key' => '_wpas_assignee', 'value' => $staff_id, 'compare' => '=');
     if (!isset($meta_query['relation'])) {
         $meta_query['relation'] = 'AND';
     }
     $wp_query->set('meta_query', $meta_query);
 }
/**
 * Find an available agent to assign a ticket to.
 *
 * This is a super basic attribution system. It just finds the agent
 * with the less tickets currently open.
 *
 * @since  3.0.0
 *
 * @param  boolean|integer $ticket_id The ticket that needs an agent
 *
 * @return integer         ID of the best agent for the job
 */
function wpas_find_agent($ticket_id = false)
{
    if (defined('WPAS_DISABLE_AUTO_ASSIGN') && true === WPAS_DISABLE_AUTO_ASSIGN) {
        return apply_filters('wpas_find_available_agent', wpas_get_option('assignee_default'), $ticket_id);
    }
    $users = shuffle_assoc(wpas_get_users(apply_filters('wpas_find_agent_get_users_args', array('cap' => 'edit_ticket'))));
    $agent = array();
    foreach ($users->members as $user) {
        $wpas_agent = new WPAS_Member_Agent($user);
        /**
         * Make sure the user really is an agent and that he can currently be assigned
         */
        if (true !== $wpas_agent->is_agent() || false === $wpas_agent->can_be_assigned()) {
            continue;
        }
        $count = $wpas_agent->open_tickets();
        // Total number of open tickets for this agent
        if (empty($agent)) {
            $agent = array('tickets' => $count, 'user_id' => $user->ID);
        } else {
            if ($count < $agent['tickets']) {
                $agent = array('tickets' => $count, 'user_id' => $user->ID);
            }
        }
    }
    if (is_array($agent) && isset($agent['user_id'])) {
        $agent_id = $agent['user_id'];
    } else {
        $default_id = wpas_get_option('assignee_default', 1);
        if (empty($default_id)) {
            $default_id = 1;
        }
        $agent_id = $default_id;
    }
    return apply_filters('wpas_find_available_agent', (int) $agent_id, $ticket_id);
}
 /**
  * Add auto-assignment user column content
  *
  * @since 3.2
  *
  * @param mixed  $value       Column value
  * @param string $column_name Column name
  * @param int    $user_id     Current user ID
  *
  * @return string
  */
 public function auto_assignment_user_column_content($value, $column_name, $user_id)
 {
     if ('wpas_auto_assignment' !== $column_name) {
         return $value;
     }
     $agent = new WPAS_Member_Agent($user_id);
     if (true !== $agent->is_agent()) {
         return 'N/A';
     }
     if (false === $agent->can_be_assigned()) {
         return '&#10005;';
     }
     return '&#10003;';
 }