| @@ -65,21 +65,22 @@ | ||
| 65 | 65 | // create ticket event to reset unresolved and workload counts of related agents to the ticket. |
| 66 | 66 | add_action( 'wpsc_create_new_ticket', array( __CLASS__, 'create_new_ticket' ), 500 ); |
| 67 | 67 | |
| 68 | 68 | // change status event to reset unresolved and workload counts of related agents to the ticket. |
| 69 | - add_action( 'wpsc_change_ticket_status', array( __CLASS__, 'change_status' ), 200, 4 ); | |
| 69 | + add_action( 'wpsc_change_ticket_status', array( __CLASS__, 'change_status' ), 500, 4 ); | |
| 70 | 70 | |
| 71 | 71 | // assigned agent event to reset unresolved and workload counts of related agents to the ticket. |
| 72 | - add_action( 'wpsc_change_assignee', array( __CLASS__, 'change_assignee' ), 200, 4 ); | |
| 72 | + add_action( 'wpsc_change_assignee', array( __CLASS__, 'change_assignee' ), 500, 4 ); | |
| 73 | 73 | |
| 74 | 74 | // change raised by event to reset unresolved and workload counts of related agents to the ticket. |
| 75 | - add_action( 'wpsc_change_raised_by', array( __CLASS__, 'change_raised_by' ), 200, 4 ); | |
| 75 | + add_action( 'wpsc_change_raised_by', array( __CLASS__, 'change_raised_by' ), 500, 4 ); | |
| 76 | 76 | |
| 77 | 77 | // archive/restore/delete ticket event to reset unresolved and workload counts of related agents to the ticket. |
| 78 | - add_action( 'wpsc_ticket_archive', array( __CLASS__, 'reset_agent_unresolved_count' ), 200 ); | |
| 79 | - add_action( 'wpsc_delete_ticket', array( __CLASS__, 'reset_agent_unresolved_count' ), 200 ); | |
| 80 | - add_action( 'wpsc_ticket_restore', array( __CLASS__, 'reset_agent_unresolved_count' ), 200 ); | |
| 81 | - add_action( 'wpsc_ticket_delete_permanently', array( __CLASS__, 'reset_agent_unresolved_count' ), 200 ); | |
| 78 | + add_action( 'wpsc_delete_ticket', array( __CLASS__, 'reset_agent_unresolved_count' ), 500 ); | |
| 79 | + add_action( 'wpsc_ticket_restore', array( __CLASS__, 'reset_agent_unresolved_count' ), 500 ); | |
| 80 | + add_action( 'wpsc_ticket_delete_permanently', array( __CLASS__, 'reset_agent_unresolved_count' ), 500 ); | |
| 81 | + add_action( 'wpsc_ticket_archive', array( __CLASS__, 'reset_agents_unresolved_count' ), 500, 2 ); | |
| 82 | + add_action( 'wpsc_archive_ticket_restore', array( __CLASS__, 'reset_agents_unresolved_count' ), 500, 2 ); | |
| 82 | 83 | |
| 83 | 84 | // agent autocomplete admin access only. |
| 84 | 85 | add_action( 'wp_ajax_wpsc_agent_autocomplete_admin_access', array( __CLASS__, 'agent_autocomplete_admin_access' ) ); |
| 85 | 86 | |
| @@ -570,12 +571,21 @@ | ||
| 570 | 571 | * @return array |
| 571 | 572 | */ |
| 572 | 573 | public static function agent_autocomplete( $filters ) { |
| 573 | 574 | |
| 575 | + // Whitelisted map of accepted sort_by values to actual DB columns - only | |
| 576 | + // the two options offered in the sort-by dropdown are valid here. | |
| 577 | + $sortable_columns = array( | |
| 578 | + 'name' => 'name', | |
| 579 | + 'workload' => 'workload', | |
| 580 | + ); | |
| 581 | + $orderby = isset( $sortable_columns[ $filters['sort_by'] ] ) ? | |
| 582 | + $sortable_columns[ $filters['sort_by'] ] : 'name'; | |
| 583 | + | |
| 574 | 584 | $args = array( |
| 575 | 585 | 'search' => $filters['term'], |
| 576 | 586 | 'items_per_page' => 25, |
| 577 | - 'orderby' => $filters['sort_by'], | |
| 587 | + 'orderby' => $orderby, | |
| 578 | 588 | 'order' => 'ASC', |
| 579 | 589 | 'meta_query' => array( |
| 580 | 590 | 'relation' => 'AND', |
| 581 | 591 | array( |
| @@ -884,14 +894,21 @@ | ||
| 884 | 894 | * Reset workload count for the agent |
| 885 | 895 | */ |
| 886 | 896 | public function reset_workload() { |
| 887 | 897 | |
| 888 | - if ( $this->is_agentgroup ) { | |
| 898 | + if ( ! empty( $this->is_agentgroup ) ) { | |
| 889 | 899 | return; |
| 890 | 900 | } |
| 891 | 901 | |
| 892 | - $more_settings = get_option( 'wpsc-tl-ms-agent-view' ); | |
| 893 | - $response = WPSC_Ticket::find( | |
| 902 | + $more_settings = get_option( 'wpsc-tl-ms-agent-view', array() ); | |
| 903 | + $statuses = isset( $more_settings['unresolved-ticket-statuses'] ) ? $more_settings['unresolved-ticket-statuses'] : array(); | |
| 904 | + if ( empty( $statuses ) ) { | |
| 905 | + $this->workload = 0; | |
| 906 | + $this->save(); | |
| 907 | + return; | |
| 908 | + } | |
| 909 | + | |
| 910 | + $count = WPSC_Ticket::count( | |
| 894 | 911 | array( |
| 895 | 912 | 'items_per_page' => 1, |
| 896 | 913 | 'meta_query' => array( |
| 897 | 914 | 'relation' => 'AND', |
| @@ -897,9 +914,9 @@ | ||
| 897 | 914 | 'relation' => 'AND', |
| 898 | 915 | array( |
| 899 | 916 | 'slug' => 'status', |
| 900 | 917 | 'compare' => 'IN', |
| 901 | - 'val' => $more_settings['unresolved-ticket-statuses'], | |
| 918 | + 'val' => $statuses, | |
| 902 | 919 | ), |
| 903 | 920 | array( |
| 904 | 921 | 'slug' => 'assigned_agent', |
| 905 | 922 | 'compare' => '=', |
| @@ -908,10 +925,9 @@ | ||
| 908 | 925 | ), |
| 909 | 926 | ) |
| 910 | 927 | ); |
| 911 | 928 | |
| 912 | - // save workload. | |
| 913 | - $this->workload = $response['total_items']; | |
| 929 | + $this->workload = (int) $count; | |
| 914 | 930 | $this->save(); |
| 915 | 931 | } |
| 916 | 932 | |
| 917 | 933 | /** |
| @@ -1031,8 +1047,20 @@ | ||
| 1031 | 1047 | } |
| 1032 | 1048 | } |
| 1033 | 1049 | |
| 1034 | 1050 | /** |
| 1051 | + * Reset unresolved count and workload after restore archive ticket | |
| 1052 | + * | |
| 1053 | + * @param WPSC_Ticket $ticket - ticket object. | |
| 1054 | + * @param WPSC_Archive_Ticket $ar_ticket - archive ticket object. | |
| 1055 | + * @return void | |
| 1056 | + */ | |
| 1057 | + public static function reset_agents_unresolved_count( $ticket, $ar_ticket ) { | |
| 1058 | + | |
| 1059 | + self::reset_agent_unresolved_count( $ticket ); | |
| 1060 | + } | |
| 1061 | + | |
| 1062 | + /** | |
| 1035 | 1063 | * Reset unresolved count and workload after archive/restore/delete ticket |
| 1036 | 1064 | * |
| 1037 | 1065 | * @param WPSC_Ticket $ticket - ticket object. |
| 1038 | 1066 | * @return void |
| @@ -1144,8 +1172,67 @@ | ||
| 1144 | 1172 | foreach ( $agents as $agent ) { |
| 1145 | 1173 | |
| 1146 | 1174 | $agent->reset_unresolved_count(); |
| 1147 | 1175 | } |
| 1176 | + } | |
| 1177 | + | |
| 1178 | + /** | |
| 1179 | + * Count tickets based on given filters | |
| 1180 | + * | |
| 1181 | + * @param array $filter - array containing array items like search, where, etc. | |
| 1182 | + * @return int | |
| 1183 | + */ | |
| 1184 | + public static function count( $filter = array() ) { | |
| 1185 | + | |
| 1186 | + global $wpdb; | |
| 1187 | + | |
| 1188 | + $filter['is_active'] = isset( $filter['is_active'] ) ? $filter['is_active'] : 1; | |
| 1189 | + | |
| 1190 | + $sql = 'SELECT count(a.id) FROM ' . $wpdb->prefix . 'psmsc_agents a '; | |
| 1191 | + $joins = self::get_joins( $filter ); | |
| 1192 | + $where = self::get_where( $filter ); | |
| 1193 | + | |
| 1194 | + $sql = $sql . $joins . $where; | |
| 1195 | + | |
| 1196 | + return (int) $wpdb->get_var( $sql ); | |
| 1197 | + } | |
| 1198 | + | |
| 1199 | + /** | |
| 1200 | + * Check current user ticket access for capability | |
| 1201 | + * | |
| 1202 | + * @param WPSC_Ticket|WPSC_Archive_Ticket $ticket Ticket object. | |
| 1203 | + * @param string $cap - capability name. | |
| 1204 | + * @return boolean | |
| 1205 | + */ | |
| 1206 | + public static function has_ticket_cap( $ticket, $cap ) { | |
| 1207 | + | |
| 1208 | + $current_user = WPSC_Current_User::$current_user; | |
| 1209 | + | |
| 1210 | + $assigned_agents = array_map( | |
| 1211 | + fn ( $agent ) => $agent->id, | |
| 1212 | + $ticket->assigned_agent | |
| 1213 | + ); | |
| 1214 | + | |
| 1215 | + $flag = false; | |
| 1216 | + if ( | |
| 1217 | + ( | |
| 1218 | + ! $ticket->assigned_agent && | |
| 1219 | + $current_user->agent->has_cap( $cap . '-unassigned' ) | |
| 1220 | + ) || | |
| 1221 | + ( | |
| 1222 | + in_array( $current_user->agent->id, $assigned_agents ) && | |
| 1223 | + $current_user->agent->has_cap( $cap . '-assigned-me' ) | |
| 1224 | + ) || | |
| 1225 | + ( | |
| 1226 | + $ticket->assigned_agent && | |
| 1227 | + ! in_array( $current_user->agent->id, $assigned_agents ) && | |
| 1228 | + $current_user->agent->has_cap( $cap . '-assigned-others' ) | |
| 1229 | + ) | |
| 1230 | + ) { | |
| 1231 | + $flag = true; | |
| 1232 | + } | |
| 1233 | + | |
| 1234 | + return apply_filters( 'wpsc_it_has_ticket_cap', $flag, $ticket, $cap ); | |
| 1148 | 1235 | } |
| 1149 | 1236 | } |
| 1150 | 1237 | endif; |
| 1151 | 1238 | |