| @@ -65,22 +65,23 @@ | ||
| 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 | - // delete ticket event to reset unresolved and workload counts of related agents to the ticket. | |
| 78 | - add_action( 'wpsc_delete_ticket', array( __CLASS__, 'delete_ticket' ), 200, 1 ); | |
| 77 | + // archive/restore/delete ticket event to reset unresolved and workload counts of related agents to the ticket. | |
| 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 ); | |
| 79 | 83 | |
| 80 | - // restore ticket event to reset unresolved and workload counts of related agents to the ticket. | |
| 81 | - add_action( 'wpsc_ticket_restore', array( __CLASS__, 'restore_ticket' ), 200, 1 ); | |
| 82 | - | |
| 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 | |
| 86 | 87 | // reset unresolved count after agent role update. |
| @@ -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,45 +1047,26 @@ | ||
| 1031 | 1047 | } |
| 1032 | 1048 | } |
| 1033 | 1049 | |
| 1034 | 1050 | /** |
| 1035 | - * Reset unresolved count and workload after delete ticket | |
| 1051 | + * Reset unresolved count and workload after restore archive ticket | |
| 1036 | 1052 | * |
| 1037 | - * @param WPSC_Ticket $ticket - ticket object. | |
| 1053 | + * @param WPSC_Ticket $ticket - ticket object. | |
| 1054 | + * @param WPSC_Archive_Ticket $ar_ticket - archive ticket object. | |
| 1038 | 1055 | * @return void |
| 1039 | 1056 | */ |
| 1040 | - public static function delete_ticket( $ticket ) { | |
| 1057 | + public static function reset_agents_unresolved_count( $ticket, $ar_ticket ) { | |
| 1041 | 1058 | |
| 1042 | - $tl_advanced = get_option( 'wpsc-tl-ms-advanced' ); | |
| 1043 | - if ( in_array( $ticket->status->id, $tl_advanced['closed-ticket-statuses'] ) ) { | |
| 1044 | - return; | |
| 1045 | - } | |
| 1046 | - | |
| 1047 | - // reset workload for applicable agents. | |
| 1048 | - foreach ( $ticket->assigned_agent as $agent ) { | |
| 1049 | - if ( ! $agent->is_active ) { | |
| 1050 | - continue; | |
| 1051 | - } | |
| 1052 | - $agent->reset_workload(); | |
| 1053 | - } | |
| 1054 | - | |
| 1055 | - // reset unresolved for applicable agents. | |
| 1056 | - $agents = $ticket->get_current_read_permission_agents(); | |
| 1057 | - foreach ( $agents as $agent ) { | |
| 1058 | - if ( ! $agent->is_active ) { | |
| 1059 | - continue; | |
| 1060 | - } | |
| 1061 | - $agent->reset_unresolved_count(); | |
| 1062 | - } | |
| 1059 | + self::reset_agent_unresolved_count( $ticket ); | |
| 1063 | 1060 | } |
| 1064 | 1061 | |
| 1065 | 1062 | /** |
| 1066 | - * Reset unresolved count and workload after restore ticket | |
| 1063 | + * Reset unresolved count and workload after archive/restore/delete ticket | |
| 1067 | 1064 | * |
| 1068 | 1065 | * @param WPSC_Ticket $ticket - ticket object. |
| 1069 | 1066 | * @return void |
| 1070 | 1067 | */ |
| 1071 | - public static function restore_ticket( $ticket ) { | |
| 1068 | + public static function reset_agent_unresolved_count( $ticket ) { | |
| 1072 | 1069 | |
| 1073 | 1070 | $tl_advanced = get_option( 'wpsc-tl-ms-advanced' ); |
| 1074 | 1071 | if ( in_array( $ticket->status->id, $tl_advanced['closed-ticket-statuses'] ) ) { |
| 1075 | 1072 | return; |
| @@ -1119,9 +1116,9 @@ | ||
| 1119 | 1116 | */ |
| 1120 | 1117 | public static function agent_autocomplete_admin_access() { |
| 1121 | 1118 | |
| 1122 | 1119 | if ( check_ajax_referer( 'wpsc_agent_autocomplete_admin_access', '_ajax_nonce', false ) !== 1 ) { |
| 1123 | - wp_send_json_error( 'Unauthorised request!', 401 ); | |
| 1120 | + wp_send_json_error( 'Unauthorized request!', 401 ); | |
| 1124 | 1121 | } |
| 1125 | 1122 | |
| 1126 | 1123 | if ( ! WPSC_Functions::is_site_admin() ) { |
| 1127 | 1124 | wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| @@ -1175,8 +1172,67 @@ | ||
| 1175 | 1172 | foreach ( $agents as $agent ) { |
| 1176 | 1173 | |
| 1177 | 1174 | $agent->reset_unresolved_count(); |
| 1178 | 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 ); | |
| 1179 | 1235 | } |
| 1180 | 1236 | } |
| 1181 | 1237 | endif; |
| 1182 | 1238 | |