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

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