PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.1
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / admin / agent-settings / class-wpsc-agent-roles.php

class-wpsc-agent-roles.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.1, at includes/admin/agent-settings/class-wpsc-agent-roles.php

971 lines 43.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_Agent_Roles' ) ) :
7
8 final class WPSC_Agent_Roles {
9
10 /**
11 * Initialize this class
12 *
13 * @return void
14 */
15 public static function init() {
16
17 // Get agent roles ajax register.
18 add_action( 'wp_ajax_wpsc_get_agent_roles', array( __CLASS__, 'get_agent_roles' ) );
19
20 // Add agent role.
21 add_action( 'wp_ajax_wpsc_get_add_agent_role', array( __CLASS__, 'get_add_agent_role' ) );
22 add_action( 'wp_ajax_wpsc_set_add_agent_role', array( __CLASS__, 'set_add_agent_role' ) );
23
24 // clone agent role.
25 add_action( 'wp_ajax_wpsc_get_clone_agent_role', array( __CLASS__, 'get_clone_agent_role' ) );
26
27 // Edit agent role.
28 add_action( 'wp_ajax_wpsc_get_edit_agent_role', array( __CLASS__, 'get_edit_agent_role' ) );
29 add_action( 'wp_ajax_wpsc_set_edit_agent_role', array( __CLASS__, 'set_edit_agent_role' ) );
30
31 // Delete agent role.
32 add_action( 'wp_ajax_wpsc_delete_agent_role', array( __CLASS__, 'delete_agent_role' ) );
33 }
34
35 /**
36 * Get agent roles ajax callback function
37 *
38 * @return void
39 */
40 public static function get_agent_roles() {
41
42 if ( ! WPSC_Functions::is_site_admin() ) {
43 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
44 }
45 $roles = get_option( 'wpsc-agent-roles', array() );?>
46
47 <div class="wpsc-setting-header">
48 <h2><?php esc_attr_e( 'Agent Roles', 'supportcandy' ); ?></h2>
49 </div>
50 <div class="wpsc-setting-section-body">
51 <div class="wpsc-dock-container">
52 <?php
53 printf(
54 /* translators: Click here to see the documentation */
55 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
56 '<a href="https://supportcandy.net/docs/add-new-agent-role/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
57 );
58 ?>
59 </div>
60 <div class="wpsc-setting-cards-container">
61 <div style="width: 100%;">
62 <table class="agent-role-list-table wpsc-setting-tbl">
63 <thead>
64 <tr>
65 <th><?php esc_attr_e( 'Role', 'supportcandy' ); ?></th>
66 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
67 </tr>
68 </thead>
69 <tbody>
70 <?php
71 if ( $roles ) :
72 foreach ( $roles as $key => $role ) :
73 ?>
74 <tr>
75 <td> <span class="title"><?php echo esc_attr( $role['label'] ); ?></span> </td>
76 <td>
77 <a href="javascript:wpsc_get_edit_agent_role(<?php echo esc_attr( $key ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_agent_role' ) ); ?>');"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></a> |
78 <a href="javascript:wpsc_get_clone_agent_role(<?php echo esc_attr( $key ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_clone_agent_role' ) ); ?>');"><?php esc_attr_e( 'Clone', 'supportcandy' ); ?></a>
79 <?php
80 if ( intval( $key ) > 2 ) :
81 ?>
82 | <a href="javascript:wpsc_delete_agent_role(<?php echo esc_attr( $key ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_agent_role' ) ); ?>');"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></a>
83 <?php
84 endif
85 ?>
86 </td>
87 </tr>
88 <?php
89 endforeach;
90 endif;
91 ?>
92 </tbody>
93 </table>
94 <script>
95 jQuery('table.agent-role-list-table').DataTable({
96 order: [[1, "asc"]],
97 pageLength: 20,
98 bLengthChange: false,
99 columnDefs: [
100 { targets: [0,-1], orderable: false },
101 { targets: -1, searchable: false },
102 { targets: '_all', className: 'dt-left' }
103 ],
104 layout: {
105 topStart: {
106 buttons: [
107 {
108 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
109 className: 'wpsc-button small primary',
110 action: function ( e, dt, node, config ) {
111 jQuery( '.wpsc-setting-section-body' ).html( supportcandy.loader_html );
112 var data = { action: 'wpsc_get_add_agent_role' , _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_add_agent_role' ) ); ?>' };
113 jQuery.post(
114 supportcandy.ajax_url,
115 data,
116 function (response) {
117 jQuery( '.wpsc-setting-section-body' ).html( response );
118 }
119 );
120 }
121 }
122 ],
123 }
124 },
125 language: supportcandy.translations.datatables
126 });
127 </script>
128 </div>
129 </div>
130 </div>
131 <?php
132 wp_die();
133 }
134
135 /**
136 * Add agent role UI
137 *
138 * @return void
139 */
140 public static function get_add_agent_role() {
141
142 if ( check_ajax_referer( 'wpsc_get_add_agent_role', '_ajax_nonce', false ) != 1 ) {
143 wp_send_json_error( 'Unauthorised request!', 401 );
144 }
145
146 if ( ! WPSC_Functions::is_site_admin() ) {
147 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
148 }
149
150 $title = esc_attr__( 'Add new', 'supportcandy' );
151
152 ?>
153 <form action="#" onsubmit="return false;" class="frm-add-agent-role">
154 <div class="wpsc-input-group">
155 <div class="label-container">
156 <label for="">
157 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
158 <span class="required-char">*</span>
159 </label>
160 </div>
161 <input id="label" type="text" name="label" autocomplete="off">
162 </div>
163
164 <table id="wpsc_add_agent_role">
165 <thead>
166 <tr>
167 <th><?php esc_attr_e( 'Capability', 'supportcandy' ); ?></th>
168 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-una"><?php esc_attr_e( 'Unassigned', 'supportcandy' ); ?></th>
169 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-ame"><?php esc_attr_e( 'Assigned to me', 'supportcandy' ); ?></th>
170 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-ao"><?php esc_attr_e( 'Assigned to others', 'supportcandy' ); ?></th>
171 </tr>
172 </thead>
173
174 <tbody>
175 <tr>
176 <td><label for=""><?php esc_attr_e( 'View Tickets', 'supportcandy' ); ?></label></td>
177 <td><input name="caps[]" type="checkbox" value="view-unassigned" class="wpsc-una"></td>
178 <td><input name="caps[]" type="checkbox" value="view-assigned-me" class="wpsc-ame"></td>
179 <td><input name="caps[]" type="checkbox" value="view-assigned-others" class="wpsc-ao"></td>
180 </tr>
181
182 <tr>
183 <td><label for=""><?php esc_attr_e( 'Reply Tickets', 'supportcandy' ); ?></label></td>
184 <td><input name="caps[]" type="checkbox" value="reply-unassigned" class="wpsc-una"></td>
185 <td><input name="caps[]" type="checkbox" value="reply-assigned-me" class="wpsc-ame"></td>
186 <td><input name="caps[]" type="checkbox" value="reply-assigned-others" class="wpsc-ao"></td>
187 </tr>
188
189 <tr>
190 <td><label for=""><?php esc_attr_e( 'Private Notes', 'supportcandy' ); ?></label></td>
191 <td><input name="caps[]" type="checkbox" value="pn-unassigned" class="wpsc-una"></td>
192 <td><input name="caps[]" type="checkbox" value="pn-assigned-me" class="wpsc-ame"></td>
193 <td><input name="caps[]" type="checkbox" value="pn-assigned-others" class="wpsc-ao"></td>
194 </tr>
195
196 <tr>
197 <td><label for=""><?php esc_attr_e( 'Duplicate Ticket', 'supportcandy' ); ?></label></td>
198 <td><input name="caps[]" type="checkbox" value="dt-unassigned" class="wpsc-una"></td>
199 <td><input name="caps[]" type="checkbox" value="dt-assigned-me" class="wpsc-ame"></td>
200 <td><input name="caps[]" type="checkbox" value="dt-assigned-others" class="wpsc-ao"></td>
201 </tr>
202
203 <tr>
204 <td><label for=""><?php esc_attr_e( 'Assign Agents', 'supportcandy' ); ?></label></td>
205 <td><input name="caps[]" type="checkbox" value="aa-unassigned" class="wpsc-una"></td>
206 <td><input name="caps[]" type="checkbox" value="aa-assigned-me" class="wpsc-ame"></td>
207 <td><input name="caps[]" type="checkbox" value="aa-assigned-others" class="wpsc-ao"></td>
208 </tr>
209
210 <tr>
211 <td><label for=""><?php esc_attr_e( 'Change Status', 'supportcandy' ); ?></label></td>
212 <td><input name="caps[]" type="checkbox" value="cs-unassigned" class="wpsc-una"></td>
213 <td><input name="caps[]" type="checkbox" value="cs-assigned-me" class="wpsc-ame"></td>
214 <td><input name="caps[]" type="checkbox" value="cs-assigned-others" class="wpsc-ao"></td>
215 </tr>
216
217 <tr>
218 <td><label for=""><?php esc_attr_e( 'Change Ticket Fields', 'supportcandy' ); ?></label></td>
219 <td><input name="caps[]" type="checkbox" value="ctf-unassigned" class="wpsc-una"></td>
220 <td><input name="caps[]" type="checkbox" value="ctf-assigned-me" class="wpsc-ame"></td>
221 <td><input name="caps[]" type="checkbox" value="ctf-assigned-others" class="wpsc-ao"></td>
222 </tr>
223
224 <tr>
225 <td><label for=""><?php esc_attr_e( 'Change Agent Only Fields', 'supportcandy' ); ?></label></td>
226 <td><input name="caps[]" type="checkbox" value="caof-unassigned" class="wpsc-una"></td>
227 <td><input name="caps[]" type="checkbox" value="caof-assigned-me" class="wpsc-ame"></td>
228 <td><input name="caps[]" type="checkbox" value="caof-assigned-others" class="wpsc-ao"></td>
229 </tr>
230
231 <tr>
232 <td><label for=""><?php esc_attr_e( 'Change Raised By', 'supportcandy' ); ?></label></td>
233 <td><input name="caps[]" type="checkbox" value="crb-unassigned" class="wpsc-una"></td>
234 <td><input name="caps[]" type="checkbox" value="crb-assigned-me" class="wpsc-ame"></td>
235 <td><input name="caps[]" type="checkbox" value="crb-assigned-others" class="wpsc-ao"></td>
236 </tr>
237
238 <tr>
239 <td><label for=""><?php esc_attr_e( 'Modify Additional Recipients', 'supportcandy' ); ?></label></td>
240 <td><input name="caps[]" type="checkbox" value="ar-unassigned" class="wpsc-una"></td>
241 <td><input name="caps[]" type="checkbox" value="ar-assigned-me" class="wpsc-ame"></td>
242 <td><input name="caps[]" type="checkbox" value="ar-assigned-others" class="wpsc-ao"></td>
243 </tr>
244
245 <tr>
246 <td><label for=""><?php esc_attr_e( 'Edit Threads', 'supportcandy' ); ?></label></td>
247 <td><input name="caps[]" type="checkbox" value="eth-unassigned" class="wpsc-una"></td>
248 <td><input name="caps[]" type="checkbox" value="eth-assigned-me" class="wpsc-ame"></td>
249 <td><input name="caps[]" type="checkbox" value="eth-assigned-others" class="wpsc-ao"></td>
250 </tr>
251
252 <tr>
253 <td><label for=""><?php esc_attr_e( 'Delete Threads', 'supportcandy' ); ?></label></td>
254 <td><input name="caps[]" type="checkbox" value="dth-unassigned" class="wpsc-una"></td>
255 <td><input name="caps[]" type="checkbox" value="dth-assigned-me" class="wpsc-ame"></td>
256 <td><input name="caps[]" type="checkbox" value="dth-assigned-others" class="wpsc-ao"></td>
257 </tr>
258
259 <tr>
260 <td><label for=""><?php esc_attr_e( 'View Logs', 'supportcandy' ); ?></label></td>
261 <td><input name="caps[]" type="checkbox" value="vl-unassigned" class="wpsc-una"></td>
262 <td><input name="caps[]" type="checkbox" value="vl-assigned-me" class="wpsc-ame"></td>
263 <td><input name="caps[]" type="checkbox" value="vl-assigned-others" class="wpsc-ao"></td>
264 </tr>
265
266 <tr>
267 <td><label for=""><?php esc_attr_e( 'Delete Ticket', 'supportcandy' ); ?></label></td>
268 <td><input name="caps[]" type="checkbox" value="dtt-unassigned" class="wpsc-una"></td>
269 <td><input name="caps[]" type="checkbox" value="dtt-assigned-me" class="wpsc-ame"></td>
270 <td><input name="caps[]" type="checkbox" value="dtt-assigned-others" class="wpsc-ao"></td>
271 </tr>
272
273 <tr>
274 <td><label for=""><?php esc_attr_e( 'Modify Tags', 'supportcandy' ); ?></label></td>
275 <td><input name="caps[]" type="checkbox" value="tt-unassigned" class="wpsc-una"></td>
276 <td><input name="caps[]" type="checkbox" value="tt-assigned-me" class="wpsc-ame"></td>
277 <td><input name="caps[]" type="checkbox" value="tt-assigned-others" class="wpsc-ao"></td>
278 </tr>
279
280 <?php do_action( 'wpsc_add_agent_role_ticket_permissions' ); ?>
281 </tbody>
282 </table>
283
284 <div class="wpsc-input-group" style="margin-top: 20px;">
285 <div class="label-container">
286 <label for=""><?php esc_attr_e( 'Other permissions', 'supportcandy' ); ?></label>
287 </div>
288 <div class="checkbox-group">
289 <div>
290 <input name="caps[]" type="checkbox" value="backend-access">
291 <span><?php esc_attr_e( 'WP dashboard access', 'supportcandy' ); ?></span>
292 </div>
293 <div>
294 <input name="caps[]" type="checkbox" value="create-as">
295 <span><?php esc_attr_e( 'Create ticket on others behalf', 'supportcandy' ); ?></span>
296 </div>
297 <div>
298 <input name="caps[]" type="checkbox" value="dtt-access">
299 <span><?php esc_attr_e( 'Deleted filter access', 'supportcandy' ); ?></span>
300 </div>
301 <div>
302 <input name="caps[]" type="checkbox" value="eci-access">
303 <span><?php esc_attr_e( 'Edit customer info', 'supportcandy' ); ?></span>
304 </div>
305 <div>
306 <input name="caps[]" type="checkbox" value="dash-access">
307 <span><?php esc_attr_e( 'Dashboard access', 'supportcandy' ); ?></span>
308 </div>
309 <?php do_action( 'wpsc_add_agent_role_other_permissions' ); ?>
310 </div>
311 </div>
312 <input type="hidden" name="action" value="wpsc_set_add_agent_role">
313 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_agent_role' ) ); ?>">
314 </form>
315
316 <button class="wpsc-button small primary" onclick="wpsc_set_add_agent_role(this);">
317 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
318 </button>
319 <button class="wpsc-button small secondary" onclick="wpsc_get_agent_roles();">
320 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
321 </button>
322
323 <script>
324 // check all unassigned action.
325 jQuery('#wpsc-all-una').change(function () {
326 if( jQuery(this).is(':checked') ) {
327 jQuery(".wpsc-una").prop('checked','checked');
328 } else {
329 jQuery(".wpsc-una").removeAttr('checked');
330 }
331 });
332 jQuery(".wpsc-una").change(function() {
333 if( jQuery('.wpsc-una:checked').length == jQuery('.wpsc-una').length ) {
334 jQuery('#wpsc-all-una').prop('checked','checked');
335 }else {
336 jQuery('#wpsc-all-una').removeAttr('checked');
337 }
338 })
339
340 // check all assigned me action.
341 jQuery('#wpsc-all-ame').change(function () {
342 if( jQuery(this).is(':checked') ) {
343 jQuery(".wpsc-ame").prop('checked','checked');
344 } else {
345 jQuery(".wpsc-ame").removeAttr('checked');
346 }
347 });
348 jQuery(".wpsc-ame").change(function() {
349 if( jQuery('.wpsc-ame:checked').length == jQuery('.wpsc-ame').length ) {
350 jQuery('#wpsc-all-ame').prop('checked','checked');
351 }else {
352 jQuery('#wpsc-all-ame').removeAttr('checked');
353 }
354 })
355
356 // check all assigned to other action.
357 jQuery('#wpsc-all-ao').change(function () {
358 if( jQuery(this).is(':checked') ) {
359 jQuery(".wpsc-ao").prop('checked','checked');
360 } else {
361 jQuery(".wpsc-ao").removeAttr('checked');
362 }
363 });
364 jQuery(".wpsc-ao").change(function() {
365 if( jQuery('.wpsc-ao:checked').length == jQuery('.wpsc-ao').length ) {
366 jQuery('#wpsc-all-ao').prop('checked','checked');
367 }else {
368 jQuery('#wpsc-all-ao').removeAttr('checked');
369 }
370 })
371
372 jQuery('#wpsc_add_agent_role').DataTable({
373 ordering: false,
374 pageLength: 20,
375 bLengthChange: false,
376 columnDefs: [
377 { targets: -1, searchable: false },
378 { targets: '_all', className: 'dt-left' }
379 ],
380 language: supportcandy.translations.datatables,
381 bPaginate: false,
382 bInfo: false,
383 });
384 </script>
385 <?php
386 wp_die();
387 }
388
389 /**
390 * Set add agent role
391 *
392 * @return void
393 */
394 public static function set_add_agent_role() {
395
396 if ( check_ajax_referer( 'wpsc_set_add_agent_role', '_ajax_nonce', false ) != 1 ) {
397 wp_send_json_error( 'Unauthorised request!', 401 );
398 }
399
400 if ( ! WPSC_Functions::is_site_admin() ) {
401 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
402 }
403
404 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
405 if ( ! $label ) {
406 wp_die( 'Label not available' );
407 }
408
409 $caps = isset( $_POST['caps'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['caps'] ) ) ) : array();
410 if ( ! $caps ) {
411 wp_die( 'caps not found' );
412 }
413
414 $roles = get_option( 'wpsc-agent-roles' );
415
416 $roles[] = apply_filters(
417 'wpsc_set_add_agent_role',
418 array(
419 'label' => $label,
420 'caps' => array(
421 'backend-access' => in_array( 'backend-access', $caps ) ? true : false,
422 'create-as' => in_array( 'create-as', $caps ) ? true : false,
423 'view-unassigned' => in_array( 'view-unassigned', $caps ) ? true : false,
424 'view-assigned-me' => in_array( 'view-assigned-me', $caps ) ? true : false,
425 'view-assigned-others' => in_array( 'view-assigned-others', $caps ) ? true : false,
426 'reply-unassigned' => in_array( 'reply-unassigned', $caps ) ? true : false,
427 'reply-assigned-me' => in_array( 'reply-assigned-me', $caps ) ? true : false,
428 'reply-assigned-others' => in_array( 'reply-assigned-others', $caps ) ? true : false,
429 'pn-unassigned' => in_array( 'pn-unassigned', $caps ) ? true : false,
430 'pn-assigned-me' => in_array( 'pn-assigned-me', $caps ) ? true : false,
431 'pn-assigned-others' => in_array( 'pn-assigned-others', $caps ) ? true : false,
432 'aa-unassigned' => in_array( 'aa-unassigned', $caps ) ? true : false,
433 'aa-assigned-me' => in_array( 'aa-assigned-me', $caps ) ? true : false,
434 'aa-assigned-others' => in_array( 'aa-assigned-others', $caps ) ? true : false,
435 'cs-unassigned' => in_array( 'cs-unassigned', $caps ) ? true : false,
436 'cs-assigned-me' => in_array( 'cs-assigned-me', $caps ) ? true : false,
437 'cs-assigned-others' => in_array( 'cs-assigned-others', $caps ) ? true : false,
438 'ctf-unassigned' => in_array( 'ctf-unassigned', $caps ) ? true : false,
439 'ctf-assigned-me' => in_array( 'ctf-assigned-me', $caps ) ? true : false,
440 'ctf-assigned-others' => in_array( 'ctf-assigned-others', $caps ) ? true : false,
441 'caof-unassigned' => in_array( 'caof-unassigned', $caps ) ? true : false,
442 'caof-assigned-me' => in_array( 'caof-assigned-me', $caps ) ? true : false,
443 'caof-assigned-others' => in_array( 'caof-assigned-others', $caps ) ? true : false,
444 'crb-unassigned' => in_array( 'crb-unassigned', $caps ) ? true : false,
445 'crb-assigned-me' => in_array( 'crb-assigned-me', $caps ) ? true : false,
446 'crb-assigned-others' => in_array( 'crb-assigned-others', $caps ) ? true : false,
447 'eth-unassigned' => in_array( 'eth-unassigned', $caps ) ? true : false,
448 'eth-assigned-me' => in_array( 'eth-assigned-me', $caps ) ? true : false,
449 'eth-assigned-others' => in_array( 'eth-assigned-others', $caps ) ? true : false,
450 'dth-unassigned' => in_array( 'dth-unassigned', $caps ) ? true : false,
451 'dth-assigned-me' => in_array( 'dth-assigned-me', $caps ) ? true : false,
452 'dth-assigned-others' => in_array( 'dth-assigned-others', $caps ) ? true : false,
453 'vl-unassigned' => in_array( 'vl-unassigned', $caps ) ? true : false,
454 'vl-assigned-me' => in_array( 'vl-assigned-me', $caps ) ? true : false,
455 'vl-assigned-others' => in_array( 'vl-assigned-others', $caps ) ? true : false,
456 'dtt-unassigned' => in_array( 'dtt-unassigned', $caps ) ? true : false,
457 'dtt-assigned-me' => in_array( 'dtt-assigned-me', $caps ) ? true : false,
458 'dtt-assigned-others' => in_array( 'dtt-assigned-others', $caps ) ? true : false,
459 'dtt-access' => in_array( 'dtt-access', $caps ) ? true : false,
460 'dt-unassigned' => in_array( 'dt-unassigned', $caps ) ? true : false,
461 'dt-assigned-me' => in_array( 'dt-assigned-me', $caps ) ? true : false,
462 'dt-assigned-others' => in_array( 'dt-assigned-others', $caps ) ? true : false,
463 'ar-unassigned' => in_array( 'ar-unassigned', $caps ) ? true : false,
464 'ar-assigned-me' => in_array( 'ar-assigned-me', $caps ) ? true : false,
465 'ar-assigned-others' => in_array( 'ar-assigned-others', $caps ) ? true : false,
466 'eci-access' => in_array( 'eci-access', $caps ) ? true : false,
467 'dash-access' => in_array( 'dash-access', $caps ) ? true : false,
468 'tt-unassigned' => in_array( 'tt-unassigned', $caps ) ? true : false,
469 'tt-assigned-me' => in_array( 'tt-assigned-me', $caps ) ? true : false,
470 'tt-assigned-others' => in_array( 'tt-assigned-others', $caps ) ? true : false,
471
472 ),
473 ),
474 $caps
475 );
476
477 update_option( 'wpsc-agent-roles', $roles );
478
479 do_action( 'wpsc_after_add_agent_role', array_key_last( $roles ) );
480 wp_die();
481 }
482
483 /**
484 * Clone existing agent role.
485 *
486 * @return void
487 */
488 public static function get_clone_agent_role() {
489
490 if ( check_ajax_referer( 'wpsc_get_clone_agent_role', '_ajax_nonce', false ) != 1 ) {
491 wp_send_json_error( 'Unauthorized request!', 400 );
492 }
493
494 if ( ! WPSC_Functions::is_site_admin() ) {
495 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
496 }
497
498 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
499 if ( ! $id ) {
500 wp_send_json_error( 'Incorrect request!', 400 );
501 }
502
503 $roles = get_option( 'wpsc-agent-roles', array() );
504 $roles[] = array(
505 'label' => $roles[ $id ]['label'] . ' clone',
506 'caps' => $roles[ $id ]['caps'],
507 );
508
509 update_option( 'wpsc-agent-roles', $roles );
510
511 wp_die();
512 }
513
514 /**
515 * Edit agent role UI
516 *
517 * @return void
518 */
519 public static function get_edit_agent_role() {
520
521 if ( check_ajax_referer( 'wpsc_get_edit_agent_role', '_ajax_nonce', false ) != 1 ) {
522 wp_send_json_error( 'Unauthorised request!', 401 );
523 }
524
525 if ( ! WPSC_Functions::is_site_admin() ) {
526 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
527 }
528
529 $role_id = isset( $_POST['role_id'] ) ? intval( $_POST['role_id'] ) : 0;
530 if ( ! $role_id ) {
531 wp_die( 'role_id not found!' );
532 }
533
534 $roles = get_option( 'wpsc-agent-roles', array() );
535 $role = isset( $roles[ $role_id ] ) ? $roles[ $role_id ] : array();
536 if ( ! $role ) {
537 wp_die( 'Role not available!' );
538 }
539
540 ?>
541 <form action="#" onsubmit="return false;" class="frm-edit-agent-role">
542
543 <div class="wpsc-input-group">
544 <div class="label-container">
545 <label for="">
546 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
547 <span class="required-char">*</span>
548 </label>
549 </div>
550 <input id="label" type="text" name="label" autocomplete="off" value="<?php echo esc_attr( $role['label'] ); ?>">
551 </div>
552
553 <table id="wpsc_edit_agent_role">
554 <thead>
555 <tr>
556 <th><?php esc_attr_e( 'Capability', 'supportcandy' ); ?></th>
557 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-una"><?php esc_attr_e( 'Unassigned', 'supportcandy' ); ?></th>
558 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-ame"><?php esc_attr_e( 'Assigned to me', 'supportcandy' ); ?></th>
559 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-ao"><?php esc_attr_e( 'Assigned to others', 'supportcandy' ); ?></th>
560 </tr>
561 </thead>
562 <tbody>
563 <tr>
564 <td><label for=""><?php esc_attr_e( 'View Tickets', 'supportcandy' ); ?></label></td>
565 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['view-unassigned'], 1 ); ?> value="view-unassigned" class="wpsc-una"></td>
566 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['view-assigned-me'], 1 ); ?> value="view-assigned-me" class="wpsc-ame"></td>
567 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['view-assigned-others'], 1 ); ?> value="view-assigned-others" class="wpsc-ao"></td>
568 </tr>
569
570 <tr>
571 <td><label for=""><?php esc_attr_e( 'Reply Tickets', 'supportcandy' ); ?></label></td>
572 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['reply-unassigned'], 1 ); ?> value="reply-unassigned" class="wpsc-una"></td>
573 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['reply-assigned-me'], 1 ); ?> value="reply-assigned-me" class="wpsc-ame"></td>
574 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['reply-assigned-others'], 1 ); ?> value="reply-assigned-others" class="wpsc-ao"></td>
575 </tr>
576
577 <tr>
578 <td><label for=""><?php esc_attr_e( 'Private Notes', 'supportcandy' ); ?></label></td>
579 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['pn-unassigned'], 1 ); ?> value="pn-unassigned" class="wpsc-una"></td>
580 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['pn-assigned-me'], 1 ); ?> value="pn-assigned-me" class="wpsc-ame"></td>
581 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['pn-assigned-others'], 1 ); ?> value="pn-assigned-others" class="wpsc-ao"></td>
582 </tr>
583
584 <tr>
585 <td><label for=""><?php esc_attr_e( 'Duplicate Ticket', 'supportcandy' ); ?></label></td>
586 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dt-unassigned'], 1 ); ?> value="dt-unassigned" class="wpsc-una"></td>
587 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dt-assigned-me'], 1 ); ?> value="dt-assigned-me" class="wpsc-ame"></td>
588 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dt-assigned-others'], 1 ); ?> value="dt-assigned-others" class="wpsc-ao"></td>
589 </tr>
590
591 <tr>
592 <td><label for=""><?php esc_attr_e( 'Assign Agents', 'supportcandy' ); ?></label></td>
593 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['aa-unassigned'], 1 ); ?> value="aa-unassigned" class="wpsc-una"></td>
594 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['aa-assigned-me'], 1 ); ?> value="aa-assigned-me" class="wpsc-ame"></td>
595 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['aa-assigned-others'], 1 ); ?> value="aa-assigned-others" class="wpsc-ao"></td>
596 </tr>
597
598 <tr>
599 <td><label for=""><?php esc_attr_e( 'Change Status', 'supportcandy' ); ?></label></td>
600 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['cs-unassigned'], 1 ); ?> value="cs-unassigned" class="wpsc-una"></td>
601 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['cs-assigned-me'], 1 ); ?> value="cs-assigned-me" class="wpsc-ame"></td>
602 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['cs-assigned-others'], 1 ); ?> value="cs-assigned-others" class="wpsc-ao"></td>
603 </tr>
604
605 <tr>
606 <td><label for=""><?php esc_attr_e( 'Change Ticket Fields', 'supportcandy' ); ?></label></td>
607 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ctf-unassigned'], 1 ); ?> value="ctf-unassigned" class="wpsc-una"></td>
608 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ctf-assigned-me'], 1 ); ?> value="ctf-assigned-me" class="wpsc-ame"></td>
609 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ctf-assigned-others'], 1 ); ?> value="ctf-assigned-others" class="wpsc-ao"></td>
610 </tr>
611
612 <tr>
613 <td><label for=""><?php esc_attr_e( 'Change Agent Only Fields', 'supportcandy' ); ?></label></td>
614 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['caof-unassigned'], 1 ); ?> value="caof-unassigned" class="wpsc-una"></td>
615 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['caof-assigned-me'], 1 ); ?> value="caof-assigned-me" class="wpsc-ame"></td>
616 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['caof-assigned-others'], 1 ); ?> value="caof-assigned-others" class="wpsc-ao"></td>
617 </tr>
618
619 <tr>
620 <td><label for=""><?php esc_attr_e( 'Change Raised By', 'supportcandy' ); ?></label></td>
621 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['crb-unassigned'], 1 ); ?> value="crb-unassigned" class="wpsc-una"></td>
622 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['crb-assigned-me'], 1 ); ?> value="crb-assigned-me" class="wpsc-ame"></td>
623 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['crb-assigned-others'], 1 ); ?> value="crb-assigned-others" class="wpsc-ao"></td>
624 </tr>
625
626 <tr>
627 <td><label for=""><?php esc_attr_e( 'Modify Additional Recipients', 'supportcandy' ); ?></label></td>
628 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ar-unassigned'], 1 ); ?> value="ar-unassigned" class="wpsc-una"></td>
629 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ar-assigned-me'], 1 ); ?> value="ar-assigned-me" class="wpsc-ame"></td>
630 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ar-assigned-others'], 1 ); ?> value="ar-assigned-others" class="wpsc-ao"></td>
631 </tr>
632
633 <tr>
634 <td><label for=""><?php esc_attr_e( 'Edit Threads', 'supportcandy' ); ?></label></td>
635 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['eth-unassigned'], 1 ); ?> value="eth-unassigned" class="wpsc-una"></td>
636 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['eth-assigned-me'], 1 ); ?> value="eth-assigned-me" class="wpsc-ame"></td>
637 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['eth-assigned-others'], 1 ); ?> value="eth-assigned-others" class="wpsc-ao"></td>
638 </tr>
639
640 <tr>
641 <td><label for=""><?php esc_attr_e( 'Delete Threads', 'supportcandy' ); ?></label></td>
642 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dth-unassigned'], 1 ); ?> value="dth-unassigned" class="wpsc-una"></td>
643 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dth-assigned-me'], 1 ); ?> value="dth-assigned-me" class="wpsc-ame"></td>
644 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dth-assigned-others'], 1 ); ?> value="dth-assigned-others" class="wpsc-ao"></td>
645 </tr>
646
647 <tr>
648 <td><label for=""><?php esc_attr_e( 'View Logs', 'supportcandy' ); ?></label></td>
649 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['vl-unassigned'], 1 ); ?> value="vl-unassigned" class="wpsc-una"></td>
650 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['vl-assigned-me'], 1 ); ?> value="vl-assigned-me" class="wpsc-ame"></td>
651 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['vl-assigned-others'], 1 ); ?> value="vl-assigned-others" class="wpsc-ao"></td>
652 </tr>
653
654 <tr>
655 <td><label for=""><?php esc_attr_e( 'Delete Ticket', 'supportcandy' ); ?></label></td>
656 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-unassigned'], 1 ); ?> value="dtt-unassigned" class="wpsc-una"></td>
657 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-assigned-me'], 1 ); ?> value="dtt-assigned-me" class="wpsc-ame"></td>
658 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-assigned-others'], 1 ); ?> value="dtt-assigned-others" class="wpsc-ao"></td>
659 </tr>
660
661 <tr>
662 <td><label for=""><?php esc_attr_e( 'Modify Tags', 'supportcandy' ); ?></label></td>
663 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['tt-unassigned'], 1 ); ?> value="tt-unassigned" class="wpsc-una"></td>
664 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['tt-assigned-me'], 1 ); ?> value="tt-assigned-me" class="wpsc-ame"></td>
665 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['tt-assigned-others'], 1 ); ?> value="tt-assigned-others" class="wpsc-ao"></td>
666 </tr>
667 <?php do_action( 'wpsc_edit_agent_role_ticket_permissions', $role ); ?>
668 </tbody>
669 </table>
670 <div class="wpsc-input-group" style="margin-top: 20px;">
671 <div class="label-container">
672 <label for=""><?php esc_attr_e( 'Other permissions', 'supportcandy' ); ?></label>
673 </div>
674 <div class="checkbox-group">
675 <div>
676 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['backend-access'], 1 ); ?> value="backend-access">
677 <span><?php esc_attr_e( 'WP dashboard access', 'supportcandy' ); ?></span>
678 </div>
679 <div>
680 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['create-as'], 1 ); ?> value="create-as">
681 <span><?php esc_attr_e( 'Create ticket on others behalf', 'supportcandy' ); ?></span>
682 </div>
683 <div>
684 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-access'], 1 ); ?> value="dtt-access">
685 <span><?php esc_attr_e( 'Deleted filter access', 'supportcandy' ); ?></span>
686 </div>
687 <div>
688 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['eci-access'], 1 ); ?> value="eci-access">
689 <span><?php esc_attr_e( 'Edit customer info', 'supportcandy' ); ?></span>
690 </div>
691 <div>
692 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['dash-access'], 1 ); ?> value="dash-access">
693 <span><?php esc_attr_e( 'Dashboard access', 'supportcandy' ); ?></span>
694 </div>
695 <?php do_action( 'wpsc_edit_agent_role_other_permissions', $role ); ?>
696 </div>
697 </div>
698 <input type="hidden" name="role_id" value="<?php echo esc_attr( $role_id ); ?>">
699 <input type="hidden" name="action" value="wpsc_set_edit_agent_role">
700 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_agent_role' ) ); ?>">
701 </form>
702 <button class="wpsc-button small primary" onclick="wpsc_set_edit_agent_role();">
703 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
704 </button>
705 <button class="wpsc-button small secondary" onclick="wpsc_get_agent_roles();">
706 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
707 </button>
708
709 <script>
710 // check all unassigned action.
711 jQuery('#wpsc-all-una').change(function () {
712 if( jQuery(this).is(':checked') ) {
713 jQuery(".wpsc-una").prop('checked','checked');
714 } else {
715 jQuery(".wpsc-una").removeAttr('checked');
716 }
717 });
718 jQuery(".wpsc-una").change(function() {
719 if( jQuery('.wpsc-una:checked').length == jQuery('.wpsc-una').length ) {
720 jQuery('#wpsc-all-una').prop('checked','checked');
721 }else {
722 jQuery('#wpsc-all-una').removeAttr('checked');
723 }
724 });
725 jQuery(".wpsc-una").trigger("change");
726
727 // check all assigned me action.
728 jQuery('#wpsc-all-ame').change(function () {
729 if( jQuery(this).is(':checked') ) {
730 jQuery(".wpsc-ame").prop('checked','checked');
731 } else {
732 jQuery(".wpsc-ame").removeAttr('checked');
733 }
734 });
735 jQuery(".wpsc-ame").change(function() {
736 if( jQuery('.wpsc-ame:checked').length == jQuery('.wpsc-ame').length ) {
737 jQuery('#wpsc-all-ame').prop('checked','checked');
738 }else {
739 jQuery('#wpsc-all-ame').removeAttr('checked');
740 }
741 });
742 jQuery(".wpsc-ame").trigger('change');
743
744 // check all assigned to other action.
745 jQuery('#wpsc-all-ao').change(function () {
746 if( jQuery(this).is(':checked') ) {
747 jQuery(".wpsc-ao").prop('checked','checked');
748 } else {
749 jQuery(".wpsc-ao").removeAttr('checked');
750 }
751 });
752 jQuery(".wpsc-ao").change(function() {
753 if( jQuery('.wpsc-ao:checked').length == jQuery('.wpsc-ao').length ) {
754 jQuery('#wpsc-all-ao').prop('checked','checked');
755 }else {
756 jQuery('#wpsc-all-ao').removeAttr('checked');
757 }
758 });
759 jQuery(".wpsc-ao").trigger('change');
760
761 jQuery('#wpsc_edit_agent_role').DataTable({
762 ordering: false,
763 pageLength: 20,
764 bLengthChange: false,
765 columnDefs: [
766 { targets: -1, searchable: false },
767 { targets: '_all', className: 'dt-left' }
768 ],
769 language: supportcandy.translations.datatables,
770 bPaginate: false,
771 bInfo: false,
772 });
773 </script>
774 <?php
775 wp_die();
776 }
777
778 /**
779 * Set edit agent role
780 *
781 * @return void
782 */
783 public static function set_edit_agent_role() {
784
785 if ( check_ajax_referer( 'wpsc_set_edit_agent_role', '_ajax_nonce', false ) != 1 ) {
786 wp_send_json_error( 'Unauthorised request!', 401 );
787 }
788
789 if ( ! WPSC_Functions::is_site_admin() ) {
790 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
791 }
792
793 $role_id = isset( $_POST['role_id'] ) ? intval( $_POST['role_id'] ) : 0;
794 if ( ! $role_id ) {
795 wp_die( 'role_id not found!' );
796 }
797
798 $roles = get_option( 'wpsc-agent-roles', array() );
799 $role = isset( $roles[ $role_id ] ) ? $roles[ $role_id ] : array();
800 if ( ! $role ) {
801 wp_die( 'Role not available!' );
802 }
803
804 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
805 if ( ! $label ) {
806 wp_die( 'Label not available' );
807 }
808
809 $caps = isset( $_POST['caps'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['caps'] ) ) ) : array();
810 if ( ! $caps ) {
811 wp_die( 'caps not found' );
812 }
813
814 $roles[ $role_id ] = apply_filters(
815 'wpsc_set_edit_agent_role',
816 array(
817 'label' => $label,
818 'caps' => array(
819 'backend-access' => in_array( 'backend-access', $caps ) ? true : false,
820 'create-as' => in_array( 'create-as', $caps ) ? true : false,
821 'view-unassigned' => in_array( 'view-unassigned', $caps ) ? true : false,
822 'view-assigned-me' => in_array( 'view-assigned-me', $caps ) ? true : false,
823 'view-assigned-others' => in_array( 'view-assigned-others', $caps ) ? true : false,
824 'reply-unassigned' => in_array( 'reply-unassigned', $caps ) ? true : false,
825 'reply-assigned-me' => in_array( 'reply-assigned-me', $caps ) ? true : false,
826 'reply-assigned-others' => in_array( 'reply-assigned-others', $caps ) ? true : false,
827 'pn-unassigned' => in_array( 'pn-unassigned', $caps ) ? true : false,
828 'pn-assigned-me' => in_array( 'pn-assigned-me', $caps ) ? true : false,
829 'pn-assigned-others' => in_array( 'pn-assigned-others', $caps ) ? true : false,
830 'aa-unassigned' => in_array( 'aa-unassigned', $caps ) ? true : false,
831 'aa-assigned-me' => in_array( 'aa-assigned-me', $caps ) ? true : false,
832 'aa-assigned-others' => in_array( 'aa-assigned-others', $caps ) ? true : false,
833 'cs-unassigned' => in_array( 'cs-unassigned', $caps ) ? true : false,
834 'cs-assigned-me' => in_array( 'cs-assigned-me', $caps ) ? true : false,
835 'cs-assigned-others' => in_array( 'cs-assigned-others', $caps ) ? true : false,
836 'ctf-unassigned' => in_array( 'ctf-unassigned', $caps ) ? true : false,
837 'ctf-assigned-me' => in_array( 'ctf-assigned-me', $caps ) ? true : false,
838 'ctf-assigned-others' => in_array( 'ctf-assigned-others', $caps ) ? true : false,
839 'caof-unassigned' => in_array( 'caof-unassigned', $caps ) ? true : false,
840 'caof-assigned-me' => in_array( 'caof-assigned-me', $caps ) ? true : false,
841 'caof-assigned-others' => in_array( 'caof-assigned-others', $caps ) ? true : false,
842 'crb-unassigned' => in_array( 'crb-unassigned', $caps ) ? true : false,
843 'crb-assigned-me' => in_array( 'crb-assigned-me', $caps ) ? true : false,
844 'crb-assigned-others' => in_array( 'crb-assigned-others', $caps ) ? true : false,
845 'eth-unassigned' => in_array( 'eth-unassigned', $caps ) ? true : false,
846 'eth-assigned-me' => in_array( 'eth-assigned-me', $caps ) ? true : false,
847 'eth-assigned-others' => in_array( 'eth-assigned-others', $caps ) ? true : false,
848 'dth-unassigned' => in_array( 'dth-unassigned', $caps ) ? true : false,
849 'dth-assigned-me' => in_array( 'dth-assigned-me', $caps ) ? true : false,
850 'dth-assigned-others' => in_array( 'dth-assigned-others', $caps ) ? true : false,
851 'vl-unassigned' => in_array( 'vl-unassigned', $caps ) ? true : false,
852 'vl-assigned-me' => in_array( 'vl-assigned-me', $caps ) ? true : false,
853 'vl-assigned-others' => in_array( 'vl-assigned-others', $caps ) ? true : false,
854 'dtt-unassigned' => in_array( 'dtt-unassigned', $caps ) ? true : false,
855 'dtt-assigned-me' => in_array( 'dtt-assigned-me', $caps ) ? true : false,
856 'dtt-assigned-others' => in_array( 'dtt-assigned-others', $caps ) ? true : false,
857 'dtt-access' => in_array( 'dtt-access', $caps ) ? true : false,
858 'dt-unassigned' => in_array( 'dt-unassigned', $caps ) ? true : false,
859 'dt-assigned-me' => in_array( 'dt-assigned-me', $caps ) ? true : false,
860 'dt-assigned-others' => in_array( 'dt-assigned-others', $caps ) ? true : false,
861 'ar-unassigned' => in_array( 'ar-unassigned', $caps ) ? true : false,
862 'ar-assigned-me' => in_array( 'ar-assigned-me', $caps ) ? true : false,
863 'ar-assigned-others' => in_array( 'ar-assigned-others', $caps ) ? true : false,
864 'eci-access' => in_array( 'eci-access', $caps ) ? true : false,
865 'dash-access' => in_array( 'dash-access', $caps ) ? true : false,
866 'tt-unassigned' => in_array( 'tt-unassigned', $caps ) ? true : false,
867 'tt-assigned-me' => in_array( 'tt-assigned-me', $caps ) ? true : false,
868 'tt-assigned-others' => in_array( 'tt-assigned-others', $caps ) ? true : false,
869 ),
870 ),
871 $role,
872 $caps
873 );
874
875 // Update backend access user capabilities.
876 if ( $role['caps']['backend-access'] !== $roles[ $role_id ]['caps']['backend-access'] ) {
877
878 $response = WPSC_Agent::find(
879 array(
880 'items_per_page' => 0,
881 'meta_query' => array(
882 'relation' => 'AND',
883 array(
884 'slug' => 'role',
885 'compare' => '=',
886 'val' => $role_id,
887 ),
888 array(
889 'slug' => 'is_active',
890 'compare' => '=',
891 'val' => 1,
892 ),
893 ),
894 )
895 );
896 foreach ( $response['results'] as $agent ) {
897
898 if ( $roles[ $role_id ]['caps']['backend-access'] && ! $agent->user->has_cap( 'wpsc_agent' ) ) {
899 $agent->user->add_cap( 'wpsc_agent' );
900 }
901
902 if ( ! $roles[ $role_id ]['caps']['backend-access'] && $agent->user->has_cap( 'wpsc_agent' ) ) {
903 $agent->user->remove_cap( 'wpsc_agent' );
904 }
905 }
906 }
907
908 update_option( 'wpsc-agent-roles', $roles );
909 do_action( 'wpsc_agent_role_update', $role_id );
910 wp_die();
911 }
912
913 /**
914 * Delete agent role
915 */
916 public static function delete_agent_role() {
917
918 if ( check_ajax_referer( 'wpsc_delete_agent_role', '_ajax_nonce', false ) != 1 ) {
919 wp_send_json_error( 'Unauthorised request!', 401 );
920 }
921
922 if ( ! WPSC_Functions::is_site_admin() ) {
923 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
924 }
925
926 $role_id = isset( $_POST['role_id'] ) ? intval( $_POST['role_id'] ) : 0;
927 if ( ! $role_id ) {
928 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
929 }
930
931 $roles = get_option( 'wpsc-agent-roles', array() );
932 $role = isset( $roles[ $role_id ] ) ? $roles[ $role_id ] : array();
933 if ( ! $role ) {
934 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
935 }
936
937 if ( $role_id <= 2 ) {
938 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
939 }
940
941 $args = array(
942 'meta_query' => array(
943 'relation' => 'AND',
944 array(
945 'slug' => 'role',
946 'compare' => '=',
947 'val' => $role_id,
948 ),
949 array(
950 'slug' => 'is_active',
951 'compare' => '=',
952 'val' => 1,
953 ),
954 ),
955 );
956 $agents = WPSC_Agent::find( $args );
957 if ( $agents['total_items'] ) {
958 wp_send_json_error( 'The role can not be deleted if existing agents are associated with it!', 400 );
959 }
960
961 unset( $roles[ $role_id ] );
962 do_action( 'wpsc_after_delete_agent_role', $role_id );
963 update_option( 'wpsc-agent-roles', $roles );
964
965 wp_die();
966 }
967 }
968 endif;
969
970 WPSC_Agent_Roles::init();
971