| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $wpdb, $current_user, $wpscfunction; |
| 7 |
|
| 8 |
$params = $request->get_params(); |
| 9 |
$current_page = isset($params['current_page']) ? intval($params['current_page']) : 0; |
| 10 |
$no_of_threads = isset($params['no_of_threads']) ? intval($params['no_of_threads']) : 20; |
| 11 |
$order = isset($params['order']) ? sanitize_text_field($params['order']) : 'DESC'; |
| 12 |
$ticket_id = isset($params['id']) ? intval($params['id']) : 0; |
| 13 |
|
| 14 |
$args = array( |
| 15 |
'post_type' => 'wpsc_ticket_thread', |
| 16 |
'post_status' => 'publish', |
| 17 |
'orderby' => 'ID', |
| 18 |
'order' => $order, |
| 19 |
'posts_per_page' => $no_of_threads, |
| 20 |
'paged' => $current_page, |
| 21 |
); |
| 22 |
|
| 23 |
$meta_query = array( |
| 24 |
'relation' => 'AND', |
| 25 |
array( |
| 26 |
'key' => 'ticket_id', |
| 27 |
'value' => $ticket_id, |
| 28 |
'compare' => '=', |
| 29 |
) |
| 30 |
); |
| 31 |
|
| 32 |
if ( $current_user->has_cap('wpsc_agent') ) { |
| 33 |
|
| 34 |
$meta_query[] = array( |
| 35 |
'key' => 'thread_type', |
| 36 |
'value' => array('report','reply','note','log'), |
| 37 |
'compare' => 'IN' |
| 38 |
); |
| 39 |
|
| 40 |
} else { |
| 41 |
|
| 42 |
$meta_query[] = array( |
| 43 |
'key' => 'thread_type', |
| 44 |
'value' => array('report','reply'), |
| 45 |
'compare' => 'IN' |
| 46 |
); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
$args['meta_query'] = $meta_query; |
| 51 |
|
| 52 |
$threads = new WP_Query($args); |
| 53 |
|
| 54 |
$total_items = $threads->found_posts; |
| 55 |
$total_pages = ceil($total_items/$no_of_threads); |
| 56 |
$is_next = $current_page < $total_pages ? 1 : 0; |
| 57 |
|
| 58 |
$response = array( |
| 59 |
'total_items' => $total_items, |
| 60 |
'total_pages' => $total_pages, |
| 61 |
'current_page' => $current_page, |
| 62 |
'current_page_items' => $threads->post_count, |
| 63 |
'is_next_page' => $is_next, |
| 64 |
'next_page' => $is_next ? $current_page+1 : 0, |
| 65 |
); |
| 66 |
|
| 67 |
$threads_response = array(); |
| 68 |
|
| 69 |
foreach ( $threads->posts as $thread ) { |
| 70 |
$threads_response[] = SELF::get_thread_response($thread); |
| 71 |
} |
| 72 |
|
| 73 |
$response['threads'] = $threads_response; |
| 74 |
|