public static function _get_tickets_widget_html($tickets)
 {
     global $zendesk_support;
     $agents = Zendesk_Wordpress_Agents::get_instance();
     $html = array();
     // Heading
     $html[] = '<p class="zendesk-heading">' . $zendesk_support->zendesk_user['default_view']['title'];
     if ($agents->_is_agent()) {
         $html[] = '<span class="zendesk-heading-link">(<a class="zendesk-change-view" href="#">' . __('change view', 'zendesk') . '</a>)</span>';
     }
     $html[] = '</p>';
     $html[] = '<table class="zendesk-tickets-table">';
     if (count($tickets) > 0 && is_array($tickets)) {
         foreach ($tickets as $ticket) {
             if (!strlen($ticket->subject)) {
                 $ticket->subject = Zendesk_Wordpress_Utilities::_excerpt($ticket->description, 15);
             }
             $html[] = '<tr>';
             $html[] = '<td class="zendesk-ticket-id"><div class="zendesk-loader" style="display: none"></div><a class="zendesk-ticket-id-text zendesk-ticket-view" data-id="' . $ticket->id . '" href="' . Zendesk_Wordpress_Utilities::_ticket_url($ticket->id) . '">#' . $ticket->id . '</a></td>';
             $html[] = '<td><a class="zendesk-ticket-view zendesk-ticket-subject" data-id="' . $ticket->id . '" href="' . Zendesk_Wordpress_Utilities::_ticket_url($ticket->id) . '">' . $ticket->subject . '</a></td>';
             $html[] = '<td class="zendesk-ticket-status"><a href="' . Zendesk_Wordpress_Utilities::_ticket_url($ticket->id) . '" target="_blank" class="zendesk-status-' . $ticket->status . '">' . $zendesk_support->_ticket_status($ticket->status) . '</a></td>';
             $html[] = '</tr>';
         }
     } else {
         $html[] = '<tr><td><span class="description">' . __('There are no tickets in this view.', 'zendesk') . '</span></td></tr>';
     }
     $html[] = '</table>';
     // Glue the HTML pieces and delimit with a line break
     return implode("\n", $html);
 }
 /**
  * AJAX Response: Convert to Ticket POST
  *
  * This requests responds upon the actual posting of the comments
  * to tickets integration, i.e. when the agent has typed a response
  * message and clicked the Create ticket button. The whole logics of
  * creating the ticket, attaching a comment to the ticket (private,
  * or public), associating a WordPress comment with the ticket and
  * posting back a WordPress comment as a reply happens here.
  *
  * @param Zendesk_Wordpress_Agents|null $agents
  */
 public function _ajax_convert_to_ticket_post($agents = null)
 {
     global $zendesk_support;
     if (empty($agents)) {
         $agents = Zendesk_Wordpress_Agents::get_instance();
     }
     // If a different response is not set use this one.
     $response = array('status' => 500, 'error' => __('Whoopsie! Problem communicating with Zendesk. Try that again.', 'zendesk'));
     // Some validation
     if (isset($_REQUEST['comment_id']) && is_numeric($_REQUEST['comment_id']) && $agents->_is_agent()) {
         $comment_id = $_REQUEST['comment_id'];
         $comment = get_comment($comment_id);
         // Make sure it's a valid comment
         if ($comment && $comment->comment_type != 'pingback') {
             // Fetch the associated post
             $post = get_post($comment->comment_post_ID);
             // Fetch the incoming data
             $message = trim(stripslashes($_REQUEST['message']));
             $comment_public = isset($_REQUEST['comment_public']) ? true : false;
             $post_reply = isset($_REQUEST['post_reply']) ? true : false;
             // Let's format the new ticket
             $subject = $post->post_title . ': ' . Zendesk_Wordpress_Utilities::_excerpt(strip_tags($comment->comment_content), 5);
             $description = strip_tags($comment->comment_content);
             $requester_name = $comment->comment_author;
             $requester_email = $comment->comment_author_email;
             // Create the ticket
             $ticket_id = $zendesk_support->api->create_ticket($subject, $description, $requester_name, $requester_email);
             if (!is_wp_error($ticket_id)) {
                 // Ticket went okay so update the comment meta to associated it.
                 update_comment_meta($comment->comment_ID, 'zendesk-ticket', $ticket_id);
                 // If we have a message set
                 if (strlen($message)) {
                     // Post a comment to the ticket
                     $ticket_comment = $zendesk_support->api->create_comment($ticket_id, $message, $comment_public);
                     if (!is_wp_error($ticket_comment)) {
                         // Let's see if we need to post a comment back to WordPress.
                         if ($post_reply) {
                             $wp_comment = array('comment_post_ID' => $post->ID, 'comment_author' => $zendesk_support->user->display_name, 'comment_author_email' => $zendesk_support->user->user_email, 'comment_content' => $message, 'comment_parent' => $comment_id, 'user_id' => $zendesk_support->user->ID, 'comment_date' => current_time('mysql'), 'comment_approved' => 1);
                             wp_insert_comment($wp_comment);
                         }
                         $response = array('status' => 200, 'ticket_id' => $ticket_id, 'ticket_url' => Zendesk_Wordpress_Utilities::_ticket_url($ticket_id));
                     } else {
                         // The ticket was created but the comment didn't get through.
                         $response = array('status' => 500, 'error' => __('A ticket has been created, but failed to post a comment to it.', 'zendesk'));
                     }
                 } else {
                     // A message is not set but the ticket was created.
                     $response = array('status' => 200, 'ticket_id' => $ticket_id, 'ticket_url' => Zendesk_Wordpress_Utilities::_ticket_url($ticket_id));
                 }
             } else {
                 // Failed to create the ticket.
                 $response = array('status' => 500, 'error' => $ticket_id->get_error_message());
             }
         }
     }
     // Return the response JSON
     echo json_encode($response);
     $this->terminate();
 }