PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.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.4.4, at includes/custom-field-types/class-wpsc-df-subject.php

866 lines 26.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_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
186 /**
187 * Load current class to reference classes
188 *
189 * @param array $classes - Associative array of class names indexed by its slug.
190 * @return array
191 */
192 public static function load_ref_class( $classes ) {
193
194 $classes[ self::$slug ] = array(
195 'class' => __CLASS__,
196 'save-key' => 'id',
197 );
198 return $classes;
199 }
200
201 /**
202 * Print operators for ticket form filter
203 *
204 * @param WPSC_Custom_Field $cf - custom field object.
205 * @param array $filter - Existing filters (if any).
206 * @return void
207 */
208 public static function get_operators( $cf, $filter = array() ) {
209 ?>
210
211 <div class="item conditional">
212 <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' ) ); ?>');">
213 <option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option>
214 <option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'LIKE' ); ?> value="LIKE"><?php esc_attr_e( 'Has Words', 'supportcandy' ); ?></option>
215 </select>
216 </div>
217 <?php
218 }
219
220 /**
221 * Print operators for ticket form filter
222 *
223 * @param string $operator - condition operator on which operands should be returned.
224 * @param WPSC_Custom_Field $cf - custom field object.
225 * @param array $filter - Exising functions (if any).
226 * @return void
227 */
228 public static function get_operands( $operator, $cf, $filter = array() ) {
229
230 $value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : ''
231 ?>
232 <div class="item conditional operand single">
233 <textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea>
234 </div>
235 <?php
236 }
237
238 /**
239 * Check condition for this type
240 *
241 * @param array $condition - condition data.
242 * @param WPSC_Custom_Field $cf - custom field object.
243 * @param mixed $value - value to compare.
244 * @return boolean
245 */
246 public static function is_valid( $condition, $cf, $value ) {
247
248 $response = false;
249 $value = stripslashes( $value );
250 $terms = explode( PHP_EOL, $condition['operand_val_1'] );
251 $response = false;
252 foreach ( $terms as $term ) {
253 $index = strpos( $value, trim( stripslashes( $term ) ) );
254 if ( is_numeric( $index ) ) {
255 $response = true;
256 break;
257 }
258 }
259 return $response;
260 }
261
262 /**
263 * Parse filter and return sql query to be merged in ticket model query builder
264 *
265 * @param WPSC_Custom_Field $cf - custom field of this type.
266 * @param mixed $compare - comparison operator.
267 * @param mixed $val - value to compare.
268 * @return string
269 */
270 public static function parse_filter( $cf, $compare, $val ) {
271
272 $str = '';
273
274 switch ( $compare ) {
275
276 case 'LIKE':
277 $arr = array();
278 $val = explode( PHP_EOL, $val );
279 foreach ( $val as $term ) {
280 $term = str_replace( '*', '%', trim( $term ) );
281 $arr[] = 'CONVERT(t.' . $cf->slug . ' USING utf8) LIKE \'%' . $term . '%\'';
282 }
283 $str = '(' . implode( ' OR ', $arr ) . ')';
284 break;
285
286 default:
287 $str = '1=1';
288 }
289
290 return $str;
291 }
292
293 /**
294 * Check ticket condition
295 *
296 * @param array $condition - array with condition data.
297 * @param WPSC_Custom_Field $cf - custom field object.
298 * @param WPSC_Ticket $ticket - ticket object.
299 * @return boolean
300 */
301 public static function is_valid_ticket_condition( $condition, $cf, $ticket ) {
302
303 $flag = true;
304 $value = stripslashes( $ticket->{$cf->slug} );
305 $terms = explode( PHP_EOL, $condition['operand_val_1'] );
306
307 switch ( $condition['operator'] ) {
308
309 case 'LIKE':
310 $flag = false;
311 foreach ( $terms as $term ) {
312 $index = strpos( $value, trim( stripslashes( $term ) ) );
313 if ( is_numeric( $index ) ) {
314 $flag = true;
315 break;
316 }
317 }
318 break;
319
320 default:
321 $flag = true;
322 }
323
324 return $flag;
325 }
326
327 /**
328 * Add ticket search compatibility for fields of this custom field type.
329 *
330 * @param array $sql - Array of sql peices that can be joined later.
331 * @param array $filter - User filter.
332 * @param array $custom_fields - Custom fields array applicable for search.
333 * @param string $search - search string.
334 * @param array $allowed_search_fields - Allowed search fields.
335 * @return array
336 */
337 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
338
339 if ( in_array( 'subject', $allowed_search_fields ) ) {
340 $sql[] = 'CONVERT(t.subject USING utf8) LIKE \'%' . $search . '%\'';
341 }
342 return $sql;
343 }
344
345 /**
346 * Print ticket form field
347 *
348 * @param WPSC_Custom_Field $cf - Custom field object.
349 * @param array $tff - Array of ticket form field settings for this field.
350 * @return string
351 */
352 public static function print_tff( $cf, $tff ) {
353
354 $val = $cf->is_auto_fill && $cf->default_value ? stripslashes( htmlspecialchars( $cf->default_value[0] ) ) : '';
355
356 ob_start();
357 ?>
358 <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 ); ?>">
359 <div class="wpsc-tff-label">
360 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
361 <?php
362 if ( $tff['is-required'] ) {
363 ?>
364 <span class="required-indicator">*</span>
365 <?php
366 }
367 ?>
368 </div>
369 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
370 <input
371 type="text"
372 name="<?php echo esc_attr( $cf->slug ); ?>"
373 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"
374 maxlength="<?php echo $cf->char_limit ? intval( $cf->char_limit ) : ''; ?>"
375 value="<?php echo esc_attr( $val ); ?>"
376 onchange="wpsc_check_tff_visibility()"
377 autocomplete="off"/>
378 </div>
379 <?php
380 return ob_get_clean();
381 }
382
383 /**
384 * Validate this type field in create ticket
385 *
386 * @return void
387 */
388 public static function js_validate_ticket_form() {
389 ?>
390
391 case '<?php echo esc_attr( self::$slug ); ?>':
392 var val = customField.find('input').first().val().trim();
393 if (customField.hasClass('required') && !val) {
394 isValid = false;
395 alert(supportcandy.translations.req_fields_missing);
396 }
397 break;
398 <?php
399 echo PHP_EOL;
400 }
401
402 /**
403 * Return custom field value in $_POST
404 * Ignore phpcs nonce issue as we already checked where it is called from.
405 *
406 * @param string $slug - Custom field slug.
407 * @param mixed $cf - Custom field object or false.
408 * @return mixed
409 */
410 public static function get_tff_value( $slug, $cf = false ) {
411
412 return isset( $_POST[ $slug ] ) ? sanitize_text_field( wp_unslash( $_POST[ $slug ] ) ) : ''; // phpcs:ignore
413 }
414
415 /**
416 * Return default value for custom field of this type
417 *
418 * @param WPSC_Custom_Field $cf - custom field object.
419 * @return mixed
420 */
421 public static function get_default_value( $cf ) {
422
423 return $cf->default_value ? $cf->default_value[0] : '';
424 }
425
426 /**
427 * Check and return custom field value for new ticket to be created.
428 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
429 * Ignore phpcs nonce issue as we already checked where it is called from.
430 *
431 * @param array $data - Array of values to to stored in ticket in an insert function.
432 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
433 * @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.
434 * @return array
435 */
436 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
437
438 $value = self::get_tff_value( 'subject' );
439 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
440
441 if (
442 $cf->char_limit && mb_strlen( $value ) > $cf->char_limit
443 ) {
444 wp_send_json_error( new WP_Error( 'WPSC_DF_Subject', 'Character limit exceed!!' ), 400 );
445 }
446
447 $default_val = self::get_default_value( $cf );
448 if (
449 ! $value && ! $default_val
450 ) {
451 wp_send_json_error( new WP_Error( 'WPSC_DF_Subject', 'You must enter subject!!' ), 400 );
452 }
453
454 $data['subject'] = ! $value && $default_val ? $default_val : $value;
455 return $data;
456 }
457
458 /**
459 * Set create ticket data for rest api request
460 *
461 * @param array $data - create ticket data array.
462 * @param WP_REST_Request $request - rest request object.
463 * @param array $custom_fields - custom field objects indexed by unique custom field types.
464 * @return array
465 */
466 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
467
468 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
469 $subject = sanitize_text_field( $request->get_param( 'subject' ) );
470 $subject = $subject ? $subject : self::get_default_value( $cf );
471
472 // check subject is present.
473 if ( ! $subject ) {
474 $data['errors']->add( 'req_fields_missing', 'subject is missing!', 'subject' );
475 return $data;
476 }
477
478 // check char limit.
479 if ( $cf->char_limit && strlen( $subject ) > $cf->char_limit ) {
480 $data['errors']->add( 'char_limit', 'character limit for subject is exceeded!', 'subject' );
481 return $data;
482 }
483
484 $data['subject'] = $subject;
485 return $data;
486 }
487
488 /**
489 * Return slug string to be used in where condition of ticket model for this type of field
490 *
491 * @param WPSC_Custom_Field $cf - custom field object.
492 * @return string
493 */
494 public static function get_sql_slug( $cf ) {
495
496 return 't.' . $cf->slug;
497 }
498
499 /**
500 * Return val field for meta query of this type of custom field
501 *
502 * @param array $condition - condition data.
503 * @return mixed
504 */
505 public static function get_meta_value( $condition ) {
506
507 $operator = $condition['operator'];
508 switch ( $operator ) {
509
510 case 'LIKE':
511 return trim( $condition['operand_val_1'] );
512 }
513 return false;
514 }
515
516 /**
517 * Get edit ticket subject
518 *
519 * @return void
520 */
521 public static function it_get_edit_subject() {
522
523 WPSC_Individual_Ticket::load_current_ticket();
524 $current_user = WPSC_Current_User::$current_user;
525 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) {
526 wp_send_json_error( 'Something went wrong!', 401 );
527 }
528
529 $ticket = WPSC_Individual_Ticket::$ticket;
530 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
531 $title = $cf->name;
532
533 ob_start();
534 ?>
535 <form action="#" onsubmit="return false;" class="edit-subject">
536 <div class="wpsc-input-group">
537 <input name="subject" type="text" value="<?php echo esc_attr( $ticket->subject ); ?>" autocomplete="off">
538 </div>
539 <?php do_action( 'wpsc_get_edit_subject_body' ); ?>
540 <input type="hidden" name="action" value="wpsc_it_set_edit_subject">
541 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
542 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_subject' ) ); ?>">
543 </form>
544 <?php
545 $body = ob_get_clean();
546
547 ob_start();
548 ?>
549 <button class="wpsc-button small primary" onclick="wpsc_it_set_edit_subject(this, <?php echo esc_attr( $ticket->id ); ?>);">
550 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
551 </button>
552 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
553 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
554 </button>
555 <?php
556 do_action( 'wpsc_get_edit_subject' );
557 $footer = ob_get_clean();
558
559 $response = array(
560 'title' => $title,
561 'body' => $body,
562 'footer' => $footer,
563 );
564 wp_send_json( $response );
565 }
566
567 /**
568 * Set edit subject for a ticket
569 *
570 * @return void
571 */
572 public static function it_set_edit_subject() {
573
574 if ( check_ajax_referer( 'wpsc_it_set_edit_subject', '_ajax_nonce', false ) != 1 ) {
575 wp_send_json_error( 'Unauthorized request!', 401 );
576 }
577
578 WPSC_Individual_Ticket::load_current_ticket();
579 $current_user = WPSC_Current_User::$current_user;
580 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) {
581 wp_send_json_error( 'Something went wrong!', 401 );
582 }
583
584 $ticket = WPSC_Individual_Ticket::$ticket;
585 if ( ! $ticket->is_active ) {
586 wp_send_json_error( 'Something went wrong!', 400 );
587 }
588
589 $cf = WPSC_Custom_Field::get_cf_by_slug( 'subject' );
590
591 $new = isset( $_POST['subject'] ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '';
592 if ( ! $new ) {
593 wp_send_json_error( 'Something went wrong!', 400 );
594 }
595
596 $prev = $ticket->subject;
597
598 // Exit if no change.
599 if ( $new == $prev ) {
600 wp_die();
601 }
602
603 $ticket->subject = $new;
604 $ticket->date_updated = new DateTime();
605 $ticket->save();
606
607 do_action( 'wpsc_change_ticket_subject', $ticket, $prev, $new, $current_user->customer->id );
608 wp_die();
609 }
610
611 /**
612 * Return data for this custom field while creating duplicate ticket
613 *
614 * @param WPSC_Custom_Field $cf - custom field object.
615 * @param WPSC_Ticket $ticket - ticket object.
616 * @return mixed
617 */
618 public static function get_duplicate_ticket_data( $cf, $ticket ) {
619
620 $subject = self::get_tff_value( 'subject' );
621 if ( ! $subject ) {
622 wp_send_json_error( 'Something went wrong!', 400 );
623 }
624 return $subject;
625 }
626
627 /**
628 * Print edit custom field properties
629 *
630 * @param WPSC_Custom_Fields $cf - custom field object.
631 * @param string $field_class - class name of field category.
632 * @return void
633 */
634 public static function get_edit_custom_field_properties( $cf, $field_class ) {
635
636 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
637 ?>
638 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
639 <div class="label-container">
640 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
641 </div>
642 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
643 </div>
644 <?php
645 endif;
646
647 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) :
648 $default_val = $cf->default_value ? $cf->default_value[0] : '';
649 ?>
650 <div data-type="textfield" data-required="false" class="wpsc-input-group default_value">
651 <div class="label-container">
652 <label for="">
653 <?php esc_attr_e( 'Default value', 'supportcandy' ); ?>
654 </label>
655 </div>
656 <input type="text" name="default_value" autocomplete="off" value="<?php echo esc_attr( $default_val ); ?>">
657 </div>
658 <?php
659 endif;
660
661 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) :
662 ?>
663 <div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill">
664 <div class="label-container">
665 <label for="">
666 <?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?>
667 </label>
668 </div>
669 <select name="is_auto_fill">
670 <option <?php selected( $cf->is_auto_fill, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
671 <option <?php selected( $cf->is_auto_fill, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
672 </select>
673 </div>
674 <?php
675 endif;
676
677 if ( in_array( 'char_limit', $field_class::$allowed_properties ) ) :
678 ?>
679 <div data-type="number" data-required="false" class="wpsc-input-group char_limit">
680 <div class="label-container">
681 <label for="">
682 <?php esc_attr_e( 'Character limit', 'supportcandy' ); ?>
683 </label>
684 </div>
685 <input type="number" name="char_limit" value="<?php echo intval( $cf->char_limit ); ?>" autocomplete="off">
686 </div>
687 <?php
688 endif;
689
690 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
691 ?>
692 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
693 <div class="label-container">
694 <label for="">
695 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
696 </label>
697 </div>
698 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
699 </div>
700 <?php
701 endif;
702
703 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :
704 ?>
705 <div data-type="number" data-required="false" class="wpsc-input-group tl_width">
706 <div class="label-container">
707 <label for="">
708 <?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?>
709 </label>
710 </div>
711 <input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off">
712 </div>
713 <?php
714 endif;
715 }
716
717 /**
718 * Set custom field properties. Can be used by add/edit custom field.
719 * Ignore phpcs nonce issue as we already checked where it is called from.
720 *
721 * @param WPSC_Custom_Field $cf - custom field object.
722 * @param string $field_class - class of field category.
723 * @return void
724 */
725 public static function set_cf_properties( $cf, $field_class ) {
726
727 // extra info.
728 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
729 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
730 }
731
732 // default value.
733 if ( in_array( 'default_value', $field_class::$allowed_properties ) ) {
734 $default_value = isset( $_POST['default_value'] ) ? sanitize_text_field( wp_unslash( $_POST['default_value'] ) ) : ''; // phpcs:ignore
735 $cf->default_value = $default_value ? array( $default_value ) : array();
736 }
737
738 // auto fill.
739 if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) {
740 $cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore
741 }
742
743 // char limit.
744 if ( in_array( 'char_limit', $field_class::$allowed_properties ) ) {
745 $cf->char_limit = isset( $_POST['char_limit'] ) ? intval( $_POST['char_limit'] ) : 0; //phpcs:ignore
746 }
747
748 // check whether char limit is honored by default value.
749 if ( $cf->default_value && $cf->char_limit && count( $cf->default_value ) > $cf->char_limit ) {
750 wp_send_json_error( 'Incorrect request!', 400 );
751 }
752
753 // placeholder!
754 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
755 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
756 }
757
758 // tl_width!
759 if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) {
760 $tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore
761 $cf->tl_width = $tl_width ? $tl_width : 100;
762 }
763
764 // save!
765 $cf->save();
766 }
767
768 /**
769 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
770 *
771 * @param WPSC_Custom_Field $cf - custom field object.
772 * @param WPSC_Ticket $ticket - ticket object.
773 * @param string $module - module name.
774 * @return string
775 */
776 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
777
778 return apply_filters( 'wpsc_ticket_field_val_subject', $ticket->subject, $cf, $ticket, $module );
779 }
780
781 /**
782 * Print ticket value for given custom field on ticket list
783 *
784 * @param WPSC_Custom_Field $cf - custom field object.
785 * @param WPSC_Ticket $ticket - ticket object.
786 * @return void
787 */
788 public static function print_tl_ticket_field_val( $cf, $ticket ) {
789
790 echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) );
791 }
792
793 /**
794 * Print given value for custom field
795 *
796 * @param WPSC_Custom_Field $cf - custom field object.
797 * @param mixed $val - value to convert and print.
798 * @return void
799 */
800 public static function print_val( $cf, $val ) {
801
802 echo esc_attr( $val );
803 }
804
805 /**
806 * Return printable value for history log macro
807 *
808 * @param WPSC_Custom_Field $cf - custom field object.
809 * @param mixed $val - value to convert and return.
810 * @return string
811 */
812 public static function get_history_log_val( $cf, $val ) {
813
814 return esc_attr( $val );
815 }
816
817 /**
818 * Print dashboard activity function
819 *
820 * @param WPSC_Custom_Field $cf - custom field object.
821 * @param array $recent_logs - recent_logs object.
822 * @param array $body - body object.
823 * @param int $view - check if frontend.
824 * @return string
825 */
826 public static function print_activity( $cf, $recent_logs, $body, $view ) {
827
828 $url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view );
829 if ( $body->new ) {
830 $value = $body->new;
831 } else {
832 $value = 'None';
833 }
834 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 );
835 }
836
837 /**
838 * Return given value for custom field
839 *
840 * @param WPSC_Custom_Field $cf - custom field object.
841 * @param mixed $val - value to convert and print.
842 * @return string
843 */
844 public static function get_field_value( $cf, $val ) {
845
846 return esc_attr( $val );
847 }
848
849 /**
850 * Clear value of hidden fields
851 *
852 * @return void
853 */
854 public static function js_clear_value_hidden_fields() {
855 ?>
856 case '<?php echo esc_attr( self::$slug ); ?>':
857 customField.find('input').first().val('');
858 break;
859 <?php
860 echo PHP_EOL;
861 }
862 }
863 endif;
864
865 WPSC_DF_Subject::init();
866