PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.2
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.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 / ticket-list / class-wpsc-agent-ticket-list-settings.php

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

1,469 lines 47.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_Ticket_List_Settings' ) ) :
7
8 final class WPSC_Agent_Ticket_List_Settings {
9
10 /**
11 * Tabs for this section
12 *
13 * @var array
14 */
15 private static $tabs;
16
17 /**
18 * Current tab
19 *
20 * @var string
21 */
22 public static $current_tab;
23
24 /**
25 * Initialize this class
26 *
27 * @return void
28 */
29 public static function init() {
30
31 // Load tabs for this section.
32 add_action( 'admin_init', array( __CLASS__, 'load_tabs' ) );
33
34 // Add current tab to admin localization data.
35 add_filter( 'wpsc_admin_localizations', array( __CLASS__, 'localizations' ) );
36
37 // Agent ticket list settings.
38 add_action( 'wp_ajax_wpsc_get_agent_tl_settings', array( __CLASS__, 'get_agent_tl_settings' ) );
39
40 // Agent ticket list items.
41 add_action( 'wp_ajax_wpsc_get_agent_tl_items', array( __CLASS__, 'get_agent_tl_items' ) );
42 add_action( 'wp_ajax_wpsc_get_add_agent_tl_item', array( __CLASS__, 'get_add_agent_tl_item' ) );
43 add_action( 'wp_ajax_wpsc_set_add_agent_tl_item', array( __CLASS__, 'set_add_agent_tl_item' ) );
44 add_action( 'wp_ajax_wpsc_delete_agent_tl_item', array( __CLASS__, 'delete_agent_tl_item' ) );
45 add_action( 'wp_ajax_wpsc_get_edit_agent_tl_item', array( __CLASS__, 'get_edit_agent_tl_item' ) );
46 add_action( 'wp_ajax_wpsc_set_edit_agent_tl_item', array( __CLASS__, 'set_edit_agent_tl_item' ) );
47
48 // Agent ticket list filter items.
49 add_action( 'wp_ajax_wpsc_get_agent_filter_items', array( __CLASS__, 'get_agent_filter_items' ) );
50 add_action( 'wp_ajax_wpsc_get_add_atl_filter_item', array( __CLASS__, 'get_add_atl_filter_item' ) );
51 add_action( 'wp_ajax_wpsc_set_add_atl_filter_item', array( __CLASS__, 'set_add_atl_filter_item' ) );
52 add_action( 'wp_ajax_wpsc_delete_atl_filter_item', array( __CLASS__, 'delete_atl_filter_item' ) );
53 add_action( 'wp_ajax_wpsc_get_edit_agent_filter_item', array( __CLASS__, 'get_edit_agent_filter_item' ) );
54 add_action( 'wp_ajax_wpsc_set_edit_agent_filter_item', array( __CLASS__, 'set_edit_agent_filter_item' ) );
55
56 // Agent default filters.
57 add_action( 'wp_ajax_wpsc_get_atl_default_filters', array( __CLASS__, 'get_atl_default_filters' ) );
58 add_action( 'wp_ajax_wpsc_get_add_atl_default_filter', array( __CLASS__, 'get_add_atl_default_filter' ) );
59 add_action( 'wp_ajax_wpsc_set_add_atl_default_filter', array( __CLASS__, 'set_add_atl_default_filter' ) );
60 add_action( 'wp_ajax_wpsc_get_edit_atl_default_filter', array( __CLASS__, 'get_edit_atl_default_filter' ) );
61 add_action( 'wp_ajax_wpsc_set_edit_atl_default_filter', array( __CLASS__, 'set_edit_atl_default_filter' ) );
62 add_action( 'wp_ajax_wpsc_delete_atl_default_filter', array( __CLASS__, 'delete_atl_default_filter' ) );
63 add_action( 'wp_ajax_wpsc_sort_atl_default_filters', array( __CLASS__, 'sort_atl_default_filters' ) );
64
65 // Delete custom field actions.
66 add_action( 'wpsc_delete_custom_field', array( __CLASS__, 'delete_custom_field' ), 10, 1 );
67 }
68
69 /**
70 * Load tabs for this section
71 */
72 public static function load_tabs() {
73
74 self::$tabs = apply_filters(
75 'wpsc_ticket_list_tabs',
76 array(
77 'list-items' => array(
78 'slug' => 'list_items',
79 'label' => esc_attr__( 'List items', 'supportcandy' ),
80 'callback' => 'wpsc_get_agent_tl_items',
81 ),
82 'filter-items' => array(
83 'slug' => 'filter_items',
84 'label' => esc_attr__( 'Filter items', 'supportcandy' ),
85 'callback' => 'wpsc_get_agent_filter_items',
86 ),
87 'default-filters' => array(
88 'slug' => 'default_filters',
89 'label' => esc_attr__( 'Default filters', 'supportcandy' ),
90 'callback' => 'wpsc_get_atl_default_filters',
91 ),
92 )
93 );
94
95 self::$current_tab = isset( $_REQUEST['tab'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ) : 'list-items'; // phpcs:ignore
96 }
97
98 /**
99 * Add localizations to local JS
100 *
101 * @param array $localizations - localization list.
102 * @return array
103 */
104 public static function localizations( $localizations ) {
105
106 if ( ! ( WPSC_Ticket_List_Settings::$is_current_page && WPSC_Ticket_List_Settings::$current_section === 'agent-ticket-list' ) ) {
107 return $localizations;
108 }
109
110 // Current section.
111 $localizations['current_tab'] = self::$current_tab;
112
113 return $localizations;
114 }
115
116 /**
117 * Agent ticket list settings
118 *
119 * @return void
120 */
121 public static function get_agent_tl_settings() {
122
123 if ( ! WPSC_Functions::is_site_admin() ) {
124 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
125 }?>
126
127 <div class="wpsc-setting-tab-container">
128 <?php
129 foreach ( self::$tabs as $key => $tab ) {
130 $active = self::$current_tab === $key ? 'active' : ''
131 ?>
132 <button
133 class="<?php echo esc_attr( $key ) . ' ' . esc_attr( $active ); ?>"
134 onclick="<?php echo esc_attr( $tab['callback'] ) . '();'; ?>">
135 <?php echo esc_attr( $tab['label'] ); ?>
136 </button>
137 <?php
138 }
139 ?>
140 </div>
141 <div class="wpsc-setting-section-body"></div>
142 <?php
143 wp_die();
144 }
145
146 /**
147 * Get agent ticket list items
148 */
149 public static function get_agent_tl_items() {
150
151 if ( ! WPSC_Functions::is_site_admin() ) {
152 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
153 }
154 $list_items = get_option( 'wpsc-atl-list-items', array() );
155 ?>
156
157 <div class="wpsc-dock-container">
158 <?php
159 printf(
160 /* translators: Click here to see the documentation */
161 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
162 '<a href="https://supportcandy.net/docs/ticket-list-items/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
163 );
164 ?>
165 </div>
166 <table class="wpsc-atl wpsc-setting-tbl">
167 <thead>
168 <tr>
169 <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th>
170 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
171 </tr>
172 </thead>
173 <tbody>
174 <?php
175 foreach ( $list_items as $slug ) {
176
177 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
178 if ( ! $cf ) {
179 continue;
180 }
181 ?>
182 <tr>
183 <td><?php echo esc_attr( $cf->name ); ?></td>
184 <td>
185 <span class="wpsc-link" onclick="wpsc_get_edit_agent_tl_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_agent_tl_item' ) ); ?>');"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></span> |
186 <span class="wpsc-link" onclick="wpsc_delete_agent_tl_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_agent_tl_item' ) ); ?>');"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></span>
187 </td>
188 </tr>
189 <?php
190 }
191 ?>
192 </tbody>
193 </table>
194 <script>
195 jQuery('table.wpsc-atl').DataTable({
196 ordering: false,
197 pageLength: 20,
198 bLengthChange: false,
199 columnDefs: [
200 { targets: -1, searchable: false },
201 { targets: '_all', className: 'dt-left' }
202 ],
203 layout: {
204 topStart: {
205 buttons: [
206 {
207 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
208 className: 'wpsc-button small primary',
209 action: function ( e, dt, node, config ) {
210 wpsc_show_modal();
211 var data = { action: 'wpsc_get_add_agent_tl_item' };
212 jQuery.post(
213 supportcandy.ajax_url,
214 data,
215 function (response) {
216
217 // Set to modal.
218 jQuery( '.wpsc-modal-header' ).text( response.title );
219 jQuery( '.wpsc-modal-body' ).html( response.body );
220 jQuery( '.wpsc-modal-footer' ).html( response.footer );
221 // Display modal.
222 wpsc_show_modal_inner_container();
223 }
224 );
225 }
226 }
227 ],
228 },
229 },
230 language: supportcandy.translations.datatables
231 });
232 </script>
233 <?php
234 wp_die();
235 }
236
237 /**
238 * Get add agent ticket list items modal UI
239 *
240 * @return void
241 */
242 public static function get_add_agent_tl_item() {
243
244 if ( ! WPSC_Functions::is_site_admin() ) {
245 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
246 }
247
248 $title = esc_attr__( 'Add new list item', 'supportcandy' );
249 $custom_fields = WPSC_Custom_Field::$custom_fields;
250 $list_items = get_option( 'wpsc-atl-list-items', array() );
251
252 ob_start();
253 ?>
254 <form action="#" onsubmit="return false;" class="frm-add-agent-tl-items">
255 <div class="wpsc-input-group field-type">
256 <div class="label-container">
257 <label for="">
258 <?php esc_attr_e( 'Select fields', 'supportcandy' ); ?>
259 <span class="required-char">*</span>
260 </label>
261 </div>
262 <select multiple id="wpsc-select-agent-lt-items" name="cf_id[]">
263 <?php
264 foreach ( $custom_fields as $cf ) {
265
266 if (
267 class_exists( $cf->type ) &&
268 in_array( $cf->field, WPSC_CF_Settings::$allowed_modules['ticket-list'] ) &&
269 $cf->type::$is_list &&
270 ! in_array( $cf->slug, $list_items )
271 ) {
272 ?>
273 <option value="<?php echo esc_attr( $cf->id ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
274 <?php
275 }
276 }
277 ?>
278 </select>
279 <script>
280 jQuery('#wpsc-select-agent-lt-items').selectWoo({
281 allowClear: false,
282 placeholder: ""
283 });
284 </script>
285 </div>
286 <input type="hidden" name="action" value="wpsc_set_add_agent_tl_item">
287 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_agent_tl_item' ) ); ?>">
288 </form>
289 <?php
290 $body = ob_get_clean();
291
292 ob_start();
293 ?>
294 <button class="wpsc-button small primary" onclick="wpsc_set_add_agent_tl_item(this);">
295 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
296 </button>
297 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
298 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
299 </button>
300 <?php
301 do_action( 'wpsc_get_add_agent_tl_item' );
302 $footer = ob_get_clean();
303
304 $response = array(
305 'title' => $title,
306 'body' => $body,
307 'footer' => $footer,
308 );
309 wp_send_json( $response );
310 }
311
312 /**
313 * Set add new agent ticket list item
314 *
315 * @return void
316 */
317 public static function set_add_agent_tl_item() {
318
319 if ( check_ajax_referer( 'wpsc_set_add_agent_tl_item', '_ajax_nonce', false ) != 1 ) {
320 wp_send_json_error( 'Unauthorized request!', 401 );
321 }
322
323 if ( ! WPSC_Functions::is_site_admin() ) {
324 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
325 }
326
327 $ids = isset( $_POST['cf_id'] ) ? array_filter( array_map( 'intval', $_POST['cf_id'] ) ) : array();
328 if ( ! $ids ) {
329 wp_send_json_error( 'Bad request', 400 );
330 }
331
332 $list_items = get_option( 'wpsc-atl-list-items', array() );
333 foreach ( $ids as $id ) {
334 $cf = new WPSC_Custom_Field( $id );
335 if ( ! $cf->id || ! $cf->type::$is_list ) {
336 continue;
337 }
338 if ( ! in_array( $cf->slug, $list_items ) ) {
339 $list_items[] = $cf->slug;
340 }
341 }
342 update_option( 'wpsc-atl-list-items', $list_items );
343 wp_die();
344 }
345
346 /**
347 * Delete agent ticket list items
348 *
349 * @return void
350 */
351 public static function delete_agent_tl_item() {
352
353 if ( check_ajax_referer( 'wpsc_delete_agent_tl_item', '_ajax_nonce', false ) != 1 ) {
354 wp_send_json_error( 'Unauthorized request!', 401 );
355 }
356
357 if ( ! WPSC_Functions::is_site_admin() ) {
358 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
359 }
360
361 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : 0;
362 if ( ! $slug ) {
363 wp_send_json_error( 'Bad request', 400 );
364 }
365
366 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
367 if ( ! $cf ) {
368 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
369 }
370
371 $list_items = get_option( 'wpsc-atl-list-items', array() );
372
373 $key = array_search( $slug, $list_items );
374 if ( $key !== false ) {
375 unset( $list_items[ $key ] );
376 $list_items = array_values( $list_items );
377 update_option( 'wpsc-atl-list-items', $list_items );
378 do_action( 'wpsc_delete_agent_tl_item', $slug );
379 }
380
381 wp_die();
382 }
383
384 /**
385 * Get default filter ajax callback
386 *
387 * @return void
388 */
389 public static function get_atl_default_filters() {
390
391 if ( ! WPSC_Functions::is_site_admin() ) {
392 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
393 }
394 $default_filters = get_option( 'wpsc-atl-default-filters', array() );
395 ?>
396
397 <div class="wpsc-dock-container">
398 <?php
399 printf(
400 /* translators: Click here to see the documentation */
401 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
402 '<a href="https://supportcandy.net/docs/default-filters/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
403 );
404 ?>
405 </div>
406 <div class="wpsc-setting-cards-container ui-sortable">
407 <?php
408 foreach ( $default_filters as $key => $filter ) {
409 $style = ! $filter['is_enable'] ? 'background-color:#eec7ca;color:#dc2222' : '';
410 ?>
411 <div class="wpsc-setting-card" data-id="<?php echo esc_attr( $key ); ?>" style="<?php echo esc_attr( $style ); ?>" >
412 <span class="wpsc-sort-handle action-btn"><?php WPSC_Icons::get( 'sort' ); ?></span>
413 <span class="title">
414 <?php
415 $filter_label = $filter['label'] ? WPSC_Translations::get( 'wpsc-atl-' . $key, stripslashes( $filter['label'] ) ) : stripslashes( $filter['label'] );
416 echo esc_attr( $filter_label )
417 ?>
418 </span>
419 <div class="actions">
420 <span class="action-btn" onclick="wpsc_get_edit_atl_default_filter('<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_atl_default_filter' ) ); ?>');"><?php WPSC_Icons::get( 'edit' ); ?></span>
421 <?php
422 if ( is_numeric( $key ) ) {
423 ?>
424 <span class="action-btn" onclick="wpsc_delete_atl_default_filter(<?php echo esc_attr( $key ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_atl_default_filter' ) ); ?>');"><?php WPSC_Icons::get( 'trash-alt' ); ?></span>
425 <?php
426 }
427 ?>
428 </div>
429 </div>
430 <?php
431 }
432 ?>
433 </div>
434 <div class="setting-footer-actions">
435 <button class="wpsc-button normal primary margin-right" onclick="wpsc_get_add_atl_default_filter();"><?php esc_attr_e( 'Add new', 'supportcandy' ); ?></button>
436 <button class="wpsc-button normal secondary wpsc-save-sort-order"><?php esc_attr_e( 'Save Order', 'supportcandy' ); ?></button>
437 </div>
438 <script>
439 var items = jQuery( ".wpsc-setting-cards-container" ).sortable({ handle: '.wpsc-sort-handle' });
440 jQuery(".wpsc-save-sort-order").click(function(){
441 var slugs = items.sortable( "toArray", {attribute: 'data-id'} );
442 jQuery('.wpsc-setting-section-body').html(supportcandy.loader_html);
443 var data = { action: 'wpsc_sort_atl_default_filters', slugs, _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_sort_atl_default_filters' ) ); ?>'};
444 jQuery.post(supportcandy.ajax_url, data, function (response) {
445 wpsc_get_atl_default_filters();
446 });
447 });
448 </script>
449 <?php
450 wp_die();
451 }
452
453 /**
454 * Get add filter modal
455 *
456 * @return void
457 */
458 public static function get_add_atl_default_filter() {
459
460 if ( ! WPSC_Functions::is_site_admin() ) {
461 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
462 }
463
464 $title = esc_attr__( 'Add new default filter', 'supportcandy' );
465 $custom_fields = WPSC_Custom_Field::$custom_fields;
466 $default_filters = get_option( 'wpsc-atl-default-filters' );
467 $more_settings = get_option( 'wpsc-tl-ms-agent-view' );
468
469 ob_start();
470 ?>
471 <form action="#" onsubmit="return false;" class="wpsc-frm-add-atl-default-filter">
472 <div class="wpsc-input-group">
473 <div class="label-container">
474 <label for="">
475 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
476 <span class="required-char">*</span>
477 </label>
478 </div>
479 <input id="wpsc-atl-df-label" type="text" name="label" autocomplete="off"/>
480 </div>
481 <div class="wpsc-input-group">
482 <div class="label-container">
483 <label for="">
484 <?php esc_attr_e( 'Parent filter', 'supportcandy' ); ?>
485 <span class="required-char">*</span>
486 </label>
487 </div>
488 <select name="parent-filter">
489 <?php
490 foreach ( $default_filters as $slug => $filter ) {
491 ?>
492 <option value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $filter['label'] ); ?></option>
493 <?php
494 }
495 ?>
496 </select>
497 </div>
498 <?php WPSC_Ticket_Conditions::print( 'default_filters', 'wpsc_default_filter_conditions', '', true, __( 'Filters', 'supportcandy' ) ); ?>
499 <div class="wpsc-input-group">
500 <div class="label-container">
501 <label for="">
502 <?php esc_attr_e( 'Sort by', 'supportcandy' ); ?>
503 <span class="required-char">*</span>
504 </label>
505 </div>
506 <select name="sort-by">
507 <?php
508 foreach ( $custom_fields as $cf ) {
509 if ( $cf->type::$is_sort ) {
510 ?>
511 <option <?php selected( $more_settings['default-sort-by'], $cf->slug ); ?> value="<?php echo esc_attr( $cf->slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
512 <?php
513 }
514 }
515 ?>
516 </select>
517 </div>
518 <div class="wpsc-input-group">
519 <div class="label-container">
520 <label for="">
521 <?php esc_attr_e( 'Sort order', 'supportcandy' ); ?>
522 <span class="required-char">*</span>
523 </label>
524 </div>
525 <select name="sort-order">
526 <option <?php selected( $more_settings['default-sort-order'], 'ASC' ); ?> value="ASC"><?php esc_attr_e( 'ASC', 'supportcandy' ); ?></option>
527 <option <?php selected( $more_settings['default-sort-order'], 'DESC' ); ?> value="DESC"><?php esc_attr_e( 'DESC', 'supportcandy' ); ?></option>
528 </select>
529 </div>
530 <div class="wpsc-input-group">
531 <div class="label-container">
532 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
533 </div>
534 <select name="is_enable">
535 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
536 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
537 </select>
538 </div>
539 <input type="hidden" name="action" value="wpsc_set_add_atl_default_filter" />
540 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_atl_default_filter' ) ); ?>">
541 </form>
542 <?php
543 $body = ob_get_clean();
544
545 ob_start();
546 ?>
547 <button class="wpsc-button small primary" onclick="wpsc_set_add_atl_default_filter(this);">
548 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
549 </button>
550 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
551 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
552 </button>
553 <?php
554 $footer = ob_get_clean();
555
556 $response = array(
557 'title' => $title,
558 'body' => $body,
559 'footer' => $footer,
560 );
561 wp_send_json( $response );
562 }
563
564 /**
565 * Add default agent default filter
566 *
567 * @return void
568 */
569 public static function set_add_atl_default_filter() {
570
571 if ( check_ajax_referer( 'wpsc_set_add_atl_default_filter', '_ajax_nonce', false ) != 1 ) {
572 wp_send_json_error( 'Unauthorized request!', 401 );
573 }
574
575 if ( ! WPSC_Functions::is_site_admin() ) {
576 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
577 }
578
579 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
580 if ( ! $label ) {
581 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
582 }
583
584 $parent_filter = isset( $_POST['parent-filter'] ) ? sanitize_text_field( wp_unslash( $_POST['parent-filter'] ) ) : '';
585 if ( ! $parent_filter ) {
586 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
587 }
588
589 $filters = isset( $_POST['filters'] ) ? sanitize_text_field( wp_unslash( $_POST['filters'] ) ) : '';
590 if ( ! $filters || $filters == '[]' || ! WPSC_Ticket_Conditions::is_valid_input_conditions( 'wpsc_default_filter_conditions', $filters ) ) {
591 wp_send_json_error( 'Bad request', 400 );
592 }
593
594 $sort_by = isset( $_POST['sort-by'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-by'] ) ) : '';
595 if ( ! $sort_by ) {
596 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
597 }
598
599 $sort_order = isset( $_POST['sort-order'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-order'] ) ) : '';
600 if ( ! $sort_order ) {
601 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
602 }
603
604 $enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
605
606 $atl_filters = get_option( 'wpsc-atl-default-filters' );
607 $index = WPSC_Functions::get_tl_df_auto_increament();
608 $atl_filters[ $index ] = array(
609 'label' => $label,
610 'parent-filter' => $parent_filter,
611 'filters' => $filters,
612 'sort-by' => $sort_by,
613 'sort-order' => $sort_order,
614 'is_enable' => $enable,
615 );
616 update_option( 'wpsc-atl-default-filters', $atl_filters );
617
618 // add string translations.
619 WPSC_Translations::add( 'wpsc-atl-' . $index, $label );
620
621 wp_die();
622 }
623
624 /**
625 * Get edit filter modal
626 *
627 * @return void
628 */
629 public static function get_edit_atl_default_filter() {
630
631 if ( check_ajax_referer( 'wpsc_get_edit_atl_default_filter', '_ajax_nonce', false ) != 1 ) {
632 wp_send_json_error( 'Unauthorized request!', 401 );
633 }
634
635 if ( ! WPSC_Functions::is_site_admin() ) {
636 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
637 }
638
639 $atl_filters = get_option( 'wpsc-atl-default-filters' );
640
641 $fslug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : 0;
642 if ( ! $fslug || ! isset( $atl_filters[ $fslug ] ) ) {
643 wp_die();
644 }
645
646 $atl_filter = $atl_filters[ $fslug ];
647
648 $flag = is_numeric( $fslug ) ? true : false;
649
650 $title = esc_attr__( 'Edit default filter', 'supportcandy' );
651 $custom_fields = WPSC_Custom_Field::$custom_fields;
652
653 ob_start();
654 ?>
655 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-atl-default-filter">
656 <div class="wpsc-input-group">
657 <div class="label-container">
658 <label for="">
659 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
660 <span class="required-char">*</span>
661 </label>
662 </div>
663 <input id="wpsc-atl-df-label" type="text" name="label" value="<?php echo esc_attr( $atl_filter['label'] ); ?>" autocomplete="off"/>
664 </div>
665 <?php
666 if ( $flag ) {
667 ?>
668 <div class="wpsc-input-group">
669 <div class="label-container">
670 <label for="">
671 <?php esc_attr_e( 'Parent filter', 'supportcandy' ); ?>
672 <span class="required-char">*</span>
673 </label>
674 </div>
675 <select name="parent-filter">
676 <?php
677 foreach ( $atl_filters as $slug => $filter ) {
678 if ( $slug == $fslug ) {
679 continue;
680 }
681 ?>
682 <option <?php selected( $atl_filter['parent-filter'], $slug ); ?> value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $filter['label'] ); ?></option>
683 <?php
684 }
685 ?>
686 </select>
687 </div>
688 <?php WPSC_Ticket_Conditions::print( 'default_filters', 'wpsc_default_filter_conditions', $atl_filter['filters'], true, __( 'Filters', 'supportcandy' ) ); ?>
689 <div class="wpsc-input-group">
690 <div class="label-container">
691 <label for="">
692 <?php esc_attr_e( 'Sort by', 'supportcandy' ); ?>
693 <span class="required-char">*</span>
694 </label>
695 </div>
696 <select name="sort-by">
697 <?php
698 foreach ( $custom_fields as $cf ) :
699 if ( $cf->type::$is_sort ) :
700 ?>
701 <option <?php selected( $atl_filter['sort-by'], $cf->slug ); ?> value="<?php echo esc_attr( $cf->slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
702 <?php
703 endif;
704 endforeach;
705 ?>
706 </select>
707 </div>
708 <div class="wpsc-input-group">
709 <div class="label-container">
710 <label for="">
711 <?php esc_attr_e( 'Sort order', 'supportcandy' ); ?>
712 <span class="required-char">*</span>
713 </label>
714 </div>
715 <select name="sort-order">
716 <option <?php selected( $atl_filter['sort-order'], 'ASC' ); ?> value="ASC"><?php esc_attr_e( 'ASC', 'supportcandy' ); ?></option>
717 <option <?php selected( $atl_filter['sort-order'], 'DESC' ); ?> value="DESC"><?php esc_attr_e( 'DESC', 'supportcandy' ); ?></option>
718 </select>
719 </div>
720 <?php
721 }
722 ?>
723 <div class="wpsc-input-group">
724 <div class="label-container">
725 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
726 </div>
727 <select name="is_enable">
728 <option <?php selected( $atl_filter['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
729 <option <?php selected( $atl_filter['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
730 </select>
731 </div>
732 <input type="hidden" name="action" value="wpsc_set_edit_atl_default_filter" />
733 <input type="hidden" name="slug" value="<?php echo esc_attr( $fslug ); ?>" />
734 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_atl_default_filter' ) ); ?>">
735 </form>
736 <?php
737 $body = ob_get_clean();
738
739 ob_start();
740 ?>
741 <button class="wpsc-button small primary" onclick="wpsc_set_edit_atl_default_filter(this, <?php echo esc_attr( $flag ); ?>);">
742 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
743 </button>
744 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
745 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
746 </button>
747 <?php
748 $footer = ob_get_clean();
749
750 $response = array(
751 'title' => $title,
752 'body' => $body,
753 'footer' => $footer,
754 );
755 wp_send_json( $response );
756 }
757
758 /**
759 * Set edit agent default filter
760 *
761 * @return void
762 */
763 public static function set_edit_atl_default_filter() {
764
765 if ( check_ajax_referer( 'wpsc_set_edit_atl_default_filter', '_ajax_nonce', false ) != 1 ) {
766 wp_send_json_error( 'Unauthorized request!', 401 );
767 }
768
769 if ( ! WPSC_Functions::is_site_admin() ) {
770 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
771 }
772
773 $atl_filters = get_option( 'wpsc-atl-default-filters' );
774 $more_settings = get_option( 'wpsc-tl-ms-agent-view' );
775
776 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : 0;
777 if ( ! $slug || ! isset( $atl_filters[ $slug ] ) ) {
778 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
779 }
780
781 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
782 if ( ! $label ) {
783 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
784 }
785
786 $enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
787 if ( ! $enable && $more_settings['default-filter'] == $slug ) {
788 $enable = 1;
789 }
790
791 // remove string translations.
792 WPSC_Translations::remove( 'wpsc-atl-' . $slug );
793
794 if ( ! is_numeric( $slug ) ) {
795 $atl_filters[ $slug ] = array(
796 'label' => $label,
797 'is_enable' => $enable,
798 );
799 update_option( 'wpsc-atl-default-filters', $atl_filters );
800
801 // add string translations if not numaric.
802 WPSC_Translations::add( 'wpsc-atl-' . $slug, $label );
803
804 wp_die();
805 }
806
807 $parent_filter = isset( $_POST['parent-filter'] ) ? sanitize_text_field( wp_unslash( $_POST['parent-filter'] ) ) : '';
808 if ( ! $parent_filter ) {
809 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
810 }
811
812 $filters = isset( $_POST['filters'] ) ? sanitize_text_field( wp_unslash( $_POST['filters'] ) ) : '';
813 if ( ! $filters || $filters == '[]' || ! WPSC_Ticket_Conditions::is_valid_input_conditions( 'wpsc_default_filter_conditions', $filters ) ) {
814 wp_send_json_error( 'Bad request', 400 );
815 }
816
817 $sort_by = isset( $_POST['sort-by'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-by'] ) ) : '';
818 if ( ! $sort_by ) {
819 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
820 }
821
822 $sort_order = isset( $_POST['sort-order'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-order'] ) ) : '';
823 if ( ! $sort_order ) {
824 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
825 }
826
827 $atl_filters[ $slug ] = array(
828 'label' => $label,
829 'parent-filter' => $parent_filter,
830 'filters' => $filters,
831 'sort-by' => $sort_by,
832 'sort-order' => $sort_order,
833 'is_enable' => $enable,
834 );
835 update_option( 'wpsc-atl-default-filters', $atl_filters );
836
837 // add string translations.
838 WPSC_Translations::add( 'wpsc-atl-' . $slug, $label );
839
840 wp_die();
841 }
842
843 /**
844 * Delete agent default filter
845 *
846 * @return void
847 */
848 public static function delete_atl_default_filter() {
849
850 if ( check_ajax_referer( 'wpsc_delete_atl_default_filter', '_ajax_nonce', false ) != 1 ) {
851 wp_send_json_error( 'Unauthorized request!', 401 );
852 }
853
854 if ( ! WPSC_Functions::is_site_admin() ) {
855 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 400 );
856 }
857
858 $atl_filters = get_option( 'wpsc-atl-default-filters' );
859
860 $slug = isset( $_POST['slug'] ) ? intval( $_POST['slug'] ) : 0;
861 if ( ! $slug || ! isset( $atl_filters[ $slug ] ) ) {
862 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
863 }
864
865 unset( $atl_filters[ $slug ] );
866 update_option( 'wpsc-atl-default-filters', $atl_filters );
867
868 // remove string translations.
869 WPSC_Translations::remove( 'wpsc-atl-' . $slug );
870 wp_die();
871 }
872
873 /**
874 * Sort agent default filters
875 */
876 public static function sort_atl_default_filters() {
877
878 if ( check_ajax_referer( 'wpsc_sort_atl_default_filters', '_ajax_nonce', false ) != 1 ) {
879 wp_send_json_error( 'Unauthorized request!', 401 );
880 }
881
882 if ( ! WPSC_Functions::is_site_admin() ) {
883 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
884 }
885
886 $atl_filters = get_option( 'wpsc-atl-default-filters' );
887
888 $slugs = isset( $_POST['slugs'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['slugs'] ) ) ) : array();
889 if ( ! $slugs ) {
890 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
891 }
892
893 $temp_filters = array();
894 foreach ( $slugs as $slug ) {
895
896 if ( ! isset( $atl_filters[ $slug ] ) ) {
897 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
898 }
899 $temp_filters[ $slug ] = $atl_filters[ $slug ];
900 }
901
902 $atl_filters_keys = array_keys( $atl_filters );
903 // Verifying if slug is present in list item.
904 foreach ( $slugs as $slug ) {
905 if ( ! in_array( $slug, $atl_filters_keys ) ) {
906 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
907 }
908 }
909
910 update_option( 'wpsc-atl-default-filters', $temp_filters );
911 wp_die();
912 }
913
914 /**
915 * Get agent filter items
916 *
917 * @return void
918 */
919 public static function get_agent_filter_items() {
920
921 if ( ! WPSC_Functions::is_site_admin() ) {
922 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
923 }
924 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
925 ?>
926
927 <div class="wpsc-dock-container">
928 <?php
929 printf(
930 /* translators: Click here to see the documentation */
931 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
932 '<a href="https://supportcandy.net/docs/ticket-list-filter-items/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
933 );
934 ?>
935 </div>
936 <table class="wpsc-afl wpsc-setting-tbl">
937 <thead>
938 <tr>
939 <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th>
940 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
941 </tr>
942 </thead>
943 <tbody>
944 <?php
945 foreach ( $filter_items as $key => $slug ) {
946 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
947 if ( ! $cf ) {
948 continue;
949 }
950 ?>
951 <tr>
952 <td><?php echo esc_attr( $cf->name ); ?></td>
953 <td>
954 <a href="javascript:wpsc_get_edit_agent_filter_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_agent_filter_item' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></a> |
955 <a href="javascript:wpsc_delete_atl_filter_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_atl_filter_item' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></a>
956 </td>
957 </tr>
958 <?php
959 }
960 ?>
961 </tbody>
962 </table>
963
964 <script>
965 jQuery('table.wpsc-afl').DataTable({
966 ordering: false,
967 pageLength: 20,
968 bLengthChange: false,
969 columnDefs: [
970 { targets: -1, searchable: false },
971 { targets: '_all', className: 'dt-left' }
972 ],
973 layout: {
974 topStart: {
975 buttons: [
976 {
977 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
978 className: 'wpsc-button small primary',
979 action: function ( e, dt, node, config ) {
980 wpsc_show_modal();
981 var data = { action: 'wpsc_get_add_atl_filter_item' };
982 jQuery.post(
983 supportcandy.ajax_url,
984 data,
985 function (response) {
986
987 // Set to modal.
988 jQuery( '.wpsc-modal-header' ).text( response.title );
989 jQuery( '.wpsc-modal-body' ).html( response.body );
990 jQuery( '.wpsc-modal-footer' ).html( response.footer );
991 // Display modal.
992 wpsc_show_modal_inner_container();
993 }
994 );
995 }
996 }
997 ],
998 },
999 },
1000 language: supportcandy.translations.datatables
1001 });
1002 </script>
1003 <?php
1004 wp_die();
1005 }
1006
1007 /**
1008 * Add agent ticket list filter items modal UI
1009 *
1010 * @return void
1011 */
1012 public static function get_add_atl_filter_item() {
1013
1014 if ( ! WPSC_Functions::is_site_admin() ) {
1015 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1016 }
1017
1018 $title = esc_attr__( 'Add New Filter Item', 'supportcandy' );
1019 $custom_fields = WPSC_Custom_Field::$custom_fields;
1020 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
1021
1022 ob_start();
1023 ?>
1024 <form action="#" onsubmit="return false;" class="frm-add-agent-tl-filter-items">
1025 <div class="wpsc-input-group field-type">
1026 <div class="label-container">
1027 <label for="">
1028 <?php esc_attr_e( 'Select fields', 'supportcandy' ); ?>
1029 <span class="required-char">*</span>
1030 </label>
1031 </div>
1032 <select multiple id="wpsc-select-agent-lt-filter-items" name="agent-tl-filter-id[]">
1033 <?php
1034 foreach ( $custom_fields as $cf ) :
1035
1036 if (
1037 class_exists( $cf->type ) &&
1038 in_array( $cf->field, WPSC_CF_Settings::$allowed_modules['ticket-filter'] ) &&
1039 $cf->type::$is_filter &&
1040 ! in_array( $cf->slug, $filter_items )
1041 ) {
1042 ?>
1043 <option value="<?php echo esc_attr( $cf->id ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
1044 <?php
1045 }
1046
1047 endforeach;
1048 ?>
1049 </select>
1050 <script>
1051 jQuery('#wpsc-select-agent-lt-filter-items').selectWoo({
1052 allowClear: false,
1053 placeholder: ""
1054 });
1055 </script>
1056 </div>
1057 <input type="hidden" name="action" value="wpsc_set_add_atl_filter_item">
1058 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_atl_filter_item' ) ); ?>">
1059 </form>
1060 <?php
1061 $body = ob_get_clean();
1062
1063 ob_start();
1064 ?>
1065 <button class="wpsc-button small primary" onclick="wpsc_set_add_atl_filter_item(this);">
1066 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
1067 </button>
1068 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
1069 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
1070 </button>
1071 <?php
1072 do_action( 'wpsc_get_add_agent_filter_item' );
1073 $footer = ob_get_clean();
1074
1075 $response = array(
1076 'title' => $title,
1077 'body' => $body,
1078 'footer' => $footer,
1079 );
1080 wp_send_json( $response );
1081 }
1082
1083 /**
1084 * Set new agent ticket list filter items
1085 *
1086 * @return void
1087 */
1088 public static function set_add_atl_filter_item() {
1089
1090 if ( check_ajax_referer( 'wpsc_set_add_atl_filter_item', '_ajax_nonce', false ) != 1 ) {
1091 wp_send_json_error( 'Unauthorized request!', 401 );
1092 }
1093
1094 if ( ! WPSC_Functions::is_site_admin() ) {
1095 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1096 }
1097
1098 $ids = isset( $_POST['agent-tl-filter-id'] ) ? array_filter( array_map( 'intval', $_POST['agent-tl-filter-id'] ) ) : array();
1099 if ( ! $ids ) {
1100 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1101 }
1102
1103 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
1104 foreach ( $ids as $id ) {
1105 $cf = new WPSC_Custom_Field( $id );
1106 if ( ! $cf->id || ! $cf->type::$is_filter ) {
1107 continue;
1108 }
1109 if ( ! in_array( $cf->slug, $filter_items ) ) {
1110 $filter_items[] = $cf->slug;
1111 }
1112 }
1113 update_option( 'wpsc-atl-filter-items', $filter_items );
1114 wp_die();
1115 }
1116
1117 /**
1118 * Delete agent ticket list filter items
1119 *
1120 * @return void
1121 */
1122 public static function delete_atl_filter_item() {
1123
1124 if ( check_ajax_referer( 'wpsc_delete_atl_filter_item', '_ajax_nonce', false ) != 1 ) {
1125 wp_send_json_error( 'Unauthorized request!', 401 );
1126 }
1127
1128 if ( ! WPSC_Functions::is_site_admin() ) {
1129 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1130 }
1131
1132 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1133 if ( ! $slug ) {
1134 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1135 }
1136
1137 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
1138 if ( ! $cf ) {
1139 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1140 }
1141
1142 $more_settings = get_option( 'wpsc-tl-ms-agent-view' );
1143 if ( $more_settings['default-filter'] == $slug ) {
1144 wp_die();
1145 }
1146
1147 $filter_items = get_option( 'wpsc-atl-filter-items' );
1148 $key = array_search( $slug, $filter_items );
1149 if ( $key !== false ) {
1150 unset( $filter_items[ $key ] );
1151 $filter_items = array_values( $filter_items );
1152 update_option( 'wpsc-atl-filter-items', $filter_items );
1153 do_action( 'wpsc_delete_agent_tl_filter_item', $slug );
1154 }
1155
1156 wp_die();
1157 }
1158
1159 /**
1160 * Remove necessery options if custom field got deleted
1161 *
1162 * @param WPSC_CF $cf - custom field object.
1163 * @return void
1164 */
1165 public static function delete_custom_field( $cf ) {
1166
1167 $list_items = get_option( 'wpsc-atl-list-items', array() );
1168 $key = array_search( $cf->slug, $list_items );
1169 if ( $key !== false ) {
1170 unset( $list_items[ $key ] );
1171 update_option( 'wpsc-atl-list-items', $list_items );
1172 }
1173
1174 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
1175 $key = array_search( $cf->slug, $filter_items );
1176 if ( $key !== false ) {
1177 unset( $filter_items[ $key ] );
1178 update_option( 'wpsc-atl-filter-items', $filter_items );
1179 }
1180 }
1181
1182 /**
1183 * Get edit agent filter list items modal UI
1184 *
1185 * @return void
1186 */
1187 public static function get_edit_agent_filter_item() {
1188
1189 if ( check_ajax_referer( 'wpsc_get_edit_agent_filter_item', '_ajax_nonce', false ) != 1 ) {
1190 wp_send_json_error( 'Unauthorized request!', 401 );
1191 }
1192
1193 if ( ! WPSC_Functions::is_site_admin() ) {
1194 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1195 }
1196
1197 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1198 if ( ! $slug ) {
1199 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1200 }
1201
1202 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
1203 if ( ! $cf ) {
1204 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1205 }
1206
1207 $title = $cf->name;
1208 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
1209
1210 // calculate load order.
1211 $offset = array_search( $cf->slug, $filter_items );
1212 $load_after = $offset == 0 ? '__TOP__' : $filter_items[ $offset - 1 ];
1213
1214 ob_start();
1215 ?>
1216 <form action="#" onsubmit="return false;" class="frm-edit-agent-fl-items">
1217 <div class="wpsc-input-group">
1218 <div class="label-container">
1219 <label for=""><?php esc_attr_e( 'Load after', 'supportcandy' ); ?></label>
1220 </div>
1221 <select name="load-after" class="load-after">
1222 <option <?php selected( $load_after, '__TOP__', true ); ?> value="__TOP__">-- <?php esc_attr_e( 'TOP', 'supportcandy' ); ?> --</option>
1223 <?php
1224 foreach ( $filter_items as $slug ) {
1225 $cff = WPSC_Custom_Field::get_cf_by_slug( $slug );
1226 if ( ! $cff || $cff == $cf ) {
1227 continue;
1228 }
1229 ?>
1230 <option <?php selected( $load_after, $cff->slug, true ); ?> value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $cff->name ); ?></option>
1231 <?php
1232 }
1233 ?>
1234 <option value="__END__">-- <?php esc_attr_e( 'END', 'supportcandy' ); ?> --</option>
1235 </select>
1236 <script>jQuery('select.load-after').selectWoo();</script>
1237 </div>
1238 <input type="hidden" name="action" value="wpsc_set_edit_agent_filter_item">
1239 <input type="hidden" name="slug" value="<?php echo esc_attr( $cf->slug ); ?>">
1240 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_agent_filter_item' ) ); ?>">
1241 </form>
1242 <?php
1243 $body = ob_get_clean();
1244
1245 ob_start();
1246 ?>
1247 <button class="wpsc-button small primary" onclick="wpsc_set_edit_agent_filter_item(this);">
1248 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
1249 </button>
1250 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
1251 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
1252 </button>
1253 <?php
1254 do_action( 'wpsc_get_edit_agent_tl_item' );
1255 $footer = ob_get_clean();
1256
1257 $response = array(
1258 'title' => $title,
1259 'body' => $body,
1260 'footer' => $footer,
1261 );
1262 wp_send_json( $response );
1263 }
1264
1265 /**
1266 * Set edit agent filter list item
1267 *
1268 * @return void
1269 */
1270 public static function set_edit_agent_filter_item() {
1271
1272 if ( check_ajax_referer( 'wpsc_set_edit_agent_filter_item', '_ajax_nonce', false ) != 1 ) {
1273 wp_send_json_error( 'Unauthorized request!', 401 );
1274 }
1275
1276 if ( ! WPSC_Functions::is_site_admin() ) {
1277 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1278 }
1279
1280 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1281 if ( ! $slug ) {
1282 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1283 }
1284
1285 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
1286 if ( ! $cf ) {
1287 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1288 }
1289
1290 $load_after = isset( $_POST['load-after'] ) ? sanitize_text_field( wp_unslash( $_POST['load-after'] ) ) : '__END__';
1291
1292 $filter_items = get_option( 'wpsc-atl-filter-items', array() );
1293
1294 // unset from list so that load after should work.
1295 $filter_items = array_values( array_diff( $filter_items, array( $cf->slug ) ) );
1296
1297 // set load after.
1298 switch ( $load_after ) {
1299
1300 case '__TOP__':
1301 $filter_items = array_merge( array( $cf->slug ), $filter_items );
1302 break;
1303
1304 case '__END__':
1305 $filter_items = array_merge( $filter_items, array( $cf->slug ) );
1306 break;
1307
1308 default:
1309 $load_after = WPSC_Custom_Field::get_cf_by_slug( $load_after );
1310 if ( ! $load_after || ! in_array( $load_after->slug, $filter_items ) ) {
1311 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1312 }
1313 $offset = array_search( $load_after->slug, $filter_items ) + 1;
1314 $arr1 = array_slice( $filter_items, 0, $offset );
1315 $arr2 = array_slice( $filter_items, $offset );
1316 $filter_items = array_merge( $arr1, array( $cf->slug ), $arr2 );
1317 break;
1318 }
1319
1320 update_option( 'wpsc-atl-filter-items', $filter_items );
1321 wp_die();
1322 }
1323
1324 /**
1325 * Get edit agent ticket list items modal UI
1326 *
1327 * @return void
1328 */
1329 public static function get_edit_agent_tl_item() {
1330
1331 if ( check_ajax_referer( 'wpsc_get_edit_agent_tl_item', '_ajax_nonce', false ) != 1 ) {
1332 wp_send_json_error( 'Unauthorized request!', 401 );
1333 }
1334
1335 if ( ! WPSC_Functions::is_site_admin() ) {
1336 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1337 }
1338
1339 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1340 if ( ! $slug ) {
1341 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1342 }
1343
1344 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
1345 if ( ! $cf ) {
1346 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1347 }
1348
1349 $title = $cf->name;
1350 $list_items = get_option( 'wpsc-atl-list-items', array() );
1351
1352 // calculate load order.
1353 $offset = array_search( $cf->slug, $list_items );
1354 $load_after = $offset == 0 ? '__TOP__' : $list_items[ $offset - 1 ];
1355
1356 ob_start();
1357 ?>
1358 <form action="#" onsubmit="return false;" class="frm-edit-agent-tl-items">
1359 <div class="wpsc-input-group">
1360 <div class="label-container">
1361 <label for=""><?php esc_attr_e( 'Load after', 'supportcandy' ); ?></label>
1362 </div>
1363 <select name="load-after" class="load-after">
1364 <option <?php selected( $load_after, '__TOP__', true ); ?> value="__TOP__">-- <?php esc_attr_e( 'TOP', 'supportcandy' ); ?> --</option>
1365 <?php
1366 foreach ( $list_items as $slug ) {
1367 $cff = WPSC_Custom_Field::get_cf_by_slug( $slug );
1368 if ( ! $cff || $cff == $cf ) {
1369 continue;
1370 }
1371 ?>
1372 <option <?php selected( $load_after, $cff->slug, true ); ?> value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $cff->name ); ?></option>
1373 <?php
1374 }
1375 ?>
1376 <option value="__END__">-- <?php esc_attr_e( 'END', 'supportcandy' ); ?> --</option>
1377 </select>
1378 <script>jQuery('select.load-after').selectWoo();</script>
1379 </div>
1380 <input type="hidden" name="action" value="wpsc_set_edit_agent_tl_item">
1381 <input type="hidden" name="slug" value="<?php echo esc_attr( $cf->slug ); ?>">
1382 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_agent_tl_item' ) ); ?>">
1383 </form>
1384 <?php
1385 $body = ob_get_clean();
1386
1387 ob_start();
1388 ?>
1389 <button class="wpsc-button small primary" onclick="wpsc_set_edit_agent_tl_item(this);">
1390 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
1391 </button>
1392 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
1393 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
1394 </button>
1395 <?php
1396 do_action( 'wpsc_get_edit_agent_tl_item' );
1397 $footer = ob_get_clean();
1398
1399 $response = array(
1400 'title' => $title,
1401 'body' => $body,
1402 'footer' => $footer,
1403 );
1404 wp_send_json( $response );
1405 }
1406
1407 /**
1408 * Set edit agent ticket list item
1409 *
1410 * @return void
1411 */
1412 public static function set_edit_agent_tl_item() {
1413
1414 if ( check_ajax_referer( 'wpsc_set_edit_agent_tl_item', '_ajax_nonce', false ) != 1 ) {
1415 wp_send_json_error( 'Unauthorized request!', 401 );
1416 }
1417
1418 if ( ! WPSC_Functions::is_site_admin() ) {
1419 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1420 }
1421
1422 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1423 if ( ! $slug ) {
1424 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1425 }
1426
1427 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
1428 if ( ! $cf ) {
1429 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1430 }
1431
1432 $load_after = isset( $_POST['load-after'] ) ? sanitize_text_field( wp_unslash( $_POST['load-after'] ) ) : '__END__';
1433
1434 $list_items = get_option( 'wpsc-atl-list-items', array() );
1435
1436 // unset from list so that load after should work.
1437 $list_items = array_values( array_diff( $list_items, array( $cf->slug ) ) );
1438
1439 // set load after.
1440 switch ( $load_after ) {
1441
1442 case '__TOP__':
1443 $list_items = array_merge( array( $cf->slug ), $list_items );
1444 break;
1445
1446 case '__END__':
1447 $list_items = array_merge( $list_items, array( $cf->slug ) );
1448 break;
1449
1450 default:
1451 $load_after = WPSC_Custom_Field::get_cf_by_slug( $load_after );
1452 if ( ! $load_after || ! in_array( $load_after->slug, $list_items ) ) {
1453 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1454 }
1455 $offset = array_search( $load_after->slug, $list_items ) + 1;
1456 $arr1 = array_slice( $list_items, 0, $offset );
1457 $arr2 = array_slice( $list_items, $offset );
1458 $list_items = array_merge( $arr1, array( $cf->slug ), $arr2 );
1459 break;
1460 }
1461
1462 update_option( 'wpsc-atl-list-items', $list_items );
1463 wp_die();
1464 }
1465 }
1466 endif;
1467
1468 WPSC_Agent_Ticket_List_Settings::init();
1469