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 / ticket-list / class-wpsc-customer-ticket-list-settings.php

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

1,470 lines 48.1 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_Customer_Ticket_List_Settings' ) ) :
7
8 final class WPSC_Customer_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 // customer ticket list settings.
38 add_action( 'wp_ajax_wpsc_get_customer_tl_settings', array( __CLASS__, 'get_customer_tl_settings' ) );
39
40 // customer ticket list items.
41 add_action( 'wp_ajax_wpsc_get_customer_tl_items', array( __CLASS__, 'get_customer_tl_items' ) );
42 add_action( 'wp_ajax_wpsc_get_add_customer_tl_item', array( __CLASS__, 'get_add_customer_tl_item' ) );
43 add_action( 'wp_ajax_wpsc_set_add_customer_tl_item', array( __CLASS__, 'set_add_customer_tl_item' ) );
44 add_action( 'wp_ajax_wpsc_get_edit_customer_tl_item', array( __CLASS__, 'get_edit_customer_tl_item' ) );
45 add_action( 'wp_ajax_wpsc_set_edit_customer_tl_item', array( __CLASS__, 'set_edit_customer_tl_item' ) );
46 add_action( 'wp_ajax_wpsc_delete_customer_tl_item', array( __CLASS__, 'delete_customer_tl_item' ) );
47
48 // customer ticket list filter items.
49 add_action( 'wp_ajax_wpsc_get_customer_filter_items', array( __CLASS__, 'get_customer_filter_items' ) );
50 add_action( 'wp_ajax_wpsc_get_add_ctl_filter_item', array( __CLASS__, 'get_add_ctl_filter_item' ) );
51 add_action( 'wp_ajax_wpsc_set_add_ctl_filter_item', array( __CLASS__, 'set_add_ctl_filter_item' ) );
52 add_action( 'wp_ajax_wpsc_get_edit_ctl_filter_item', array( __CLASS__, 'get_edit_ctl_filter_item' ) );
53 add_action( 'wp_ajax_wpsc_set_edit_ctl_filter_item', array( __CLASS__, 'set_edit_ctl_filter_item' ) );
54 add_action( 'wp_ajax_wpsc_delete_ctl_filter_item', array( __CLASS__, 'delete_ctl_filter_item' ) );
55
56 // customer default filters.
57 add_action( 'wp_ajax_wpsc_get_ctl_default_filters', array( __CLASS__, 'get_ctl_default_filters' ) );
58 add_action( 'wp_ajax_wpsc_get_add_ctl_default_filter', array( __CLASS__, 'get_add_ctl_default_filter' ) );
59 add_action( 'wp_ajax_wpsc_set_add_ctl_default_filter', array( __CLASS__, 'set_add_ctl_default_filter' ) );
60 add_action( 'wp_ajax_wpsc_get_edit_ctl_default_filter', array( __CLASS__, 'get_edit_ctl_default_filter' ) );
61 add_action( 'wp_ajax_wpsc_set_edit_ctl_default_filter', array( __CLASS__, 'set_edit_ctl_default_filter' ) );
62 add_action( 'wp_ajax_wpsc_delete_ctl_default_filter', array( __CLASS__, 'delete_ctl_default_filter' ) );
63 add_action( 'wp_ajax_wpsc_sort_ctl_default_filters', array( __CLASS__, 'sort_ctl_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_customer_ticket_list_tabs',
76 array(
77 'list-items' => array(
78 'slug' => 'list_items',
79 'label' => esc_attr__( 'List items', 'supportcandy' ),
80 'callback' => 'wpsc_get_customer_tl_items',
81 ),
82 'filter-items' => array(
83 'slug' => 'filter_items',
84 'label' => esc_attr__( 'Filter items', 'supportcandy' ),
85 'callback' => 'wpsc_get_customer_filter_items',
86 ),
87 'default-filters' => array(
88 'slug' => 'default_filters',
89 'label' => esc_attr__( 'Default filters', 'supportcandy' ),
90 'callback' => 'wpsc_get_ctl_default_filters',
91 ),
92 )
93 );
94 self::$current_tab = isset( $_REQUEST['tab'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tab'] ) ) : 'list-items'; // phpcs:ignore
95 }
96
97 /**
98 * Add localizations to local JS
99 *
100 * @param array $localizations - localization list.
101 * @return array
102 */
103 public static function localizations( $localizations ) {
104
105 if ( ! ( WPSC_Ticket_List_Settings::$is_current_page && WPSC_Ticket_List_Settings::$current_section === 'customer-ticket-list' ) ) {
106 return $localizations;
107 }
108
109 // Current section.
110 $localizations['current_tab'] = self::$current_tab;
111
112 return $localizations;
113 }
114
115 /**
116 * Customer ticket list settings
117 *
118 * @return void
119 */
120 public static function get_customer_tl_settings() {
121
122 if ( ! WPSC_Functions::is_site_admin() ) {
123 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
124 }?>
125
126 <div class="wpsc-setting-tab-container">
127 <?php
128 foreach ( self::$tabs as $key => $tab ) :
129 $active = self::$current_tab === $key ? 'active' : ''
130 ?>
131 <button
132 class="<?php echo esc_attr( $key ) . ' ' . esc_attr( $active ); ?>"
133 onclick="<?php echo esc_attr( $tab['callback'] ) . '();'; ?>">
134 <?php echo esc_attr( $tab['label'] ); ?>
135 </button>
136 <?php
137 endforeach;
138 ?>
139 </div>
140 <div class="wpsc-setting-section-body"></div>
141 <?php
142 wp_die();
143 }
144
145 /**
146 * Get customer ticket list items
147 */
148 public static function get_customer_tl_items() {
149
150 if ( ! WPSC_Functions::is_site_admin() ) {
151 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
152 }
153 $list_items = get_option( 'wpsc-ctl-list-items', array() );
154 ?>
155
156 <div class="wpsc-dock-container">
157 <?php
158 printf(
159 /* translators: Click here to see the documentation */
160 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
161 '<a href="https://supportcandy.net/docs/ticket-list-items/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
162 );
163 ?>
164 </div>
165 <table class="wpsc-atl wpsc-setting-tbl">
166 <thead>
167 <tr>
168 <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th>
169 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
170 </tr>
171 </thead>
172 <tbody>
173 <?php
174 foreach ( $list_items as $slug ) {
175 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
176 if ( ! $cf ) {
177 continue;
178 }
179 ?>
180 <tr>
181 <td><?php echo esc_attr( $cf->name ); ?></td>
182 <td>
183 <a href="javascript:wpsc_get_edit_customer_tl_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_customer_tl_item' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></a> |
184 <a href="javascript:wpsc_delete_customer_tl_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_customer_tl_item' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></a>
185 </td>
186 </tr>
187 <?php
188 }
189 ?>
190 </tbody>
191 </table>
192 <script>
193 jQuery('table.wpsc-atl').DataTable({
194 ordering: false,
195 pageLength: 20,
196 bLengthChange: false,
197 columnDefs: [
198 { targets: -1, searchable: false },
199 { targets: '_all', className: 'dt-left' }
200 ],
201 layout: {
202 topStart: {
203 buttons: [
204 {
205 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
206 className: 'wpsc-button small primary',
207 action: function ( e, dt, node, config ) {
208
209 wpsc_show_modal();
210 var data = { action: 'wpsc_get_add_customer_tl_item' };
211 jQuery.post(
212 supportcandy.ajax_url,
213 data,
214 function (response) {
215
216 // Set to modal.
217 jQuery( '.wpsc-modal-header' ).text( response.title );
218 jQuery( '.wpsc-modal-body' ).html( response.body );
219 jQuery( '.wpsc-modal-footer' ).html( response.footer );
220 // Display modal.
221 wpsc_show_modal_inner_container();
222 }
223 );
224 }
225 }
226 ],
227 },
228 },
229 language: supportcandy.translations.datatables
230 });
231
232 </script>
233 <?php
234 wp_die();
235 }
236
237 /**
238 * Get add customer ticket list items modal UI
239 *
240 * @return void
241 */
242 public static function get_add_customer_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-ctl-list-items', array() );
251
252 ob_start();
253 ?>
254 <form action="#" onsubmit="return false;" class="frm-add-customer-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-customer-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 endforeach;
278 ?>
279 </select>
280 <script>
281 jQuery('#wpsc-select-customer-lt-items').selectWoo({
282 allowClear: false,
283 placeholder: ""
284 });
285 </script>
286 </div>
287 <input type="hidden" name="action" value="wpsc_set_add_customer_tl_item">
288 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_customer_tl_item' ) ); ?>">
289 </form>
290 <?php
291 $body = ob_get_clean();
292
293 ob_start();
294 ?>
295 <button class="wpsc-button small primary" onclick="wpsc_set_add_customer_tl_item(this);">
296 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
297 </button>
298 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
299 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
300 </button>
301 <?php
302 do_action( 'wpsc_get_add_customer_tl_item' );
303 $footer = ob_get_clean();
304
305 $response = array(
306 'title' => $title,
307 'body' => $body,
308 'footer' => $footer,
309 );
310 wp_send_json( $response );
311 }
312
313 /**
314 * Set add new customer ticket list item
315 *
316 * @return void
317 */
318 public static function set_add_customer_tl_item() {
319
320 if ( check_ajax_referer( 'wpsc_set_add_customer_tl_item', '_ajax_nonce', false ) != 1 ) {
321 wp_send_json_error( 'Unauthorised request!', 401 );
322 }
323
324 if ( ! WPSC_Functions::is_site_admin() ) {
325 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
326 }
327
328 $ids = isset( $_POST['cf_id'] ) ? array_filter( array_map( 'intval', $_POST['cf_id'] ) ) : array();
329 if ( ! $ids ) {
330 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
331 }
332
333 $list_items = get_option( 'wpsc-ctl-list-items', array() );
334 foreach ( $ids as $id ) {
335 $cf = new WPSC_Custom_Field( $id );
336 if ( ! $cf->id || ! $cf->type::$is_list ) {
337 continue;
338 }
339 if ( ! in_array( $cf->slug, $list_items ) ) {
340 $list_items[] = $cf->slug;
341 }
342 }
343 update_option( 'wpsc-ctl-list-items', $list_items );
344 wp_die();
345 }
346
347 /**
348 * Delete customer ticket list items
349 *
350 * @return void
351 */
352 public static function delete_customer_tl_item() {
353
354 if ( check_ajax_referer( 'wpsc_delete_customer_tl_item', '_ajax_nonce', false ) != 1 ) {
355 wp_send_json_error( 'Unauthorised request!', 401 );
356 }
357
358 if ( ! WPSC_Functions::is_site_admin() ) {
359 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
360 }
361
362 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : 0;
363 if ( ! $slug ) {
364 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
365 }
366
367 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
368 if ( ! $cf ) {
369 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
370 }
371
372 $list_items = get_option( 'wpsc-ctl-list-items', array() );
373
374 $key = array_search( $slug, $list_items );
375 if ( $key !== false ) {
376 unset( $list_items[ $key ] );
377 $list_items = array_values( $list_items );
378 update_option( 'wpsc-ctl-list-items', $list_items );
379 do_action( 'wpsc_delete_customer_tl_item', $slug );
380 }
381
382 wp_die();
383 }
384
385 /**
386 * Get default filter ajax callback
387 *
388 * @return void
389 */
390 public static function get_ctl_default_filters() {
391
392 if ( ! WPSC_Functions::is_site_admin() ) {
393 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
394 }
395 $default_filters = get_option( 'wpsc-ctl-default-filters', array() );
396 ?>
397
398 <div class="wpsc-dock-container">
399 <?php
400 printf(
401 /* translators: Click here to see the documentation */
402 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
403 '<a href="https://supportcandy.net/docs/default-filters/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
404 );
405 ?>
406 </div>
407 <div class="wpsc-setting-cards-container ui-sortable">
408 <?php
409 foreach ( $default_filters as $key => $filter ) :
410 $style = ! $filter['is_enable'] ? 'background-color:#eec7ca;color:#dc2222' : '';
411 ?>
412 <div class="wpsc-setting-card" data-id="<?php echo esc_attr( $key ); ?>" style="<?php echo esc_attr( $style ); ?>">
413 <span class="wpsc-sort-handle action-btn"><?php WPSC_Icons::get( 'sort' ); ?></span>
414 <?php
415 $filter_label = $filter['label'] ? WPSC_Translations::get( 'wpsc-ctl-' . $key, stripslashes( $filter['label'] ) ) : stripslashes( $filter['label'] );
416 ?>
417 <span class="title"><?php echo esc_attr( $filter_label ); ?></span>
418 <div class="actions">
419 <span class="action-btn" onclick="wpsc_get_edit_ctl_default_filter('<?php echo esc_attr( $key ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_ctl_default_filter' ) ); ?>');"><?php WPSC_Icons::get( 'edit' ); ?></span>
420 <?php
421 if ( is_numeric( $key ) ) :
422 ?>
423 <span class="action-btn" onclick="wpsc_delete_ctl_default_filter(<?php echo esc_attr( $key ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_ctl_default_filter' ) ); ?>');"><?php WPSC_Icons::get( 'trash-alt' ); ?></span>
424 <?php
425 endif;
426 ?>
427 </div>
428 </div>
429 <?php
430 endforeach;
431 ?>
432 </div>
433 <div class="setting-footer-actions">
434 <button class="wpsc-button normal primary margin-right" onclick="wpsc_get_add_ctl_default_filter();"><?php esc_attr_e( 'Add new', 'supportcandy' ); ?></button>
435 <button class="wpsc-button normal secondary wpsc-save-sort-order"><?php esc_attr_e( 'Save Order', 'supportcandy' ); ?></button>
436 </div>
437 <script>
438 var items = jQuery( ".wpsc-setting-cards-container" ).sortable({ handle: '.wpsc-sort-handle' });
439 jQuery(".wpsc-save-sort-order").click(function(){
440 var slugs = items.sortable( "toArray", {attribute: 'data-id'} );
441 jQuery('.wpsc-setting-section-body').html(supportcandy.loader_html);
442 var data = { action: 'wpsc_sort_ctl_default_filters', slugs, _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_sort_ctl_default_filters' ) ); ?>' };
443 jQuery.post(supportcandy.ajax_url, data, function (response) {
444 wpsc_get_ctl_default_filters();
445 });
446 });
447 </script>
448 <?php
449 wp_die();
450 }
451
452 /**
453 * Get add filter modal
454 *
455 * @return void
456 */
457 public static function get_add_ctl_default_filter() {
458
459 if ( ! WPSC_Functions::is_site_admin() ) {
460 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
461 }
462
463 $title = esc_attr__( 'Add new default filter', 'supportcandy' );
464 $custom_fields = WPSC_Custom_Field::$custom_fields;
465 $default_filters = get_option( 'wpsc-ctl-default-filters' );
466 $more_settings = get_option( 'wpsc-tl-ms-customer-view' );
467
468 ob_start();
469 ?>
470 <form action="#" onsubmit="return false;" class="wpsc-frm-add-ctl-default-filter">
471 <div class="wpsc-input-group">
472 <div class="label-container">
473 <label for="">
474 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
475 <span class="required-char">*</span>
476 </label>
477 </div>
478 <input id="wpsc-ctl-df-label" type="text" name="label" autocomplete="off"/>
479 </div>
480 <div class="wpsc-input-group">
481 <div class="label-container">
482 <label for="">
483 <?php esc_attr_e( 'Parent filter', 'supportcandy' ); ?>
484 <span class="required-char">*</span>
485 </label>
486 </div>
487 <select name="parent-filter">
488 <?php
489 foreach ( $default_filters as $slug => $filter ) :
490 ?>
491 <option value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $filter['label'] ); ?></option>
492 <?php
493 endforeach;
494 ?>
495 </select>
496 </div>
497 <?php WPSC_Ticket_Conditions::print( 'default_filters', 'wpsc_default_filter_conditions', '', true, __( 'Filters', 'supportcandy' ) ); ?>
498 <div class="wpsc-input-group">
499 <div class="label-container">
500 <label for="">
501 <?php esc_attr_e( 'Sort by', 'supportcandy' ); ?>
502 <span class="required-char">*</span>
503 </label>
504 </div>
505 <select name="sort-by">
506 <?php
507 foreach ( $custom_fields as $cf ) :
508 if ( $cf->type::$is_sort ) :
509 ?>
510 <option <?php selected( $more_settings['default-sort-by'], $cf->slug ); ?> value="<?php echo esc_attr( $cf->slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
511 <?php
512 endif;
513 endforeach;
514 ?>
515 </select>
516 </div>
517 <div class="wpsc-input-group">
518 <div class="label-container">
519 <label for="">
520 <?php esc_attr_e( 'Sort order', 'supportcandy' ); ?>
521 <span class="required-char">*</span>
522 </label>
523 </div>
524 <select name="sort-order">
525 <option <?php selected( $more_settings['default-sort-order'], 'ASC' ); ?> value="ASC"><?php esc_attr_e( 'ASC', 'supportcandy' ); ?></option>
526 <option <?php selected( $more_settings['default-sort-order'], 'DESC' ); ?> value="DESC"><?php esc_attr_e( 'DESC', 'supportcandy' ); ?></option>
527 </select>
528 </div>
529 <div class="wpsc-input-group">
530 <div class="label-container">
531 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
532 </div>
533 <select name="is_enable">
534 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
535 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
536 </select>
537 </div>
538 <input type="hidden" name="action" value="wpsc_set_add_ctl_default_filter" />
539 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_ctl_default_filter' ) ); ?>">
540 </form>
541 <?php
542 $body = ob_get_clean();
543
544 ob_start();
545 ?>
546 <button class="wpsc-button small primary" onclick="wpsc_set_add_ctl_default_filter(this);">
547 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
548 </button>
549 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
550 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
551 </button>
552 <?php
553 $footer = ob_get_clean();
554
555 $response = array(
556 'title' => $title,
557 'body' => $body,
558 'footer' => $footer,
559 );
560 wp_send_json( $response );
561 }
562
563 /**
564 * Add default customer default filter
565 *
566 * @return void
567 */
568 public static function set_add_ctl_default_filter() {
569
570 if ( check_ajax_referer( 'wpsc_set_add_ctl_default_filter', '_ajax_nonce', false ) != 1 ) {
571 wp_send_json_error( 'Unauthorised request!', 401 );
572 }
573
574 if ( ! WPSC_Functions::is_site_admin() ) {
575 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
576 }
577
578 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
579 if ( ! $label ) {
580 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
581 }
582
583 $parent_filter = isset( $_POST['parent-filter'] ) ? sanitize_text_field( wp_unslash( $_POST['parent-filter'] ) ) : '';
584 if ( ! $parent_filter ) {
585 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
586 }
587
588 $filters = isset( $_POST['filters'] ) ? sanitize_text_field( wp_unslash( $_POST['filters'] ) ) : '';
589 if ( ! $filters || $filters == '[]' || ! WPSC_Ticket_Conditions::is_valid_input_conditions( 'wpsc_default_filter_conditions', $filters ) ) {
590 wp_send_json_error( 'Bad request', 400 );
591 }
592
593 $sort_by = isset( $_POST['sort-by'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-by'] ) ) : '';
594 if ( ! $sort_by ) {
595 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
596 }
597
598 $sort_order = isset( $_POST['sort-order'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-order'] ) ) : '';
599 if ( ! $sort_order ) {
600 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
601 }
602
603 $enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
604
605 $ctl_filters = get_option( 'wpsc-ctl-default-filters' );
606 $index = WPSC_Functions::get_tl_df_auto_increament();
607 $ctl_filters[ $index ] = array(
608 'label' => $label,
609 'parent-filter' => $parent_filter,
610 'filters' => $filters,
611 'sort-by' => $sort_by,
612 'sort-order' => $sort_order,
613 'is_enable' => $enable,
614 );
615 update_option( 'wpsc-ctl-default-filters', $ctl_filters );
616
617 // add string translations.
618 WPSC_Translations::add( 'wpsc-ctl-' . $index, $label );
619
620 wp_die();
621 }
622
623 /**
624 * Get edit filter modal
625 *
626 * @return void
627 */
628 public static function get_edit_ctl_default_filter() {
629
630 if ( check_ajax_referer( 'wpsc_get_edit_ctl_default_filter', '_ajax_nonce', false ) != 1 ) {
631 wp_send_json_error( 'Unauthorised request!', 401 );
632 }
633
634 if ( ! WPSC_Functions::is_site_admin() ) {
635 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
636 }
637
638 $ctl_filters = get_option( 'wpsc-ctl-default-filters' );
639
640 $fslug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
641 if ( ! $fslug || ! isset( $ctl_filters[ $fslug ] ) ) {
642 wp_send_json_error( 'Unauthorised request!', 401 );
643 }
644
645 $ctl_filter = $ctl_filters[ $fslug ];
646
647 $flag = is_numeric( $fslug ) ? true : false;
648
649 $title = esc_attr__( 'Edit default filter', 'supportcandy' );
650 $custom_fields = WPSC_Custom_Field::$custom_fields;
651
652 ob_start();
653 ?>
654 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-ctl-default-filter">
655 <div class="wpsc-input-group">
656 <div class="label-container">
657 <label for="">
658 <?php esc_attr_e( 'Label', 'supportcandy' ); ?>
659 <span class="required-char">*</span>
660 </label>
661 </div>
662 <input id="wpsc-ctl-df-label" type="text" name="label" value="<?php echo esc_attr( $ctl_filter['label'] ); ?>" autocomplete="off"/>
663 </div>
664 <?php
665 if ( $flag ) :
666 ?>
667 <div class="wpsc-input-group">
668 <div class="label-container">
669 <label for="">
670 <?php esc_attr_e( 'Parent filter', 'supportcandy' ); ?>
671 <span class="required-char">*</span>
672 </label>
673 </div>
674 <select name="parent-filter">
675 <?php
676 foreach ( $ctl_filters as $slug => $filter ) :
677 if ( $slug == $fslug ) {
678 continue;
679 }
680 ?>
681 <option <?php selected( $ctl_filter['parent-filter'], $slug ); ?> value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $filter['label'] ); ?></option>
682 <?php
683 endforeach;
684 ?>
685 </select>
686 </div>
687 <?php WPSC_Ticket_Conditions::print( 'default_filters', 'wpsc_default_filter_conditions', $ctl_filter['filters'], true, __( 'Filters', 'supportcandy' ) ); ?>
688 <div class="wpsc-input-group">
689 <div class="label-container">
690 <label for="">
691 <?php esc_attr_e( 'Sort by', 'supportcandy' ); ?>
692 <span class="required-char">*</span>
693 </label>
694 </div>
695 <select name="sort-by">
696 <?php
697 foreach ( $custom_fields as $cf ) :
698 if ( $cf->type::$is_sort ) :
699 ?>
700 <option <?php selected( $ctl_filter['sort-by'], $cf->slug ); ?> value="<?php echo esc_attr( $cf->slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
701 <?php
702 endif;
703 endforeach;
704 ?>
705 </select>
706 </div>
707 <div class="wpsc-input-group">
708 <div class="label-container">
709 <label for="">
710 <?php esc_attr_e( 'Sort order', 'supportcandy' ); ?>
711 <span class="required-char">*</span>
712 </label>
713 </div>
714 <select name="sort-order">
715 <option <?php selected( $ctl_filter['sort-order'], 'ASC' ); ?> value="ASC"><?php esc_attr_e( 'ASC', 'supportcandy' ); ?></option>
716 <option <?php selected( $ctl_filter['sort-order'], 'DESC' ); ?> value="DESC"><?php esc_attr_e( 'DESC', 'supportcandy' ); ?></option>
717 </select>
718 </div>
719 <?php
720 endif;
721 ?>
722 <div class="wpsc-input-group">
723 <div class="label-container">
724 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
725 </div>
726 <select name="is_enable">
727 <option <?php selected( $ctl_filter['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
728 <option <?php selected( $ctl_filter['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
729 </select>
730 </div>
731 <input type="hidden" name="action" value="wpsc_set_edit_ctl_default_filter" />
732 <input type="hidden" name="slug" value="<?php echo esc_attr( $fslug ); ?>" />
733 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_ctl_default_filter' ) ); ?>">
734 </form>
735 <?php
736 $body = ob_get_clean();
737
738 ob_start();
739 ?>
740 <button class="wpsc-button small primary" onclick="wpsc_set_edit_ctl_default_filter(this, <?php echo esc_attr( $flag ); ?>);">
741 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
742 </button>
743 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
744 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
745 </button>
746 <?php
747 $footer = ob_get_clean();
748
749 $response = array(
750 'title' => $title,
751 'body' => $body,
752 'footer' => $footer,
753 );
754 wp_send_json( $response );
755 }
756
757 /**
758 * Set edit customer default filter
759 *
760 * @return void
761 */
762 public static function set_edit_ctl_default_filter() {
763
764 if ( check_ajax_referer( 'wpsc_set_edit_ctl_default_filter', '_ajax_nonce', false ) != 1 ) {
765 wp_send_json_error( 'Unauthorised request!', 401 );
766 }
767
768 if ( ! WPSC_Functions::is_site_admin() ) {
769 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
770 }
771
772 $ctl_filters = get_option( 'wpsc-ctl-default-filters' );
773 $more_settings = get_option( 'wpsc-tl-ms-customer-view' );
774
775 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : 0;
776 if ( ! $slug || ! isset( $ctl_filters[ $slug ] ) ) {
777 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
778 }
779
780 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
781 if ( ! $label ) {
782 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
783 }
784
785 $enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
786 if ( ! $enable && $more_settings['default-filter'] == $slug ) {
787 $enable = 1;
788 }
789
790 // remove string translations.
791 WPSC_Translations::remove( 'wpsc-ctl-' . $slug );
792
793 if ( ! is_numeric( $slug ) ) {
794 $ctl_filters[ $slug ] = array(
795 'label' => $label,
796 'is_enable' => $enable,
797 );
798 update_option( 'wpsc-ctl-default-filters', $ctl_filters );
799
800 // add string translations if not numaric.
801 WPSC_Translations::add( 'wpsc-ctl-' . $slug, $label );
802
803 wp_die();
804 }
805
806 $parent_filter = isset( $_POST['parent-filter'] ) ? sanitize_text_field( wp_unslash( $_POST['parent-filter'] ) ) : '';
807 if ( ! $parent_filter ) {
808 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
809 }
810
811 $filters = isset( $_POST['filters'] ) ? sanitize_text_field( wp_unslash( $_POST['filters'] ) ) : '';
812 if ( ! $filters ) {
813 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
814 }
815
816 $sort_by = isset( $_POST['sort-by'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-by'] ) ) : '';
817 if ( ! $sort_by ) {
818 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
819 }
820
821 $sort_order = isset( $_POST['sort-order'] ) ? sanitize_text_field( wp_unslash( $_POST['sort-order'] ) ) : '';
822 if ( ! $sort_order ) {
823 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
824 }
825
826 $ctl_filters[ $slug ] = array(
827 'label' => $label,
828 'parent-filter' => $parent_filter,
829 'filters' => $filters,
830 'sort-by' => $sort_by,
831 'sort-order' => $sort_order,
832 'is_enable' => $enable,
833 );
834 update_option( 'wpsc-ctl-default-filters', $ctl_filters );
835
836 // add string translations.
837 WPSC_Translations::add( 'wpsc-ctl-' . $slug, $label );
838
839 wp_die();
840 }
841
842 /**
843 * Delete customer default filter
844 *
845 * @return void
846 */
847 public static function delete_ctl_default_filter() {
848
849 if ( check_ajax_referer( 'wpsc_delete_ctl_default_filter', '_ajax_nonce', false ) != 1 ) {
850 wp_send_json_error( 'Unauthorised request!', 401 );
851 }
852
853 if ( ! WPSC_Functions::is_site_admin() ) {
854 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
855 }
856
857 $ctl_filters = get_option( 'wpsc-ctl-default-filters' );
858
859 $slug = isset( $_POST['slug'] ) ? intval( $_POST['slug'] ) : 0;
860 if ( ! $slug || ! isset( $ctl_filters[ $slug ] ) ) {
861 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
862 }
863
864 unset( $ctl_filters[ $slug ] );
865 update_option( 'wpsc-ctl-default-filters', $ctl_filters );
866 // remove string translations.
867 WPSC_Translations::remove( 'wpsc-ctl-' . $slug );
868 wp_die();
869 }
870
871 /**
872 * Sort customer default filters
873 */
874 public static function sort_ctl_default_filters() {
875
876 if ( check_ajax_referer( 'wpsc_sort_ctl_default_filters', '_ajax_nonce', false ) != 1 ) {
877 wp_send_json_error( 'Unauthorised request!', 401 );
878 }
879
880 if ( ! WPSC_Functions::is_site_admin() ) {
881 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
882 }
883
884 $ctl_filters = get_option( 'wpsc-ctl-default-filters' );
885
886 $slugs = isset( $_POST['slugs'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['slugs'] ) ) ) : array();
887 if ( ! $slugs ) {
888 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
889 }
890
891 $temp_filters = array();
892 foreach ( $slugs as $slug ) {
893
894 if ( ! isset( $ctl_filters[ $slug ] ) ) {
895 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
896 }
897 $temp_filters[ $slug ] = $ctl_filters[ $slug ];
898 }
899
900 $ctl_filters_keys = array_keys( $ctl_filters );
901 // Verifying if slug is present in list item.
902 foreach ( $slugs as $slug ) {
903 if ( ! in_array( $slug, $ctl_filters_keys ) ) {
904 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
905 }
906 }
907
908 update_option( 'wpsc-ctl-default-filters', $temp_filters );
909 wp_die();
910 }
911
912 /**
913 * Get customer filter items
914 *
915 * @return void
916 */
917 public static function get_customer_filter_items() {
918
919 if ( ! WPSC_Functions::is_site_admin() ) {
920 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
921 }
922 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
923 ?>
924
925 <div class="wpsc-dock-container">
926 <?php
927 printf(
928 /* translators: Click here to see the documentation */
929 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
930 '<a href="https://supportcandy.net/docs/ticket-list-filter-items/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
931 );
932 ?>
933 </div>
934 <table class="wpsc-cfl wpsc-setting-tbl">
935 <thead>
936 <tr>
937 <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th>
938 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
939 </tr>
940 </thead>
941 <tbody>
942 <?php
943 foreach ( $filter_items as $slug ) {
944 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
945 if ( ! $cf ) {
946 continue;
947 }
948 ?>
949 <tr>
950 <td><?php echo esc_attr( $cf->name ); ?></td>
951 <td>
952 <a href="javascript:wpsc_get_edit_ctl_filter_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_ctl_filter_item' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></a> |
953 <a href="javascript:wpsc_delete_ctl_filter_item('<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_ctl_filter_item' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></a>
954 </td>
955 </tr>
956 <?php
957 }
958 ?>
959 </tbody>
960 </table>
961 <script>
962 jQuery('table.wpsc-cfl').DataTable({
963 ordering: false,
964 pageLength: 20,
965 bLengthChange: false,
966 columnDefs: [
967 { targets: -1, searchable: false },
968 { targets: '_all', className: 'dt-left' }
969 ],
970 layout: {
971 topStart: {
972 buttons: [
973 {
974 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
975 className: 'wpsc-button small primary',
976 action: function ( e, dt, node, config ) {
977
978 wpsc_show_modal();
979 var data = { action: 'wpsc_get_add_ctl_filter_item' };
980 jQuery.post(
981 supportcandy.ajax_url,
982 data,
983 function (response) {
984
985 // Set to modal.
986 jQuery( '.wpsc-modal-header' ).text( response.title );
987 jQuery( '.wpsc-modal-body' ).html( response.body );
988 jQuery( '.wpsc-modal-footer' ).html( response.footer );
989 // Display modal.
990 wpsc_show_modal_inner_container();
991 }
992 );
993 }
994 }
995 ],
996 },
997 },
998 language: supportcandy.translations.datatables
999 });
1000
1001 </script>
1002 <?php
1003 wp_die();
1004 }
1005
1006 /**
1007 * Add customer ticket list filter items modal UI
1008 *
1009 * @return void
1010 */
1011 public static function get_add_ctl_filter_item() {
1012
1013 if ( ! WPSC_Functions::is_site_admin() ) {
1014 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1015 }
1016
1017 $title = esc_attr__( 'Add New Filter Item', 'supportcandy' );
1018 $custom_fields = WPSC_Custom_Field::$custom_fields;
1019 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
1020
1021 ob_start();
1022 ?>
1023 <form action="#" onsubmit="return false;" class="frm-add-customer-tl-filter-items">
1024 <div class="wpsc-input-group field-type">
1025 <div class="label-container">
1026 <label for="">
1027 <?php esc_attr_e( 'Select fields', 'supportcandy' ); ?>
1028 <span class="required-char">*</span>
1029 </label>
1030 </div>
1031 <select multiple id="wpsc-select-customer-lt-filter-items" name="customer-tl-filter-id[]">
1032 <?php
1033 foreach ( $custom_fields as $cf ) :
1034
1035 if (
1036 class_exists( $cf->type ) &&
1037 in_array( $cf->field, WPSC_CF_Settings::$allowed_modules['ticket-filter'] ) &&
1038 $cf->type::$is_filter &&
1039 ! in_array( $cf->slug, $filter_items )
1040 ) {
1041 ?>
1042 <option value="<?php echo esc_attr( $cf->id ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
1043 <?php
1044 }
1045
1046 endforeach;
1047 ?>
1048 </select>
1049 <script>
1050 jQuery('#wpsc-select-customer-lt-filter-items').selectWoo({
1051 allowClear: false,
1052 placeholder: ""
1053 });
1054 </script>
1055 </div>
1056 <input type="hidden" name="action" value="wpsc_set_add_ctl_filter_item">
1057 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_ctl_filter_item' ) ); ?>">
1058 </form>
1059 <?php
1060 $body = ob_get_clean();
1061
1062 ob_start();
1063 ?>
1064 <button class="wpsc-button small primary" onclick="wpsc_set_add_ctl_filter_item(this);">
1065 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
1066 </button>
1067 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
1068 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
1069 </button>
1070 <?php
1071 do_action( 'wpsc_get_add_customer_filter_item' );
1072 $footer = ob_get_clean();
1073
1074 $response = array(
1075 'title' => $title,
1076 'body' => $body,
1077 'footer' => $footer,
1078 );
1079 wp_send_json( $response );
1080 }
1081
1082 /**
1083 * Set new customer ticket list filter items
1084 *
1085 * @return void
1086 */
1087 public static function set_add_ctl_filter_item() {
1088
1089 if ( check_ajax_referer( 'wpsc_set_add_ctl_filter_item', '_ajax_nonce', false ) != 1 ) {
1090 wp_send_json_error( 'Unauthorised request!', 401 );
1091 }
1092
1093 if ( ! WPSC_Functions::is_site_admin() ) {
1094 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1095 }
1096
1097 $ids = isset( $_POST['customer-tl-filter-id'] ) ? array_filter( array_map( 'intval', $_POST['customer-tl-filter-id'] ) ) : array();
1098 if ( ! $ids ) {
1099 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1100 }
1101
1102 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
1103 foreach ( $ids as $id ) {
1104 $cf = new WPSC_Custom_Field( $id );
1105 if ( ! $cf->id || ! $cf->type::$is_filter ) {
1106 continue;
1107 }
1108 if ( ! in_array( $cf->slug, $filter_items ) ) {
1109 $filter_items[] = $cf->slug;
1110 }
1111 }
1112 update_option( 'wpsc-ctl-filter-items', $filter_items );
1113 wp_die();
1114 }
1115
1116 /**
1117 * Delete customer ticket list filter items
1118 *
1119 * @return void
1120 */
1121 public static function delete_ctl_filter_item() {
1122
1123 if ( check_ajax_referer( 'wpsc_delete_ctl_filter_item', '_ajax_nonce', false ) != 1 ) {
1124 wp_send_json_error( 'Unauthorised request!', 401 );
1125 }
1126
1127 if ( ! WPSC_Functions::is_site_admin() ) {
1128 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
1129 }
1130
1131 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1132 if ( ! $slug ) {
1133 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1134 }
1135
1136 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
1137 if ( ! $cf ) {
1138 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
1139 }
1140
1141 $more_settings = get_option( 'wpsc-tl-ms-customer-view' );
1142 if ( $more_settings['default-filter'] == $slug ) {
1143 wp_die();
1144 }
1145
1146 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
1147
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-ctl-filter-items', $filter_items );
1153 do_action( 'wpsc_delete_customer_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-ctl-list-items', array() );
1168 $key = array_search( $cf->slug, $list_items );
1169 if ( $key !== false ) {
1170 unset( $list_items[ $key ] );
1171 update_option( 'wpsc-ctl-list-items', $list_items );
1172 }
1173
1174 $filter_items = get_option( 'wpsc-ctl-filter-items', array() );
1175 $key = array_search( $cf->slug, $filter_items );
1176 if ( $key !== false ) {
1177 unset( $filter_items[ $key ] );
1178 update_option( 'wpsc-ctl-filter-items', $filter_items );
1179 }
1180 }
1181
1182 /**
1183 * Get edit customer ticket filter list items modal UI
1184 *
1185 * @return void
1186 */
1187 public static function get_edit_ctl_filter_item() {
1188
1189 if ( check_ajax_referer( 'wpsc_get_edit_ctl_filter_item', '_ajax_nonce', false ) != 1 ) {
1190 wp_send_json_error( 'Unauthorised 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-ctl-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-customer-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_ctl_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_ctl_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_ctl_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_ctl_filter_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 customer ticket filter list item
1267 *
1268 * @return void
1269 */
1270 public static function set_edit_ctl_filter_item() {
1271
1272 if ( check_ajax_referer( 'wpsc_set_edit_ctl_filter_item', '_ajax_nonce', false ) != 1 ) {
1273 wp_send_json_error( 'Unauthorised 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-ctl-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-ctl-filter-items', $filter_items );
1321 wp_die();
1322 }
1323
1324 /**
1325 * Get edit customer ticket list items modal UI
1326 *
1327 * @return void
1328 */
1329 public static function get_edit_customer_tl_item() {
1330
1331 if ( check_ajax_referer( 'wpsc_get_edit_customer_tl_item', '_ajax_nonce', false ) != 1 ) {
1332 wp_send_json_error( 'Unauthorised 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-ctl-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-customer-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_customer_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_customer_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_customer_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_customer_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 customer ticket list item
1409 *
1410 * @return void
1411 */
1412 public static function set_edit_customer_tl_item() {
1413
1414 if ( check_ajax_referer( 'wpsc_set_edit_customer_tl_item', '_ajax_nonce', false ) != 1 ) {
1415 wp_send_json_error( 'Unauthorised 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-ctl-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
1456 $offset = array_search( $load_after->slug, $list_items ) + 1;
1457 $arr1 = array_slice( $list_items, 0, $offset );
1458 $arr2 = array_slice( $list_items, $offset );
1459 $list_items = array_merge( $arr1, array( $cf->slug ), $arr2 );
1460 break;
1461 }
1462
1463 update_option( 'wpsc-ctl-list-items', $list_items );
1464 wp_die();
1465 }
1466 }
1467 endif;
1468
1469 WPSC_Customer_Ticket_List_Settings::init();
1470