| 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 |
$historyId = isset($params['historyId']) ? intval($params['historyId']) : 0; |
| 12 |
|
| 13 |
if (!$historyId) { |
| 14 |
$response = new WP_Error( |
| 15 |
'invalid_api_parameters', |
| 16 |
'Invalid api parameters provided.', |
| 17 |
array( |
| 18 |
'status' => 403, |
| 19 |
) |
| 20 |
); |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Now we need only tickets that current user is allowed to read |
| 27 |
*/ |
| 28 |
|
| 29 |
$meta_query = array( |
| 30 |
'relation' => 'AND', |
| 31 |
); |
| 32 |
|
| 33 |
if ( !is_multisite() || !is_super_admin($current_user->ID) ) { |
| 34 |
$meta_query[] = SELF::get_tl_meta_query_restrict_rules(); |
| 35 |
} |
| 36 |
|
| 37 |
$meta_query = array_merge($meta_query,$wpscfunction->get_default_filter_query('all')); |
| 38 |
|
| 39 |
$meta_query[] = array( |
| 40 |
'key' => 'active', |
| 41 |
'value' => 1, |
| 42 |
'compare' => '=' |
| 43 |
); |
| 44 |
|
| 45 |
$meta_query[] = array( |
| 46 |
'key' => 'historyId', |
| 47 |
'value' => $historyId, |
| 48 |
'compare' => '>' |
| 49 |
); |
| 50 |
|
| 51 |
$select_str = 'DISTINCT t.id'; |
| 52 |
$sql = $wpscfunction->get_sql_query( $select_str, $meta_query ); |
| 53 |
$tickets = $wpdb->get_results($sql); |
| 54 |
|
| 55 |
$ticket_ids = array(); |
| 56 |
foreach ($tickets as $ticket) { |
| 57 |
$ticket_ids[] = $ticket->id; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Up to this point we have ticekt ids to which user has access to. |
| 63 |
* Now we need to get thread ids greater than $histortId of which ticket_id falls in $ticket_ids |
| 64 |
* This way we get only those threds to which user has access. This includes all threads like report, reply, private notes, logs, etc. no matter which user is logged in. |
| 65 |
*/ |
| 66 |
if ($ticket_ids) { |
| 67 |
|
| 68 |
$args = array( |
| 69 |
'post_type' => 'wpsc_ticket_thread', |
| 70 |
'post_status' => 'publish', |
| 71 |
'orderby' => 'ID', |
| 72 |
'order' => 'ASC', |
| 73 |
'posts_per_page' => $no_of_threads, |
| 74 |
'paged' => $current_page, |
| 75 |
'meta_query' => array( |
| 76 |
'relation' => 'AND', |
| 77 |
array( |
| 78 |
'key' => 'ticket_id', |
| 79 |
'value' => $ticket_ids, |
| 80 |
'compare' => 'IN', |
| 81 |
) |
| 82 |
), |
| 83 |
); |
| 84 |
|
| 85 |
$filter_handler = function( $where = '' ) use ( $historyId ) { |
| 86 |
|
| 87 |
global $wpdb; |
| 88 |
return $where . " AND {$wpdb->posts}.ID > $historyId"; |
| 89 |
|
| 90 |
}; |
| 91 |
|
| 92 |
add_filter( 'posts_where', $filter_handler ); |
| 93 |
$threads = new WP_Query($args); |
| 94 |
remove_filter( 'posts_where', $filter_handler ); |
| 95 |
|
| 96 |
$total_items = $threads->found_posts; |
| 97 |
$total_pages = ceil($total_items/$no_of_threads); |
| 98 |
$is_next = $current_page < $total_pages ? 1 : 0; |
| 99 |
$page_items = $threads->post_count; |
| 100 |
|
| 101 |
} else { |
| 102 |
|
| 103 |
$total_items = 0; |
| 104 |
$total_pages = 1; |
| 105 |
$is_next = 0; |
| 106 |
$page_items = 0; |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
$response = array( |
| 112 |
'total_items' => $total_items, |
| 113 |
'total_pages' => $total_pages, |
| 114 |
'current_page' => $current_page, |
| 115 |
'current_page_items' => $page_items, |
| 116 |
'is_next_page' => $is_next, |
| 117 |
'next_page' => $is_next ? $current_page+1 : 0, |
| 118 |
); |
| 119 |
|
| 120 |
$threads_response = array(); |
| 121 |
|
| 122 |
foreach ( $threads->posts as $thread ) { |
| 123 |
$threads_response[] = SELF::get_thread_response($thread); |
| 124 |
} |
| 125 |
|
| 126 |
$response['threads'] = $threads_response; |
| 127 |
|
| 128 |
|