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-cf-file-attachment-single.php

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

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