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

class-wpsc-tff.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.1, at includes/admin/custom-fields/class-wpsc-tff.php

663 lines 20.6 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_TFF' ) ) :
7
8 final class WPSC_TFF {
9
10 /**
11 * Ticket form fields
12 *
13 * @var array
14 */
15 private static $tff = array();
16
17 /**
18 * Initialize this class
19 */
20 public static function init() {
21
22 // List.
23 add_action( 'wp_ajax_wpsc_get_tff', array( __CLASS__, 'get_tff' ) );
24
25 // Add new.
26 add_action( 'wp_ajax_wpsc_get_add_new_tff', array( __CLASS__, 'get_add_new_tff' ) );
27 add_action( 'wp_ajax_wpsc_set_add_new_tff', array( __CLASS__, 'set_add_new_tff' ) );
28
29 // Edit.
30 add_action( 'wp_ajax_wpsc_get_edit_tff', array( __CLASS__, 'get_edit_tff' ) );
31 add_action( 'wp_ajax_wpsc_set_edit_tff', array( __CLASS__, 'set_edit_tff' ) );
32
33 // Delete.
34 add_action( 'wp_ajax_wpsc_delete_tff', array( __CLASS__, 'delete_tff' ) );
35
36 // Remove if custom field deleted.
37 add_action( 'wpsc_delete_custom_field', array( __CLASS__, 'delete_custom_field' ), 10, 1 );
38
39 // visibility conditions filter.
40 add_filter( 'wpsc_visibility_conditions', array( __CLASS__, 'visibility_conditions' ) );
41 }
42
43 /**
44 * Get ticket form fields
45 *
46 * @return void
47 */
48 public static function get_tff() {
49
50 if ( ! WPSC_Functions::is_site_admin() ) {
51 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
52 }
53
54 $tff = get_option( 'wpsc-tff', array() );
55 ?>
56
57 <div class="wpsc-setting-header">
58 <h2><?php esc_attr_e( 'Ticket Form Fields', 'supportcandy' ); ?></h2>
59 </div>
60 <div class="wpsc-setting-section-body">
61 <div class="wpsc-dock-container">
62 <?php
63 printf(
64 /* translators: Click here to see the documentation */
65 esc_attr__( '%s to see the documentation!', 'supportcandy' ),
66 '<a href="https://supportcandy.net/docs/ticket-form-fields/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>'
67 );
68 ?>
69 </div>
70 <table class="form-fields wpsc-setting-tbl">
71 <thead>
72 <tr>
73 <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th>
74 <th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th>
75 </tr>
76 </thead>
77 <tbody>
78 <?php
79 foreach ( $tff as $slug => $settings ) :
80 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
81 if ( ! $cf ) {
82 continue;
83 }
84 ?>
85 <tr>
86 <td><?php echo esc_attr( $cf->name ); ?></td>
87 <td>
88 <a href="javascript:wpsc_get_edit_tff(<?php echo esc_attr( $cf->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_tff' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></a>
89 <?php
90 if ( ! ( $cf->slug == 'name' || $cf->slug == 'email' ) ) :
91 echo esc_attr( ' | ' );
92 ?>
93 <a href="javascript:wpsc_delete_tff(<?php echo esc_attr( $cf->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_tff' ) ); ?>');" class="wpsc-link"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></a>
94 <?php
95 endif;
96 ?>
97 </td>
98 </tr>
99 <?php
100 endforeach;
101 ?>
102 </tbody>
103 </table>
104 </div>
105 <script>
106 jQuery('table.form-fields').DataTable({
107 ordering: false,
108 pageLength: 20,
109 bLengthChange: false,
110 columnDefs: [
111 { targets: -1, searchable: false },
112 { targets: '_all', className: 'dt-left' }
113 ],
114 layout: {
115 topStart: {
116 buttons: [
117 {
118 text: '<?php esc_attr_e( 'Add new', 'supportcandy' ); ?>',
119 className: 'wpsc-button small primary',
120 action: function ( e, dt, node, config ) {
121 wpsc_show_modal();
122 var data = { action: 'wpsc_get_add_new_tff' };
123 jQuery.post(
124 supportcandy.ajax_url,
125 data,
126 function (response) {
127 jQuery( '.wpsc-modal-header' ).text( response.title );
128 jQuery( '.wpsc-modal-body' ).html( response.body );
129 jQuery( '.wpsc-modal-footer' ).html( response.footer );
130 wpsc_show_modal_inner_container();
131 }
132 );
133 }
134 },
135 ],
136 },
137 },
138 language: supportcandy.translations.datatables
139 });
140 </script>
141 <?php
142 wp_die();
143 }
144
145 /**
146 * Add new ticket form field modal popup UI
147 *
148 * @return void
149 */
150 public static function get_add_new_tff() {
151
152 if ( ! WPSC_Functions::is_site_admin() ) {
153 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
154 }
155
156 $title = esc_attr__( 'Add new field', 'supportcandy' );
157 $custom_fields = WPSC_Custom_Field::$custom_fields;
158 $tff = get_option( 'wpsc-tff', array() );
159 $unique_id = uniqid( 'wpsc_' );
160
161 ob_start();
162 ?>
163 <form action="#" onsubmit="return false;" class="frm-add-new-ticket-form-field">
164 <div class="wpsc-input-group">
165 <div class="label-container">
166 <label for="">
167 <?php esc_attr_e( 'Select field', 'supportcandy' ); ?>
168 <span class="required-char">*</span>
169 </label>
170 </div>
171 <select id="wpsc-select-ticket-form-field" name="id" onchange="<?php echo esc_attr( $unique_id ); ?>(this)">
172 <option value=""></option>
173 <?php
174 foreach ( $custom_fields as $cf ) {
175
176 if (
177 class_exists( $cf->type ) &&
178 in_array( $cf->field, WPSC_CF_Settings::$allowed_modules['ticket-form'] ) &&
179 $cf->type::$is_ctf &&
180 ! isset( $tff[ $cf->slug ] )
181 ) {
182 if ( $cf->field == 'customer' && ! $cf->allow_ticket_form ) {
183 continue;
184 }
185 ?>
186 <option class="field_<?php echo esc_attr( $cf->id ); ?>" data-fieldtype="<?php echo esc_attr( $cf->type ); ?>" data-slug="<?php echo esc_attr( $cf->slug ); ?>" value="<?php echo esc_attr( $cf->id ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
187 <?php
188 }
189 }
190 ?>
191 </select>
192 <script>
193 jQuery('#wpsc-select-ticket-form-field').selectWoo({
194 allowClear: true,
195 placeholder: ""
196 });
197 function <?php echo esc_attr( $unique_id ); ?>(el) {
198
199 var id = jQuery(el).val();
200 var option = jQuery('option.field_'+id);
201 var slug = option.data('slug');
202 <?php
203 do_action( 'wpsc_jse_add_tff_change_field', $unique_id );
204 ?>
205 }
206 jQuery(document).ready(function() {
207 jQuery('#wpsc-select-ticket-form-field').change(function() {
208 var slug = jQuery('option:selected', this).data('fieldtype');
209 if(slug =='WPSC_CF_HTML'){
210 jQuery('#wpsc-create-ticket-required').hide();
211 }
212 });
213 });
214 </script>
215 </div>
216 <div class="wpsc-input-group" id="wpsc-create-ticket-required">
217 <div class="label-container">
218 <label for=""><?php esc_attr_e( 'Is required?', 'supportcandy' ); ?></label>
219 </div>
220 <select name="is-required">
221 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
222 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
223 </select>
224 </div>
225 <div class="wpsc-input-group">
226 <div class="label-container">
227 <label for=""><?php esc_attr_e( 'Width', 'supportcandy' ); ?></label>
228 </div>
229 <select id="wpsc-select-ticket-form-field-width" name="width">
230 <option value="1/3"><?php esc_attr_e( '1/3rd of row', 'supportcandy' ); ?></option>
231 <option value="half"><?php esc_attr_e( 'Half-width of row', 'supportcandy' ); ?></option>
232 <option value="full"><?php esc_attr_e( 'Full-width of row', 'supportcandy' ); ?></option>
233 </select>
234 </div>
235 <div class="wpsc-input-group">
236 <div class="label-container">
237 <label for=""><?php esc_attr_e( 'Load after', 'supportcandy' ); ?></label>
238 </div>
239 <select name="load-after" class="load-after">
240 <option value="__TOP__">-- <?php esc_attr_e( 'TOP', 'supportcandy' ); ?> --</option>
241 <?php
242 foreach ( $tff as $slug => $settings ) {
243 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
244 if ( ! $cf ) {
245 continue;
246 }
247 ?>
248 <option value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $cf->name ); ?></option>
249 <?php
250 }
251 ?>
252 <option value="__END__" selected>-- <?php esc_attr_e( 'END', 'supportcandy' ); ?> --</option>
253 </select>
254 <script>jQuery('select.load-after').selectWoo();</script>
255 </div>
256 <?php do_action( 'wpsc_get_add_new_tff', $unique_id ); ?>
257 <?php WPSC_Ticket_Conditions::print( 'visibility', 'wpsc_visibility_conditions', '', false, __( 'Visibility conditions', 'supportcandy' ) ); ?>
258 <input type="hidden" name="action" value="wpsc_set_add_new_tff">
259 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_add_new_tff' ) ); ?>">
260 </form>
261 <?php
262 $body = ob_get_clean();
263
264 ob_start();
265 ?>
266 <button class="wpsc-button small primary" onclick="wpsc_set_add_new_tff(this);">
267 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
268 </button>
269 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
270 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
271 </button>
272 <?php
273 $footer = ob_get_clean();
274
275 $response = array(
276 'title' => $title,
277 'body' => $body,
278 'footer' => $footer,
279 );
280 wp_send_json( $response );
281 }
282
283 /**
284 * Insert form field
285 *
286 * @return void
287 */
288 public static function set_add_new_tff() {
289
290 if ( check_ajax_referer( 'wpsc_set_add_new_tff', '_ajax_nonce', false ) != 1 ) {
291 wp_send_json_error( 'Unauthorised request!', 401 );
292 }
293
294 if ( ! WPSC_Functions::is_site_admin() ) {
295 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
296 }
297
298 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
299 if ( ! $id ) {
300 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
301 }
302
303 $tff = get_option( 'wpsc-tff', array() );
304
305 $cf = new WPSC_Custom_Field( $id );
306 if (
307 ! $cf->id ||
308 ! $cf->type::$is_ctf ||
309 ! in_array( $cf->field, WPSC_CF_Settings::$allowed_modules['ticket-form'] ) ||
310 isset( $tff[ $cf->slug ] )
311 ) {
312 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
313 }
314
315 $is_required = isset( $_POST['is-required'] ) ? intval( $_POST['is-required'] ) : 1;
316 $width = isset( $_POST['width'] ) ? sanitize_text_field( wp_unslash( $_POST['width'] ) ) : 'full';
317 $load_after = isset( $_POST['load-after'] ) ? sanitize_text_field( wp_unslash( $_POST['load-after'] ) ) : '__END__';
318
319 $visibility = isset( $_POST['visibility'] ) ? sanitize_text_field( wp_unslash( $_POST['visibility'] ) ) : '';
320 if ( ! WPSC_Ticket_Conditions::is_valid_input_conditions( 'wpsc_visibility_conditions', $visibility ) ) {
321 wp_send_json_error( 'Bad request', 400 );
322 }
323
324 $field = apply_filters(
325 'wpsc_tff_add_new',
326 array(
327 'is-required' => $is_required,
328 'width' => $width,
329 'visibility' => $visibility,
330 ),
331 $cf
332 );
333
334 switch ( $load_after ) {
335
336 case '__TOP__':
337 $tff = array_merge( array( $cf->slug => $field ), $tff );
338 break;
339
340 case '__END__':
341 $tff = array_merge( $tff, array( $cf->slug => $field ) );
342 break;
343
344 default:
345 $load_after = WPSC_Custom_Field::get_cf_by_slug( $load_after );
346 if ( ! $load_after || ! isset( $tff[ $load_after->slug ] ) ) {
347 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
348 }
349
350 if ( count( $tff ) == 0 ) {
351
352 $tff = array_merge( $tff, array( $cf->slug => $field ) );
353
354 } else {
355
356 $offset = array_search( $load_after->slug, array_keys( $tff ) ) + 1;
357 $arr1 = array_slice( $tff, 0, $offset );
358 $arr2 = array_slice( $tff, $offset );
359 $tff = array_merge( $arr1, array( $cf->slug => $field ), $arr2 );
360 }
361 break;
362 }
363
364 update_option( 'wpsc-tff', $tff );
365 wp_die();
366 }
367
368 /**
369 * Edit ticket form field modal
370 *
371 * @return void
372 */
373 public static function get_edit_tff() {
374
375 if ( check_ajax_referer( 'wpsc_get_edit_tff', '_ajax_nonce', false ) != 1 ) {
376 wp_send_json_error( 'Unauthorised request!', 401 );
377 }
378
379 if ( ! WPSC_Functions::is_site_admin() ) {
380 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
381 }
382
383 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
384 if ( ! $id ) {
385 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
386 }
387
388 $tff = get_option( 'wpsc-tff', array() );
389 $cf = new WPSC_Custom_Field( $id );
390
391 if (
392 ! $cf->id ||
393 ! $cf->type::$is_ctf ||
394 ! isset( $tff[ $cf->slug ] )
395 ) {
396 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
397 }
398
399 $field = $tff[ $cf->slug ];
400 $title = $cf->name;
401 $unique_id = uniqid( 'wpsc_' );
402
403 // calculate load order.
404 $tff_keys = array_keys( $tff );
405 $offset = array_search( $cf->slug, $tff_keys );
406 $load_after = $offset == 0 ? '__TOP__' : $tff_keys[ $offset - 1 ];
407
408 ob_start();
409 ?>
410 <form action="#" onsubmit="return false;" class="frm-edit-ticket-form-field">
411
412 <?php
413 if ( ! ( $cf->slug == 'name' || $cf->slug == 'email' ) ) :
414 ?>
415 <div class="wpsc-input-group" style="<?php echo esc_attr( $cf->type ) === 'WPSC_CF_HTML' ? 'display:none;' : ''; ?>" >
416 <div class="label-container">
417 <label for=""><?php esc_attr_e( 'Is required?', 'supportcandy' ); ?></label>
418 </div>
419 <select name="is-required">
420 <option <?php selected( $field['is-required'], 1 ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
421 <option <?php selected( $field['is-required'], 0 ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
422 </select>
423 </div>
424 <?php
425 endif;
426 ?>
427 <div class="wpsc-input-group">
428 <div class="label-container">
429 <label for=""><?php esc_attr_e( 'Width', 'supportcandy' ); ?></label>
430 </div>
431 <select id="wpsc-select-ticket-form-field-width" name="width">
432 <option <?php selected( $field['width'], '1/3' ); ?> value="1/3"><?php esc_attr_e( '1/3rd of row', 'supportcandy' ); ?></option>
433 <option <?php selected( $field['width'], 'half' ); ?> value="half"><?php esc_attr_e( 'Half-width of row', 'supportcandy' ); ?></option>
434 <option <?php selected( $field['width'], 'full' ); ?> value="full"><?php esc_attr_e( 'Full-width of row', 'supportcandy' ); ?></option>
435 </select>
436 </div>
437 <div class="wpsc-input-group">
438 <div class="label-container">
439 <label for=""><?php esc_attr_e( 'Load after', 'supportcandy' ); ?></label>
440 </div>
441 <select name="load-after" class="load-after">
442 <option <?php selected( $load_after, '__TOP__', true ); ?> value="__TOP__">-- <?php esc_attr_e( 'TOP', 'supportcandy' ); ?> --</option>
443 <?php
444 foreach ( $tff as $slug => $settings ) {
445 $cff = WPSC_Custom_Field::get_cf_by_slug( $slug );
446 if ( ! $cff || $cff == $cf ) {
447 continue;
448 }
449 ?>
450 <option <?php selected( $load_after, $cff->slug, true ); ?> value="<?php echo esc_attr( $slug ); ?>"><?php echo esc_attr( $cff->name ); ?></option>
451 <?php
452 }
453 ?>
454 <option value="__END__">-- <?php esc_attr_e( 'END', 'supportcandy' ); ?> --</option>
455 </select>
456 <script>jQuery('select.load-after').selectWoo();</script>
457 </div>
458 <?php
459 do_action( 'wpsc_get_edit_tff', $field, $cf );
460
461 if ( ! ( $cf->slug == 'name' || $cf->slug == 'email' ) ) {
462 WPSC_Ticket_Conditions::print( 'visibility', 'wpsc_visibility_conditions', $field['visibility'], false, __( 'Visibility conditions', 'supportcandy' ) );
463 }
464 ?>
465 <input type="hidden" name="id" value="<?php echo esc_attr( $cf->id ); ?>">
466 <input type="hidden" name="action" value="wpsc_set_edit_tff">
467 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_tff' ) ); ?>">
468 </form>
469 <?php
470 $body = ob_get_clean();
471
472 ob_start();
473 ?>
474 <button class="wpsc-button small primary" onclick="wpsc_set_edit_tff(this);">
475 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
476 </button>
477 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
478 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
479 </button>
480 <?php
481 $footer = ob_get_clean();
482
483 $response = array(
484 'title' => $title,
485 'body' => $body,
486 'footer' => $footer,
487 );
488 wp_send_json( $response );
489 }
490
491 /**
492 * Set edit data
493 *
494 * @return void
495 */
496 public static function set_edit_tff() {
497
498 if ( check_ajax_referer( 'wpsc_set_edit_tff', '_ajax_nonce', false ) != 1 ) {
499 wp_send_json_error( 'Unauthorised request!', 401 );
500 }
501
502 if ( ! WPSC_Functions::is_site_admin() ) {
503 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
504 }
505
506 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
507 if ( ! $id ) {
508 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
509 }
510
511 $tff = get_option( 'wpsc-tff', array() );
512 $cf = new WPSC_Custom_Field( $id );
513 $load_after = isset( $_POST['load-after'] ) ? sanitize_text_field( wp_unslash( $_POST['load-after'] ) ) : '__END__';
514
515 if (
516 ! $cf->id ||
517 ! $cf->type::$is_ctf ||
518 ! isset( $tff[ $cf->slug ] )
519 ) {
520 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
521 }
522
523 $visibility = isset( $_POST['visibility'] ) ? sanitize_text_field( wp_unslash( $_POST['visibility'] ) ) : '';
524 if ( ! WPSC_Ticket_Conditions::is_valid_input_conditions( 'wpsc_visibility_conditions', $visibility ) ) {
525 wp_send_json_error( 'Bad request', 400 );
526 }
527
528 // unset from tff so that load after should work.
529 unset( $tff[ $cf->slug ] );
530
531 $field = apply_filters(
532 'wpsc_set_edit_tff',
533 array(
534 'is-required' => isset( $_POST['is-required'] ) ? intval( $_POST['is-required'] ) : 1,
535 'width' => isset( $_POST['width'] ) ? sanitize_text_field( wp_unslash( $_POST['width'] ) ) : 'full',
536 'visibility' => $visibility,
537 ),
538 $cf
539 );
540
541 // set load after.
542 switch ( $load_after ) {
543
544 case '__TOP__':
545 $tff = array_merge( array( $cf->slug => $field ), $tff );
546 break;
547
548 case '__END__':
549 $tff = array_merge( $tff, array( $cf->slug => $field ) );
550 break;
551
552 default:
553 $load_after = WPSC_Custom_Field::get_cf_by_slug( $load_after );
554 if ( ! $load_after || ! isset( $tff[ $load_after->slug ] ) ) {
555 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
556 }
557 $offset = array_search( $load_after->slug, array_keys( $tff ) ) + 1;
558 $arr1 = array_slice( $tff, 0, $offset );
559 $arr2 = array_slice( $tff, $offset );
560 $tff = array_merge( $arr1, array( $cf->slug => $field ), $arr2 );
561 break;
562 }
563
564 update_option( 'wpsc-tff', $tff );
565 wp_die();
566 }
567
568 /**
569 * Delete form field
570 *
571 * @return void
572 */
573 public static function delete_tff() {
574
575 if ( check_ajax_referer( 'wpsc_delete_tff', '_ajax_nonce', false ) != 1 ) {
576 wp_send_json_error( 'Unauthorised request!', 401 );
577 }
578
579 if ( ! WPSC_Functions::is_site_admin() ) {
580 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
581 }
582
583 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
584 if ( ! $id ) {
585 wp_send_json_error( 'Unauthorised request!', 401 );
586 }
587
588 $tff = get_option( 'wpsc-tff', array() );
589
590 $cf = new WPSC_Custom_Field( $id );
591 if ( ! $cf->id || ! $cf->type::$is_ctf || ! in_array( $cf->field, array( 'customer', 'ticket' ) ) || ! isset( $tff[ $cf->slug ] ) ) {
592 wp_send_json_error( 'Unauthorised request!', 401 );
593 }
594
595 unset( $tff[ $cf->slug ] );
596 update_option( 'wpsc-tff', $tff );
597 wp_die();
598 }
599
600 /**
601 * Return JSON object for tff
602 *
603 * @param WPSC_TFF $tff - ticket form field.
604 * @param boolean $is_array - is array.
605 * @return array
606 */
607 public static function get_visibility( $tff, $is_array = false ) {
608
609 $visibility = $tff['visibility'] ? html_entity_decode( $tff['visibility'] ) : '';
610 if ( $is_array ) {
611 return $visibility ? json_decode( $visibility, true ) : array();
612 } else {
613 return $visibility ? json_decode( $visibility ) : new stdClass();
614 }
615 }
616
617 /**
618 * Remove tff if related custom field got deleted
619 *
620 * @param WPSC_CF $cf - tff custom field.
621 * @return void
622 */
623 public static function delete_custom_field( $cf ) {
624
625 $tff = get_option( 'wpsc-tff', array() );
626 if ( array_key_exists( $cf->slug, $tff ) ) {
627 unset( $tff[ $cf->slug ] );
628 update_option( 'wpsc-tff', $tff );
629 }
630 }
631
632 /**
633 * Filter conditions for visibility
634 *
635 * @param array $conditions - conditions to filter.
636 * @return array
637 */
638 public static function visibility_conditions( $conditions ) {
639
640 foreach ( $conditions as $slug => $item ) {
641
642 if ( $item['type'] == 'cf' ) {
643
644 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
645 if (
646 ! $cf->type::$is_visibility_conditions ||
647 ! in_array( $cf->field, array( 'ticket', 'customer' ) )
648 ) {
649 unset( $conditions[ $slug ] );
650 }
651 } else { // not custom field type.
652
653 unset( $conditions[ $slug ] );
654 }
655 }
656
657 return $conditions;
658 }
659 }
660 endif;
661
662 WPSC_TFF::init();
663