Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active July 28, 2017 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JarrydLong/28c5eb4e581559855e1290e70a2d174c to your computer and use it in GitHub Desktop.
Save JarrydLong/28c5eb4e581559855e1290e70a2d174c to your computer and use it in GitHub Desktop.
Creates the endpoint /get_tickets to return tickets from a specific number of days ago, or between two dates. Pagination is supported.
/**
* Creates the endpoint /get_tickets to return tickets from a specific number of days ago, or between two dates.
* Pagination is supported.
* Dates are required to be in a formatted string
*/
add_action( 'nifty_desk_api_route_hook', 'nifty_desk_api_route_get_all_tickets' );
function nifty_desk_api_route_get_all_tickets(){
register_rest_route('nifty_desk/v1','/get_tickets', array(
'methods' => 'GET, POST',
'callback' => 'nifty_desk_api_get_all_tickets'
));
}
function nifty_desk_api_get_all_tickets(WP_REST_Request $request){
$return_array = array();
if(isset($request)){
if(isset($request['token'])){
$check_token = get_option('nifty_desk_api_secret_token');
if($check_token !== false && $request['token'] === $check_token){
$page = isset( $request['page'] ) ? intval( $request['page'] ) : 1;
if( isset( $request['date_after'] ) && isset( $request['date_before'] ) ){
/**
* date_start
* date_end
*/
$args = array(
'posts_per_page' => '20',
'paged' => $page,
'post_type' => 'nifty_desk_tickets',
'date_query' => array(
'after' => $request['date_after'],
'before' => $request['date_before']
)
);
} else {
/**
* days_ago
*/
$days_ago = isset( $request['days_ago'] ) ? intval( $request['days_ago'] ) : 7;
$current_day = current_time( 'mysql' );
$start_date = date( 'Y-m-d H:i:s', strtotime( $current_day . ' -'.$days_ago.' DAY' ) );
$args = array(
'posts_per_page' => '20',
'paged' => $page,
'post_type' => 'nifty_desk_tickets',
'date_query' => array(
'after' => $start_date,
)
);
}
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$ticket_id = get_the_ID();
$author_id = get_the_author_meta('ID');
$nd_tags = nifty_desk_get_allowed_tags();
$ticket_subject = get_the_title();
$ticket_content = wp_kses(utf8_decode(get_the_content()),$nd_tags);
$ticket_request_date = nifty_desk_parse_date(strtotime(get_the_date()));
$ticket_requester_id = get_the_author();
$ticket_meta_data = get_post_meta($ticket_id);
$ticket_status = $ticket_meta_data['ticket_status'][0];
if (isset($ticket_meta_data['ticket_priority'][0])) { $ticket_priority = $ticket_meta_data['ticket_priority'][0]; } else { $ticket_priority = NULL; }
//Author data
$ticket_author_data = get_userdata( $author_id );
$ticket_author_name = $ticket_author_data->display_name;
$ticket_author_email = $ticket_author_data->user_email;
$ticket_author_image = get_avatar( $author_id, '40' );
//Additional Data - TO be processed
$meta_data = nifty_desk_get_post_meta_all( $ticket_id );
$note_data = nifty_desk_get_note_meta_all( $ticket_id );
//Work our way through notes
$note_array = array();
if(isset($note_data) && is_array($note_data)){
krsort($note_data);
foreach ($note_data as $key => $note) {
//Create a new array for output
$note_array[$key] = array();
//Get this notes data
$note_data = nifty_desk_get_response_data($note->post_id);
$author_data = get_userdata($note_data->post_author);
//Check user role
if (isset($author_data->roles[0])) {
$role = $author_data->roles[0];
} else {
if (isset($author_data->roles[1])) {
$role = $author_data->roles[1];
} else {
$role = "";
}
}
$note_array[$key]["user_name"] = $author_data->display_name;
$note_array[$key]["message"] = wp_kses(utf8_decode($note_data->post_content), $nd_tags);
$note_array[$key]["post_date"] = $note_data->post_date;
$note_array[$key]["post_time"] = nifty_desk_time_elapsed_string(strtotime($note_data->post_date));
$note_array[$key]["user_avatar"] = get_avatar($author_data->user_email, '40');
$note_array[$key]["user_role"] = $role;
}
}
//Work through the meta?
$responses_array = array();
if(isset($meta_data) && is_array($meta_data)){
foreach ($meta_data as $key => $response) {
//Get base data
$response_data = nifty_desk_get_response_data($response->post_id);
if($response_data !== false){
//Creat a new array for output
$responses_array[$key] = array();
$author_data = get_userdata($response_data->post_author);
//Check user role
if (isset($author_data->roles[0])) {
$role = $author_data->roles[0];
} else {
if (isset($author_data->roles[1])) {
$role = $author_data->roles[1];
} else {
$role = "";
}
}
$responses_array[$key]["user_name"] = $author_data->display_name;
$responses_array[$key]["message"] = wp_kses(utf8_decode($response_data->post_content),$nd_tags);
$responses_array[$key]["post_date"] = $response_data->post_date;
$responses_array[$key]["post_time"] = nifty_desk_time_elapsed_string(strtotime($response_data->post_date));
$responses_array[$key]["user_avatar"] = get_avatar($author_data->user_email, '40');
$responses_array[$key]["user_role"] = $role;
$responses_array[$key]["is_author"] = $ticket_requester_id == $author_data->ID ? true : false;
}
}
}
//Now on to attachements
$attachement_array = array();
if( isset( $ticket_meta_data['ticket_attachments'] ) ){
$ticket_attachments = maybe_unserialize($ticket_meta_data['ticket_attachments'][0]);
if(isset($ticket_attachments) && is_array($ticket_attachments)){
$upload_dir = wp_upload_dir();
$udir = $upload_dir['baseurl'].'/nifty-desk-uploads/'.$ticket_id."/";
foreach ($ticket_attachments as $key => $att) {
$att = maybe_unserialize($att);
if (is_array($att)) {
foreach ($att as $att_for_realz) {
$attachement_array[$key] = array();
$attachement_array[$key]['filename'] = $udir.$att_for_realz;
$attachement_array[$key]['url'] = $att_for_realz;
}
}
}
}
}
//Now let's prep our output array
$return_array['response'] = "Ticket data retrieved";
$return_array['code'] = "200";
$return_array['code_def'] = "Success";
//Create data array
$return_array['data'] = array();
$return_array['data']['ticket_id'] = $ticket_id;
$return_array['data']['subject'] = $ticket_subject;
$return_array['data']['message'] = $ticket_content;
$return_array['data']['status'] = $ticket_status;
//Create author array
$return_array['data']['author'] = array();
$return_array['data']['author']['user_name'] = $ticket_author_name;
$return_array['data']['author']['email'] = $ticket_author_email;
$return_array['data']['author']['user_avatar'] = $ticket_author_image;
//Now do notes, responses, and attachements
$return_array['data']['notes'] = $note_array;
$return_array['data']['responses'] = $responses_array;
$return_array['data']['attachments'] = $attachement_array;
$return_array['data']['query_data'] = array(
'current_page' => $page,
'maximum_pages' => $the_query->max_num_pages,
'tickets_per_page' => 20
);
$return_array = apply_filters("nifty_desk_api_view_ticket_hook", $return_array, $request);
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
$return_array['response'] = "No tickets found";
$return_array['code'] = "404";
$return_array['code_def'] = "Content Not Found";
}
} else {
$return_array['response'] = "Secret token is invalid";
$return_array['code'] = "401";
$return_array['code_def'] = "Unauthorized";
}
}else{
$return_array['response'] = "No secret 'token' found";
$return_array['code'] = "401";
$return_array['code_def'] = "Unauthorized";
$return_array['requirements'] = array("token" => "YOUR_SECRET_TOKEN",
"ticket_id" => "Ticket ID");
}
}else{
$return_array['response'] = "No request data found";
$return_array['code'] = "400";
$return_array['code_def'] = "Bad Request";
$return_array['requirements'] = array("token" => "YOUR_SECRET_TOKEN",
"ticket_id" => "Ticket ID");
}
return $return_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment