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);
 }
 public function _ajax_view_ticket($agents = null)
 {
     global $zendesk_support;
     if (empty($agents)) {
         $agents = Zendesk_Wordpress_Agents::get_instance();
     }
     if (isset($_REQUEST['ticket_id']) && is_numeric($_REQUEST['ticket_id'])) {
         $ticket_id = $_REQUEST['ticket_id'];
         // API requests based on the Zendesk role.
         if ($agents->_is_agent()) {
             $ticket = $zendesk_support->api->get_ticket_info($ticket_id);
         } else {
             $request = $zendesk_support->api->get_request_info($ticket_id);
             $ticket = $request->request;
         }
         // If there was no error fetch further
         if (!is_wp_error($ticket)) {
             // If there's a requester ID let's resolve it
             if (isset($ticket->requester_id)) {
                 $requester = $zendesk_support->api->get_user($ticket->requester_id);
                 if (!is_wp_error($requester)) {
                     $requester = $requester->user->name;
                 } else {
                     $requester = __('Unknown', 'zendesk');
                 }
                 // Otherwise set it to blank, blank fields don't show up.
             } else {
                 $requester = '';
             }
             // Updated field is not viewable by end-users, so if it's
             // not set then set it to blank.
             if (!isset($ticket->updated_at)) {
                 $ticket->updated_at = '';
             }
             // Create the table values, where key is the label and value
             // is the value, doh!
             $table_values = array(__('Subject:', 'zendesk') => htmlspecialchars($ticket->subject), __('Description:', 'zendesk') => nl2br(htmlspecialchars($ticket->description)), __('Ticket Status:', 'zendesk') => '<span class="zendesk-status-' . $ticket->status . '">' . $zendesk_support->_ticket_status($ticket->status) . '</span>', __('Requested by:', 'zendesk') => '<a target="_blank" href="' . Zendesk_Wordpress_Utilities::_user_url($ticket->requester_id) . '">' . $requester . '</a>', __('Created:', 'zendesk') => date(get_option('date_format') . ' \\a\\t ' . get_option('time_format'), strtotime($ticket->created_at)), __('Updated:', 'zendesk') => date(get_option('date_format') . ' \\a\\t ' . get_option('time_format'), strtotime($ticket->updated_at)));
             // Agents only data
             if ($agents->_is_agent()) {
                 // Custom fields
                 $table_custom_fields = array();
                 $ticket_fields = $zendesk_support->api->get_ticket_fields();
                 // Perhaps optimize this a little bit, though this is
                 // the way values come in from Zendesk.
                 if (!is_wp_error($ticket_fields) && !empty($ticket->custom_fields)) {
                     $custom_fields_array = array();
                     // Build an array with custom field values and ID as index
                     foreach ($ticket->custom_fields as $custom_field) {
                         $custom_fields_array[$custom_field->id] = $custom_field->value;
                     }
                     $custom_fields_ids = array_keys($custom_fields_array);
                     foreach ($ticket_fields as $ticket_field) {
                         if (!in_array($ticket_field->id, $custom_fields_ids)) {
                             continue;
                         }
                         // Use numeric index in case there are duplicate field titles
                         $table_custom_fields[$ticket_field->id] = array('title' => $ticket_field->title, 'value' => '');
                         // Use readable value for 'tagger' types
                         if ('tagger' === $ticket_field->type) {
                             foreach ($ticket_field->custom_field_options as $custom_field_option) {
                                 if ($custom_fields_array[$ticket_field->id] === $custom_field_option->value) {
                                     $table_custom_fields[$ticket_field->id]['value'] = $custom_field_option->name;
                                 }
                             }
                         } else {
                             $table_custom_fields[$ticket_field->id]['value'] = $custom_fields_array[$ticket_field->id];
                         }
                     }
                 }
                 $table_actions = array(__('Comments:', 'zendesk') => '<a data-id="' . $ticket->id . '" href="#" class="zendesk-view-comments">' . __('View the comments thread', 'zendesk') . '</a>', __('View:', 'zendesk') => '<a target="_blank" href="' . Zendesk_Wordpress_Utilities::_ticket_url($ticket->id) . '">' . __('View this ticket on Zendesk', 'zendesk') . '</a>');
             }
             // Use these for debug values
             //$table_values['Ticket'] = print_r($ticket, true);
             //$table_values['Fields'] = print_r($ticket_fields, true);
             // Start formatting the general HTML table.
             $html = '<table id="zendesk-ticket-details-table" class="zendesk-ticket-details-table">';
             foreach ($table_values as $label => $value) {
                 if (strlen($value) < 1) {
                     continue;
                 }
                 $html .= '<tr><td class="zendesk-first"><span class="description">' . $label . '</span></td>';
                 $html .= '<td>' . $value . '</td></tr>';
             }
             // Custom Fields Table (agents only)
             if (isset($table_custom_fields) && !empty($table_custom_fields)) {
                 $html .= '<tr><td colspan="2"><p class="zendesk-heading" style="margin-bottom: 0px;">' . __('Custom Fields', 'zendesk') . '</p></td></tr>';
                 foreach ($table_custom_fields as $table_custom_field) {
                     if (strlen($table_custom_field['value']) < 1) {
                         continue;
                     }
                     $html .= '<tr><td class="zendesk-first"><span class="description">' . esc_html($table_custom_field['title']) . '</span></td>';
                     $html .= '<td>' . esc_html($table_custom_field['value']) . '</td></tr>';
                 }
             }
             // Actions Table (agents only)
             if (isset($table_actions) && !empty($table_actions)) {
                 $html .= '<tr><td colspan="2"><p class="zendesk-heading" style="margin-bottom: 0px;">' . __('Actions', 'zendesk') . '</p></td></tr>';
                 foreach ($table_actions as $label => $value) {
                     $html .= '<tr><td class="zendesk-first"><span class="description">' . $label . '</span></td>';
                     $html .= '<td>' . $value . '</td></tr>';
                 }
             }
             $html .= '</table>';
             // Format the response to output.
             $response = array('status' => 200, 'ticket' => $ticket, 'html' => $html);
         } else {
             // Something went wrong
             $response = array('status' => 404, 'data' => $ticket->get_error_message());
         }
     } else {
         // Something went really wrong
         $response = array('status' => 404, 'data' => __('Ticket was not found.', 'zendesk'));
     }
     // Output the response array as a JSON object.
     echo json_encode($response);
     $this->terminate();
 }
 public function _comments_columns_action($column)
 {
     global $comment;
     $agents = Zendesk_Wordpress_Agents::get_instance();
     if ($column == 'zendesk' && $agents->_is_agent()) {
         $ticket_id = get_comment_meta($comment->comment_ID, 'zendesk-ticket', true);
         // Make sure it's valid before printing.
         if ($comment->comment_type != 'pingback' && $ticket_id) {
             echo '<a target="_blank" class="zendesk-comment-ticket-id" href="' . Zendesk_Wordpress_Utilities::_ticket_url($ticket_id) . '">#' . $ticket_id . '</a>';
         }
     }
 }