/**
 * Add ticket count in admin menu item.
 *
 * @return boolean True if the ticket count was added, false otherwise
 * @since  1.0.0
 */
function wpas_tickets_count()
{
    if (false === (bool) wpas_get_option('show_count')) {
        return false;
    }
    global $menu, $current_user;
    if (current_user_can('administrator') && false === boolval(wpas_get_option('admin_see_all')) || !current_user_can('administrator') && current_user_can('edit_ticket') && false === boolval(wpas_get_option('agent_see_all'))) {
        $agent = new WPAS_Member_Agent($current_user->ID);
        $count = $agent->open_tickets();
    } else {
        $count = count(wpas_get_tickets('open'));
    }
    if (0 === $count) {
        return false;
    }
    foreach ($menu as $key => $value) {
        if ($menu[$key][2] == 'edit.php?post_type=ticket') {
            $menu[$key][0] .= ' <span class="awaiting-mod count-' . $count . '"><span class="pending-count">' . $count . '</span></span>';
        }
    }
    return true;
}
/**
 * Add link to agent's tickets.
 *
 * @since  3.0.0
 *
 * @param  object $wp_admin_bar The WordPress toolbar object
 *
 * @return void
 */
function wpas_toolbar_tickets_link($wp_admin_bar)
{
    if (!current_user_can('edit_ticket')) {
        return;
    }
    $hide = (bool) wpas_get_option('hide_closed');
    $agent_see_all = (bool) wpas_get_option('agent_see_all');
    $admin_see_all = (bool) wpas_get_option('admin_see_all');
    $args = array('post_type' => 'ticket');
    // In case the current user can only see his own tickets
    if (current_user_can('administrator') && false === $admin_see_all || !current_user_can('administrator') && false === $agent_see_all) {
        global $current_user;
        $agent = new WPAS_Member_Agent($current_user->ID);
        $tickets_count = $agent->open_tickets();
    } else {
        $tickets_count = count(wpas_get_tickets('open', $args));
    }
    if (true === $hide) {
        $args['wpas_status'] = 'open';
    }
    $node = array('id' => 'wpas_tickets', 'parent' => null, 'group' => null, 'title' => '<span class="ab-icon"></span> ' . $tickets_count, 'href' => add_query_arg($args, admin_url('edit.php')), 'meta' => array('target' => '_self', 'title' => esc_html__('Open tickets assigned to you', 'awesome-support'), 'class' => 'wpas-my-tickets'));
    $wp_admin_bar->add_node($node);
}
/**
 * Get a dropdown of the tickets.
 *
 * @since  3.1.3
 * @param  array  $args   Dropdown arguments
 * @param  string $status Specific ticket status to look for
 * @return void
 */
function wpas_tickets_dropdown($args = array(), $status = '')
{
    $defaults = array('name' => 'wpas_tickets', 'id' => '', 'class' => '', 'exclude' => array(), 'selected' => '', 'select2' => true, 'please_select' => false);
    /* List all tickets */
    $tickets = wpas_get_tickets($status);
    $options = '';
    foreach ($tickets as $ticket) {
        $options .= "<option value='{$ticket->ID}'>{$ticket->post_title}</option>";
    }
    echo wpas_dropdown(wp_parse_args($args, $defaults), $options);
}
/**
 * Get the tickets count by ticket status
 *
 * @since 3.2
 *
 * @param string $state
 * @param string $status
 *
 * @return int Tickets count
 */
function wpas_get_ticket_count_by_status($state = '', $status = 'open')
{
    $args = array();
    $post_status = wpas_get_post_status();
    // Make the state an array
    if (!is_array($state)) {
        $state = array_filter((array) $state);
    }
    // Sanitize the status
    if (!in_array($status, array('open', 'closed', 'any'))) {
        $status = 'open';
    }
    // Restrict tickets to the specified status
    if (!empty($state)) {
        // Force open status if a state is defined. Who cares about counting closed "In Progress" tickets.
        $status = 'open';
        // Make sure the requested ticket state is declared
        foreach ($state as $key => $s) {
            if (!array_key_exists($s, $post_status)) {
                unset($state[$key]);
            }
        }
        $args['post_status'] = $state;
    }
    // Maybe restrict the count to the current user only
    if (current_user_can('administrator') && false === (bool) wpas_get_option('admin_see_all') || !current_user_can('administrator') && current_user_can('edit_ticket') && false === (bool) wpas_get_option('agent_see_all')) {
        global $current_user;
        $args['meta_query'][] = array('key' => '_wpas_assignee', 'value' => $current_user->ID, 'compare' => '=');
    }
    return count(wpas_get_tickets($status, $args));
}
 * User Profile.
 *
 * This metabox is used to display the user profile. It gives quick access to basic information about the client.
 *
 * @since 3.3
 */
// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}
global $post;
// Get the user object
$user = get_userdata($post->post_author);
// Get tickets
$open = wpas_get_tickets('open', array('posts_per_page' => apply_filters('wpas_user_profile_tickets_open_limit', 10), 'author' => $post->post_author));
$closed = wpas_get_tickets('closed', array('posts_per_page' => apply_filters('wpas_user_profile_tickets_closed_limit', 5), 'author' => $post->post_author));
// Sort open tickets
$by_status = array();
$all_status = wpas_get_post_status();
foreach ($open as $t) {
    if (!is_a($t, 'WP_Post')) {
        continue;
    }
    if (!array_key_exists($t->post_status, $all_status)) {
        continue;
    }
    if (!array_key_exists($t->post_status, $by_status)) {
        $by_status[$t->post_status] = array();
    }
    $by_status[$t->post_status][] = $t;
}
/**
 * Wrapper function to easily get a user tickets
 *
 * This function is a wrapper for wpas_get_user_tickets() with the user ID preset
 *
 * @since 3.2.2
 *
 * @param int    $user_id
 * @param string $ticket_status
 * @param string $post_status
 *
 * @return array
 */
function wpas_get_user_tickets($user_id = 0, $ticket_status = 'open', $post_status = 'any')
{
    if (0 === $user_id) {
        $user_id = get_current_user_id();
    }
    $args = array('author' => $user_id);
    $tickets = wpas_get_tickets($ticket_status, $args, $post_status);
    return $tickets;
}
 /**
  * Get all open tickets assigned to the agent
  *
  * @since 3.2
  * @return array
  */
 public function get_open_tickets()
 {
     $args = array();
     $args['meta_query'][] = array('key' => '_wpas_assignee', 'value' => $this->user_id, 'compare' => '=', 'type' => 'NUMERIC');
     $open_tickets = wpas_get_tickets('open', $args);
     return $open_tickets;
 }
 /**
  * Add ticket count in admin menu item.
  *
  * @return boolean True if the ticket count was added, false otherwise
  * @since  1.0.0
  */
 public function tickets_count()
 {
     if (false === (bool) wpas_get_option('show_count')) {
         return false;
     }
     global $menu, $current_user;
     $args = array();
     if (current_user_can('administrator') && false === boolval(wpas_get_option('admin_see_all')) || !current_user_can('administrator') && current_user_can('edit_ticket') && false === boolval(wpas_get_option('agent_see_all'))) {
         $args['meta_query'][] = array('key' => '_wpas_assignee', 'value' => $current_user->ID, 'compare' => '=');
     }
     $count = count(wpas_get_tickets('open', $args));
     if (0 === $count) {
         return false;
     }
     foreach ($menu as $key => $value) {
         if ($menu[$key][2] == 'edit.php?post_type=ticket') {
             $menu[$key][0] .= ' <span class="awaiting-mod count-' . $count . '"><span class="pending-count">' . $count . '</span></span>';
         }
     }
     return true;
 }