PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.1
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / 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.1, at includes/custom-field-types/class-wpsc-df-subject.php

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