PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.7
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.7
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-cf-file-attachment-single.php

class-wpsc-cf-file-attachment-single.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.7, at includes/custom-field-types/class-wpsc-cf-file-attachment-single.php

1,110 lines 34.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly!
4 }
5
6 if ( ! class_exists( 'WPSC_CF_File_Attachment_Single' ) ) :
7
8 final class WPSC_CF_File_Attachment_Single {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'cf_file_attachment_single';
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 = 'INT NULL DEFAULT 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 = true;
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 = 'wpsc_attachment';
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 = false;
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 = true;
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 = false;
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 = false;
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 = false;
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 available for ticket list sorting
117 *
118 * @var boolean
119 */
120 public static $is_sort = false;
121
122 /**
123 * Set whether fields created from this custom field type can be auto-filled
124 *
125 * @var boolean
126 */
127 public static $is_auto_fill = 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 = false;
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 = false;
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 // Set custom field type.
168 add_filter( 'wpsc_cf_types', array( __CLASS__, 'add_cf_type' ), 13 );
169
170 // ticket form.
171 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
172 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
173
174 // create ticket data for rest api.
175 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
176
177 // Add ticket id to attachmets after create ticket.
178 add_action( 'wpsc_create_new_ticket', array( __CLASS__, 'add_attachment_ticket_id' ), 1 );
179
180 // ticket search query.
181 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
182 }
183
184 /**
185 * Load current class to reference classes
186 *
187 * @param array $classes - Associative array of class names indexed by its slug.
188 * @return array
189 */
190 public static function load_ref_class( $classes ) {
191
192 $classes[ self::$slug ] = array(
193 'class' => __CLASS__,
194 'save-key' => 'id',
195 );
196 return $classes;
197 }
198
199 /**
200 * Add custom field type to list
201 *
202 * @param array $cf_types - custom field types array.
203 * @return array
204 */
205 public static function add_cf_type( $cf_types ) {
206
207 $cf_types[ self::$slug ] = array(
208 'label' => esc_attr__( 'File Attachment (Single)', 'supportcandy' ),
209 'class' => __CLASS__,
210 );
211 return $cf_types;
212 }
213
214 /**
215 * Print ticket form field
216 *
217 * @param WPSC_Custom_Field $cf - Custom field object.
218 * @param array $tff - Array of ticket form field settings for this field.
219 * @return string
220 */
221 public static function print_tff( $cf, $tff ) {
222
223 $current_user = WPSC_Current_User::$current_user;
224 $unique_id = uniqid( 'wpsc_' );
225 $attachment = new WPSC_Attachment();
226 if ( $cf->field == 'customer' ) {
227 $attachment = $current_user->is_customer && $current_user->customer->{$cf->slug} ? $current_user->customer->{$cf->slug} : new WPSC_Attachment();
228 }
229
230 ob_start();?>
231 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>">
232 <div class="wpsc-tff-label">
233 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
234 <?php
235 if ( $tff['is-required'] ) {
236 ?>
237 <span class="required-indicator">*</span>
238 <?php
239 }
240 ?>
241 </div>
242 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
243 <input
244 class="<?php echo esc_attr( $unique_id ); ?>"
245 type="file"
246 onchange="wpsc_set_attach_single(this, '<?php echo esc_attr( $unique_id ); ?>', '<?php echo esc_attr( $cf->slug ); ?>')"/>
247 <div class="<?php echo esc_attr( $unique_id ); ?> wpsc-editor-attachment-container">
248 <?php
249 if ( $attachment->id ) {
250 ?>
251 <div class="wpsc-editor-attachment upload-success">
252 <div class="attachment-label"><?php echo esc_attr( $attachment->name ); ?></div>
253 <div
254 class="attachment-remove"
255 onclick="wpsc_remove_attachment(this)"
256 data-single="true"
257 data-uniqueid="<?php echo esc_attr( $unique_id ); ?>">
258 <?php WPSC_Icons::get( 'times' ); ?>
259 </div>
260 <input type="hidden" name="<?php echo esc_attr( $cf->slug ); ?>[]" value="<?php echo esc_attr( $attachment->id ); ?>"/>
261 </div>
262 <?php
263 }
264 ?>
265 </div>
266 <?php
267 if ( $attachment->id ) :
268 ?>
269 <script>jQuery('input.<?php echo esc_attr( $unique_id ); ?>').hide();</script>
270 <?php
271 endif;
272 ?>
273 </div>
274 <?php
275 return ob_get_clean();
276 }
277
278 /**
279 * Validate this type field in create ticket
280 *
281 * @return void
282 */
283 public static function js_validate_ticket_form() {
284 ?>
285
286 case '<?php echo esc_attr( self::$slug ); ?>':
287 var count = customField.find('input[type=hidden]').length;
288 if (customField.hasClass('required') && count === 0) {
289 isValid = false;
290 alert(supportcandy.translations.req_fields_missing);
291 }
292 break;
293 <?php
294 echo PHP_EOL;
295 }
296
297 /**
298 * Return custom field value in $_POST
299 * Ignore phpcs nonce issue as we already checked where it is called from.
300 *
301 * @param string $slug - Custom field slug.
302 * @param mixed $cf - Custom field object or false.
303 * @return mixed
304 */
305 public static function get_tff_value( $slug, $cf = false ) {
306
307 return isset( $_POST[ $slug ] ) && isset( $_POST[ $slug ][0] ) ? intval( $_POST[ $slug ][0] ) : 0; // phpcs:ignore
308 }
309
310 /**
311 * Check and return custom field value for new ticket to be created.
312 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
313 * Ignore phpcs nonce issue as we already checked where it is called from.
314 *
315 * @param array $data - Array of values to to stored in ticket in an insert function.
316 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
317 * @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.
318 * @return array
319 */
320 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
321
322 if ( isset( $custom_fields[ self::$slug ] ) ) {
323 foreach ( $custom_fields[ self::$slug ] as $cf ) {
324 $value = self::get_tff_value( $cf->slug );
325 if ( $cf->field == 'ticket' ) {
326
327 $data[ $cf->slug ] = '';
328
329 if ( $value ) {
330 $attachment = new WPSC_Attachment( $value );
331 // Check if attachment is already active and linked to a ticket.
332 if ( ! ( $attachment->is_active && $attachment->ticket_id ) ) {
333 $attachment->is_active = 1;
334 $attachment->save();
335 $data[ $cf->slug ] = $value;
336 }
337 }
338 } elseif ( $cf->field == 'agentonly' ) {
339
340 $data[ $cf->slug ] = '';
341
342 } elseif ( $cf->field == 'customer' && $data['customer'] != 0 ) {
343
344 $tff = get_option( 'wpsc-tff' );
345 if ( ! $is_my_profile && ! isset( $tff[ $cf->slug ] ) ) {
346 continue;
347 }
348 $customer = new WPSC_Customer( $data['customer'] );
349 $existing_val = $customer->{$cf->slug} ? $customer->{$cf->slug}->id : null;
350 if ( $existing_val != $value ) {
351
352 $new_attach_id = '';
353 if ( $value ) {
354 $attachment = new WPSC_Attachment( $value );
355 if ( ! ( $attachment->is_active && $attachment->ticket_id ) ) {
356
357 $attachment->is_active = 1;
358 $attachment->source = 'cf';
359 $attachment->source_id = $cf->id;
360 $attachment->customer_id = $customer->id;
361 $attachment->save();
362
363 $new_attach_id = $attachment->id;
364 }
365 }
366
367 $customer->{$cf->slug} = $new_attach_id;
368 $customer->save();
369
370 // Set log for this change.
371 WPSC_Log::insert(
372 array(
373 'type' => 'customer',
374 'ref_id' => $customer->id,
375 'modified_by' => WPSC_Current_User::$current_user->customer->id,
376 'body' => wp_json_encode(
377 array(
378 'slug' => $cf->slug,
379 'prev' => $existing_val,
380 'new' => $new_attach_id,
381 )
382 ),
383 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
384 )
385 );
386 }
387 }
388 }
389 }
390 return $data;
391 }
392
393 /**
394 * Set create ticket data for rest api request
395 *
396 * @param array $data - create ticket data array.
397 * @param WP_REST_Request $request - rest request object.
398 * @param array $custom_fields - custom field objects indexed by unique custom field types.
399 * @return array
400 */
401 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
402
403 $current_user = WPSC_Current_User::$current_user;
404 $tff = get_option( 'wpsc-tff' );
405
406 if ( isset( $custom_fields[ self::$slug ] ) ) {
407 foreach ( $custom_fields[ self::$slug ] as $cf ) {
408
409 if (
410 ! in_array( $cf->field, array( 'ticket', 'agentonly', 'customer' ) ) ||
411 ( $cf->field == 'customer' && ! isset( $tff[ $cf->slug ] ) )
412 ) {
413 continue;
414 }
415
416 $attachment = WPSC_Functions::sanitize_attachment( intval( $request->get_param( $cf->slug ) ) );
417
418 if ( in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ) {
419
420 if ( $cf->field == 'ticket' && $attachment ) {
421 $attachment = new WPSC_Attachment( $attachment );
422 // Check if attachment is already active and linked to a ticket.
423 if ( ! ( $attachment->is_active && $attachment->ticket_id ) ) {
424 $attachment->is_active = 1;
425 $attachment->save();
426 $data[ $cf->slug ] = $attachment->id;
427 }
428 }
429 } else {
430
431 $customer = new WPSC_Customer( $data['customer'] );
432 $existing_val = $customer->{$cf->slug};
433 $existing_val = $existing_val && $existing_val->id ? $existing_val->id : '';
434
435 if ( $attachment && $attachment != $existing_val ) {
436
437 $attachment = new WPSC_Attachment( $attachment );
438
439 if ( ! ( $attachment->is_active && $attachment->ticket_id ) ) {
440
441 $attachment->is_active = 1;
442 $attachment->source = 'cf';
443 $attachment->source_id = $cf->id;
444 $attachment->customer_id = $customer->id;
445 $attachment->save();
446
447 $customer->{$cf->slug} = $attachment;
448 $customer->save();
449
450 // Set log for this change.
451 WPSC_Log::insert(
452 array(
453 'type' => 'customer',
454 'ref_id' => $customer->id,
455 'modified_by' => $current_user->customer->id,
456 'body' => wp_json_encode(
457 array(
458 'slug' => $cf->slug,
459 'prev' => $existing_val,
460 'new' => $attachment->id,
461 )
462 ),
463 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
464 )
465 );
466 }
467 }
468 }
469 }
470 }
471
472 return $data;
473 }
474
475 /**
476 * Add ticket search compatibility for fields of this custom field type.
477 *
478 * @param array $sql - Array of sql peices that can be joined later.
479 * @param array $filter - User filter.
480 * @param array $custom_fields - Custom fields array applicable for search.
481 * @param string $search - search string.
482 * @param array $allowed_search_fields - Allowed search fields.
483 * @return array
484 */
485 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
486
487 if ( isset( $custom_fields[ self::$slug ] ) ) {
488
489 $search_items = false;
490 foreach ( $custom_fields[ self::$slug ] as $cf ) {
491 if ( in_array( $cf->slug, $allowed_search_fields ) ) {
492 if ( ! $search_items ) {
493 $search_items = WPSC_Attachment::get_tl_search_string( $search );
494 if ( ! $search_items ) {
495 break;
496 }
497 }
498 $join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.';
499 $sql[] = $join_char . $cf->slug . ' IN(' . implode( ', ', $search_items ) . ')';
500 }
501 }
502 }
503
504 return $sql;
505 }
506
507 /**
508 * Print edit ticket custom field in individual ticket
509 *
510 * @param WPSC_Custom_Field $cf - custom field object.
511 * @param WPSC_Ticket $ticket - ticket object.
512 * @return void
513 */
514 public static function print_edit_ticket_cf( $cf, $ticket ) {
515
516 $unique_id = uniqid( 'wpsc_' );
517 $attachment = $ticket->{$cf->slug} ? $ticket->{$cf->slug} : new WPSC_Attachment();
518 ?>
519 <div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>">
520 <div class="wpsc-tff-label">
521 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
522 </div>
523 <?php
524 $extra_info = stripslashes( $cf->extra_info );
525 if ( $extra_info ) :
526 ?>
527 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
528 <?php
529 endif
530 ?>
531 <input
532 class="<?php echo esc_attr( $unique_id ); ?>"
533 type="file"
534 onchange="wpsc_set_attach_single(this, '<?php echo esc_attr( $unique_id ); ?>', '<?php echo esc_attr( $cf->slug ); ?>')"/>
535 <div class="<?php echo esc_attr( $unique_id ); ?> wpsc-editor-attachment-container">
536 <?php
537 if ( $attachment->id ) {
538 ?>
539 <div class="wpsc-editor-attachment upload-success">
540 <div class="attachment-label"><?php echo esc_attr( $attachment->name ); ?></div>
541 <div
542 class="attachment-remove"
543 onclick="wpsc_remove_attachment(this)"
544 data-single="true"
545 data-uniqueid="<?php echo esc_attr( $unique_id ); ?>">
546 <?php WPSC_Icons::get( 'times' ); ?>
547 </div>
548 <input type="hidden" name="<?php echo esc_attr( $cf->slug ); ?>[]" value="<?php echo esc_attr( $attachment->id ); ?>"/>
549 </div>
550 <?php
551 }
552 ?>
553 </div>
554 <?php
555 if ( $attachment->id ) :
556 ?>
557 <script>jQuery('input.<?php echo esc_attr( $unique_id ); ?>').hide();</script>
558 <?php
559 endif;
560 ?>
561 </div>
562 <?php
563 }
564
565 /**
566 * Set edit individual ticket for this custom field type.
567 * Ignore phpcs nonce issue as we already checked where it is called from.
568 *
569 * @param WPSC_Custom_Field $cf - custom field object.
570 * @param WPSC_Ticket $ticket - ticket object.
571 * @return WPSC_Ticket
572 */
573 public static function set_edit_ticket_cf( $cf, $ticket ) {
574
575 $prev = $ticket->{$cf->slug} ? $ticket->{$cf->slug}->id : '';
576 $new = isset( $_POST[ $cf->slug ] ) && isset( $_POST[ $cf->slug ][0] ) ? intval( $_POST[ $cf->slug ][0] ) : ''; // phpcs:ignore
577 if ( $new === 0 ) {
578 return $ticket;
579 }
580
581 // Exit if there is no change.
582 if ( $prev == $new ) {
583 return $ticket;
584 }
585
586 $new = new WPSC_Attachment( $new );
587 // Check if attachment is already active and linked to a ticket.
588 if ( ! ( $new->is_active && $new->ticket_id ) ) {
589 $new->is_active = 1;
590 $new->ticket_id = $ticket->id;
591 $new->source = 'cf';
592 $new->source_id = $cf->id;
593 $new->save();
594
595 // Change value.
596 $ticket->{$cf->slug} = $new->id;
597 $ticket->save();
598 }
599
600 return $ticket;
601 }
602
603 /**
604 * Modify ticket field value of this custom field type using rest api
605 *
606 * @param WPSC_Ticket $ticket - ticket object.
607 * @param WPSC_Custom_Field $cf - custom field.
608 * @param mixed $value - value to be set.
609 * @return void
610 */
611 public static function set_rest_edit_ticket_cf( $ticket, $cf, $value ) {
612
613 $id = WPSC_Functions::sanitize_attachment( intval( $value ) );
614 if ( ! $id ) {
615 return;
616 }
617
618 $attachment = new WPSC_Attachment( $id );
619
620 if ( is_object( $ticket->{$cf->slug} ) && $ticket->{$cf->slug}->id != $attachment->id ) {
621
622 // Check if attachment is already active and linked to a ticket.
623 if ( ! ( $attachment->is_active && $attachment->ticket_id ) ) {
624 $ticket->{$cf->slug} = $attachment->id;
625
626 $attachment->is_active = 1;
627 $attachment->source = 'cf';
628 $attachment->source_id = $cf->id;
629 $attachment->ticket_id = $ticket->id;
630 $attachment->save();
631 }
632 }
633 }
634
635 /**
636 * Insert log thread for this custom field type change
637 *
638 * @param WPSC_Custom_Field $cf - current custom field of this type.
639 * @param WPSC_Ticket $prev - ticket object before making any changes.
640 * @param WPSC_Ticket $new - ticket object after making changes.
641 * @param string $current_date - date string to be stored as create time.
642 * @param int $customer_id - current user customer id for blame.
643 * @return void
644 */
645 public static function insert_ticket_log( $cf, $prev, $new, $current_date, $customer_id ) {
646
647 // Exit if there is no change.
648 if ( $prev->{$cf->slug} == $new->{$cf->slug} ) {
649 return;
650 }
651
652 $prev_val = $prev->{$cf->slug} ? $prev->{$cf->slug}->id : '';
653 $new_val = $new->{$cf->slug} ? $new->{$cf->slug}->id : '';
654
655 $thread = WPSC_Thread::insert(
656 array(
657 'ticket' => $prev->id,
658 'customer' => $customer_id,
659 'type' => 'log',
660 'body' => wp_json_encode(
661 array(
662 'slug' => $cf->slug,
663 'prev' => $prev_val,
664 'new' => $new_val,
665 )
666 ),
667 'date_created' => $current_date,
668 'date_updated' => $current_date,
669 )
670 );
671 }
672
673 /**
674 * Return data for this custom field while creating duplicate ticket
675 *
676 * @param WPSC_Custom_Field $cf - custom field object.
677 * @param WPSC_Ticket $ticket - ticket object.
678 * @return mixed
679 */
680 public static function get_duplicate_ticket_data( $cf, $ticket ) {
681
682 return $ticket->{$cf->slug} ? WPSC_Individual_Ticket::get_duplicate_attachment_id( $ticket->{$cf->slug}->id ) : '';
683 }
684
685 /**
686 * Print edit field for this type in edit customer info
687 *
688 * @param WPSC_Custom_field $cf - custom field object.
689 * @param WPSC_Customer $customer - customer object.
690 * @param array $tff - ticket form field data.
691 * @return string
692 */
693 public static function print_edit_customer_info( $cf, $customer, $tff ) {
694
695 $unique_id = uniqid( 'wpsc_' );
696 $attachment = $customer->id && $customer->{$cf->slug} ? $customer->{$cf->slug} : new WPSC_Attachment();
697
698 ob_start();
699 ?>
700 <div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>">
701 <div class="wpsc-tff-label">
702 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
703 </div>
704 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
705 <input
706 class="<?php echo esc_attr( $unique_id ); ?>"
707 type="file"
708 onchange="wpsc_set_attach_single(this, '<?php echo esc_attr( $unique_id ); ?>', '<?php echo esc_attr( $cf->slug ); ?>')"/>
709 <div class="<?php echo esc_attr( $unique_id ); ?> wpsc-editor-attachment-container">
710 <?php
711 if ( $attachment->id ) {
712 ?>
713 <div class="wpsc-editor-attachment upload-success">
714 <div class="attachment-label"><?php echo esc_attr( $attachment->name ); ?></div>
715 <div
716 class="attachment-remove"
717 onclick="wpsc_remove_attachment(this)"
718 data-single="true"
719 data-uniqueid="<?php echo esc_attr( $unique_id ); ?>">
720 <?php WPSC_Icons::get( 'times' ); ?>
721 </div>
722 <input type="hidden" name="<?php echo esc_attr( $cf->slug ); ?>[]" value="<?php echo esc_attr( $attachment->id ); ?>"/>
723 </div>
724 <?php
725 }
726 ?>
727 </div>
728 <?php
729 if ( $attachment->id ) {
730 ?>
731 <script>jQuery('input.<?php echo esc_attr( $unique_id ); ?>').hide();</script>
732 <?php
733 }
734 ?>
735 </div>
736 <?php
737 return ob_get_clean();
738 }
739
740 /**
741 * Add ticket id to attachments after create ticket
742 *
743 * @param WPSC_Ticket $ticket - ticket object.
744 * @return void
745 */
746 public static function add_attachment_ticket_id( $ticket ) {
747
748 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
749
750 if (
751 $cf->type::$slug != self::$slug ||
752 $cf->field != 'ticket'
753 ) {
754 continue;
755 }
756
757 $attachment = $ticket->{$cf->slug};
758 if ( $attachment ) {
759 $attachment->ticket_id = $ticket->id;
760 $attachment->source = 'cf';
761 $attachment->source_id = $cf->id;
762 $attachment->save();
763 }
764 }
765 }
766
767 /**
768 * Print add new custom field setting properties
769 *
770 * @param string $field_class - Class name of the field.
771 * @return void
772 */
773 public static function get_add_new_custom_field_properties( $field_class ) {
774
775 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
776 ?>
777 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
778 <div class="label-container">
779 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
780 </div>
781 <input name="extra_info" type="text" autocomplete="off" />
782 </div>
783 <?php
784 endif;
785
786 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) :
787 ?>
788 <div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info">
789 <div class="label-container">
790 <label for="">
791 <?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?>
792 </label>
793 </div>
794 <select name="is_personal_info">
795 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
796 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
797 </select>
798 </div>
799 <?php
800 endif;
801
802 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) :
803 ?>
804 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile">
805 <div class="label-container">
806 <label for="">
807 <?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?>
808 </label>
809 </div>
810 <select name="allow_my_profile">
811 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
812 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
813 </select>
814 </div>
815 <?php
816 endif;
817
818 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) :
819 ?>
820 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form">
821 <div class="label-container">
822 <label for="">
823 <?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?>
824 </label>
825 </div>
826 <select name="allow_ticket_form">
827 <option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
828 <option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
829 </select>
830 </div>
831 <?php
832 endif;
833 }
834
835 /**
836 * Print edit custom field properties
837 *
838 * @param WPSC_Custom_Fields $cf - custom field object.
839 * @param string $field_class - class name of field category.
840 * @return void
841 */
842 public static function get_edit_custom_field_properties( $cf, $field_class ) {
843
844 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
845 ?>
846 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
847 <div class="label-container">
848 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
849 </div>
850 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
851 </div>
852 <?php
853 endif;
854
855 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) :
856 ?>
857 <div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info">
858 <div class="label-container">
859 <label for="">
860 <?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?>
861 </label>
862 </div>
863 <select name="is_personal_info">
864 <option <?php selected( $cf->is_personal_info, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
865 <option <?php selected( $cf->is_personal_info, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
866 </select>
867 </div>
868 <?php
869 endif;
870
871 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) :
872 ?>
873 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile">
874 <div class="label-container">
875 <label for="">
876 <?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?>
877 </label>
878 </div>
879 <select name="allow_my_profile">
880 <option <?php selected( $cf->allow_my_profile, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
881 <option <?php selected( $cf->allow_my_profile, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
882 </select>
883 </div>
884 <?php
885 endif;
886
887 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) :
888 ?>
889 <div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form">
890 <div class="label-container">
891 <label for="">
892 <?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?>
893 </label>
894 </div>
895 <select name="allow_ticket_form">
896 <option <?php selected( $cf->allow_ticket_form, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
897 <option <?php selected( $cf->allow_ticket_form, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
898 </select>
899 </div>
900 <?php
901 endif;
902 }
903
904 /**
905 * Set custom field properties. Can be used by add/edit custom field.
906 * Ignore phpcs nonce issue as we already checked where it is called from.
907 *
908 * @param WPSC_Custom_Field $cf - custom field object.
909 * @param string $field_class - class of field category.
910 * @return void
911 */
912 public static function set_cf_properties( $cf, $field_class ) {
913
914 // extra info.
915 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
916 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
917 }
918
919 // personal info.
920 if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) {
921 $cf->is_personal_info = isset( $_POST['is_personal_info'] ) ? intval( $_POST['is_personal_info'] ) : 0; // phpcs:ignore
922 }
923
924 // my-profile.
925 if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) {
926 $cf->allow_my_profile = isset( $_POST['allow_my_profile'] ) ? intval( $_POST['allow_my_profile'] ) : 0; // phpcs:ignore
927 }
928
929 // ticket form.
930 if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) {
931 $cf->allow_ticket_form = isset( $_POST['allow_ticket_form'] ) ? intval( $_POST['allow_ticket_form'] ) : 0; // phpcs:ignore
932 }
933
934 // save!
935 $cf->save();
936 }
937
938 /**
939 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
940 *
941 * @param WPSC_Custom_Field $cf - custom field object.
942 * @param WPSC_Ticket $ticket - ticket object.
943 * @param string $module - module name.
944 * @return string
945 */
946 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
947
948 $attachment = $ticket->{$cf->slug};
949
950 $name = '';
951 if ( $attachment && $attachment->id ) {
952 WPSC_Macros::$attachments = array_merge( WPSC_Macros::$attachments, array( $attachment ) );
953
954 if ( $module == 'email-notification' ) {
955 $link = site_url( '/' ) . '?wpsc_attachment=' . $attachment->id . '&auth_code=' . $ticket->auth_code;
956 $name = '<a href= ' . $link . ' >' . $attachment->name . '</a>';
957 } else {
958 $name = $attachment->name;
959 }
960 }
961
962 return apply_filters( 'wpsc_ticket_field_val_attach_single', $name, $cf, $ticket, $module );
963 }
964
965 /**
966 * Print ticket value for given custom field on widget
967 *
968 * @param WPSC_Custom_Field $cf - custom field object.
969 * @param WPSC_Ticket $ticket - ticket object.
970 * @return void
971 */
972 public static function print_widget_ticket_field_val( $cf, $ticket ) {
973
974 $attachment = $ticket->{$cf->slug};
975 if ( ! $attachment->id ) {
976 return;
977 }
978 $unique_id = uniqid( 'wpsc_' );
979 ?>
980 <div class="<?php echo esc_attr( $unique_id ); ?>">
981 <div class="wpsc-attachment-item">
982 <?php
983 $download_url = site_url( '/' ) . '?wpsc_attachment=' . $attachment->id . '&auth_code=' . $ticket->auth_code;
984 ?>
985 <a class="wpsc-link" href="<?php echo esc_url( $download_url ); ?>" target="_blank">
986 <span class="wpsc-attachment-name"><?php echo esc_attr( $attachment->name ); ?></span></a>
987 </div>
988 </div>
989 <script>jQuery('.<?php echo esc_attr( $unique_id ); ?>').parent().addClass('fullwidth');</script>
990 <?php
991 }
992
993 /**
994 * Returns printable customer value for custom field. Can be used in export tickets, replace macros etc.
995 *
996 * @param WPSC_Custom_Field $cf - custom field object.
997 * @param WPSC_Customer $customer - customer object.
998 * @return string
999 */
1000 public static function get_customer_field_val( $cf, $customer ) {
1001
1002 $attachment = $customer->{$cf->slug};
1003 return $attachment && $attachment->id ? $attachment->name : '';
1004 }
1005
1006 /**
1007 * Print customer value for given custom field on widget
1008 *
1009 * @param WPSC_Custom_Field $cf - custom field object.
1010 * @param WPSC_Customer $customer - customer object.
1011 * @return void
1012 */
1013 public static function print_widget_customer_field_val( $cf, $customer ) {
1014
1015 $attachment = $customer->{$cf->slug};
1016 if ( ! is_object( $attachment ) ) {
1017 return;
1018 }
1019 $unique_id = uniqid( 'wpsc_' );
1020 ?>
1021 <div class="<?php echo esc_attr( $unique_id ); ?>">
1022 <div class="wpsc-attachment-item">
1023 <?php
1024 $download_url = site_url( '/' ) . '?wpsc_attachment=' . $attachment->id;
1025 ?>
1026 <a class="wpsc-link" href="<?php echo esc_url( $download_url ); ?>" target="_blank">
1027 <span class="wpsc-attachment-name"><?php echo esc_attr( $attachment->name ); ?></span></a>
1028 </div>
1029 </div>
1030 <script>jQuery('.<?php echo esc_attr( $unique_id ); ?>').parent().addClass('fullwidth');</script>
1031 <?php
1032 }
1033
1034 /**
1035 * Print given value for custom field
1036 *
1037 * @param WPSC_Custom_Field $cf - custom field object.
1038 * @param mixed $val - value to convert and print.
1039 * @return void
1040 */
1041 public static function print_val( $cf, $val ) {
1042
1043 $attachment = is_object( $val ) ? $val : new WPSC_Attachment( $val );
1044 if ( ! $attachment->id ) {
1045 esc_attr_e( 'None', 'supportcandy' );
1046 return;
1047 }
1048 $ticket = new WPSC_Ticket( $attachment->ticket_id );
1049 ?>
1050 <div class="wpsc-attachment-item">
1051 <?php
1052 $download_url = site_url( '/' ) . '?wpsc_attachment=' . $attachment->id . '&auth_code=' . $ticket->auth_code;
1053 ?>
1054 <a class="wpsc-link" href="<?php echo esc_url( $download_url ); ?>" target="_blank">
1055 <span class="wpsc-attachment-name"><?php echo esc_attr( $attachment->name ); ?></span>
1056 </a>
1057 </div>
1058 <?php
1059 }
1060
1061 /**
1062 * Return printable value for history log macro
1063 *
1064 * @param WPSC_Custom_Field $cf - custom field object.
1065 * @param mixed $val - value to convert and return.
1066 * @return string
1067 */
1068 public static function get_history_log_val( $cf, $val ) {
1069
1070 $attachment = is_object( $val ) ? $val : new WPSC_Attachment( $val );
1071 return $attachment->id ? $attachment->name : esc_attr__( 'None', 'supportcandy' );
1072 }
1073
1074 /**
1075 * Return default value for custom field of this type
1076 *
1077 * @param WPSC_Custom_Field $cf - custom field object.
1078 * @return mixed
1079 */
1080 public static function get_default_value( $cf ) {
1081
1082 return '';
1083 }
1084
1085 /**
1086 * Print dashboard activity function
1087 *
1088 * @param WPSC_Custom_Field $cf - custom field object.
1089 * @param array $recent_logs - recent_logs object.
1090 * @param array $body - body object.
1091 * @param int $view - check if frontend.
1092 * @return string
1093 */
1094 public static function print_activity( $cf, $recent_logs, $body, $view ) {
1095
1096 $url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view );
1097 if ( $body->new ) {
1098 $attachment = new WPSC_Attachment( $body->new );
1099 $url = site_url( '/' ) . '?wpsc_attachment=' . $attachment->id;
1100 $link = '<a class="wpsc-link" href=" ' . esc_url( $url ) . ' " target="_blank">' . $attachment->name . '</a>';
1101 } else {
1102 $link = 'None';
1103 }
1104 return esc_attr( $recent_logs->customer->name ) . ' updated the' . esc_attr( $cf->name ) . ' value of <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . wp_kses_post( $link );
1105 }
1106 }
1107 endif;
1108
1109 WPSC_CF_File_Attachment_Single::init();
1110