PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.2
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.2
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-additional-recipients.php

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

566 lines 16.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_DF_Additional_Recipients' ) ) :
7
8 final class WPSC_DF_Additional_Recipients {
9
10 /**
11 * Slug for this custom field type
12 *
13 * @var string
14 */
15 public static $slug = 'df_add_recipients';
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 = true;
37
38 /**
39 * Data type for column created in tickets table
40 *
41 * @var string
42 */
43 public static $data_type = 'TEXT 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 = 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 = 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 auto-filled
117 *
118 * @var boolean
119 */
120 public static $is_auto_fill = false;
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 = 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 // Ticket model.
168 add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
169 add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 );
170
171 // ticket form.
172 add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) );
173 add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 );
174 add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) );
175
176 // create ticket data for rest api.
177 add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 );
178 }
179
180 /**
181 * Load current class to reference classes
182 *
183 * @param array $classes - Associative array of class names indexed by its slug.
184 * @return array
185 */
186 public static function load_ref_class( $classes ) {
187
188 $classes[ self::$slug ] = array(
189 'class' => __CLASS__,
190 'save-key' => 'id',
191 );
192 return $classes;
193 }
194
195 /**
196 * Add ticket search compatibility for fields of this custom field type.
197 *
198 * @param array $sql - Array of sql peices that can be joined later.
199 * @param array $filter - User filter.
200 * @param array $custom_fields - Custom fields array applicable for search.
201 * @param string $search - search string.
202 * @param array $allowed_search_fields - Allowed search fields.
203 * @return array
204 */
205 public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) {
206
207 if ( isset( $custom_fields[ self::$slug ] ) ) {
208 foreach ( $custom_fields[ self::$slug ] as $cf ) {
209 if ( in_array( $cf->slug, $allowed_search_fields ) ) {
210 $sql[] = 'CONVERT( t.assigned_agent USING utf8) LIKE \'%' . $search . '%\'';
211 }
212 }
213 }
214
215 return $sql;
216 }
217
218 /**
219 * Print ticket form field
220 *
221 * @param WPSC_Custom_Field $cf - Custom field object.
222 * @param array $tff - Array of ticket form field settings for this field.
223 * @return string
224 */
225 public static function print_tff( $cf, $tff ) {
226
227 ob_start();?>
228 <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 ); ?>">
229 <div class="wpsc-tff-label">
230 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
231 <?php
232 if ( $tff['is-required'] ) :
233 ?>
234 <span class="required-indicator">*</span>
235 <?php
236 endif;
237 ?>
238 </div>
239 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
240 <textarea
241 type="text"
242 name="<?php echo esc_attr( $cf->slug ); ?>"
243 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"
244 onchange="wpsc_check_tff_visibility()"
245 autocomplete="off"></textarea>
246 </div>
247 <?php
248 return ob_get_clean();
249 }
250
251 /**
252 * Validate this type field in create ticket
253 *
254 * @return void
255 */
256 public static function js_validate_ticket_form() {
257 ?>
258
259 case '<?php echo esc_attr( self::$slug ); ?>':
260 var val = customField.find('textarea').first().val().trim();
261 if (customField.hasClass('required') && !val) {
262 isValid = false;
263 alert(supportcandy.translations.req_fields_missing);
264 }
265 break;
266 <?php
267 echo PHP_EOL;
268 }
269
270 /**
271 * Return custom field value in $_POST
272 * Ignore phpcs nonce issue as we already checked where it is called from.
273 *
274 * @param string $slug - Custom field slug.
275 * @param mixed $cf - Custom field object or false.
276 * @return mixed
277 */
278 public static function get_tff_value( $slug, $cf = false ) {
279
280 return isset( $_POST[ $slug ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $slug ] ) ) : ''; // phpcs:ignore
281 }
282
283 /**
284 * Check and return custom field value for new ticket to be created.
285 * This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields.
286 * Ignore phpcs nonce issue as we already checked where it is called from.
287 *
288 * @param array $data - Array of values to to stored in ticket in an insert function.
289 * @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types.
290 * @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.
291 * @return array
292 */
293 public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) {
294
295 if ( $is_my_profile ) {
296 return;
297 }
298
299 if ( isset( $custom_fields[ self::$slug ] ) ) {
300 $new = array();
301 foreach ( $custom_fields[ self::$slug ] as $cf ) {
302
303 $recipients = self::get_tff_value( $cf->slug );
304 if ( ! $recipients ) {
305 continue;
306 }
307 $recipients = array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $recipients ) ) );
308 $data[ $cf->slug ] = $recipients ? implode( '|', $recipients ) : '';
309 }
310 }
311 return $data;
312 }
313
314 /**
315 * Set create ticket data for rest api request
316 *
317 * @param array $data - create ticket data array.
318 * @param WP_REST_Request $request - rest request object.
319 * @param array $custom_fields - custom field objects indexed by unique custom field types.
320 * @return array
321 */
322 public static function set_rest_ticket_data( $data, $request, $custom_fields ) {
323
324 $cf = WPSC_Custom_Field::get_cf_by_slug( 'add_recipients' );
325 $add_recipients = array_unique(
326 array_filter(
327 array_map(
328 fn( $email ) => WPSC_Functions::sanitize_email( trim( $email ) ),
329 explode( ',', sanitize_text_field( $request->get_param( 'add_recipients' ) ) )
330 )
331 )
332 );
333 $data['add_recipients'] = implode( '|', $add_recipients );
334 return $data;
335 }
336
337 /**
338 * Print generic input field for this type.
339 *
340 * @param WPSC_Custom_Field $cf - Custom field object.
341 * @param mixed $value - optional. Input field will be printed with given value if given.
342 * @return void
343 */
344 public static function print_cf_input( $cf, $value = '' ) {
345
346 $add_recipients = implode( PHP_EOL, explode( '|', $value ) );
347 ?>
348 <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 ); ?>">
349 <div class="wpsc-tff-label">
350 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
351 </div>
352 <?php
353 $extra_info = stripslashes( $cf->extra_info );
354 if ( $extra_info ) {
355 ?>
356 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
357 <?php
358 }
359 ?>
360 <textarea
361 name="<?php echo esc_attr( $cf->slug ); ?>"
362 rows="5"
363 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"><?php echo esc_attr( $add_recipients ); ?></textarea>
364 </div>
365 <?php
366 }
367
368 /**
369 * Get value for generic field from post.
370 * Ignore phpcs nonce issue as we already checked where it is called from.
371 *
372 * @param WPSC_Custom_Field $cf - custom field object.
373 * @return string
374 */
375 public static function get_cf_input_val( $cf ) {
376
377 $value = isset( $_POST[ $cf->slug ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $cf->slug ] ) ) : ''; // phpcs:ignore
378 $value = explode( PHP_EOL, $value );
379 return $value ? implode( '|', $value ) : '';
380 }
381
382 /**
383 * Returns value of edit custom field
384 *
385 * @param WPSC_Custom_Field $cf - custom field object.
386 * @return mixed
387 */
388 public static function get_edit_cf_val( $cf ) {
389
390 // ignore phpcs nonce issue as we already checked where it is called from.
391 $recipients = isset( $_POST[ $cf->slug ] ) ? sanitize_textarea_field( wp_unslash( $_POST[ $cf->slug ] ) ) : ''; // phpcs:ignore
392 if ( ! $recipients ) {
393 return '';
394 }
395 $recipients = array_filter( array_map( 'sanitize_email', explode( PHP_EOL, $recipients ) ) );
396 return $recipients ? implode( '|', $recipients ) : '';
397 }
398
399 /**
400 * Print edit custom field properties
401 *
402 * @param WPSC_Custom_Fields $cf - custom field object.
403 * @param string $field_class - class name of field category.
404 * @return void
405 */
406 public static function get_edit_custom_field_properties( $cf, $field_class ) {
407
408 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) :
409 ?>
410 <div data-type="textfield" data-required="false" class="wpsc-input-group extra-info">
411 <div class="label-container">
412 <label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label>
413 </div>
414 <input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" />
415 </div>
416 <?php
417 endif;
418
419 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) :
420 ?>
421 <div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text">
422 <div class="label-container">
423 <label for="">
424 <?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?>
425 </label>
426 </div>
427 <input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off">
428 </div>
429 <?php
430 endif;
431 }
432
433 /**
434 * Set custom field properties. Can be used by add/edit custom field.
435 * Ignore phpcs nonce issue as we already checked where it is called from.
436 *
437 * @param WPSC_Custom_Field $cf - custom field object.
438 * @param string $field_class - class of field category.
439 * @return void
440 */
441 public static function set_cf_properties( $cf, $field_class ) {
442
443 // extra info.
444 if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) {
445 $cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore
446 }
447
448 // placeholder!
449 if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) {
450 $cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore
451 }
452
453 // save!
454 $cf->save();
455
456 // update widget title!
457 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
458 $ticket_widgets['additional-recipients']['title'] = $cf->name;
459 update_option( 'wpsc-ticket-widget', $ticket_widgets );
460 }
461
462 /**
463 * Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc.
464 *
465 * @param WPSC_Custom_Field $cf - custom field object.
466 * @param WPSC_Ticket $ticket - ticket object.
467 * @param string $module - module name.
468 * @return string
469 */
470 public static function get_ticket_field_val( $cf, $ticket, $module = '' ) {
471
472 $recipients = $ticket->{$cf->slug};
473 $value = $recipients ? implode( ', ', $recipients ) : '';
474 return apply_filters( 'wpsc_ticket_field_val_add_recipients', $value, $cf, $ticket, $module );
475 }
476
477 /**
478 * Print ticket value for given custom field on widget
479 *
480 * @param WPSC_Custom_Field $cf - custom field object.
481 * @param WPSC_Ticket $ticket - ticket object.
482 * @return void
483 */
484 public static function print_widget_ticket_field_val( $cf, $ticket ) {
485
486 $recipients = $ticket->{$cf->slug};
487 if ( $recipients ) {
488 foreach ( $recipients as $recipient ) {
489 ?>
490 <div><?php echo esc_attr( $recipient ); ?></div>
491 <?php
492 }
493 } else {
494 esc_attr_e( 'Not Applicable', 'supportcandy' );
495 }
496 }
497
498 /**
499 * Print given value for custom field
500 *
501 * @param WPSC_Custom_Field $cf - custom field object.
502 * @param mixed $val - value to convert and print.
503 * @return void
504 */
505 public static function print_val( $cf, $val ) {
506
507 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
508 echo $val ? esc_attr( implode( ', ', $val ) ) : esc_attr__( 'None', 'supportcandy' );
509 }
510
511 /**
512 * Return printable value for history log macro
513 *
514 * @param WPSC_Custom_Field $cf - custom field object.
515 * @param mixed $val - value to convert and return.
516 * @return string
517 */
518 public static function get_history_log_val( $cf, $val ) {
519
520 ob_start();
521 self::print_val( $cf, $val );
522 return ob_get_clean();
523 }
524
525 /**
526 * Return default value for custom field of this type
527 *
528 * @param WPSC_Custom_Field $cf - custom field object.
529 * @return mixed
530 */
531 public static function get_default_value( $cf ) {
532
533 return '';
534 }
535
536 /**
537 * Return given value for custom field
538 *
539 * @param WPSC_Custom_Field $cf - custom field object.
540 * @param mixed $val - value to convert and print.
541 * @return string
542 */
543 public static function get_field_value( $cf, $val ) {
544
545 $val = is_array( $val ) ? $val : array_filter( explode( '|', $val ) );
546 return $val ? esc_attr( implode( ', ', $val ) ) : esc_attr__( 'None', 'supportcandy' );
547 }
548
549 /**
550 * Clear value of hidden fields
551 *
552 * @return void
553 */
554 public static function js_clear_value_hidden_fields() {
555 ?>
556 case '<?php echo esc_attr( self::$slug ); ?>':
557 customField.find('textarea').first().val('');
558 break;
559 <?php
560 echo PHP_EOL;
561 }
562 }
563 endif;
564
565 WPSC_DF_Additional_Recipients::init();
566