PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.7
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.7, at includes/admin/custom-fields/class-wpsc-tff.php

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