/**
 * 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);
}
/**
 * Change a ticket status to closed.
 *
 * @since  3.0.2
 *
 * @param  integer $ticket_id ID of the ticket to close
 * @param int      $user_id   ID of the user who closed the ticket
 *
 * @return integer|boolean            ID of the post meta if exists, true on success or false on failure
 */
function wpas_close_ticket($ticket_id, $user_id = 0)
{
    global $current_user;
    // Set the user who closed the ticket to the current user if nothing is specified
    if (0 === $user_id) {
        $user_id = $current_user->ID;
    }
    if (!current_user_can('close_ticket')) {
        wp_die(__('You do not have the capacity to close this ticket', 'awesome-support'), __('Can’t close ticket', 'awesome-support'), array('back_link' => true));
    }
    $ticket_id = intval($ticket_id);
    if ('ticket' == get_post_type($ticket_id)) {
        $update = update_post_meta(intval($ticket_id), '_wpas_status', 'closed');
        /* Decrement the number of tickets open for this agent */
        $agent_id = get_post_meta($ticket_id, '_wpas_assignee', true);
        $agent = new WPAS_Member_Agent($agent_id);
        $agent->ticket_minus();
        /* Log the action */
        wpas_log($ticket_id, __('The ticket was closed.', 'awesome-support'));
        /**
         * wpas_after_close_ticket hook
         *
         * @since  3.0.0
         */
        do_action('wpas_after_close_ticket', $ticket_id, $update, $user_id);
        if (is_admin()) {
            /**
             * Fires after the ticket was closed in the admin only.
             *
             * @since  3.1.2
             *
             * @param integer $ticket_id ID of the ticket we just closed
             * @param integer $user_id   ID of the user who did the action
             * @param boolean $update    True on success, false on fialure
             */
            do_action('wpas_after_close_ticket_admin', $ticket_id, $user_id, $update);
        } else {
            /**
             * Fires after the ticket was closed in the front-end only.
             *
             * @since  3.1.2
             *
             * @param integer $ticket_id ID of the ticket we just closed
             * @param integer $user_id   ID of the user who did the action
             * @param boolean $update    True on success, false on failure
             */
            do_action('wpas_after_close_ticket_public', $ticket_id, $user_id, $update);
        }
        return $update;
    } else {
        return false;
    }
}
/**
 * Update the open agent tickets count when a ticket is transferred from one agent to another
 *
 * We do not need to add a new ticket to the new agent because it is automatically done in wpas_assign_ticket()
 *
 * @since 3.2.8
 *
 * @param int $agent_id          ID of the current ticket assignee
 * @param int $previous_agent_id ID of the previous assignee
 *
 * @return void
 */
function wpas_update_ticket_count_on_transfer($agent_id, $previous_agent_id)
{
    $agent_prev = new WPAS_Member_Agent($previous_agent_id);
    $agent_prev->ticket_minus();
}
/**
 * Delete ticket dependencies.
 *
 * Delete all ticket dependencies when a ticket is deleted. This includes
 * ticket replies and ticket history. Ticket attachments are deleted by
 * WPAS_File_Upload::delete_attachments()
 *
 * @param  integer $post_id ID of the post to be deleted
 *
 * @return void
 */
function wpas_delete_ticket_dependencies($post_id)
{
    global $post_type;
    if ('ticket' !== $post_type) {
        return;
    }
    /* First of all we remove this action to avoid creating a loop */
    remove_action('before_delete_post', 'wpas_delete_ticket_dependencies', 10);
    $args = array('post_parent' => $post_id, 'post_type' => apply_filters('wpas_replies_post_type', array('ticket_history', 'ticket_reply')), 'post_status' => 'any', 'posts_per_page' => -1, 'no_found_rows' => true, 'cache_results' => false, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
    $posts = new WP_Query($args);
    foreach ($posts->posts as $id => $post) {
        do_action('wpas_before_delete_dependency', $post->ID, $post);
        wp_delete_post($post->ID, true);
        do_action('wpas_after_delete_dependency', $post->ID, $post);
    }
    /* Decrement the number of tickets open for this agent */
    $agent_id = get_post_meta($post_id, '_wpas_assignee', true);
    $agent = new WPAS_Member_Agent($agent_id);
    $agent->ticket_minus();
}
 /**
  * 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);
 }
 /**
  * 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;';
 }