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

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