| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fetch ticket replies count based on user id |
| 4 |
* @global object $wpdb |
| 5 |
* @param int $ticket_id |
| 6 |
* @param int $user_id |
| 7 |
* |
| 8 |
* @return int |
| 9 |
*/ |
| 10 |
function wpas_count_user_replies( $ticket_id, $user_id ) { |
| 11 |
|
| 12 |
global $wpdb; |
| 13 |
$count = 0; |
| 14 |
if($user_id) { |
| 15 |
$query = $wpdb->prepare("SELECT count(*) FROM {$wpdb->prefix}posts WHERE post_author = %d AND post_parent = %d AND post_type = %s AND post_status != 'trash'", $user_id, $ticket_id, 'ticket_reply'); |
| 16 |
$count = $wpdb->get_var("$query"); |
| 17 |
} |
| 18 |
|
| 19 |
return $count; |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Fetch ticket total replies count |
| 25 |
* @global object $wpdb |
| 26 |
* @param int $ticket_id |
| 27 |
* |
| 28 |
* @return int |
| 29 |
*/ |
| 30 |
function wpas_count_total_replies( $ticket_id ) { |
| 31 |
|
| 32 |
global $wpdb; |
| 33 |
$count = 0; |
| 34 |
|
| 35 |
$query = $wpdb->prepare("SELECT count(*) FROM {$wpdb->prefix}posts WHERE post_parent = %d AND post_type = %s AND post_status != 'trash'", $ticket_id, 'ticket_reply'); |
| 36 |
$count = $wpdb->get_var("$query"); |
| 37 |
|
| 38 |
|
| 39 |
return $count; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get ticket agent replies count |
| 45 |
* @param int $ticket_id |
| 46 |
* |
| 47 |
* @return int |
| 48 |
*/ |
| 49 |
function wpas_num_agent_replies( $ticket_id ) { |
| 50 |
|
| 51 |
$count = wpas_get_cf_value( 'ttl_replies_by_agent', $ticket_id ); |
| 52 |
return ( $count ? $count : 0 ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Get ticket customer replies count |
| 59 |
* @param int $ticket_id |
| 60 |
* |
| 61 |
* @return int |
| 62 |
*/ |
| 63 |
function wpas_num_customer_replies( $ticket_id ) { |
| 64 |
|
| 65 |
$count = wpas_get_cf_value( 'ttl_replies_by_customer', $ticket_id ); |
| 66 |
return ( $count ? $count : 0 ); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get Ticket total replies |
| 72 |
* @param int $ticket_id |
| 73 |
* |
| 74 |
* @return int |
| 75 |
*/ |
| 76 |
function wpas_num_total_replies( $ticket_id ) { |
| 77 |
|
| 78 |
$count = wpas_get_cf_value( 'ttl_replies', $ticket_id ); |
| 79 |
return ( $count ? $count : 0 ); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Calculate and store ticket replies count |
| 85 |
* @param int $ticket_id |
| 86 |
*/ |
| 87 |
function wpas_count_replies( $ticket_id ) { |
| 88 |
|
| 89 |
$ticket = get_post( $ticket_id ); |
| 90 |
|
| 91 |
if ( 'ticket' == get_post_type( $ticket ) ) { |
| 92 |
|
| 93 |
$agent_id = (int) get_post_meta( $ticket_id, '_wpas_assignee', true ); |
| 94 |
$customer_id = (int) $ticket->post_author; |
| 95 |
|
| 96 |
$total_replies_count = wpas_count_total_replies($ticket_id); |
| 97 |
$customer_replies_count = wpas_count_user_replies($ticket_id, $customer_id); |
| 98 |
$agent_replies_count = $total_replies_count - $customer_replies_count; |
| 99 |
|
| 100 |
update_post_meta( $ticket_id, '_wpas_ttl_replies_by_customer', $customer_replies_count ); |
| 101 |
update_post_meta( $ticket_id, '_wpas_ttl_replies_by_agent', $agent_replies_count ); |
| 102 |
update_post_meta( $ticket_id, '_wpas_ttl_replies', $total_replies_count ); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
add_action( 'wpas_add_reply_after', 'wpas_ticket_reset_replies_count', 10, 2 ); |
| 110 |
add_action( 'wpas_admin_reply_trashed', 'wpas_ticket_reset_replies_count', 10, 3 ); |
| 111 |
|
| 112 |
/** |
| 113 |
* Reset replies count |
| 114 |
* @param int $reply_id |
| 115 |
* @param array $data |
| 116 |
* @param int $ticket_id |
| 117 |
*/ |
| 118 |
function wpas_ticket_reset_replies_count( $reply_id, $data = array(), $ticket_id = '' ) { |
| 119 |
|
| 120 |
if ( empty( $ticket_id ) ) { |
| 121 |
$ticket_id = wp_get_post_parent_id($reply_id); |
| 122 |
} |
| 123 |
|
| 124 |
wpas_count_replies( $ticket_id ); |
| 125 |
} |