/**
* Check if a reply is needed.
*
* Takes a ticket ID and checks if a reply is needed. The check is based
* on who replied last. If a client was the last to reply, or if the ticket
* was just transferred from one agent to another, then it is considered
* as "awaiting reply".
*
* @since 3.0.0
*
* @param integer $post_id The ID of the ticket to check
* @param WP_Query|null $replies The object containing the ticket replies. If the object was previously generated we
* pass it directly in order to avoid re-querying
*
* @return boolean True if a reply is needed, false otherwise
*/
function wpas_is_reply_needed($post_id, $replies = null)
{
if ('closed' === wpas_get_ticket_status($post_id)) {
return false;
}
/* Prepare the new object */
if (is_null($replies) || is_object($replies) && !is_a($replies, 'WP_Query')) {
$replies = WPAS_Tickets_List::get_instance()->get_replies_query($post_id);
}
/* No reply yet. */
if (empty($replies->posts)) {
$post = get_post($post_id);
/* Make sure the ticket wan not created by an agent on behalf of the client. */
if (!user_can($post->post_author, 'edit_ticket')) {
return true;
}
} else {
$last = $replies->post_count - 1;
// If the last agent reply was not from the currently logged-in agent then there are two possible scenarios
if (user_can($replies->posts[$last]->post_author, 'edit_ticket') && (int) $replies->posts[$last]->post_author !== get_current_user_id()) {
// First, the plugin is set to show all tickets to every agent. In this case, we don't want all agents to see the awaiting reply tag
if (true === (bool) wpas_get_option('agent_see_all')) {
return false;
} else {
return true;
}
}
// If the last reply is not from an agent and the reply is still unread we need the ticket to stand out
if (!user_can($replies->posts[$last]->post_author, 'edit_ticket') && 'unread' === $replies->posts[$last]->post_status) {
return true;
}
}
return false;
}