PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.4
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.4, at includes/admin/agent-settings/class-wpsc-agent-roles.php

1,011 lines 46.3 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( 'Unauthorized 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 <tr>
281 <td><label for=""><?php esc_attr_e( 'Archive tickets', 'supportcandy' ); ?></label></td>
282 <td><input name="caps[]" type="checkbox" value="at-unassigned" class="wpsc-una"></td>
283 <td><input name="caps[]" type="checkbox" value="at-assigned-me" class="wpsc-ame"></td>
284 <td><input name="caps[]" type="checkbox" value="at-assigned-others" class="wpsc-ao"></td>
285 </tr>
286
287 <?php do_action( 'wpsc_add_agent_role_ticket_permissions' ); ?>
288 </tbody>
289 </table>
290
291 <div class="wpsc-input-group" style="margin-top: 20px;">
292 <div class="label-container">
293 <label for=""><?php esc_attr_e( 'Other permissions', 'supportcandy' ); ?></label>
294 </div>
295 <div class="checkbox-group">
296 <div>
297 <input name="caps[]" type="checkbox" value="backend-access">
298 <span><?php esc_attr_e( 'WP dashboard access', 'supportcandy' ); ?></span>
299 </div>
300 <div>
301 <input name="caps[]" type="checkbox" value="create-as">
302 <span><?php esc_attr_e( 'Create ticket on others behalf', 'supportcandy' ); ?></span>
303 </div>
304 <div>
305 <input name="caps[]" type="checkbox" value="dtt-access">
306 <span><?php esc_attr_e( 'Deleted filter access', 'supportcandy' ); ?></span>
307 </div>
308 <div>
309 <input name="caps[]" type="checkbox" value="eci-access">
310 <span><?php esc_attr_e( 'Edit customer info', 'supportcandy' ); ?></span>
311 </div>
312 <div>
313 <input name="caps[]" type="checkbox" value="dash-access">
314 <span><?php esc_attr_e( 'Dashboard access', 'supportcandy' ); ?></span>
315 </div>
316 <div>
317 <input name="caps[]" type="checkbox" value="at-access">
318 <span><?php esc_attr_e( 'Archive ticket access', 'supportcandy' ); ?></span>
319 </div>
320 <div>
321 <input name="caps[]" type="checkbox" value="at-delete-access">
322 <span><?php esc_attr_e( 'Archive ticket Permanent delete access', 'supportcandy' ); ?></span>
323 </div>
324 <?php do_action( 'wpsc_add_agent_role_other_permissions' ); ?>
325 </div>
326 </div>
327 <input type="hidden" name="action" value="wpsc_set_add_agent_role">
328 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_agent_role' ) ); ?>">
329 </form>
330
331 <button class="wpsc-button small primary" onclick="wpsc_set_add_agent_role(this);">
332 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
333 </button>
334 <button class="wpsc-button small secondary" onclick="wpsc_get_agent_roles();">
335 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
336 </button>
337
338 <script>
339 // check all unassigned action.
340 jQuery('#wpsc-all-una').change(function () {
341 if( jQuery(this).is(':checked') ) {
342 jQuery(".wpsc-una").prop('checked','checked');
343 } else {
344 jQuery(".wpsc-una").removeAttr('checked');
345 }
346 });
347 jQuery(".wpsc-una").change(function() {
348 if( jQuery('.wpsc-una:checked').length == jQuery('.wpsc-una').length ) {
349 jQuery('#wpsc-all-una').prop('checked','checked');
350 }else {
351 jQuery('#wpsc-all-una').removeAttr('checked');
352 }
353 })
354
355 // check all assigned me action.
356 jQuery('#wpsc-all-ame').change(function () {
357 if( jQuery(this).is(':checked') ) {
358 jQuery(".wpsc-ame").prop('checked','checked');
359 } else {
360 jQuery(".wpsc-ame").removeAttr('checked');
361 }
362 });
363 jQuery(".wpsc-ame").change(function() {
364 if( jQuery('.wpsc-ame:checked').length == jQuery('.wpsc-ame').length ) {
365 jQuery('#wpsc-all-ame').prop('checked','checked');
366 }else {
367 jQuery('#wpsc-all-ame').removeAttr('checked');
368 }
369 })
370
371 // check all assigned to other action.
372 jQuery('#wpsc-all-ao').change(function () {
373 if( jQuery(this).is(':checked') ) {
374 jQuery(".wpsc-ao").prop('checked','checked');
375 } else {
376 jQuery(".wpsc-ao").removeAttr('checked');
377 }
378 });
379 jQuery(".wpsc-ao").change(function() {
380 if( jQuery('.wpsc-ao:checked').length == jQuery('.wpsc-ao').length ) {
381 jQuery('#wpsc-all-ao').prop('checked','checked');
382 }else {
383 jQuery('#wpsc-all-ao').removeAttr('checked');
384 }
385 })
386
387 jQuery('#wpsc_add_agent_role').DataTable({
388 ordering: false,
389 pageLength: 20,
390 bLengthChange: false,
391 columnDefs: [
392 { targets: -1, searchable: false },
393 { targets: '_all', className: 'dt-left' }
394 ],
395 language: supportcandy.translations.datatables,
396 bPaginate: false,
397 bInfo: false,
398 });
399 </script>
400 <?php
401 wp_die();
402 }
403
404 /**
405 * Set add agent role
406 *
407 * @return void
408 */
409 public static function set_add_agent_role() {
410
411 if ( check_ajax_referer( 'wpsc_set_add_agent_role', '_ajax_nonce', false ) != 1 ) {
412 wp_send_json_error( 'Unauthorized request!', 401 );
413 }
414
415 if ( ! WPSC_Functions::is_site_admin() ) {
416 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
417 }
418
419 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
420 if ( ! $label ) {
421 wp_die( 'Label not available' );
422 }
423
424 $caps = isset( $_POST['caps'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['caps'] ) ) ) : array();
425 if ( ! $caps ) {
426 wp_die( 'caps not found' );
427 }
428
429 $roles = get_option( 'wpsc-agent-roles' );
430
431 $roles[] = apply_filters(
432 'wpsc_set_add_agent_role',
433 array(
434 'label' => $label,
435 'caps' => array(
436 'backend-access' => in_array( 'backend-access', $caps ) ? true : false,
437 'create-as' => in_array( 'create-as', $caps ) ? true : false,
438 'view-unassigned' => in_array( 'view-unassigned', $caps ) ? true : false,
439 'view-assigned-me' => in_array( 'view-assigned-me', $caps ) ? true : false,
440 'view-assigned-others' => in_array( 'view-assigned-others', $caps ) ? true : false,
441 'reply-unassigned' => in_array( 'reply-unassigned', $caps ) ? true : false,
442 'reply-assigned-me' => in_array( 'reply-assigned-me', $caps ) ? true : false,
443 'reply-assigned-others' => in_array( 'reply-assigned-others', $caps ) ? true : false,
444 'pn-unassigned' => in_array( 'pn-unassigned', $caps ) ? true : false,
445 'pn-assigned-me' => in_array( 'pn-assigned-me', $caps ) ? true : false,
446 'pn-assigned-others' => in_array( 'pn-assigned-others', $caps ) ? true : false,
447 'aa-unassigned' => in_array( 'aa-unassigned', $caps ) ? true : false,
448 'aa-assigned-me' => in_array( 'aa-assigned-me', $caps ) ? true : false,
449 'aa-assigned-others' => in_array( 'aa-assigned-others', $caps ) ? true : false,
450 'cs-unassigned' => in_array( 'cs-unassigned', $caps ) ? true : false,
451 'cs-assigned-me' => in_array( 'cs-assigned-me', $caps ) ? true : false,
452 'cs-assigned-others' => in_array( 'cs-assigned-others', $caps ) ? true : false,
453 'ctf-unassigned' => in_array( 'ctf-unassigned', $caps ) ? true : false,
454 'ctf-assigned-me' => in_array( 'ctf-assigned-me', $caps ) ? true : false,
455 'ctf-assigned-others' => in_array( 'ctf-assigned-others', $caps ) ? true : false,
456 'caof-unassigned' => in_array( 'caof-unassigned', $caps ) ? true : false,
457 'caof-assigned-me' => in_array( 'caof-assigned-me', $caps ) ? true : false,
458 'caof-assigned-others' => in_array( 'caof-assigned-others', $caps ) ? true : false,
459 'crb-unassigned' => in_array( 'crb-unassigned', $caps ) ? true : false,
460 'crb-assigned-me' => in_array( 'crb-assigned-me', $caps ) ? true : false,
461 'crb-assigned-others' => in_array( 'crb-assigned-others', $caps ) ? true : false,
462 'eth-unassigned' => in_array( 'eth-unassigned', $caps ) ? true : false,
463 'eth-assigned-me' => in_array( 'eth-assigned-me', $caps ) ? true : false,
464 'eth-assigned-others' => in_array( 'eth-assigned-others', $caps ) ? true : false,
465 'dth-unassigned' => in_array( 'dth-unassigned', $caps ) ? true : false,
466 'dth-assigned-me' => in_array( 'dth-assigned-me', $caps ) ? true : false,
467 'dth-assigned-others' => in_array( 'dth-assigned-others', $caps ) ? true : false,
468 'vl-unassigned' => in_array( 'vl-unassigned', $caps ) ? true : false,
469 'vl-assigned-me' => in_array( 'vl-assigned-me', $caps ) ? true : false,
470 'vl-assigned-others' => in_array( 'vl-assigned-others', $caps ) ? true : false,
471 'dtt-unassigned' => in_array( 'dtt-unassigned', $caps ) ? true : false,
472 'dtt-assigned-me' => in_array( 'dtt-assigned-me', $caps ) ? true : false,
473 'dtt-assigned-others' => in_array( 'dtt-assigned-others', $caps ) ? true : false,
474 'dtt-access' => in_array( 'dtt-access', $caps ) ? true : false,
475 'dt-unassigned' => in_array( 'dt-unassigned', $caps ) ? true : false,
476 'dt-assigned-me' => in_array( 'dt-assigned-me', $caps ) ? true : false,
477 'dt-assigned-others' => in_array( 'dt-assigned-others', $caps ) ? true : false,
478 'ar-unassigned' => in_array( 'ar-unassigned', $caps ) ? true : false,
479 'ar-assigned-me' => in_array( 'ar-assigned-me', $caps ) ? true : false,
480 'ar-assigned-others' => in_array( 'ar-assigned-others', $caps ) ? true : false,
481 'eci-access' => in_array( 'eci-access', $caps ) ? true : false,
482 'dash-access' => in_array( 'dash-access', $caps ) ? true : false,
483 'tt-unassigned' => in_array( 'tt-unassigned', $caps ) ? true : false,
484 'tt-assigned-me' => in_array( 'tt-assigned-me', $caps ) ? true : false,
485 'tt-assigned-others' => in_array( 'tt-assigned-others', $caps ) ? true : false,
486 'at-unassigned' => in_array( 'at-unassigned', $caps ) ? true : false,
487 'at-assigned-me' => in_array( 'at-assigned-me', $caps ) ? true : false,
488 'at-assigned-others' => in_array( 'at-assigned-others', $caps ) ? true : false,
489 'at-access' => in_array( 'at-access', $caps ) ? true : false,
490 'at-delete-access' => in_array( 'at-delete-access', $caps ) ? true : false,
491
492 ),
493 ),
494 $caps
495 );
496
497 update_option( 'wpsc-agent-roles', $roles );
498
499 do_action( 'wpsc_after_add_agent_role', array_key_last( $roles ) );
500 wp_die();
501 }
502
503 /**
504 * Clone existing agent role.
505 *
506 * @return void
507 */
508 public static function get_clone_agent_role() {
509
510 if ( check_ajax_referer( 'wpsc_get_clone_agent_role', '_ajax_nonce', false ) != 1 ) {
511 wp_send_json_error( 'Unauthorized request!', 400 );
512 }
513
514 if ( ! WPSC_Functions::is_site_admin() ) {
515 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
516 }
517
518 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
519 if ( ! $id ) {
520 wp_send_json_error( 'Incorrect request!', 400 );
521 }
522
523 $roles = get_option( 'wpsc-agent-roles', array() );
524 $roles[] = array(
525 'label' => $roles[ $id ]['label'] . ' clone',
526 'caps' => $roles[ $id ]['caps'],
527 );
528
529 update_option( 'wpsc-agent-roles', $roles );
530
531 wp_die();
532 }
533
534 /**
535 * Edit agent role UI
536 *
537 * @return void
538 */
539 public static function get_edit_agent_role() {
540
541 if ( check_ajax_referer( 'wpsc_get_edit_agent_role', '_ajax_nonce', false ) != 1 ) {
542 wp_send_json_error( 'Unauthorized request!', 401 );
543 }
544
545 if ( ! WPSC_Functions::is_site_admin() ) {
546 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
547 }
548
549 $role_id = isset( $_POST['role_id'] ) ? intval( $_POST['role_id'] ) : 0;
550 if ( ! $role_id ) {
551 wp_die( 'role_id not found!' );
552 }
553
554 $roles = get_option( 'wpsc-agent-roles', array() );
555 $role = isset( $roles[ $role_id ] ) ? $roles[ $role_id ] : array();
556 if ( ! $role ) {
557 wp_die( 'Role not available!' );
558 }
559
560 ?>
561 <form action="#" onsubmit="return false;" class="frm-edit-agent-role">
562
563 <div class="wpsc-input-group">
564 <div class="label-container">
565 <label for="">
566 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
567 <span class="required-char">*</span>
568 </label>
569 </div>
570 <input id="label" type="text" name="label" autocomplete="off" value="<?php echo esc_attr( $role['label'] ); ?>">
571 </div>
572
573 <table id="wpsc_edit_agent_role">
574 <thead>
575 <tr>
576 <th><?php esc_attr_e( 'Capability', 'supportcandy' ); ?></th>
577 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-una"><?php esc_attr_e( 'Unassigned', 'supportcandy' ); ?></th>
578 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-ame"><?php esc_attr_e( 'Assigned to me', 'supportcandy' ); ?></th>
579 <th style="padding: 10px;"><input type="checkbox" id="wpsc-all-ao"><?php esc_attr_e( 'Assigned to others', 'supportcandy' ); ?></th>
580 </tr>
581 </thead>
582 <tbody>
583 <tr>
584 <td><label for=""><?php esc_attr_e( 'View Tickets', 'supportcandy' ); ?></label></td>
585 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['view-unassigned'], 1 ); ?> value="view-unassigned" class="wpsc-una"></td>
586 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['view-assigned-me'], 1 ); ?> value="view-assigned-me" class="wpsc-ame"></td>
587 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['view-assigned-others'], 1 ); ?> value="view-assigned-others" class="wpsc-ao"></td>
588 </tr>
589
590 <tr>
591 <td><label for=""><?php esc_attr_e( 'Reply Tickets', 'supportcandy' ); ?></label></td>
592 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['reply-unassigned'], 1 ); ?> value="reply-unassigned" class="wpsc-una"></td>
593 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['reply-assigned-me'], 1 ); ?> value="reply-assigned-me" class="wpsc-ame"></td>
594 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['reply-assigned-others'], 1 ); ?> value="reply-assigned-others" class="wpsc-ao"></td>
595 </tr>
596
597 <tr>
598 <td><label for=""><?php esc_attr_e( 'Private Notes', 'supportcandy' ); ?></label></td>
599 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['pn-unassigned'], 1 ); ?> value="pn-unassigned" class="wpsc-una"></td>
600 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['pn-assigned-me'], 1 ); ?> value="pn-assigned-me" class="wpsc-ame"></td>
601 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['pn-assigned-others'], 1 ); ?> value="pn-assigned-others" class="wpsc-ao"></td>
602 </tr>
603
604 <tr>
605 <td><label for=""><?php esc_attr_e( 'Duplicate Ticket', 'supportcandy' ); ?></label></td>
606 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dt-unassigned'], 1 ); ?> value="dt-unassigned" class="wpsc-una"></td>
607 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dt-assigned-me'], 1 ); ?> value="dt-assigned-me" class="wpsc-ame"></td>
608 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dt-assigned-others'], 1 ); ?> value="dt-assigned-others" class="wpsc-ao"></td>
609 </tr>
610
611 <tr>
612 <td><label for=""><?php esc_attr_e( 'Assign Agents', 'supportcandy' ); ?></label></td>
613 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['aa-unassigned'], 1 ); ?> value="aa-unassigned" class="wpsc-una"></td>
614 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['aa-assigned-me'], 1 ); ?> value="aa-assigned-me" class="wpsc-ame"></td>
615 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['aa-assigned-others'], 1 ); ?> value="aa-assigned-others" class="wpsc-ao"></td>
616 </tr>
617
618 <tr>
619 <td><label for=""><?php esc_attr_e( 'Change Status', 'supportcandy' ); ?></label></td>
620 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['cs-unassigned'], 1 ); ?> value="cs-unassigned" class="wpsc-una"></td>
621 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['cs-assigned-me'], 1 ); ?> value="cs-assigned-me" class="wpsc-ame"></td>
622 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['cs-assigned-others'], 1 ); ?> value="cs-assigned-others" class="wpsc-ao"></td>
623 </tr>
624
625 <tr>
626 <td><label for=""><?php esc_attr_e( 'Change Ticket Fields', 'supportcandy' ); ?></label></td>
627 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ctf-unassigned'], 1 ); ?> value="ctf-unassigned" class="wpsc-una"></td>
628 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ctf-assigned-me'], 1 ); ?> value="ctf-assigned-me" class="wpsc-ame"></td>
629 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ctf-assigned-others'], 1 ); ?> value="ctf-assigned-others" class="wpsc-ao"></td>
630 </tr>
631
632 <tr>
633 <td><label for=""><?php esc_attr_e( 'Change Agent Only Fields', 'supportcandy' ); ?></label></td>
634 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['caof-unassigned'], 1 ); ?> value="caof-unassigned" class="wpsc-una"></td>
635 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['caof-assigned-me'], 1 ); ?> value="caof-assigned-me" class="wpsc-ame"></td>
636 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['caof-assigned-others'], 1 ); ?> value="caof-assigned-others" class="wpsc-ao"></td>
637 </tr>
638
639 <tr>
640 <td><label for=""><?php esc_attr_e( 'Change Raised By', 'supportcandy' ); ?></label></td>
641 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['crb-unassigned'], 1 ); ?> value="crb-unassigned" class="wpsc-una"></td>
642 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['crb-assigned-me'], 1 ); ?> value="crb-assigned-me" class="wpsc-ame"></td>
643 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['crb-assigned-others'], 1 ); ?> value="crb-assigned-others" class="wpsc-ao"></td>
644 </tr>
645
646 <tr>
647 <td><label for=""><?php esc_attr_e( 'Modify Additional Recipients', 'supportcandy' ); ?></label></td>
648 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ar-unassigned'], 1 ); ?> value="ar-unassigned" class="wpsc-una"></td>
649 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ar-assigned-me'], 1 ); ?> value="ar-assigned-me" class="wpsc-ame"></td>
650 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['ar-assigned-others'], 1 ); ?> value="ar-assigned-others" class="wpsc-ao"></td>
651 </tr>
652
653 <tr>
654 <td><label for=""><?php esc_attr_e( 'Edit Threads', 'supportcandy' ); ?></label></td>
655 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['eth-unassigned'], 1 ); ?> value="eth-unassigned" class="wpsc-una"></td>
656 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['eth-assigned-me'], 1 ); ?> value="eth-assigned-me" class="wpsc-ame"></td>
657 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['eth-assigned-others'], 1 ); ?> value="eth-assigned-others" class="wpsc-ao"></td>
658 </tr>
659
660 <tr>
661 <td><label for=""><?php esc_attr_e( 'Delete Threads', 'supportcandy' ); ?></label></td>
662 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dth-unassigned'], 1 ); ?> value="dth-unassigned" class="wpsc-una"></td>
663 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dth-assigned-me'], 1 ); ?> value="dth-assigned-me" class="wpsc-ame"></td>
664 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dth-assigned-others'], 1 ); ?> value="dth-assigned-others" class="wpsc-ao"></td>
665 </tr>
666
667 <tr>
668 <td><label for=""><?php esc_attr_e( 'View Logs', 'supportcandy' ); ?></label></td>
669 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['vl-unassigned'], 1 ); ?> value="vl-unassigned" class="wpsc-una"></td>
670 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['vl-assigned-me'], 1 ); ?> value="vl-assigned-me" class="wpsc-ame"></td>
671 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['vl-assigned-others'], 1 ); ?> value="vl-assigned-others" class="wpsc-ao"></td>
672 </tr>
673
674 <tr>
675 <td><label for=""><?php esc_attr_e( 'Delete Ticket', 'supportcandy' ); ?></label></td>
676 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-unassigned'], 1 ); ?> value="dtt-unassigned" class="wpsc-una"></td>
677 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-assigned-me'], 1 ); ?> value="dtt-assigned-me" class="wpsc-ame"></td>
678 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-assigned-others'], 1 ); ?> value="dtt-assigned-others" class="wpsc-ao"></td>
679 </tr>
680
681 <tr>
682 <td><label for=""><?php esc_attr_e( 'Modify Tags', 'supportcandy' ); ?></label></td>
683 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['tt-unassigned'], 1 ); ?> value="tt-unassigned" class="wpsc-una"></td>
684 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['tt-assigned-me'], 1 ); ?> value="tt-assigned-me" class="wpsc-ame"></td>
685 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['tt-assigned-others'], 1 ); ?> value="tt-assigned-others" class="wpsc-ao"></td>
686 </tr>
687
688 <tr>
689 <td><label for=""><?php esc_attr_e( 'Archive ticket', 'supportcandy' ); ?></label></td>
690 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['at-unassigned'], 1 ); ?> value="at-unassigned" class="wpsc-una"></td>
691 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['at-assigned-me'], 1 ); ?> value="at-assigned-me" class="wpsc-ame"></td>
692 <td><input name="caps[]" type="checkbox" <?php checked( $role['caps']['at-assigned-others'], 1 ); ?> value="at-assigned-others" class="wpsc-ao"></td>
693 </tr>
694 <?php do_action( 'wpsc_edit_agent_role_ticket_permissions', $role ); ?>
695 </tbody>
696 </table>
697 <div class="wpsc-input-group" style="margin-top: 20px;">
698 <div class="label-container">
699 <label for=""><?php esc_attr_e( 'Other permissions', 'supportcandy' ); ?></label>
700 </div>
701 <div class="checkbox-group">
702 <div>
703 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['backend-access'], 1 ); ?> value="backend-access">
704 <span><?php esc_attr_e( 'WP dashboard access', 'supportcandy' ); ?></span>
705 </div>
706 <div>
707 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['create-as'], 1 ); ?> value="create-as">
708 <span><?php esc_attr_e( 'Create ticket on others behalf', 'supportcandy' ); ?></span>
709 </div>
710 <div>
711 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['dtt-access'], 1 ); ?> value="dtt-access">
712 <span><?php esc_attr_e( 'Deleted filter access', 'supportcandy' ); ?></span>
713 </div>
714 <div>
715 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['eci-access'], 1 ); ?> value="eci-access">
716 <span><?php esc_attr_e( 'Edit customer info', 'supportcandy' ); ?></span>
717 </div>
718 <div>
719 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['dash-access'], 1 ); ?> value="dash-access">
720 <span><?php esc_attr_e( 'Dashboard access', 'supportcandy' ); ?></span>
721 </div>
722 <div>
723 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['at-access'], 1 ); ?> value="at-access">
724 <span><?php esc_attr_e( 'Archive ticket access', 'supportcandy' ); ?></span>
725 </div>
726 <div>
727 <input name="caps[]" type="checkbox" <?php checked( $role['caps']['at-delete-access'], 1 ); ?> value="at-delete-access">
728 <span><?php esc_attr_e( 'Archive ticket Permanent delete access', 'supportcandy' ); ?></span>
729 </div>
730 <?php do_action( 'wpsc_edit_agent_role_other_permissions', $role ); ?>
731 </div>
732 </div>
733 <input type="hidden" name="role_id" value="<?php echo esc_attr( $role_id ); ?>">
734 <input type="hidden" name="action" value="wpsc_set_edit_agent_role">
735 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_agent_role' ) ); ?>">
736 </form>
737 <button class="wpsc-button small primary" onclick="wpsc_set_edit_agent_role();">
738 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
739 </button>
740 <button class="wpsc-button small secondary" onclick="wpsc_get_agent_roles();">
741 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
742 </button>
743
744 <script>
745 // check all unassigned action.
746 jQuery('#wpsc-all-una').change(function () {
747 if( jQuery(this).is(':checked') ) {
748 jQuery(".wpsc-una").prop('checked','checked');
749 } else {
750 jQuery(".wpsc-una").removeAttr('checked');
751 }
752 });
753 jQuery(".wpsc-una").change(function() {
754 if( jQuery('.wpsc-una:checked').length == jQuery('.wpsc-una').length ) {
755 jQuery('#wpsc-all-una').prop('checked','checked');
756 }else {
757 jQuery('#wpsc-all-una').removeAttr('checked');
758 }
759 });
760 jQuery(".wpsc-una").trigger("change");
761
762 // check all assigned me action.
763 jQuery('#wpsc-all-ame').change(function () {
764 if( jQuery(this).is(':checked') ) {
765 jQuery(".wpsc-ame").prop('checked','checked');
766 } else {
767 jQuery(".wpsc-ame").removeAttr('checked');
768 }
769 });
770 jQuery(".wpsc-ame").change(function() {
771 if( jQuery('.wpsc-ame:checked').length == jQuery('.wpsc-ame').length ) {
772 jQuery('#wpsc-all-ame').prop('checked','checked');
773 }else {
774 jQuery('#wpsc-all-ame').removeAttr('checked');
775 }
776 });
777 jQuery(".wpsc-ame").trigger('change');
778
779 // check all assigned to other action.
780 jQuery('#wpsc-all-ao').change(function () {
781 if( jQuery(this).is(':checked') ) {
782 jQuery(".wpsc-ao").prop('checked','checked');
783 } else {
784 jQuery(".wpsc-ao").removeAttr('checked');
785 }
786 });
787 jQuery(".wpsc-ao").change(function() {
788 if( jQuery('.wpsc-ao:checked').length == jQuery('.wpsc-ao').length ) {
789 jQuery('#wpsc-all-ao').prop('checked','checked');
790 }else {
791 jQuery('#wpsc-all-ao').removeAttr('checked');
792 }
793 });
794 jQuery(".wpsc-ao").trigger('change');
795
796 jQuery('#wpsc_edit_agent_role').DataTable({
797 ordering: false,
798 pageLength: 20,
799 bLengthChange: false,
800 columnDefs: [
801 { targets: -1, searchable: false },
802 { targets: '_all', className: 'dt-left' }
803 ],
804 language: supportcandy.translations.datatables,
805 bPaginate: false,
806 bInfo: false,
807 });
808 </script>
809 <?php
810 wp_die();
811 }
812
813 /**
814 * Set edit agent role
815 *
816 * @return void
817 */
818 public static function set_edit_agent_role() {
819
820 if ( check_ajax_referer( 'wpsc_set_edit_agent_role', '_ajax_nonce', false ) != 1 ) {
821 wp_send_json_error( 'Unauthorized request!', 401 );
822 }
823
824 if ( ! WPSC_Functions::is_site_admin() ) {
825 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
826 }
827
828 $role_id = isset( $_POST['role_id'] ) ? intval( $_POST['role_id'] ) : 0;
829 if ( ! $role_id ) {
830 wp_die( 'role_id not found!' );
831 }
832
833 $roles = get_option( 'wpsc-agent-roles', array() );
834 $role = isset( $roles[ $role_id ] ) ? $roles[ $role_id ] : array();
835 if ( ! $role ) {
836 wp_die( 'Role not available!' );
837 }
838
839 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
840 if ( ! $label ) {
841 wp_die( 'Label not available' );
842 }
843
844 $caps = isset( $_POST['caps'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['caps'] ) ) ) : array();
845 if ( ! $caps ) {
846 wp_die( 'caps not found' );
847 }
848
849 $roles[ $role_id ] = apply_filters(
850 'wpsc_set_edit_agent_role',
851 array(
852 'label' => $label,
853 'caps' => array(
854 'backend-access' => in_array( 'backend-access', $caps ) ? true : false,
855 'create-as' => in_array( 'create-as', $caps ) ? true : false,
856 'view-unassigned' => in_array( 'view-unassigned', $caps ) ? true : false,
857 'view-assigned-me' => in_array( 'view-assigned-me', $caps ) ? true : false,
858 'view-assigned-others' => in_array( 'view-assigned-others', $caps ) ? true : false,
859 'reply-unassigned' => in_array( 'reply-unassigned', $caps ) ? true : false,
860 'reply-assigned-me' => in_array( 'reply-assigned-me', $caps ) ? true : false,
861 'reply-assigned-others' => in_array( 'reply-assigned-others', $caps ) ? true : false,
862 'pn-unassigned' => in_array( 'pn-unassigned', $caps ) ? true : false,
863 'pn-assigned-me' => in_array( 'pn-assigned-me', $caps ) ? true : false,
864 'pn-assigned-others' => in_array( 'pn-assigned-others', $caps ) ? true : false,
865 'aa-unassigned' => in_array( 'aa-unassigned', $caps ) ? true : false,
866 'aa-assigned-me' => in_array( 'aa-assigned-me', $caps ) ? true : false,
867 'aa-assigned-others' => in_array( 'aa-assigned-others', $caps ) ? true : false,
868 'cs-unassigned' => in_array( 'cs-unassigned', $caps ) ? true : false,
869 'cs-assigned-me' => in_array( 'cs-assigned-me', $caps ) ? true : false,
870 'cs-assigned-others' => in_array( 'cs-assigned-others', $caps ) ? true : false,
871 'ctf-unassigned' => in_array( 'ctf-unassigned', $caps ) ? true : false,
872 'ctf-assigned-me' => in_array( 'ctf-assigned-me', $caps ) ? true : false,
873 'ctf-assigned-others' => in_array( 'ctf-assigned-others', $caps ) ? true : false,
874 'caof-unassigned' => in_array( 'caof-unassigned', $caps ) ? true : false,
875 'caof-assigned-me' => in_array( 'caof-assigned-me', $caps ) ? true : false,
876 'caof-assigned-others' => in_array( 'caof-assigned-others', $caps ) ? true : false,
877 'crb-unassigned' => in_array( 'crb-unassigned', $caps ) ? true : false,
878 'crb-assigned-me' => in_array( 'crb-assigned-me', $caps ) ? true : false,
879 'crb-assigned-others' => in_array( 'crb-assigned-others', $caps ) ? true : false,
880 'eth-unassigned' => in_array( 'eth-unassigned', $caps ) ? true : false,
881 'eth-assigned-me' => in_array( 'eth-assigned-me', $caps ) ? true : false,
882 'eth-assigned-others' => in_array( 'eth-assigned-others', $caps ) ? true : false,
883 'dth-unassigned' => in_array( 'dth-unassigned', $caps ) ? true : false,
884 'dth-assigned-me' => in_array( 'dth-assigned-me', $caps ) ? true : false,
885 'dth-assigned-others' => in_array( 'dth-assigned-others', $caps ) ? true : false,
886 'vl-unassigned' => in_array( 'vl-unassigned', $caps ) ? true : false,
887 'vl-assigned-me' => in_array( 'vl-assigned-me', $caps ) ? true : false,
888 'vl-assigned-others' => in_array( 'vl-assigned-others', $caps ) ? true : false,
889 'dtt-unassigned' => in_array( 'dtt-unassigned', $caps ) ? true : false,
890 'dtt-assigned-me' => in_array( 'dtt-assigned-me', $caps ) ? true : false,
891 'dtt-assigned-others' => in_array( 'dtt-assigned-others', $caps ) ? true : false,
892 'dtt-access' => in_array( 'dtt-access', $caps ) ? true : false,
893 'dt-unassigned' => in_array( 'dt-unassigned', $caps ) ? true : false,
894 'dt-assigned-me' => in_array( 'dt-assigned-me', $caps ) ? true : false,
895 'dt-assigned-others' => in_array( 'dt-assigned-others', $caps ) ? true : false,
896 'ar-unassigned' => in_array( 'ar-unassigned', $caps ) ? true : false,
897 'ar-assigned-me' => in_array( 'ar-assigned-me', $caps ) ? true : false,
898 'ar-assigned-others' => in_array( 'ar-assigned-others', $caps ) ? true : false,
899 'eci-access' => in_array( 'eci-access', $caps ) ? true : false,
900 'dash-access' => in_array( 'dash-access', $caps ) ? true : false,
901 'tt-unassigned' => in_array( 'tt-unassigned', $caps ) ? true : false,
902 'tt-assigned-me' => in_array( 'tt-assigned-me', $caps ) ? true : false,
903 'tt-assigned-others' => in_array( 'tt-assigned-others', $caps ) ? true : false,
904 'at-unassigned' => in_array( 'at-unassigned', $caps ) ? true : false,
905 'at-assigned-me' => in_array( 'at-assigned-me', $caps ) ? true : false,
906 'at-assigned-others' => in_array( 'at-assigned-others', $caps ) ? true : false,
907 'at-access' => in_array( 'at-access', $caps ) ? true : false,
908 'at-delete-access' => in_array( 'at-delete-access', $caps ) ? true : false,
909 ),
910 ),
911 $role,
912 $caps
913 );
914
915 // Update backend access user capabilities.
916 if ( $role['caps']['backend-access'] !== $roles[ $role_id ]['caps']['backend-access'] ) {
917
918 $response = WPSC_Agent::find(
919 array(
920 'items_per_page' => 0,
921 'meta_query' => array(
922 'relation' => 'AND',
923 array(
924 'slug' => 'role',
925 'compare' => '=',
926 'val' => $role_id,
927 ),
928 array(
929 'slug' => 'is_active',
930 'compare' => '=',
931 'val' => 1,
932 ),
933 ),
934 )
935 );
936 foreach ( $response['results'] as $agent ) {
937
938 if ( $roles[ $role_id ]['caps']['backend-access'] && ! $agent->user->has_cap( 'wpsc_agent' ) ) {
939 $agent->user->add_cap( 'wpsc_agent' );
940 }
941
942 if ( ! $roles[ $role_id ]['caps']['backend-access'] && $agent->user->has_cap( 'wpsc_agent' ) ) {
943 $agent->user->remove_cap( 'wpsc_agent' );
944 }
945 }
946 }
947
948 update_option( 'wpsc-agent-roles', $roles );
949 do_action( 'wpsc_agent_role_update', $role_id );
950 wp_die();
951 }
952
953 /**
954 * Delete agent role
955 */
956 public static function delete_agent_role() {
957
958 if ( check_ajax_referer( 'wpsc_delete_agent_role', '_ajax_nonce', false ) != 1 ) {
959 wp_send_json_error( 'Unauthorized request!', 401 );
960 }
961
962 if ( ! WPSC_Functions::is_site_admin() ) {
963 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
964 }
965
966 $role_id = isset( $_POST['role_id'] ) ? intval( $_POST['role_id'] ) : 0;
967 if ( ! $role_id ) {
968 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
969 }
970
971 $roles = get_option( 'wpsc-agent-roles', array() );
972 $role = isset( $roles[ $role_id ] ) ? $roles[ $role_id ] : array();
973 if ( ! $role ) {
974 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
975 }
976
977 if ( $role_id <= 2 ) {
978 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
979 }
980
981 $args = array(
982 'meta_query' => array(
983 'relation' => 'AND',
984 array(
985 'slug' => 'role',
986 'compare' => '=',
987 'val' => $role_id,
988 ),
989 array(
990 'slug' => 'is_active',
991 'compare' => '=',
992 'val' => 1,
993 ),
994 ),
995 );
996 $agents = WPSC_Agent::find( $args );
997 if ( $agents['total_items'] ) {
998 wp_send_json_error( 'The role can not be deleted if existing agents are associated with it!', 400 );
999 }
1000
1001 unset( $roles[ $role_id ] );
1002 do_action( 'wpsc_after_delete_agent_role', $role_id );
1003 update_option( 'wpsc-agent-roles', $roles );
1004
1005 wp_die();
1006 }
1007 }
1008 endif;
1009
1010 WPSC_Agent_Roles::init();
1011