Beispiel #1
0
/**
 * Notify all reviewers on new ticket submission
 *
 * @param $email array The email notification data
 * @param $case  string The email notification case
 *
 * @return array Email data, possibly updated with BCCs
 */
function awp_as_notify_reviewers($email, $case)
{
    /* Only alter e-mail notifications on new ticket assignment */
    if ('new_ticket_assigned' !== $case) {
        return $email;
    }
    $reviewers = wpas_get_users(array('cap' => 'edit_ticket'));
    // Get the list of agents/reviewers
    $recipient = (array) $email['recipient_email'];
    if (empty($reviewers)) {
        return $email;
    }
    foreach ($reviewers as $reviewer) {
        /* Make sure we have a WP_User object first */
        if (!is_a($reviewer, 'WP_User')) {
            continue;
        }
        /* Don't add the original recipient to the BCCs */
        if ($email['recipient_email'] === $reviewer->data->user_email) {
            continue;
        }
        array_push($recipient, $reviewer->data->user_email);
    }
    if (count($recipient) > 1) {
        $email['recipient_email'] = $recipient;
    }
    return $email;
}
Beispiel #2
0
 function test_get_users_cache()
 {
     $args = array('exclude' => array(), 'cap' => 'edit_ticket', 'cap_exclude' => '', 'search' => array());
     $hash = md5(serialize($args));
     $users = wpas_get_users($args);
     $cache = wp_cache_get('users_' . $hash, 'wpas');
     delete_transient("wpas_list_users_{$hash}");
     $this->assertInternalType('array', $cache);
     $this->assertCount(2, $cache);
 }
Beispiel #3
0
 function test_get_users_cache()
 {
     $args = array('exclude' => array(), 'cap' => 'edit_ticket', 'cap_exclude' => '');
     $hash = substr(md5(serialize($args)), 0, 10);
     $users = wpas_get_users($args);
     $transient = get_transient("wpas_list_users_{$hash}");
     delete_transient("wpas_list_users_{$hash}");
     $this->assertInternalType('array', $transient);
     $this->assertCount(2, $transient);
 }
/**
 * 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);
}
/**
 * Creates a dropdown list of users.
 *
 * @since  3.1.2
 * @param  array  $args Arguments
 * @return string       Users dropdown
 */
function wpas_users_dropdown($args = array())
{
    global $current_user, $post;
    $defaults = array('name' => 'wpas_user', 'id' => '', 'class' => '', 'exclude' => array(), 'selected' => '', 'cap' => '', 'cap_exclude' => '', 'agent_fallback' => false, 'please_select' => false, 'select2' => false, 'disabled' => false);
    $args = wp_parse_args($args, $defaults);
    /* List all users */
    $all_users = wpas_get_users(array('cap' => $args['cap'], 'cap_exclude' => $args['cap_exclude'], 'exclude' => $args['exclude']));
    /**
     * We use a marker to keep track of when a user was selected.
     * This allows for adding a fallback if nobody was selected.
     * 
     * @var boolean
     */
    $marker = false;
    $options = '';
    /* The ticket is being created, use the current user by default */
    if (!empty($args['selected'])) {
        $user = get_user_by('id', intval($args['selected']));
        if (false !== $user && !is_wp_error($user)) {
            $marker = true;
            $options .= "<option value='{$user->ID}' selected='selected'>{$user->data->display_name}</option>";
        }
    }
    foreach ($all_users as $user) {
        /* This user was already added, skip it */
        if (!empty($args['selected']) && $user->ID === intval($args['selected'])) {
            continue;
        }
        $user_id = $user->ID;
        $user_name = $user->data->display_name;
        $selected_attr = '';
        if (false === $marker) {
            if (false !== $args['selected']) {
                if (!empty($args['selected'])) {
                    if ($args['selected'] === $user_id) {
                        $selected_attr = 'selected="selected"';
                    }
                } else {
                    if (isset($post) && $user_id == $post->post_author) {
                        $selected_attr = 'selected="selected"';
                    }
                }
            }
        }
        /* Set the marker as true to avoid selecting more than one user */
        if (!empty($selected_attr)) {
            $marker = true;
        }
        /* Output the option */
        $options .= "<option value='{$user_id}' {$selected_attr}>{$user_name}</option>";
    }
    /* In case there is no selected user yet we add the post author, or the currently logged user (most likely an admin) */
    if (true === $args['agent_fallback'] && false === $marker) {
        $fallback = $current_user;
        $fb_selected = false === $marker ? 'selected="selected"' : '';
        $options .= "<option value='{$fallback->ID}' {$fb_selected}>{$fallback->data->display_name}</option>";
    }
    $contents = wpas_dropdown(wp_parse_args($args, $defaults), $options);
    return $contents;
}
/**
 * Get AS users using Ajax
 *
 * @since 3.3
 *
 * @param array $args Query parameters
 *
 * @return void
 */
function wpas_get_users_ajax($args = array())
{
    $defaults = array('cap' => 'edit_ticket', 'cap_exclude' => '', 'exclude' => '', 'q' => '');
    if (empty($args)) {
        foreach ($defaults as $key => $value) {
            if (isset($_POST[$key])) {
                $args[$key] = $_POST[$key];
            }
        }
    }
    $args = wp_parse_args($args, $defaults);
    /**
     * @var WPAS_Member_Query $users
     */
    $users = wpas_get_users(array('cap' => array_map('sanitize_text_field', array_filter((array) $args['cap'])), 'cap_exclude' => array_map('sanitize_text_field', array_filter((array) $args['cap_exclude'])), 'exclude' => array_map('intval', array_filter((array) $args['exclude'])), 'search' => array('query' => sanitize_text_field($args['q']), 'fields' => array('user_nicename', 'display_name'), 'relation' => 'OR')));
    $result = array();
    foreach ($users->members as $user) {
        $result[] = array('user_id' => $user->ID, 'user_name' => $user->display_name, 'user_email' => $user->user_email, 'user_avatar' => get_avatar_url($user->ID, array('size' => 32, 'default' => 'mm')));
    }
    echo json_encode($result);
    die;
}
/**
 * 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(array('cap' => 'edit_ticket')));
    $agent = array();
    foreach ($users as $user) {
        $posts_args = array('post_type' => 'ticket', 'post_status' => 'any', 'posts_per_page' => -1, 'no_found_rows' => true, 'cache_results' => false, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'meta_query' => array(array('key' => '_wpas_status', 'value' => 'open', 'type' => 'CHAR', 'compare' => '='), array('key' => '_wpas_assignee', 'value' => $user->ID, 'type' => 'NUMERIC', 'compare' => '=')));
        $open_tickets = new WP_Query($posts_args);
        $count = count($open_tickets->posts);
        // 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);
            }
        }
    }
    return apply_filters('wpas_find_available_agent', $agent['user_id'], $ticket_id);
}
/**
 * Clear the agents metas that can be
 *
 * @since 3.2
 * @return void
 */
function wpas_clear_agents_metas()
{
    $agents = wpas_get_users(array('cap' => 'edit_ticket'));
    foreach ($agents as $user) {
        delete_user_meta($user->ID, 'wpas_open_tickets');
        // Delete the open tickets count
    }
}