PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
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 / custom-field-types / class-wpsc-df-subject.php

class-wpsc-df-subject.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/custom-field-types/class-wpsc-df-subject.php

897 lines 26.9 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_DF_Subject' ) ) :
7
8 final class WPSC_DF_Subject {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_subject';
16
17 /**
18 * Set whether this custom field type is of type date
19 *
20 * @var boolean
21 */
22 public static $is_date = false;
23
24 /**
25 * Set whether this custom field type has applicable to date range
26 *
27 * @var boolean
28 */
29 public static $has_date_range = false;
30
31 /**
32 * Set whether this custom field type has multiple values
33 *
34 * @var boolean
35 */
36 public static $has_multiple_val = false;
37
38 /**
39 * Data type for column created in tickets table
40 *
41 * @var string
42 */
43 public static $data_type = 'TEXT NOT NULL';
44
45 /**
46 * Set whether this custom field type has reference to other class
47 *
48 * @var boolean
49 */
50 public static $has_ref = false;
51
52 /**
53 * Reference class for this custom field type so that its value(s) return with object or array of objects automatically. Empty string indicate no reference.
54 *
55 * @var string
56 */
57 public static $ref_class = '';
58
59 /**
60 * Set whether this custom field field type is system default (no fields can be created from it).
61 *
62 * @var boolean
63 */
64 public static $is_default = true;
65
66 /**
67 * Set whether this field type has extra information that can be used in ticket form, edit custom fields, etc.
68 *
69 * @var boolean
70 */
71 public static $has_extra_info = true;
72
73 /**
74 * Set whether this custom field type can accept personal info.
75 *
76 * @var boolean
77 */
78 public static $has_personal_info = false;
79
80 /**
81 * Set whether fields created from this custom field type is allowed in create ticket form
82 *
83 * @var boolean
84 */
85 public static $is_ctf = true;
86
87 /**
88 * Set whether fields created from this custom field type is allowed in ticket list
89 *
90 * @var boolean
91 */
92 public static $is_list = true;
93
94 /**
95 * Set whether fields created from this custom field type is allowed in ticket filter
96 *
97 * @var boolean
98 */
99 public static $is_filter = true;
100
101 /**
102 * Set whether fields created from this custom field type can be given character limits
103 *
104 * @var boolean
105 */
106 public static $has_char_limit = true;
107
108 /**
109 * Set whether fields created from this custom field type has custom options set in options table
110 *
111 * @var boolean
112 */
113 public static $has_options = false;
114
115 /**
116 * Set whether fields created from this custom field type can be auto-filled
117 *
118 * @var boolean
119 */
120 public static $is_auto_fill = true;
121
122 /**
123 * Set whether fields created from this custom field type can be available for ticket list sorting
124 *
125 * @var boolean
126 */
127 public static $is_sort = false;
128
129 /**
130 * Set whether fields created from this custom field type can have placeholder
131 *
132 * @var boolean
133 */
134 public static $is_placeholder = true;
135
136 /**
137 * Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form
138 *
139 * @var boolean
140 */
141 public static $is_visibility_conditions = true;
142
143 /**
144 * Set whether fields created from this custom field type is applicable for macros
145 *
146 * @var boolean
147 */
148 public static $has_macro = true;
149
150 /**
151 * Set whether fields of this custom field type is applicalbe for search on ticket list page.
152 *
153 * @var boolean
154 */
155 public static $is_search = true;
156
157 /**
158 * Initialize the class
159 *
160 * @return void
161 */
162 public static function init() {
163
164 // Get object of this class.
165 add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) );
166
167 // ticket form.
168 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
169 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
170 add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) );
171
172 // create ticket data for rest api.
173 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
174
175 // individual ticket.
176 add_action( 'wp_ajax_wpsc_it_get_edit_subject', array( __CLASS__, 'it_get_edit_subject' ) );
177 add_action( 'wp_ajax_nopriv_wpsc_it_get_edit_subject', array( __CLASS__, 'it_get_edit_subject' ) );
178 add_action( 'wp_ajax_wpsc_it_set_edit_subject', array( __CLASS__, 'it_set_edit_subject' ) );
179 add_action( 'wp_ajax_nopriv_wpsc_it_set_edit_subject', array( __CLASS__, 'it_set_edit_subject' ) );
180
181 // ticket search query.
182 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
183 add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
184
185 // Set custom field type.
186 add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 );
187 }
188
189 /**
190 * Load current class to reference classes
191 *
192 * @param array $classes - Associative array of class names indexed by its slug.
193 * @return array
194 */
195 public static function load_ref_class( $classes ) {
196
197 $classes[ self::$slug ] = array(
198 'class' => __CLASS__,
199 'save-key' => 'id',
200 );
201 return $classes;
202 }
203
204 /**
205 * Add default custom field type to list
206 *
207 * @param array $default_cf_types - default custom field types array.
208 * @return array
209 */
210 public static function default_cf_types( $default_cf_types ) {
211 $default_cf_types[ self::$slug ] = array(
212 'label' => esc_attr__( 'Subject', 'supportcandy' ),
213 'class' => __CLASS__,
214 );
215 return $default_cf_types;
216 }
217
218 /**
219 * Print operators for ticket form filter
220 *
221 * @param WPSC_Custom_Field $cf - custom field object.
222 * @param array $filter - Existing filters (if any).
223 * @return void
224 */
225 public static function get_operators( $cf, $filter = array() ) {
226 ?>
227
228 <div class="item conditional">
229 <select class="operator" onchange="wpsc_tc_get_operand(this, '<?php echo esc_attr( $cf->slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_tc_get_operand' ) ); ?>');">
230 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
231 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'LIKE' ); ?> value="LIKE"><?php esc_attr_e( 'Has Words', 'supportcandy' ); ?></option>
232 </select>
233 </div>
234 <?php
235 }
236
237 /**
238 * Print operators for ticket form filter
239 *
240 * @param string $operator - condition operator on which operands should be returned.
241 * @param WPSC_Custom_Field $cf - custom field object.
242 * @param array $filter - Exising functions (if any).
243 * @return void
244 */
245 public static function get_operands( $operator, $cf, $filter = array() ) {
246
247 $value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : ''
248 ?>
249 <div class="item conditional operand single">
250 <textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea>
251 </div>
252 <?php
253 }
254
255 /**
256 * Check condition for this type
257 *
258 * @param array $condition - condition data.
259 * @param WPSC_Custom_Field $cf - custom field object.
260 * @param mixed $value - value to compare.
261 * @return boolean
262 */
263 public static function is_valid( $condition, $cf, $value ) {
264
265 $response = false;
266 $value = stripslashes( $value );
267 $terms = explode( PHP_EOL, $condition['operand_val_1'] );
268 $response = false;
269 foreach ( $terms as $term ) {
270 $index = strpos( $value, trim( stripslashes( $term ) ) );
271 if ( is_numeric( $index ) ) {
272 $response = true;
273 break;
274 }
275 }
276 return $response;
277 }
278
279 /**
280 * Parse filter and return sql query to be merged in ticket model query builder
281 *
282 * @param WPSC_Custom_Field $cf - custom field of this type.
283 * @param mixed $compare - comparison operator.
284 * @param mixed $val - value to compare.
285 * @return string
286 */
287 public static function parse_filter( $cf, $compare, $val ) {
288
289 $slug = is_string( $cf->slug ) ? trim( $cf->slug ) : '';
290 if ( $slug === '' || ! preg_match( '/^[a-zA-Z0-9_]+$/', $slug ) ) {
291 return '1=0';
292 }
293
294 $column = 'CONVERT(t.' . $slug . ' USING utf8)';
295 switch ( $compare ) {
296
297 case 'LIKE':
298 if ( ! is_string( $val ) || trim( $val ) === '' ) {
299 return '1=0';
300 }
301
302 $terms = preg_split( "/\r\n|\n|\r/", $val );
303 $likes = array();
304
305 foreach ( $terms as $term ) {
306 $term = trim( $term );
307
308 if ( $term === '' ) {
309 continue;
310 }
311 $term = str_replace( '*', '%', $term );
312 $term = esc_sql( $term );
313 $likes[] = $column . " LIKE '%" . $term . "%'";
314 }
315
316 if ( empty( $likes ) ) {
317 return '1=0';
318 }
319 return '(' . implode( ' OR ', $likes ) . ')';
320 default:
321 return '1=1';
322 }
323 }
324
325 /**
326 * Check ticket condition
327 *
328 * @param array $condition - array with condition data.
329 * @param WPSC_Custom_Field $cf - custom field object.
330 * @param WPSC_Ticket $ticket - ticket object.
331 * @return boolean
332 */
333 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
334
335 $flag = true;
336 $value = stripslashes( $ticket->{$cf->slug} );
337 $terms = explode( PHP_EOL, $condition['operand_val_1'] );
338
339 switch ( $condition['operator'] ) {
340
341 case 'LIKE':
342 $flag = false;
343 foreach ( $terms as $term ) {
344 $index = strpos( $value, trim( stripslashes( $term ) ) );
345 if ( is_numeric( $index ) ) {
346 $flag = true;
347 break;
348 }
349 }
350 break;
351
352 default:
353 $flag = true;
354 }
355
356 return $flag;
357 }
358
359 /**
360 * Add ticket search compatibility for fields of this custom field type.
361 *
362 * @param array $sql - Array of sql peices that can be joined later.
363 * @param array $filter - User filter.
364 * @param array $custom_fields - Custom fields array applicable for search.
365 * @param string $search - search string.
366 * @param array $allowed_search_fields - Allowed search fields.
367 * @return array
368 */
369 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
370
371 if ( in_array( 'subject', $allowed_search_fields ) ) {
372 $sql[] = 'CONVERT(t.subject USING utf8) LIKE \'%' . $search . '%\'';
373 }
374 return $sql;
375 }
376
377 /**
378 * Print ticket form field
379 *
380 * @param WPSC_Custom_Field $cf - Custom field object.
381 * @param array $tff - Array of ticket form field settings for this field.
382 * @return string
383 */
384 public static function print_tff( $cf, $tff ) {
385
386 $val = $cf->is_auto_fill && $cf->default_value ? stripslashes( htmlspecialchars( $cf->default_value[0] ) ) : '';
387
388 ob_start();
389 ?>
390 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>" data-slug="<?php echo esc_attr( $cf->slug ); ?>">
391 <div class="wpsc-tff-label">
392 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
393 <?php
394 if ( $tff['is-required'] ) {
395 ?>
396 <span class="required-indicator">*</span>
397 <?php
398 }
399 ?>
400 </div>
401 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
402 <input
403 type="text"
404 name="<?php echo esc_attr( $cf->slug ); ?>"
405 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"
406 maxlength="<?php echo $cf->char_limit ? intval( $cf->char_limit ) : ''; ?>"
407 value="<?php echo esc_attr( $val ); ?>"
408 onchange="wpsc_check_tff_visibility()"
409 autocomplete="off"/>
410 </div>
411 <?php
412 return ob_get_clean();
413 }
414
415 /**
416 * Validate this type field in create ticket
417 *
418 * @return void
419 */
420 public static function js_validate_ticket_form() {
421 ?>
422
423 case '<?php echo esc_attr( self::$slug ); ?>':
424 var val = customField.find('input').first().val().trim();
425 if (isRequired && !val) {
426 isValid = false;
427 }
428 break;
429 <?php
430 echo PHP_EOL;
431 }
432
433 /**
434 * Return custom field value in $_POST
435 * Ignore phpcs nonce issue as we already checked where it is called from.
436 *
437 * @param string $slug - Custom field slug.
438 * @param mixed $cf - Custom field object or false.
439 * @return mixed
440 */
441 public static function get_tff_value( $slug, $cf = false ) {
442
443 return isset( $_POST[ $slug ] ) ? sanitize_text_field( wp_unslash( $_POST[ $slug ] ) ) : ''; // phpcs:ignore
444 }
445
446 /**
447 * Return default value for custom field of this type
448 *
449 * @param WPSC_Custom_Field $cf - custom field object.
450 * @return mixed
451 */
452 public static function get_default_value( $cf ) {
453
454 return $cf->default_value ? $cf->default_value[0] : '';
455 }
456
457 /**
458 * Check and return custom field value for new ticket to be created.
459 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
460 * Ignore phpcs nonce issue as we already checked where it is called from.
461 *
462 * @param array $data - Array of values to to stored in ticket in an insert function.
463 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
464 * @param boolean $is_my_profile - Whether it or not it is created from my-profile. This function is used by create ticket as well as my-profile. Due to customer fields handling is done same way, this flag gives apportunity to identify where it being called.
465 * @return array
466 */
467 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
468
469 $value = self::get_tff_value( 'subject' );
470 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
471
472 if (
473 $cf->char_limit && mb_strlen( $value ) > $cf->char_limit
474 ) {
475 wp_send_json_error( new WP_Error( 'WPSC_DF_Subject', 'Character limit exceed!!' ), 400 );
476 }
477
478 $default_val = self::get_default_value( $cf );
479 if (
480 ! $value && ! $default_val
481 ) {
482 wp_send_json_error( new WP_Error( 'WPSC_DF_Subject', 'You must enter subject!!' ), 400 );
483 }
484
485 $data['subject'] = ! $value && $default_val ? $default_val : $value;
486 return $data;
487 }
488
489 /**
490 * Set create ticket data for rest api request
491 *
492 * @param array $data - create ticket data array.
493 * @param WP_REST_Request $request - rest request object.
494 * @param array $custom_fields - custom field objects indexed by unique custom field types.
495 * @return array
496 */
497 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
498
499 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
500 $subject = sanitize_text_field( $request->get_param( 'subject' ) );
501 $subject = $subject ? $subject : self::get_default_value( $cf );
502
503 // check subject is present.
504 if ( ! $subject ) {
505 $data['errors']->add( 'req_fields_missing', 'subject is missing!', 'subject' );
506 return $data;
507 }
508
509 // check char limit.
510 if ( $cf->char_limit && strlen( $subject ) > $cf->char_limit ) {
511 $data['errors']->add( 'char_limit', 'character limit for subject is exceeded!', 'subject' );
512 return $data;
513 }
514
515 $data['subject'] = $subject;
516 return $data;
517 }
518
519 /**
520 * Return slug string to be used in where condition of ticket model for this type of field
521 *
522 * @param WPSC_Custom_Field $cf - custom field object.
523 * @return string
524 */
525 public static function get_sql_slug( $cf ) {
526
527 return 't.' . $cf->slug;
528 }
529
530 /**
531 * Return val field for meta query of this type of custom field
532 *
533 * @param array $condition - condition data.
534 * @return mixed
535 */
536 public static function get_meta_value( $condition ) {
537
538 $operator = $condition['operator'];
539 switch ( $operator ) {
540
541 case 'LIKE':
542 return trim( $condition['operand_val_1'] );
543 }
544 return false;
545 }
546
547 /**
548 * Get edit ticket subject
549 *
550 * @return void
551 */
552 public static function it_get_edit_subject() {
553
554 WPSC_Individual_Ticket::load_current_ticket();
555 $current_user = WPSC_Current_User::$current_user;
556 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) {
557 wp_send_json_error( 'Something went wrong!', 401 );
558 }
559
560 $ticket = WPSC_Individual_Ticket::$ticket;
561 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
562 $title = $cf->name;
563
564 ob_start();
565 ?>
566 <form action="#" onsubmit="return false;" class="edit-subject">
567 <div class="wpsc-input-group">
568 <input name="subject" type="text" value="<?php echo esc_attr( $ticket->subject ); ?>" autocomplete="off">
569 </div>
570 <?php do_action( 'wpsc_get_edit_subject_body' ); ?>
571 <input type="hidden" name="action" value="wpsc_it_set_edit_subject">
572 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
573 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_subject' ) ); ?>">
574 </form>
575 <?php
576 $body = ob_get_clean();
577
578 ob_start();
579 ?>
580 <button class="wpsc-button small primary" onclick="wpsc_it_set_edit_subject(this, <?php echo esc_attr( $ticket->id ); ?>);">
581 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
582 </button>
583 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
584 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
585 </button>
586 <?php
587 do_action( 'wpsc_get_edit_subject' );
588 $footer = ob_get_clean();
589
590 $response = array(
591 'title' => $title,
592 'body' => $body,
593 'footer' => $footer,
594 );
595 wp_send_json( $response );
596 }
597
598 /**
599 * Set edit subject for a ticket
600 *
601 * @return void
602 */
603 public static function it_set_edit_subject() {
604
605 if ( check_ajax_referer( 'wpsc_it_set_edit_subject', '_ajax_nonce', false ) != 1 ) {
606 wp_send_json_error( 'Unauthorized request!', 401 );
607 }
608
609 WPSC_Individual_Ticket::load_current_ticket();
610 $current_user = WPSC_Current_User::$current_user;
611 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) {
612 wp_send_json_error( 'Something went wrong!', 401 );
613 }
614
615 $ticket = WPSC_Individual_Ticket::$ticket;
616 if ( ! $ticket->is_active ) {
617 wp_send_json_error( 'Something went wrong!', 400 );
618 }
619
620 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
621
622 $new = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '';
623 if ( ! $new ) {
624 wp_send_json_error( 'Something went wrong!', 400 );
625 }
626
627 $prev = $ticket->subject;
628
629 // Exit if no change.
630 if ( $new == $prev ) {
631 wp_die();
632 }
633
634 $ticket->subject = $new;
635 $ticket->date_updated = new DateTime();
636 $ticket->save();
637
638 do_action( 'wpsc_change_ticket_subject', $ticket, $prev, $new, $current_user->customer->id );
639 wp_die();
640 }
641
642 /**
643 * Return data for this custom field while creating duplicate ticket
644 *
645 * @param WPSC_Custom_Field $cf - custom field object.
646 * @param WPSC_Ticket $ticket - ticket object.
647 * @return mixed
648 */
649 public static function get_duplicate_ticket_data( $cf, $ticket ) {
650
651 $subject = self::get_tff_value( 'subject' );
652 if ( ! $subject ) {
653 wp_send_json_error( 'Something went wrong!', 400 );
654 }
655 return $subject;
656 }
657
658 /**
659 * Print edit custom field properties
660 *
661 * @param WPSC_Custom_Fields $cf - custom field object.
662 * @param string $field_class - class name of field category.
663 * @return void
664 */
665 public static function get_edit_custom_field_properties( $cf, $field_class ) {
666
667 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
668 ?>
669 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
670 <div class="label-container">
671 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
672 </div>
673 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
674 </div>
675 <?php
676 endif;
677
678 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
679 $default_val = $cf->default_value ? $cf->default_value[0] : '';
680 ?>
681 <div data-type="textfield" data-required="false" class="wpsc-input-group default_value">
682 <div class="label-container">
683 <label for="">
684 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
685 </label>
686 </div>
687 <input type="text" name="default_value" autocomplete="off" value="<?php echo esc_attr( $default_val ); ?>">
688 </div>
689 <?php
690 endif;
691
692 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
693 ?>
694 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
695 <div class="label-container">
696 <label for="">
697 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
698 </label>
699 </div>
700 <select name="is_auto_fill">
701 <option <?php selected( $cf->is_auto_fill, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
702 <option <?php selected( $cf->is_auto_fill, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
703 </select>
704 </div>
705 <?php
706 endif;
707
708 if ( in_array( 'char_limit', $field_class::$allowed_properties ) ) :
709 ?>
710 <div data-type="number" data-required="false" class="wpsc-input-group char_limit">
711 <div class="label-container">
712 <label for="">
713 <?php esc_attr_e( 'Character limit', 'supportcandy' ); ?>
714 </label>
715 </div>
716 <input type="number" name="char_limit" value="<?php echo intval( $cf->char_limit ); ?>" autocomplete="off">
717 </div>
718 <?php
719 endif;
720
721 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
722 ?>
723 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
724 <div class="label-container">
725 <label for="">
726 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
727 </label>
728 </div>
729 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
730 </div>
731 <?php
732 endif;
733
734 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
735 ?>
736 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
737 <div class="label-container">
738 <label for="">
739 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
740 </label>
741 </div>
742 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
743 </div>
744 <?php
745 endif;
746 }
747
748 /**
749 * Set custom field properties. Can be used by add/edit custom field.
750 * Ignore phpcs nonce issue as we already checked where it is called from.
751 *
752 * @param WPSC_Custom_Field $cf - custom field object.
753 * @param string $field_class - class of field category.
754 * @return void
755 */
756 public static function set_cf_properties( $cf, $field_class ) {
757
758 // extra info.
759 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
760 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
761 }
762
763 // default value.
764 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) {
765 $default_value = isset( $_POST['default_value'] ) ? sanitize_text_field( wp_unslash( $_POST['default_value'] ) ) : ''; // phpcs:ignore
766 $cf->default_value = $default_value ? array( $default_value ) : array();
767 }
768
769 // auto fill.
770 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) {
771 $cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore
772 }
773
774 // char limit.
775 if ( in_array( 'char_limit', $field_class::$allowed_properties ) ) {
776 $cf->char_limit = isset( $_POST['char_limit'] ) ? intval( $_POST['char_limit'] ) : 0; //phpcs:ignore
777 }
778
779 // check whether char limit is honored by default value.
780 if ( $cf->default_value && $cf->char_limit && count( $cf->default_value ) > $cf->char_limit ) {
781 wp_send_json_error( 'Incorrect request!', 400 );
782 }
783
784 // placeholder!
785 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
786 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
787 }
788
789 // tl_width!
790 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
791 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
792 $cf->tl_width = $tl_width ? $tl_width : 100;
793 }
794
795 // save!
796 $cf->save();
797 }
798
799 /**
800 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
801 *
802 * @param WPSC_Custom_Field $cf - custom field object.
803 * @param WPSC_Ticket $ticket - ticket object.
804 * @param string $module - module name.
805 * @return string
806 */
807 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
808
809 return apply_filters( 'wpsc_ticket_field_val_subject', $ticket->subject, $cf, $ticket, $module );
810 }
811
812 /**
813 * Print ticket value for given custom field on ticket list
814 *
815 * @param WPSC_Custom_Field $cf - custom field object.
816 * @param WPSC_Ticket $ticket - ticket object.
817 * @return void
818 */
819 public static function print_tl_ticket_field_val( $cf, $ticket ) {
820
821 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
822 }
823
824 /**
825 * Print given value for custom field
826 *
827 * @param WPSC_Custom_Field $cf - custom field object.
828 * @param mixed $val - value to convert and print.
829 * @return void
830 */
831 public static function print_val( $cf, $val ) {
832
833 echo esc_attr( $val );
834 }
835
836 /**
837 * Return printable value for history log macro
838 *
839 * @param WPSC_Custom_Field $cf - custom field object.
840 * @param mixed $val - value to convert and return.
841 * @return string
842 */
843 public static function get_history_log_val( $cf, $val ) {
844
845 return esc_attr( $val );
846 }
847
848 /**
849 * Print dashboard activity function
850 *
851 * @param WPSC_Custom_Field $cf - custom field object.
852 * @param array $recent_logs - recent_logs object.
853 * @param array $body - body object.
854 * @param int $view - check if frontend.
855 * @return string
856 */
857 public static function print_activity( $cf, $recent_logs, $body, $view ) {
858
859 $url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view );
860 if ( $body->new ) {
861 $value = $body->new;
862 } else {
863 $value = 'None';
864 }
865 return esc_attr( $recent_logs->customer->name ) . ' updated the ' . esc_attr( $cf->name ) . ' of <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . esc_attr( $value );
866 }
867
868 /**
869 * Return given value for custom field
870 *
871 * @param WPSC_Custom_Field $cf - custom field object.
872 * @param mixed $val - value to convert and print.
873 * @return string
874 */
875 public static function get_field_value( $cf, $val ) {
876
877 return esc_attr( $val );
878 }
879
880 /**
881 * Clear value of hidden fields
882 *
883 * @return void
884 */
885 public static function js_clear_value_hidden_fields() {
886 ?>
887 case '<?php echo esc_attr( self::$slug ); ?>':
888 customField.find('input').first().val('');
889 break;
890 <?php
891 echo PHP_EOL;
892 }
893 }
894 endif;
895
896 WPSC_DF_Subject::init();
897