/** * 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 object $latest 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, $latest = null) { if ('closed' === wpas_get_ticket_status($post_id)) { return false; } /* Prepare the new object */ if (is_null($latest)) { $latest = new WP_Query(array('posts_per_page' => 1, 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'ticket_reply', 'post_parent' => $post_id, 'post_status' => array('unread', 'read'), 'no_found_rows' => true, 'cache_results' => false, 'update_post_term_cache' => false, 'update_post_meta_cache' => false)); } /* No reply yet. */ if (empty($latest->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 = $latest->post_count - 1; /* Check if the last user who replied is an agent. */ if (!user_can($latest->posts[$last]->post_author, 'edit_ticket') && 'unread' === $latest->posts[$last]->post_status) { return true; } } return false; }
/** * Display the post status. * * Gets the ticket status and formats it according to the plugin settings. * * @since 3.0.0 * * @param string $name Field / column name. This parameter is important as it is automatically passed by some * filters * @param integer $post_id ID of the post being processed * * @return string Formatted ticket status */ function wpas_cf_display_status($name, $post_id) { $status = wpas_get_ticket_status($post_id); if ('closed' === $status) { $label = __('Closed', 'awesome-support'); $color = wpas_get_option("color_{$status}", '#dd3333'); $tag = "<span class='wpas-label' style='background-color:{$color};'>{$label}</span>"; } else { $post = get_post($post_id); $post_status = $post->post_status; $custom_status = wpas_get_post_status(); if (!array_key_exists($post_status, $custom_status)) { $label = __('Open', 'awesome-support'); $color = wpas_get_option("color_{$status}", '#169baa'); $tag = "<span class='wpas-label' style='background-color:{$color};'>{$label}</span>"; } else { $defaults = array('queued' => '#1e73be', 'processing' => '#a01497', 'hold' => '#b56629'); $label = $custom_status[$post_status]; $color = wpas_get_option("color_{$post_status}", false); if (false === $color) { if (isset($defaults[$post_status])) { $color = $defaults[$post_status]; } else { $color = '#169baa'; } } $tag = "<span class='wpas-label' style='background-color:{$color};'>{$label}</span>"; } } echo $tag; }
/** * Get tickets lit columns content. * * Based on the columns displayed in the front-end tickets list, * this function will display the column content by using its callback. * The callback can be a "standard" case like the title, or a custom function * as used by the custom fields mostly. * * @since 3.0.0 * @param string $column_id ID of the current column * @param array $column Columns data * @return void */ function wpas_get_tickets_list_column_content($column_id, $column) { $callback = $column['callback']; switch ($callback) { case 'id': echo '#' . get_the_ID(); break; case 'status': echo wpas_get_ticket_status(get_the_ID()); break; case 'title': ?> <a href="<?php echo get_permalink(get_the_ID()); ?> "><?php the_title(); ?> </a><?php break; case 'date': $offset = wpas_get_offset_html5(); ?> <time datetime="<?php echo get_the_date('Y-m-d\\TH:i:s') . $offset; ?> "><?php echo get_the_date(get_option('date_format')) . ' ' . get_the_date(get_option('time_format')); ?> </time><?php break; case 'taxonomy': $terms = get_the_terms(get_the_ID(), $column_id); $list = array(); if (empty($terms)) { continue; } foreach ($terms as $term) { array_push($list, $term->name); } echo implode(', ', $list); break; default: if (function_exists($callback)) { call_user_func($callback, $column_id, get_the_ID()); } break; } }
/** * Get the ticket state slug. * * Gets the ticket status. If the ticket is closed nothing fancy. * If not, we return the ticket state instead of the "Open" status. * * The difference with wpas_get_ticket_status_state() is that only slugs are returned. No translation or capitalized * terms. * * @since 3.3 * * @param integer $post_id Post ID * * @return string Ticket status / state */ function wpas_get_ticket_status_state_slug($post_id) { $status = wpas_get_ticket_status($post_id); if ('closed' === $status) { return $status; } $post = get_post($post_id); $post_status = $post->post_status; $custom_status = wpas_get_post_status(); if (!array_key_exists($post_status, $custom_status)) { return 'open'; } return $post->post_status; }
/** * Get the ticket state. * * Gets the ticket status. If the ticket is closed nothing fancy. * If not, we return the ticket state instead of the "Open" status. * * @since 3.1.5 * * @param integer $post_id Post ID * * @return string Ticket status / state */ function wpas_get_ticket_status_state($post_id) { $status = wpas_get_ticket_status($post_id); if ('closed' === $status) { $output = __('Closed', 'wpas'); } else { $post = get_post($post_id); $post_status = $post->post_status; $custom_status = wpas_get_post_status(); if (!array_key_exists($post_status, $custom_status)) { $output = __('Open', 'wpas'); } else { $output = $custom_status[$post_status]; } } return $output; }
/** * Add items in action row. * * Add a quick option to open or close a ticket * directly from the tickets list. * * @since 3.0.0 * @param array $actions List of existing options * @param object $post Current post object * @return array List of options with ours added */ public function ticket_action_row($actions, $post) { if ('ticket' === $post->post_type) { $status = wpas_get_ticket_status($post->ID); if ('open' === $status) { $actions['close'] = '<a href="' . wpas_get_close_ticket_url($post->ID) . '">' . __('Close', 'awesome-support') . '</a>'; } elseif ('closed' === $status) { $actions['open'] = '<a href="' . wpas_get_open_ticket_url($post->ID) . '">' . __('Open', 'awesome-support') . '</a>'; } } return $actions; }
foreach ($columns as $column_id => $column) { $data_attributes = ''; // Add the data attributes if any if (isset($column['column_attributes']['head']) && is_array($column['column_attributes']['head'])) { $data_attributes = wpas_array_to_data_attributes($column['column_attributes']['head']); } printf('<th id="wpas-ticket-%1$s" %3$s>%2$s</th>', $column_id, $column['title'], $data_attributes); } ?> </tr> </thead> <tbody> <?php while ($wpas_tickets->have_posts()) { $wpas_tickets->the_post(); echo '<tr class="wpas-status-' . wpas_get_ticket_status($wpas_tickets->post->ID) . '" id="wpas_ticket_' . $wpas_tickets->post->ID . '">'; foreach ($columns as $column_id => $column) { $data_attributes = ''; // Add the data attributes if any if (isset($column['column_attributes']['body']) && is_array($column['column_attributes']['body'])) { $data_attributes = wpas_array_to_data_attributes($column['column_attributes']['body'], true); } printf('<td %s>', $data_attributes); /* Display the content for this column */ wpas_get_tickets_list_column_content($column_id, $column); echo '</td>'; } echo '</tr>'; } wp_reset_query(); ?>
/** * 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; }
/** * 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 the ticket must have been transferred and we need to to stand out if (user_can($replies->posts[$last]->post_author, 'edit_ticket') && (int) $replies->posts[$last]->post_author !== get_current_user_id()) { 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; }