PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.3.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.3.9
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 / admin / tickets / widgets / class-wpsc-itw-raisedby.php

class-wpsc-itw-raisedby.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.9, at includes/admin/tickets/widgets/class-wpsc-itw-raisedby.php

1,018 lines 32.3 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_ITW_Raisedby' ) ) :
7
8 final class WPSC_ITW_Raisedby {
9
10 /**
11 * Actions for raised by
12 *
13 * @var array
14 */
15 public static $actions;
16
17 /**
18 * Initialize this class
19 *
20 * @return void
21 */
22 public static function init() {
23
24 // Edit Raised by widget.
25 add_action( 'wp_ajax_wpsc_get_tw_raised_by', array( __CLASS__, 'get_tw_raised_by' ) );
26 add_action( 'wp_ajax_wpsc_set_tw_raised_by', array( __CLASS__, 'set_tw_raised_by' ) );
27
28 // Edit ticket raised by.
29 add_action( 'wp_ajax_wpsc_it_get_edit_raised_by', array( __CLASS__, 'get_edit_raised_by' ) );
30 add_action( 'wp_ajax_wpsc_raisedby_autocomplete', array( __CLASS__, 'get_raisedby_autocomplete' ) );
31 add_action( 'wp_ajax_wpsc_it_set_edit_raised_by', array( __CLASS__, 'set_edit_raised_by' ) );
32
33 // Customer other tickets.
34 add_action( 'wp_ajax_wpsc_get_rb_other_tickets', array( __CLASS__, 'get_rb_other_tickets' ) );
35 add_action( 'wp_ajax_nopriv_wpsc_get_rb_other_tickets', array( __CLASS__, 'get_rb_other_tickets' ) );
36
37 // Raised by info.
38 add_action( 'wp_ajax_wpsc_get_rb_info', array( __CLASS__, 'get_rb_info' ) );
39 add_action( 'wp_ajax_nopriv_wpsc_get_rb_info', array( __CLASS__, 'get_rb_info' ) );
40
41 // View WordPress user profile.
42 add_action( 'wp_ajax_wpsc_wp_user_profile', array( __CLASS__, 'wp_customers_info' ) );
43
44 // Edit raised by info.
45 add_action( 'wp_ajax_wpsc_get_edit_rb_info', array( __CLASS__, 'get_edit_rb_info' ) );
46 add_action( 'wp_ajax_nopriv_wpsc_get_edit_rb_info', array( __CLASS__, 'get_edit_rb_info' ) );
47 add_action( 'wp_ajax_wpsc_set_edit_rb_info', array( __CLASS__, 'set_edit_rb_info' ) );
48 add_action( 'wp_ajax_nopriv_wpsc_set_edit_rb_info', array( __CLASS__, 'set_edit_rb_info' ) );
49 }
50
51 /**
52 * Prints body of current widget
53 *
54 * @param WPSC_Ticket $ticket - ticket object.
55 * @param array $settings - widget settings.
56 * @return void
57 */
58 public static function print_widget( $ticket, $settings ) {
59
60 $actions = self::load_actions();
61
62 $current_user = WPSC_Current_User::$current_user;
63 if ( ! (
64 (
65 (
66 WPSC_Individual_Ticket::$view_profile == 'customer' ||
67 $ticket->customer->id == $current_user->customer->id
68 ) &&
69 $settings['allow-customer']
70 ) ||
71 ( WPSC_Individual_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) )
72 ) ) {
73 return;
74 }?>
75 <div class="wpsc-it-widget wpsc-itw-raised-by">
76 <div class="wpsc-widget-header">
77 <h2>
78 <?php
79 $settings_title = $settings['title'] ? WPSC_Translations::get( 'wpsc-twt-raised-by', stripslashes( $settings['title'] ) ) : stripslashes( $settings['title'] );
80 echo esc_attr( $settings_title )
81 ?>
82 </h2>
83 <?php
84 if ( $ticket->is_active && WPSC_Individual_Ticket::$view_profile == 'agent' && WPSC_Individual_Ticket::has_ticket_cap( 'crb' ) ) :
85 ?>
86 <span onclick="wpsc_it_get_edit_raised_by(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_it_get_edit_raised_by' ) ); ?>')"><?php WPSC_Icons::get( 'edit' ); ?></span>
87 <?php
88 endif
89 ?>
90 <span class="wpsc-itw-toggle" data-widget="wpsc-itw-raised-by"><?php WPSC_Icons::get( 'chevron-up' ); ?></span>
91 </div>
92 <div class="wpsc-widget-body">
93 <div class="user-list-item raised-by">
94 <?php echo get_avatar( $ticket->customer->email, 40 ); ?>
95 <div class="ul-body">
96 <div class="ul-label"><?php echo esc_attr( $ticket->customer->name ); ?></div>
97 <?php
98 if ( WPSC_Individual_Ticket::$view_profile == 'agent' ) :
99 ?>
100 <div class="ul-actions">
101 <?php
102 foreach ( $actions as $action ) :
103 ?>
104 <span
105 onclick="<?php echo esc_attr( $action['callback'] ) . '(this, ' . esc_attr( $ticket->id ) . ', \'' . esc_attr( wp_create_nonce( $action['callback'] ) ) . '\')'; ?>"
106 title="<?php echo esc_attr( $action['label'] ); ?>">
107 <?php WPSC_Icons::get( $action['icon'] ); ?>
108 </span>
109 <?php
110 endforeach;
111 ?>
112 </div>
113 <?php
114 endif;
115 ?>
116 </div>
117 </div>
118 <?php do_action( 'wpsc_itw_raisedby', $ticket ); ?>
119 </div>
120 </div>
121 <?php
122 }
123
124 /**
125 * Load actions for raised by user
126 *
127 * @return array
128 */
129 public static function load_actions() {
130
131 $actions = array(
132 'info' => array(
133 'label' => esc_attr__( 'Info', 'supportcandy' ),
134 'icon' => 'info-circle',
135 'callback' => 'wpsc_get_rb_info',
136 ),
137 'other-tickets' => array(
138 'label' => esc_attr__( 'All other tickets of this user', 'supportcandy' ),
139 'icon' => 'ticket-alt',
140 'callback' => 'wpsc_get_rb_other_tickets',
141 ),
142 );
143
144 WPSC_Individual_Ticket::load_current_ticket();
145 $ticket = WPSC_Individual_Ticket::$ticket;
146 if ( WPSC_Functions::is_site_admin() && $ticket->customer->user ) {
147 $actions['wp-user-profile'] = array(
148 'label' => esc_attr__( 'WordPress Profile', 'supportcandy' ),
149 'icon' => 'wp-logo',
150 'callback' => 'wpsc_wp_user_profile',
151 );
152 }
153 return apply_filters( 'wpsc_itw_raisedby_actions', $actions );
154 }
155
156 /**
157 * Get Raised By
158 */
159 public static function get_tw_raised_by() {
160
161 if ( ! WPSC_Functions::is_site_admin() ) {
162 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
163 }
164
165 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
166 $raised_by = $ticket_widgets['raised-by'];
167 $title = $raised_by['title'];
168 $roles = get_option( 'wpsc-agent-roles', array() );
169 ob_start();
170 ?>
171
172 <form action="#" onsubmit="return false;" class="wpsc-frm-edit-ticket-raised-by">
173 <div class="wpsc-input-group">
174 <div class="label-container">
175 <label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label>
176 </div>
177 <input name="label" type="text" value="<?php echo esc_attr( $raised_by['title'] ); ?>" autocomplete="off">
178 </div>
179 <div class="wpsc-input-group">
180 <div class="label-container">
181 <label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label>
182 </div>
183 <select name="is_enable">
184 <option <?php selected( $raised_by['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
185 <option <?php selected( $raised_by['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
186 </select>
187 </div>
188 <div class="wpsc-input-group">
189 <div class="label-container">
190 <label for=""><?php esc_attr_e( 'Allowed for customer', 'supportcandy' ); ?></label>
191 </div>
192 <select id="allow-customer" name="allow-customer">
193 <option <?php selected( $raised_by['allow-customer'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option>
194 <option <?php selected( $raised_by['allow-customer'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option>
195 </select>
196 </div>
197 <div class="wpsc-input-group">
198 <div class="label-container">
199 <label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label>
200 </div>
201 <select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent...">
202 <?php
203 foreach ( $roles as $key => $role ) :
204 $selected = in_array( $key, $raised_by['allowed-agent-roles'] ) ? 'selected="selected"' : ''
205 ?>
206 <option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option>
207 <?php
208 endforeach;
209 ?>
210 </select>
211 </div>
212 <script>
213 jQuery('#wpsc-select-agents').selectWoo({
214 allowClear: false,
215 placeholder: ""
216 });
217 </script>
218 <?php do_action( 'wpsc_get_raised_by_body' ); ?>
219 <input type="hidden" name="action" value="wpsc_set_tw_raised_by">
220 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_raised_by' ) ); ?>">
221 </form>
222 <?php
223 $body = ob_get_clean();
224
225 ob_start();
226 ?>
227 <button class="wpsc-button small primary" onclick="wpsc_set_tw_raised_by(this);">
228 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
229 </button>
230 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
231 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
232 </button>
233 <?php
234 do_action( 'wpsc_get_tw_raised_by_widget_footer' );
235 $footer = ob_get_clean();
236
237 $response = array(
238 'title' => $title,
239 'body' => $body,
240 'footer' => $footer,
241 );
242 wp_send_json( $response );
243 }
244
245 /**
246 * Set Raised By
247 */
248 public static function set_tw_raised_by() {
249
250 if ( check_ajax_referer( 'wpsc_set_tw_raised_by', '_ajax_nonce', false ) != 1 ) {
251 wp_send_json_error( 'Unauthorised request!', 401 );
252 }
253
254 if ( ! WPSC_Functions::is_site_admin() ) {
255 wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 );
256 }
257
258 $label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : '';
259 if ( ! $label ) {
260 wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
261 }
262
263 // Also change customer cf name.
264 $cf = WPSC_Custom_Field::get_cf_by_slug( 'customer' );
265 $cf->name = $label;
266 $cf->save();
267
268 $is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0;
269 $allow_for_customer = isset( $_POST['allow-customer'] ) ? intval( $_POST['allow-customer'] ) : 0;
270 $agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array();
271
272 $ticket_widgets = get_option( 'wpsc-ticket-widget', array() );
273
274 $ticket_widgets['raised-by']['title'] = $label;
275 $ticket_widgets['raised-by']['is_enable'] = $is_enable;
276 $ticket_widgets['raised-by']['allow-customer'] = $allow_for_customer;
277 $ticket_widgets['raised-by']['allowed-agent-roles'] = $agents;
278 update_option( 'wpsc-ticket-widget', $ticket_widgets );
279
280 // remove string translations.
281 WPSC_Translations::remove( 'wpsc-twt-raised-by' );
282 WPSC_Translations::add( 'wpsc-twt-raised-by', stripslashes( $label ) );
283 wp_die();
284 }
285
286 /**
287 * Change create as or raised by modal
288 *
289 * @return void
290 */
291 public static function get_edit_raised_by() {
292
293 if ( check_ajax_referer( 'wpsc_it_get_edit_raised_by', '_ajax_nonce', false ) != 1 ) {
294 wp_send_json_error( 'Unauthorised request!', 401 );
295 }
296
297 WPSC_Individual_Ticket::load_current_ticket();
298
299 $current_user = WPSC_Current_User::$current_user;
300 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'crb' ) ) ) {
301 wp_send_json_error( 'Something went wrong!', 400 );
302 }
303
304 $ticket = WPSC_Individual_Ticket::$ticket;
305
306 $widgets = get_option( 'wpsc-ticket-widget' );
307 $title = $widgets['raised-by']['title'];
308
309 ob_start();
310 $unique_id = uniqid( 'wpsc_' );
311 ?>
312 <form action="#" onsubmit="return false;" class="change-raised-by <?php echo esc_attr( $unique_id ); ?>">
313 <div class="wpsc-input-group label">
314 <div class="label-container">
315 <label for="">
316 <?php esc_attr_e( 'User type', 'supportcandy' ); ?>
317 </label>
318 </div>
319 <select class="type <?php echo esc_attr( $unique_id ); ?>" name="type">
320 <option value="existing" <?php echo $ticket->customer->id ? 'selected' : ''; ?>><?php esc_attr_e( 'Existing Customer / Registered User', 'supportcandy' ); ?></option>
321 <option value="new" <?php echo ! $ticket->customer->id ? 'selected' : ''; ?>><?php esc_attr_e( 'New Customer', 'supportcandy' ); ?></option>
322 </select>
323 <script>
324 jQuery('select.type.<?php echo esc_attr( $unique_id ); ?>').change(function(){
325 var type = jQuery(this).val().trim();
326 if (type == 'existing') {
327 jQuery('.wpsc-input-group.new').hide();
328 jQuery('.wpsc-input-group.existing').show();
329 } else {
330 jQuery('.wpsc-input-group.existing').hide();
331 jQuery('.wpsc-input-group.new').show();
332 }
333 });
334 </script>
335 </div>
336 <div class="wpsc-input-group existing" style="<?php echo ! $ticket->customer->id ? 'display:none;' : ''; ?>">
337 <div class="label-container">
338 <label for="">
339 <?php esc_attr_e( 'Existing Customer / Registered User', 'supportcandy' ); ?>
340 </label>
341 </div>
342 <select class="existing-user <?php echo esc_attr( $unique_id ); ?>" name="customer_email">
343 <?php
344 if ( $ticket->customer->id ) :
345 ?>
346 <option value="<?php echo esc_attr( $ticket->customer->id ); ?>"><?php echo esc_attr( $ticket->customer->name ) . ' (' . esc_attr( $ticket->customer->email ) . ')'; ?></option>
347 <?php
348 endif
349 ?>
350 </select>
351 <script>
352 jQuery('select.existing-user.<?php echo esc_attr( $unique_id ); ?>').selectWoo({
353 ajax: {
354 url: supportcandy.ajax_url,
355 type: 'POST',
356 dataType: 'json',
357 delay: 250,
358 data: function (params) {
359 return {
360 q: params.term, // search term
361 ticket_id: <?php echo esc_attr( $ticket->id ); ?>,
362 action: 'wpsc_raisedby_autocomplete',
363 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_raisedby_autocomplete' ) ); ?>',
364 };
365 },
366 processResults: function (data, params) {
367 var terms = [];
368 if ( data ) {
369 jQuery.each( data, function( id, customer ) {
370 terms.push({
371 id: customer.email,
372 text: customer.text,
373 email: customer.email,
374 name: customer.name
375 });
376 });
377 }
378 return {
379 results: terms
380 };
381 },
382 cache: true
383 },
384 escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
385 minimumInputLength: 1,
386 allowClear: true,
387 placeholder: ""
388 });
389 supportcandy.translations.raisedByEditWidget = {
390 invalidEmail: '<?php esc_attr_e( 'Invalid email address!', 'supportcandy' ); ?>'
391 }
392 </script>
393 </div>
394 <div class="wpsc-input-group label new" style="<?php echo $ticket->customer->id ? 'display:none;' : ''; ?>">
395 <div class="label-container">
396 <label for="">
397 <?php esc_attr_e( 'Name', 'supportcandy' ); ?>
398 <span class="required-char">*</span>
399 </label>
400 </div>
401 <input class="name <?php echo esc_attr( $unique_id ); ?>" type="text" name="name" autocomplete="off"/>
402 </div>
403 <div class="wpsc-input-group label new" style="<?php echo $ticket->customer->id ? 'display:none;' : ''; ?>">
404 <div class="label-container">
405 <label for="">
406 <?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>
407 <span class="required-char">*</span>
408 </label>
409 </div>
410 <input class="email <?php echo esc_attr( $unique_id ); ?>" type="text" name="email" autocomplete="off"/>
411 </div>
412 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
413 <input type="hidden" name="action" value="wpsc_it_set_edit_raised_by">
414 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_raised_by' ) ); ?>">
415 </form>
416 <?php
417 $body = ob_get_clean();
418
419 ob_start();
420 ?>
421 <button class="wpsc-button small primary" onclick="wpsc_it_set_edit_raised_by(this, <?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( $unique_id ); ?>');">
422 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
423 </button>
424 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
425 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
426 </button>
427 <?php
428 do_action( 'wpsc_get_add_new_tff' );
429 $footer = ob_get_clean();
430
431 $response = array(
432 'title' => $title,
433 'body' => $body,
434 'footer' => $footer,
435 );
436 wp_send_json( $response );
437 }
438
439 /**
440 * Set raised by
441 *
442 * @return void
443 */
444 public static function set_edit_raised_by() {
445
446 if ( check_ajax_referer( 'wpsc_it_set_edit_raised_by', '_ajax_nonce', false ) != 1 ) {
447 wp_send_json_error( 'Unauthorised request!', 401 );
448 }
449
450 WPSC_Individual_Ticket::load_current_ticket();
451
452 $current_user = WPSC_Current_User::$current_user;
453 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'crb' ) ) ) {
454 wp_send_json_error( 'Something went wrong!', 400 );
455 }
456
457 $ticket = WPSC_Individual_Ticket::$ticket;
458 $clone = clone $ticket;
459 if ( ! $ticket->is_active ) {
460 wp_send_json_error( 'Something went wrong!', 400 );
461 }
462
463 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
464 if ( ! $type ) {
465 wp_send_json_error( 'Something went wrong!', 400 );
466 }
467
468 $customer = new WPSC_Customer();
469 if ( $type == 'new' ) {
470
471 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
472 if ( ! $type ) {
473 wp_send_json_error( 'Something went wrong!', 400 );
474 }
475
476 $email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
477 if ( ! $email || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
478 wp_send_json_error( 'Something went wrong!', 400 );
479 }
480
481 // Check if this email address is already present in customer model.
482 $customer = WPSC_Customer::get_by_email( $email );
483
484 // Check if this email address is already a registered user and if yes, create a customer record.
485 if ( ! $customer->id ) {
486
487 $user = get_user_by( 'email', $email );
488 if ( $user ) {
489
490 $customer = WPSC_Customer::insert(
491 array(
492 'user' => $user->ID,
493 'name' => $user->display_name,
494 'email' => $email,
495 )
496 );
497
498 } else {
499
500 $customer = WPSC_Customer::insert(
501 array(
502 'user' => 0,
503 'name' => $name,
504 'email' => $email,
505 )
506 );
507 }
508 }
509 } else {
510
511 $email = isset( $_POST['customer_email'] ) ? sanitize_text_field( wp_unslash( $_POST['customer_email'] ) ) : '';
512 if ( ! $email || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
513 wp_die();
514 }
515
516 // Check if this email address is already present in customer model.
517 $customer = WPSC_Customer::get_by_email( $email );
518 if ( ! $customer->id ) {
519
520 $user = get_user_by( 'email', $email );
521 if ( ! $user ) {
522 wp_send_json_error( 'Something went wrong!', 400 ); // We are expecting either existing customer record or registered user.
523 }
524 $customer = WPSC_Customer::insert(
525 array(
526 'user' => $user->ID,
527 'name' => $user->display_name,
528 'email' => $email,
529 )
530 );
531 }
532 }
533
534 // Change raised by if it has been changed.
535 if ( $customer->id && $ticket->customer->id != $customer->id ) {
536
537 $current_user = WPSC_Current_User::$current_user;
538 $prev = $ticket->customer;
539 WPSC_Individual_Ticket::change_raised_by( $prev, $customer, $current_user->customer->id );
540 }
541
542 // Count prev customer ticket after change raised by.
543 WPSC_Customers::customer_ticket_count( $clone );
544 wp_die();
545 }
546
547 /**
548 * Get raised by autocomplete
549 *
550 * @return void
551 */
552 public static function get_raisedby_autocomplete() {
553
554 if ( check_ajax_referer( 'wpsc_raisedby_autocomplete', '_ajax_nonce', false ) != 1 ) {
555 wp_send_json_error( 'Unauthorised request!', 401 );
556 }
557
558 WPSC_Individual_Ticket::load_current_ticket();
559
560 $current_user = WPSC_Current_User::$current_user;
561 if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'crb' ) ) ) {
562 wp_send_json_error( 'Something went wrong!', 400 );
563 }
564
565 $term = isset( $_POST['q'] ) ? sanitize_text_field( wp_unslash( $_POST['q'] ) ) : '';
566 if ( ! $term ) {
567 wp_send_json_error( new WP_Error( '005', 'Search term should have at least one character!' ), 400 );
568 }
569
570 wp_send_json(
571 array_map(
572 fn( $customer ) => array(
573 'id' => $customer->id,
574 'text' => sprintf(
575 /* translators: %1$s: Name, %2$s: Email Address */
576 esc_attr__( '%1$s (%2$s)', 'supportcandy' ),
577 $customer->name,
578 $customer->email
579 ),
580 'email' => $customer->email,
581 'name' => $customer->name,
582 ),
583 WPSC_Customer::customer_search( $term )
584 )
585 );
586 }
587
588 /**
589 * Get customer other tickets
590 *
591 * @return void
592 */
593 public static function get_rb_other_tickets() {
594
595 if ( check_ajax_referer( 'wpsc_get_rb_other_tickets', '_ajax_nonce', false ) != 1 ) {
596 wp_send_json_error( 'Unauthorised request!', 401 );
597 }
598
599 WPSC_Individual_Ticket::load_current_ticket();
600
601 $current_user = WPSC_Current_User::$current_user;
602 if ( ! ( WPSC_Individual_Ticket::$view_profile == 'agent' && WPSC_Individual_Ticket::has_ticket_cap( 'view' ) ) ) {
603 wp_send_json_error( 'Something went wrong!', 400 );
604 }
605
606 $ticket = WPSC_Individual_Ticket::$ticket;
607
608 $title = esc_attr( $ticket->customer->name );
609
610 // filters.
611 $filters = array(
612 'filterSlug' => 'all',
613 'orderby' => 'date_created',
614 'order' => 'DESC',
615 'items_per_page' => 0,
616 'is_active' => 1,
617 );
618
619 // system query.
620 $filters['system_query'] = $current_user->get_tl_system_query( $filters );
621
622 // meta query.
623 $filters['meta_query'] = array(
624 'relation' => 'AND',
625 array(
626 'slug' => 'customer',
627 'compare' => '=',
628 'val' => $ticket->customer->id,
629 ),
630 );
631
632 $tickets = WPSC_Ticket::find( $filters )['results'];
633 $list_items = get_option( 'wpsc-atl-list-items' );
634 $unique_id = uniqid();
635
636 ob_start();
637 ?>
638 <div style="overflow-x:auto; width:100%;">
639 <table class="wpsc-cust-all-tickets wpsc-setting-tbl">
640 <thead>
641 <tr>
642 <?php
643 foreach ( $list_items as $slug ) :
644 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
645 if ( ! $cf ) {
646 continue;
647 }
648 ?>
649 <th style="min-width: <?php echo esc_attr( $cf->tl_width ); ?>px;"><?php echo esc_attr( $cf->name ); ?></th>
650 <?php
651 endforeach;
652 ?>
653 </tr>
654 </thead>
655 <tbody>
656 <?php
657 foreach ( $tickets as $ticket ) :
658 ?>
659 <tr class="wpsc-raised-by-ticket" onclick="if(link) wpsc_open_customer_ticket(<?php echo esc_attr( $ticket->id ); ?>)">
660 <?php
661 foreach ( $list_items as $slug ) :
662 $cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
663 if ( ! $cf ) {
664 continue;
665 }
666 ?>
667 <td onmouseover="link=true;">
668 <?php
669 if ( in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ) {
670 $cf->type::print_tl_ticket_field_val( $cf, $ticket );
671 } else {
672 $cf->type::print_tl_customer_field_val( $cf, $ticket->customer );
673 }
674 ?>
675 </td>
676 <?php
677 endforeach;
678 ?>
679 </tr>
680 <?php
681 endforeach;
682 ?>
683 </tbody>
684 </table>
685 <script>
686 jQuery('table.wpsc-cust-all-tickets').DataTable({
687 ordering: false,
688 pageLength: 20,
689 bLengthChange: false,
690 columnDefs: [
691 { targets: -1, searchable: false },
692 { targets: '_all', className: 'dt-left' }
693 ],
694 language: supportcandy.translations.datatables
695 });
696
697 function wpsc_open_customer_ticket( id ) {
698
699 if ( wpsc_is_description_text() ) {
700 if ( confirm( supportcandy.translations.warning_message ) ) {
701 wpsc_close_modal();
702 ticket_id = jQuery('#wpsc-current-ticket').val();
703 wpsc_clear_saved_draft_reply( ticket_id );
704 wpsc_get_individual_ticket( id );
705 } else {
706 return;
707 }
708 }else{
709 wpsc_close_modal();
710 wpsc_get_individual_ticket( id );
711 }
712 }
713 </script>
714 </div>
715 <?php
716
717 $body = ob_get_clean();
718
719 ob_start();
720 ?>
721 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
722 <?php esc_attr_e( 'Close', 'supportcandy' ); ?>
723 </button>
724 <?php
725 $footer = ob_get_clean();
726
727 $response = array(
728 'title' => $title,
729 'body' => $body,
730 'footer' => $footer,
731 );
732 wp_send_json( $response );
733 }
734
735 /**
736 * Get raised by info
737 *
738 * @return void
739 */
740 public static function get_rb_info() {
741
742 if ( check_ajax_referer( 'wpsc_get_rb_info', '_ajax_nonce', false ) != 1 ) {
743 wp_send_json_error( 'Unauthorised request!', 401 );
744 }
745
746 WPSC_Individual_Ticket::load_current_ticket();
747 $current_user = WPSC_Current_User::$current_user;
748 if ( ! ( WPSC_Individual_Ticket::$view_profile == 'agent' && WPSC_Individual_Ticket::has_ticket_cap( 'view' ) ) ) {
749 wp_send_json_error( 'Something went wrong!', 400 );
750 }
751
752 $ticket = WPSC_Individual_Ticket::$ticket;
753 $customer = $ticket->customer;
754 $title = esc_attr__( 'Customer info', 'supportcandy' );
755 $gs = get_option( 'wpsc-gs-general', array() );
756 $unique_id = uniqid();
757
758 ob_start();
759 ?>
760 <div class="wpsc-thread-info">
761
762 <div style="width: 100%;">
763
764 <table class="wpsc-setting-tbl <?php echo esc_attr( $unique_id ); ?>" style="margin-bottom: 15px;">
765 <thead>
766 <tr>
767 <th><?php esc_attr_e( 'Field', 'supportcandy' ); ?></th>
768 <th><?php esc_attr_e( 'Value', 'supportcandy' ); ?></th>
769 </tr>
770 </thead>
771 <tbody>
772 <tr>
773 <td><?php esc_attr_e( 'Name', 'supportcandy' ); ?>:</td>
774 <td><?php echo esc_attr( $customer->name ); ?></td>
775 </tr>
776 <?php
777 if ( $current_user->is_agent && in_array( $current_user->agent->role, $gs['allow-ar-thread-email'] ) ) {
778 ?>
779 <tr>
780 <td><?php esc_attr_e( 'Email Address', 'supportcandy' ); ?>:</td>
781 <td><?php echo esc_attr( $customer->email ); ?></td>
782 </tr>
783 <?php
784 }
785
786 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) :
787 if ( $cf->field !== 'customer' || in_array( $cf->slug, WPSC_DF_Customer::$ignore_customer_info_cft ) ) {
788 continue;
789 }
790 ?>
791 <tr>
792 <td><?php echo esc_attr( $cf->name ); ?>:</td>
793 <td><?php $cf->type::print_widget_customer_field_val( $cf, $customer ); ?></td>
794 </tr>
795 <?php
796 endforeach;
797 ?>
798 </tbody>
799 </table>
800 <script>
801 jQuery('.<?php echo esc_attr( $unique_id ); ?>').DataTable({
802 ordering: false,
803 pageLength: 20,
804 bLengthChange: false,
805 columnDefs: [
806 { targets: -1, searchable: false },
807 { targets: '_all', className: 'dt-left' }
808 ],
809 language: supportcandy.translations.datatables
810 });
811 </script>
812 </div>
813 <?php do_action( 'wpsc_get_rb_info', $customer ); ?>
814 </div>
815 <?php
816 $body = ob_get_clean();
817
818 ob_start();
819 if ( $current_user->agent->has_cap( 'eci-access' ) ) {
820 ?>
821 <button class="wpsc-button small primary" onclick="wpsc_get_edit_rb_info(this, <?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_rb_info' ) ); ?>');">
822 <?php esc_attr_e( 'Edit Info', 'supportcandy' ); ?>
823 </button>
824 <?php
825 }
826 ?>
827 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
828 <?php esc_attr_e( 'Close', 'supportcandy' ); ?>
829 </button>
830 <?php
831 do_action( 'wpsc_get_rb_info_footer', $customer );
832 $footer = ob_get_clean();
833
834 $response = array(
835 'title' => $title,
836 'body' => $body,
837 'footer' => $footer,
838 );
839 wp_send_json( $response );
840 }
841
842 /**
843 * URL to WordPress edit customer.
844 *
845 * @return void
846 */
847 public static function wp_customers_info() {
848
849 if ( check_ajax_referer( 'wpsc_wp_user_profile', '_ajax_nonce', false ) != 1 ) {
850 wp_send_json_error( 'Unauthorised request!', 401 );
851 }
852
853 WPSC_Individual_Ticket::load_current_ticket();
854 $ticket = WPSC_Individual_Ticket::$ticket;
855 $customer_id = $ticket->customer->user->ID;
856 wp_send_json( array( 'url' => admin_url( 'user-edit.php?user_id=' . $customer_id ) ) );
857 }
858
859 /**
860 * Get edit customer info
861 *
862 * @return void
863 */
864 public static function get_edit_rb_info() {
865
866 if ( check_ajax_referer( 'wpsc_get_edit_rb_info', '_ajax_nonce', false ) != 1 ) {
867 wp_send_json_error( 'Unauthorised request!', 401 );
868 }
869
870 WPSC_Individual_Ticket::load_current_ticket();
871 $current_user = WPSC_Current_User::$current_user;
872 if ( ! ( WPSC_Individual_Ticket::$view_profile == 'agent' && $current_user->agent->has_cap( 'eci-access' ) ) ) {
873 wp_send_json_error( 'Something went wrong!', 400 );
874 }
875
876 $ticket = WPSC_Individual_Ticket::$ticket;
877 $customer = $ticket->customer;
878
879 $title = esc_attr__( 'Edit customer info', 'supportcandy' );
880 ob_start();
881 ?>
882 <form action="#" onsubmit="return false;" class="frm-edit-rb-info">
883
884 <div class="wpsc-input-group" style="align-items:flex-end;">
885 <input type="text" id="rb-search" placeholder="<?php esc_attr_e( 'Search...', 'supportcandy' ); ?>" autocomplete="off" style="max-width:200px;">
886 </div>
887
888 <?php
889
890 $cf = WPSC_Custom_Field::get_cf_by_slug( 'name' )
891 ?>
892 <div class="wpsc-tff">
893 <div class="wpsc-tff-label">
894 <span class="name"><?php echo esc_attr( $cf->name ); ?></span>
895 </div>
896 <span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span>
897 <input
898 type="text"
899 name="<?php echo esc_attr( $cf->slug ); ?>"
900 value="<?php echo esc_attr( $customer->name ); ?>"
901 placeholder="<?php echo esc_attr( $cf->placeholder_text ); ?>"
902 autocomplete="off"/>
903 </div>
904 <?php
905 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
906 if ( $cf->field !== 'customer' || in_array( $cf->slug, WPSC_DF_Customer::$ignore_customer_info_cft ) ) {
907 continue;
908 }
909 $properties = array(
910 'is-required' => 0,
911 'width' => 'full',
912 'visibility' => '',
913 );
914 echo $cf->type::print_edit_customer_info( $cf, $customer, $properties ); // phpcs:ignore
915 }
916 do_action( 'wpsc_get_edit_rb_info_body', $customer );
917 ?>
918 <input type="hidden" name="action" value="wpsc_set_edit_rb_info"/>
919 <input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>">
920 <input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_rb_info' ) ); ?>">
921
922 <script>
923 jQuery('#rb-search').keyup(function(e) {
924 var val = jQuery(this).val().trim();
925 var regex = new RegExp(val, "i");
926 jQuery('.frm-edit-rb-info .wpsc-tff').each(function(){
927 var name = jQuery(this).find('.wpsc-tff-label').text();
928 if (name.search(regex) < 0) {
929 jQuery(this).hide();
930 } else {
931 jQuery(this).show();
932 }
933 })
934 });
935 </script>
936
937 </form>
938 <?php
939 do_action( 'wpsc_get_edit_rb_info_footer', $customer );
940 $body = ob_get_clean();
941
942 ob_start();
943 ?>
944 <button class="wpsc-button small primary" onclick="wpsc_set_edit_rb_info(this, <?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_rb_info' ) ); ?>');">
945 <?php esc_attr_e( 'Submit', 'supportcandy' ); ?>
946 </button>
947 <button class="wpsc-button small secondary" onclick="wpsc_close_modal();">
948 <?php esc_attr_e( 'Cancel', 'supportcandy' ); ?>
949 </button>
950 <?php
951 $footer = ob_get_clean();
952
953 $response = array(
954 'title' => $title,
955 'body' => $body,
956 'footer' => $footer,
957 );
958 wp_send_json( $response );
959 }
960
961 /**
962 * Save raised by info
963 *
964 * @return void
965 */
966 public static function set_edit_rb_info() {
967
968 if ( check_ajax_referer( 'wpsc_set_edit_rb_info', '_ajax_nonce', false ) != 1 ) {
969 wp_send_json_error( 'Unauthorised request!', 401 );
970 }
971
972 WPSC_Individual_Ticket::load_current_ticket();
973 $current_user = WPSC_Current_User::$current_user;
974 if ( ! ( WPSC_Individual_Ticket::$view_profile == 'agent' && $current_user->agent->has_cap( 'eci-access' ) ) ) {
975 wp_send_json_error( 'Something went wrong!', 400 );
976 }
977
978 $ticket = WPSC_Individual_Ticket::$ticket;
979 $customer = $ticket->customer;
980
981 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
982 if ( ! $name ) {
983 wp_send_json_error( new WP_Error( '002', 'Bad request!' ), 400 );
984 }
985
986 if ( $customer->name != $name ) {
987 $customer->name = $name;
988 $customer->save();
989 // Update WP User if available.
990 if ( $customer->user ) {
991 wp_update_user(
992 array(
993 'ID' => $customer->user->ID,
994 'display_name' => $name,
995 )
996 );
997 }
998 }
999
1000 $cfs = array();
1001 foreach ( WPSC_Custom_Field::$custom_fields as $cf ) {
1002
1003 if ( $cf->field !== 'customer' || $cf->type::$is_default ) {
1004 continue;
1005 }
1006 $cfs[ $cf->type::$slug ][] = $cf;
1007 }
1008
1009 foreach ( $cfs as $slug => $fields ) {
1010 WPSC_Functions::$ref_classes[ $slug ]['class']::set_create_ticket_data( array( 'customer' => $customer->id ), $cfs, true );
1011 }
1012 wp_die();
1013 }
1014 }
1015 endif;
1016
1017 WPSC_ITW_Raisedby::init();
1018