function test_wpas_get_replies() { $ticket_id = wpas_insert_ticket($this->ticket_data, false); $reply_id = wpas_insert_reply($this->reply_data, $ticket_id); $reply_id_2 = wpas_insert_reply($this->reply_data, $ticket_id); $replies = wpas_get_replies($ticket_id); $this->assertNotEmpty($replies); $this->assertCount(2, $replies); }
/** * 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': // If the replies are displayed from the oldest to the newest we want to link directly to the latest reply in case there are multiple reply pages if ('ASC' === wpas_get_option('replies_order', 'ASC')) { $last_reply = wpas_get_replies(get_the_ID(), array('read', 'unread'), array('posts_per_page' => 1, 'order' => 'DESC')); $link = !empty($last_reply) ? wpas_get_reply_link($last_reply[0]->ID) : get_permalink(get_the_ID()); } else { $link = get_permalink(get_the_ID()); } ?> <a href="<?php echo $link; ?> "><?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; } }
</td> </tr> <?php // Set the number of replies $replies_per_page = wpas_get_option('replies_per_page', 10); $force_all_replies = WPAS()->session->get('force_all_replies'); // Check if we need to force displaying all the replies (direct link to a specific reply for instance) if (true === $force_all_replies) { $replies_per_page = -1; WPAS()->session->clean('force_all_replies'); // Clean the session } $args = array('posts_per_page' => $replies_per_page, 'no_found_rows' => false); $replies = wpas_get_replies($post->ID, array('read', 'unread'), $args, 'wp_query'); if ($replies->have_posts()) { while ($replies->have_posts()) { $replies->the_post(); $user = get_userdata($post->post_author); $user_role = get_the_author_meta('roles'); $user_role = $user_role[0]; $time_ago = human_time_diff(get_the_time('U', $post->ID), current_time('timestamp')); wpas_get_template('partials/ticket-reply', array('time_ago' => $time_ago, 'user' => $user, 'post' => $post)); } } wp_reset_query(); ?> </tbody> </table>
/** * Get the link to a ticket reply * * @since 3.2 * * @param int $reply_id ID of the reply to get the link to * * @return string|bool Reply link or false if the reply doesn't exist */ function wpas_get_reply_link($reply_id) { $reply = get_post($reply_id); if (empty($reply)) { return false; } if ('ticket_reply' !== $reply->post_type || 0 === (int) $reply->post_parent) { return false; } $replies = wpas_get_replies($reply->post_parent, array('read', 'unread')); if (empty($replies)) { return false; } $position = 0; foreach ($replies as $key => $post) { if ($reply_id === $post->ID) { $position = $key + 1; } } // We have more replies that what's displayed on one page, so let's set a session var to force displaying all replies if ($position > wpas_get_option('replies_per_page', 10)) { WPAS()->session->add('force_all_replies', true); } $link = get_permalink($reply->post_parent) . "#reply-{$reply_id}"; return esc_url($link); }
/** * Ajax function that returns a number of ticket replies * * @since 3.3 * @return void */ function wpas_get_ticket_replies_ajax() { // Make sure we have a ticket ID to work with if (!isset($_POST['ticket_id'])) { echo json_encode(array('error' => esc_html__('No ticket ID given', 'awesome-support'))); die; } $ticket_id = (int) $_POST['ticket_id']; $offset = isset($_POST['ticket_replies_total']) ? (int) $_POST['ticket_replies_total'] : 0; $ticket = get_post($ticket_id); // Make sure the ID exists if (!is_object($ticket) || !is_a($ticket, 'WP_Post')) { echo json_encode(array('error' => esc_html__('Invalid ticket ID', 'awesome-support'))); die; } // Make sure the post is actually a ticket if ('ticket' !== $ticket->post_type) { echo json_encode(array('error' => esc_html__('Given ID is not a ticket', 'awesome-support'))); die; } $number_replies = apply_filters('wpas_get_ticket_replies_ajax_replies', wpas_get_option('replies_per_page', 10)); $replies = wpas_get_replies($ticket_id, 'any', array('posts_per_page' => $number_replies, 'no_found_rows' => false, 'offset' => $offset), 'wp_query'); if (empty($replies->posts)) { echo json_encode(array()); die; } $output = array('total' => (int) $replies->found_posts, 'current' => $offset + (int) $replies->post_count, 'html' => ''); $html = array(); while ($replies->have_posts()) { $replies->the_post(); $user = get_userdata($replies->post->post_author); $time_ago = human_time_diff(get_the_time('U', $replies->post->ID), current_time('timestamp')); ob_start(); wpas_get_template('partials/ticket-reply', array('time_ago' => $time_ago, 'user' => $user, 'post' => $replies->post)); $reply = ob_get_contents(); ob_end_clean(); $html[] = $reply; } $output['html'] = implode('', $html); echo json_encode($output); die; }
/** * Mark replies as read. * * When an agent replies to a ticket, we mark all previous replies * as read. We suppose it's all been read when the agent replies. * This allows for keeping replies unread until an agent replies * or manually marks the last reply as read. * * @since 3.0.0 * @return void */ public function mark_replies_read($reply_id, $data) { $replies = wpas_get_replies(intval($data['post_parent']), 'unread'); foreach ($replies as $reply) { wpas_mark_reply_read($reply->ID); } }
/** * Get the link to a ticket reply * * @since 3.2 * * @param int $reply_id ID of the reply to get the link to * * @return string|bool Reply link or false if the reply doesn't exist */ function wpas_get_reply_link($reply_id) { $reply = get_post($reply_id); if (empty($reply)) { return false; } if ('ticket_reply' !== $reply->post_type || 0 === (int) $reply->post_parent) { return false; } $replies = wpas_get_replies($reply->post_parent, array('read', 'unread')); if (empty($replies)) { return false; } $position = 0; foreach ($replies as $key => $post) { if ($reply_id === $post->ID) { $position = $key + 1; } } if (0 === $position) { return false; } $page = ceil($position / 10); $base = 1 !== (int) $page ? add_query_arg('as-page', $page, get_permalink($reply->post_parent)) : get_permalink($reply->post_parent); $link = $base . "#reply-{$reply_id}"; return esc_url($link); }