PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.6
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.6
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 / upgrade / functions.php

functions.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.6, at upgrade/functions.php

1,107 lines 29.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Includes common functions for upgrade
4 *
5 * @package SupportCandy
6 */
7
8 /**
9 * Return email notification events mapping
10 *
11 * @return array
12 */
13 function wpsc_get_en_event_map() {
14
15 return array(
16 'new_ticket' => 'create-ticket',
17 'ticket_reply' => 'reply-ticket',
18 'change_status' => 'change-ticket-status',
19 'assign_agent' => 'change-assignee',
20 'delete_ticket' => 'delete-ticket',
21 'private_note' => 'submit-note',
22 'change_category' => 'change-ticket-category',
23 'change_priority' => 'change-ticket-priority',
24 'out_of_sla' => 'out-of-sla',
25 'ticket_rating' => 'ticket-feedback',
26 'ticket_feedback' => 'ticket-feedback',
27 );
28 }
29
30 /**
31 * Upgrade macros
32 *
33 * @param string $str - string to upgrade macros in.
34 * @return string
35 */
36 function wpsc_upgrade_macros( $str ) {
37
38 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
39 preg_match_all( '/\{(.*?)\}/', $str, $matches );
40 if ( $matches[1] ) {
41 foreach ( array_unique( $matches[1] ) as $slug ) {
42 $field = get_term_by( 'slug', $slug, 'wpsc_ticket_custom_fields' );
43 if ( $field !== false && isset( $cf_map[ $field->term_id ] ) ) {
44 $cf = wpsc_get_cf_by( 'id', $cf_map[ $field->term_id ] );
45 if ( $cf->id ) {
46 $str = str_replace( '{' . $slug . '}', '{{' . $cf->slug . '}}', $str );
47 } else {
48 $str = str_replace( '{' . $slug . '}', '', $str );
49 }
50 } elseif ( $field !== false ) {
51 $str = str_replace( '{' . $slug . '}', '{{' . $slug . '}}', $str );
52 } else {
53 $str = str_replace( '{' . $slug . '}', '', $str );
54 }
55 }
56 }
57 return $str;
58 }
59
60 /**
61 * Extract BCC from old template
62 *
63 * @param mixed $template - term object for email template.
64 * @return array
65 */
66 function wpsc_en_extract_bcc( $template ) {
67
68 $recipients = get_term_meta( $template->term_id, 'recipients', true );
69 $recipient_map = array(
70 'customer' => 'customer',
71 'assigned_agent' => 'assignee',
72 'usergroup_supervisors' => 'usergroup-supervisors',
73 'usergroup_members' => 'usergroup-members',
74 'extra_ticket_users' => 'add-recipients',
75 'previously_assigned_agent' => 'prev-assignee',
76 'current_user' => 'current-user',
77 );
78 $general_recipients = array();
79 $agent_roles = array();
80 foreach ( $recipients as $recipient ) {
81 if ( is_numeric( $recipient ) ) {
82 $agent_roles[] = $recipient;
83 } else {
84 $general_recipients[] = $recipient_map[ $recipient ];
85 }
86 }
87 $additional_recipients = get_term_meta( $template->term_id, 'extra_recipients', true );
88 return array(
89 'general-recipients' => $general_recipients,
90 'agent-roles' => $agent_roles,
91 'custom' => $additional_recipients,
92 );
93 }
94
95 /**
96 * Add customer record if not exists and return customer object
97 *
98 * @param string $name - Name of the customer.
99 * @param string $email - Email address of the customer.
100 * @return stdClass
101 */
102 function wpsc_import_customer( $name, $email ) {
103
104 global $wpdb;
105 $customer = wpsc_get_customer_by( 'email', $email );
106 if ( ! $customer ) {
107 $user = get_user_by( 'email', $email );
108 $user_id = $user ? $user->ID : 0;
109 $user_display_name = $user ? $user->display_name : $name;
110 $wpdb->insert(
111 $wpdb->prefix . 'psmsc_customers',
112 array(
113 'user' => $user_id,
114 'name' => $user_display_name,
115 'email' => $email,
116 )
117 );
118 $customer = wpsc_get_customer_by( 'id', $wpdb->insert_id );
119 }
120 return $customer;
121 }
122
123 /**
124 * Delete old ticket data and its threads
125 *
126 * @param WP_Post $ticket - ticket post object.
127 * @return void
128 */
129 function wpsc_upgrade_delete_ticket( $ticket ) {
130
131 $ticket_id = get_post_meta( $ticket->ID, 'ticket_id', true );
132
133 // delete thread posts.
134 $threads = get_posts(
135 array(
136 'post_type' => 'wpsc_ticket_thread',
137 'post_status' => 'publish',
138 'orderby' => 'date',
139 'order' => 'ASC',
140 'posts_per_page' => -1,
141 'meta_query' => array(
142 'relation' => 'AND',
143 array(
144 'key' => 'ticket_id',
145 'value' => $ticket_id,
146 'compare' => '=',
147 ),
148 ),
149 )
150 );
151 foreach ( $threads as $thread ) {
152 wp_delete_post( $thread->ID, true );
153 }
154 // delete ticket.
155 wp_delete_post( $ticket->ID, true );
156 }
157
158 /**
159 * Return installed plugin info
160 *
161 * @return array
162 */
163 function wpsc_upgrade_get_installed_plugin_info() {
164
165 $installed_addons = array(
166 0 => array(
167 'name' => 'Automatic close tickets',
168 'installer' => 'WPSC_ATC_Installation',
169 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-automatic-close-ticket/wpsc-automatic-close-ticket.php',
170 ),
171 1 => array(
172 'name' => 'Canned reply',
173 'installer' => 'WPSC_CR_Installation',
174 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-canned-reply/wpsc-canned-reply.php',
175 ),
176 2 => array(
177 'name' => 'Export tickets',
178 'installer' => 'WPSC_EXPORT_Installation',
179 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-export-ticket/wpsc-export-ticket.php',
180 ),
181 3 => array(
182 'name' => 'Email piping',
183 'installer' => 'WPSC_EP_Installation',
184 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-email-piping/wpsc-email-piping.php',
185 ),
186 4 => array(
187 'name' => 'SLA',
188 'installer' => 'WPSC_SLA_Installation',
189 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-sla/wpsc-sla.php',
190 ),
191 5 => array(
192 'name' => 'WooCommerce integration',
193 'installer' => 'WPSC_WOO_Installation',
194 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-woocommerce/wpsc-woocommerce.php',
195 ),
196 6 => array(
197 'name' => 'Reports',
198 'installer' => 'WPSC_RP_Installation',
199 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-reports/wpsc-reports.php',
200 ),
201 7 => array(
202 'name' => 'Usergroups',
203 'installer' => 'WPSC_UG_Installation',
204 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-usergroup/wpsc-usergroup.php',
205 ),
206 8 => array(
207 'name' => 'EDD intergration',
208 'installer' => 'WPSC_EDD_Installation',
209 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-edd/wpsc-edd.php',
210 ),
211 9 => array(
212 'name' => 'Gravity Forms integration',
213 'installer' => 'WPSC_GF_Installation',
214 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-gravity-forms/wpsc-gravity-form-integration.php',
215 ),
216 10 => array(
217 'name' => 'Assign agent rules',
218 'installer' => 'WPSC_AAR_Installation',
219 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-assign-agent-rules/wpsc-assign-agent-rules.php',
220 ),
221 11 => array(
222 'name' => 'FAQ integration',
223 'installer' => 'WPSC_FAQ_Installation',
224 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-ultimate-faq/wpsc-ultimate-faq.php',
225 ),
226 12 => array(
227 'name' => 'Knowledgebase integration',
228 'installer' => 'WPSC_KB_Installation',
229 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-pressapps-knowledge-base/wpsc-pressapps-knowledge-base.php',
230 ),
231 13 => array(
232 'name' => 'Timer',
233 'installer' => 'WPSC_Timer_Installation',
234 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-timer/wpsc-timer.php',
235 ),
236 14 => array(
237 'name' => 'Schedule tickets',
238 'installer' => 'WPSC_ST_Installation',
239 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-schedule-tickets/wpsc-schedule-tickets.php',
240 ),
241 15 => array(
242 'name' => 'Satisfaction survey',
243 'installer' => 'WPSC_SF_Installation',
244 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-satisfaction-survey/wpsc-satisfaction-survey.php',
245 ),
246 16 => array(
247 'name' => 'Agentgroups',
248 'installer' => 'WPSC_AG_Installation',
249 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-agentgroup/wpsc-agentgroup.php',
250 ),
251 17 => array(
252 'name' => 'Private credentials',
253 'installer' => 'WPSC_PC_Installation',
254 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-private-credentials/wpsc_private_credentials.php',
255 ),
256 18 => array(
257 'name' => 'Print ticket',
258 'installer' => 'WPSC_PRINT_Installation',
259 'plugin_file' => WP_PLUGIN_DIR . '/wpsc-print-ticket/wpsc-print-ticket.php',
260 ),
261 );
262
263 foreach ( $installed_addons as $index => $addon ) {
264
265 $plugin_data = array();
266 $is_active = 0;
267 if ( file_exists( $addon['plugin_file'] ) ) {
268 $plugin_data = get_plugin_data( $addon['plugin_file'] );
269 preg_match( '/plugins\/(.*\.php)/', $addon['plugin_file'], $matches );
270 $is_active = is_plugin_active( $matches[1] );
271 }
272 $installed_addons[ $index ]['is_installed'] = $plugin_data ? true : false;
273 $installed_addons[ $index ]['version'] = $plugin_data ? $plugin_data['Version'] : 0;
274 $installed_addons[ $index ]['is_active'] = $is_active;
275 }
276
277 return $installed_addons;
278 }
279
280 /**
281 * Enqueue scripts for upgrade
282 *
283 * @return void
284 */
285 function wpsc_upgrade_enqueue_scripts() {
286
287 // jquery.
288 wp_enqueue_script( 'jquery' );
289 wp_enqueue_script( 'jquery-ui-core' );
290 wp_enqueue_style( 'wpsc-jquery-ui', WPSC_PLUGIN_URL . 'asset/css/jquery-ui.css', array(), WPSC_VERSION );
291
292 // Progrss bar.
293 wp_enqueue_script( 'jquery-ui-progressbar' );
294
295 // localize scripts.
296 wp_enqueue_script( 'wpsc-admin', WPSC_PLUGIN_URL . 'asset/js/admin.js', array( 'jquery' ), WPSC_VERSION, true );
297 wp_localize_script(
298 'wpsc-admin',
299 'supportcandy',
300 array(
301 'ajax_url' => admin_url( 'admin-ajax.php' ),
302 'plugin_url' => WPSC_PLUGIN_URL,
303 'version' => WPSC_VERSION,
304 'loader_html' => wpsc_upgrade_loader_html(),
305 )
306 );
307 }
308
309 /**
310 * Loader html
311 *
312 * @return string
313 */
314 function wpsc_upgrade_loader_html() {
315
316 ob_start();
317 ?>
318 <div class="wpsc-loader">
319 <img src="<?php echo esc_url( WPSC_PLUGIN_URL . 'asset/images/loader.gif' ); ?>" alt="Loading..." />
320 </div>
321 <?php
322 return ob_get_clean();
323 }
324
325 /**
326 * Check whether current use is site admin or not
327 *
328 * @return boolean
329 */
330 function wpsc_upgrade_is_site_admin() {
331
332 global $current_user;
333 return $current_user->ID && $current_user->has_cap( 'manage_options' ) ? true : false;
334 }
335
336 /**
337 * Send an email for site administrator for upgrade only once.
338 *
339 * @return void
340 */
341 function wpsc_upgrade_send_admin_email() {
342
343 $is_sent = get_option( 'wpsc_upgrade_v3_admin_email', 0 );
344 if ( ! $is_sent ) {
345 update_option( 'wpsc_upgrade_v3_admin_email', 1 );
346 $to = get_bloginfo( 'admin_email' );
347 $subject = '[SupportCandy] Action needed!';
348 $upgrade_link = admin_url( 'admin.php?page=wpsc-tickets' );
349 $body = '<p>Hello,</p><p>This is to inform you that SupportCandy has been updated to version v3.0.0. This version has significant changes that may break your current workflow. To avoid this, we need you to manually review the changes and proceed to the database upgrade.</p><p><a href="' . $upgrade_link . '">Click here</a> to review the changes.</p><p>If you are not yet ready for this update, you can revert it back to the old version manually.</p><p>Please note that SupportCandy is in disabled mode. That means neither your customers can create tickets nor your agents can view/reply to tickets until you do this.</p><p>Regards,</p><p>SupportCandy Team</p>';
350 $headers = array( 'Content-Type: text/html; charset=UTF-8' );
351 wp_mail( $to, $subject, $body, $headers );
352 }
353 }
354
355 /**
356 * Register post types in order to get data
357 *
358 * @return void
359 */
360 function wpsc_upgrade_register_post_type() {
361
362 $args = array(
363 'public' => false,
364 'rewrite' => false,
365 );
366 register_post_type( 'wpsc_ticket', $args );
367 register_post_type( 'wpsc_ticket_thread', $args );
368 register_taxonomy( 'wpsc_categories', 'wpsc_ticket', $args );
369 register_taxonomy( 'wpsc_statuses', 'wpsc_ticket', $args );
370 register_taxonomy( 'wpsc_priorities', 'wpsc_ticket', $args );
371 register_taxonomy( 'wpsc_ticket_custom_fields', 'wpsc_ticket', $args );
372 register_taxonomy( 'wpsc_ticket_widget', 'wpsc_ticket', $args );
373 register_taxonomy( 'wpsc_agents', 'wpsc_ticket', $args );
374 register_taxonomy( 'wpsc_attachment', 'wpsc_ticket', $args );
375 register_taxonomy( 'wpsc_en', 'wpsc_ticket', $args );
376 // conditional assigned agent (assigned agent rules).
377 register_taxonomy( 'wpsc_caa', 'wpsc_ticket', $args );
378 // canned reply.
379 register_post_type( 'wpsc_canned_reply', $args );
380 register_taxonomy( 'wpsc_canned_reply_categories', 'wpsc_canned_reply', $args );
381 // satisfaction survey.
382 register_taxonomy( 'wpsc_sf_rating', 'wpsc_ticket', $args );
383 // email piping.
384 register_taxonomy( 'wpsc_ep_rules', 'wpsc_ticket', $args );
385 // schedule tickets.
386 register_taxonomy( 'wpsc_schedule_tickets', 'wpsc_ticket', $args );
387 // sla.
388 register_taxonomy( 'wpsc_sla', 'wpsc_ticket', $args );
389 // usergroup.
390 register_taxonomy( 'wpsc_usergroup_data', 'wpsc_ticket', $args );
391 register_taxonomy( 'wpsc_usergroup_custom_field', 'wpsc_ticket', $args );
392 // gravity forms.
393 register_taxonomy( 'wpsc_gf', 'wpsc_ticket', $args );
394 }
395
396 /**
397 * Insert custom field
398 *
399 * @param array $data - array of data to create custom field.
400 * @return array
401 */
402 function wpsc_upgrade_insert_custom_field( $data ) {
403
404 global $wpdb;
405
406 // insert record.
407 $wpdb->insert(
408 $wpdb->prefix . 'psmsc_custom_fields',
409 $data
410 );
411
412 // update slug.
413 $id = $wpdb->insert_id;
414 $slug = 'cust_' . $id;
415 $wpdb->update(
416 $wpdb->prefix . 'psmsc_custom_fields',
417 array( 'slug' => $slug ),
418 array( 'id' => $id )
419 );
420
421 return array(
422 'id' => $id,
423 'slug' => $slug,
424 );
425 }
426
427 /**
428 * Get custom field row by id or slug.
429 *
430 * @param string $field - id or slug.
431 * @param string $value - field value.
432 * @return Collection
433 */
434 function wpsc_get_cf_by( $field, $value ) {
435
436 global $wpdb;
437
438 if ( $field == 'id' ) {
439
440 return $wpdb->get_row( "SELECT * from {$wpdb->prefix}psmsc_custom_fields WHERE id = " . $value );
441
442 } else {
443
444 return $wpdb->get_row( "SELECT * from {$wpdb->prefix}psmsc_custom_fields WHERE slug = '" . $value . "'" );
445 }
446 }
447
448 /**
449 * Get customer by either id, email or user_id
450 *
451 * @param string $field - either id, email or user_id.
452 * @param string $value - field value.
453 * @return stdClass
454 */
455 function wpsc_get_customer_by( $field, $value ) {
456
457 global $wpdb;
458 if ( $field == 'id' ) {
459
460 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_customers WHERE id = " . $value );
461
462 } elseif ( $field == 'email' ) {
463
464 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_customers WHERE email = '" . $value . "'" );
465
466 } else {
467
468 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_customers WHERE user = '" . $value . "'" );
469 }
470 }
471
472 /**
473 * Get agent by either id, customer_id or user_id
474 *
475 * @param string $field - either id, customer_id or user_id.
476 * @param string $value - field value.
477 * @return stdClass
478 */
479 function wpsc_get_agent_by( $field, $value ) {
480
481 global $wpdb;
482 if ( $field == 'id' ) {
483
484 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_agents WHERE id = " . $value );
485
486 } elseif ( $field == 'customer_id' ) {
487
488 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_agents WHERE customer = '" . $value . "'" );
489
490 } else {
491
492 return $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_agents WHERE user = '" . $value . "'" );
493 }
494 }
495
496 /**
497 * Get custom field options
498 *
499 * @param int $cf_id - custom field id.
500 * @return array
501 */
502 function wpsc_get_cf_options( $cf_id ) {
503
504 global $wpdb;
505 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_options WHERE custom_field = ' . $cf_id;
506 return $wpdb->get_results( $sql );
507 }
508
509 /**
510 * Get ticket meta for version 2
511 *
512 * @param integer $ticket_id - ticket id.
513 * @param string $meta_key - meta key.
514 * @param boolean $flag - flag.
515 * @return string
516 */
517 function get_ticket_meta( $ticket_id, $meta_key, $flag = false ) {
518
519 global $wpdb;
520 if ( $flag ) {
521
522 $meta_value = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->prefix}wpsc_ticketmeta WHERE ticket_id= " . $ticket_id . " AND meta_key = '" . $meta_key . "'" );
523 $meta_value = stripslashes( $meta_value ) ? stripslashes( $meta_value ) : '';
524
525 } else {
526
527 $meta_value = array();
528 $results = $wpdb->get_results( "SELECT meta_value FROM {$wpdb->prefix}wpsc_ticketmeta WHERE ticket_id= " . $ticket_id . " AND meta_key = '" . $meta_key . "'" );
529 if ( $results ) {
530 foreach ( $results as $result ) {
531 $meta_value[] = stripslashes( $result->meta_value );
532 }
533 }
534 }
535 return $meta_value;
536 }
537
538 /**
539 * Convert seconds to date interval string
540 *
541 * @param integer $seconds - seconds.
542 * @return string
543 */
544 function convert_sec_to_date_interval_string( $seconds ) {
545
546 $m = floor( ( $seconds % 3600 ) / 60 );
547 $h = floor( ( $seconds % 86400 ) / 3600 );
548 $days = floor( ( $seconds % 2592000 ) / 86400 );
549
550 $str = 'P';
551
552 if ( $days ) {
553
554 $str .= $days . 'D';
555
556 }
557
558 if ( $h || $m ) {
559
560 $str .= 'T';
561 if ( $h ) {
562 $str .= $h . 'H';
563 }
564 if ( $m ) {
565 $str .= $m . 'M';
566 }
567 }
568
569 if ( $str === 'P' ) {
570 $str = 'PT0M';
571 }
572
573 return $str;
574 }
575
576 /**
577 * Upgrade email notification conditions, Assign agent rules, sla policies.
578 *
579 * @param string $prev_conditions - conditions.
580 * @return string
581 */
582 function upgrade_conditions( $prev_conditions ) {
583
584 global $wpdb;
585 if ( ! $prev_conditions ) {
586 return '{}';
587 }
588
589 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
590 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
591 $status_map = get_option( 'wpsc_upgrade_status_map' );
592 $category_map = get_option( 'wpsc_upgrade_category_map' );
593 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
594 $usergroup_map = get_option( 'wpsc_upgrade_ug_term_id_map' );
595
596 $other_cnds = array(
597 'agent_created' => 'submitted_by',
598 'user_type' => 'cf_user_type',
599 'user_wp_roles' => 'user_role',
600 'ep_forwarded_from' => 'en_from',
601 'usergroup' => 'cf_usergroups',
602 );
603
604 $conditions = array();
605 $prev_conditions = json_decode( $prev_conditions, true );
606 foreach ( $prev_conditions as $key => $val ) {
607
608 $compare = '';
609
610 switch ( $val['compare'] ) {
611 case 'match':
612 $compare = 'IN';
613 break;
614
615 case 'not_match':
616 $compare = 'NOT IN';
617 break;
618
619 case 'contain':
620 $compare = 'LIKE';
621 break;
622 }
623
624 if ( array_key_exists( $val['field'], $cf_map ) ) {
625
626 $cf = wpsc_get_cf_by( 'id', $cf_map[ $val['field'] ] );
627 if ( ! $cf->id || in_array( $cf->type, array( 'df_customer_name' ) ) ) {
628 continue;
629 }
630
631 if ( array_key_exists( 'cf_' . $cf->slug, $conditions ) &&
632 (
633 ( in_array( $conditions[ 'cf_' . $cf->slug ]['operator'], array( 'IN', 'LIKE' ) ) && $compare == 'NOT IN' ) ||
634 ( $conditions[ 'cf_' . $cf->slug ]['operator'] == 'NOT IN' && in_array( $compare, array( 'IN', 'LIKE' ) ) )
635 )
636 ) {
637 unset( $conditions[ 'cf_' . $cf->slug ] );
638 continue;
639 }
640
641 switch ( $cf->slug ) {
642
643 case 'status':
644 if ( array_key_exists( 'cf_status', $conditions ) ) {
645
646 $conditions['cf_status']['operand_val_1'][] = $status_map[ $val['cond_val'] ];
647
648 } else {
649
650 $conditions['cf_status'] = array(
651 'operator' => $compare,
652 'operand_val_1' => array(
653 $status_map[ $val['cond_val'] ],
654 ),
655 );
656 }
657 break;
658
659 case 'category':
660 if ( array_key_exists( 'cf_category', $conditions ) ) {
661
662 $conditions['cf_category']['operand_val_1'][] = $category_map[ $val['cond_val'] ];
663
664 } else {
665
666 $conditions['cf_category'] = array(
667 'operator' => $compare,
668 'operand_val_1' => array(
669 $category_map[ $val['cond_val'] ],
670 ),
671 );
672 }
673 break;
674
675 case 'priority':
676 if ( array_key_exists( 'cf_priority', $conditions ) ) {
677
678 $conditions['cf_priority']['operand_val_1'][] = $priority_map[ $val['cond_val'] ];
679
680 } else {
681
682 $conditions['cf_priority'] = array(
683 'operator' => $compare,
684 'operand_val_1' => array(
685 $priority_map[ $val['cond_val'] ],
686 ),
687 );
688 }
689 break;
690
691 case 'assigned_agent':
692 $map = get_option( 'wpsc_upgrade_agent_map' );
693 if ( array_key_exists( 'cf_' . $cf->slug, $conditions ) ) {
694
695 $conditions[ 'cf_' . $cf->slug ]['operand_val_1'][] = $map[ $val['cond_val'] ];
696
697 } else {
698
699 $conditions[ 'cf_' . $cf->slug ] = array(
700 'operator' => $compare,
701 'operand_val_1' => array(
702 $map[ $val['cond_val'] ],
703 ),
704 );
705 }
706 break;
707
708 case 'description':
709 if ( $compare == 'IN' || $compare == 'LIKE' ) {
710
711 if ( array_key_exists( 'cf_' . $cf->slug, $conditions ) ) {
712
713 $prev_val = array( $conditions[ 'cf_' . $cf->slug ]['operand_val_1'] );
714 $prev_val[] = $val['cond_val'];
715 $conditions[ 'cf_' . $cf->slug ]['operand_val_1'] = implode( "\n", $prev_val );
716
717 } else {
718
719 $conditions[ 'cf_' . $cf->slug ] = array(
720 'operator' => 'LIKE',
721 'operand_val_1' => $val['cond_val'],
722 );
723 }
724 }
725 break;
726
727 default:
728 if ( in_array( $cf->type, array( 'cf_checkbox', 'cf_radio_button', 'cf_single_select' ) ) ) {
729
730 if ( array_key_exists( 'cf_' . $cf->slug, $conditions ) ) {
731
732 $conditions[ 'cf_' . $cf->slug ]['operand_val_1'][] = $options_map[ $cf->id ][ $val['cond_val'] ];
733
734 } else {
735
736 $conditions[ 'cf_' . $cf->slug ] = array(
737 'operator' => $compare,
738 'operand_val_1' => array(
739 $options_map[ $cf->id ][ $val['cond_val'] ],
740 ),
741 );
742 }
743 } elseif ( in_array( $cf->type, array( 'df_customer_email' ) ) ) {
744
745 $customer = wpsc_get_customer_by( 'email', $val['cond_val'] );
746 if ( $customer ) {
747 if ( array_key_exists( 'cf_customer', $conditions ) ) {
748
749 $conditions['cf_customer']['operand_val_1'][] = $customer->id;
750
751 } else {
752
753 $conditions['cf_customer'] = array(
754 'operator' => $compare,
755 'operand_val_1' => array(
756 $customer->id,
757 ),
758 );
759 }
760 }
761 } elseif ( $cf->type == 'cf_woo_product' || $cf->type == 'cf_edd_product' ) {
762
763 if ( array_key_exists( 'cf_' . $cf->slug, $conditions ) ) {
764
765 $conditions[ 'cf_' . $cf->slug ]['operand_val_1'][] = $val['cond_val'];
766
767 } else {
768
769 $conditions[ 'cf_' . $cf->slug ] = array(
770 'operator' => $compare,
771 'operand_val_1' => array(
772 $val['cond_val'],
773 ),
774 );
775 }
776 } elseif ( array_key_exists( 'cf_' . $cf->slug, $conditions ) ) {
777
778 $prev_val = array( $conditions[ 'cf_' . $cf->slug ]['operand_val_1'] );
779 $prev_val[] = $val['cond_val'];
780 $conditions[ 'cf_' . $cf->slug ]['operand_val_1'] = implode( "\n", $prev_val );
781
782 } else {
783
784 $conditions[ 'cf_' . $cf->slug ] = array(
785 'operator' => $compare,
786 'operand_val_1' => $val['cond_val'],
787 );
788 }
789 }
790 } elseif ( array_key_exists( $val['field'], $other_cnds ) ) {
791
792 $new_slug = $other_cnds[ $val['field'] ];
793 if ( array_key_exists( $new_slug, $conditions ) &&
794 (
795 ( in_array( $conditions[ $new_slug ]['operator'], array( 'IN', 'LIKE' ) ) && $compare == 'NOT IN' ) ||
796 ( $conditions[ $new_slug ]['operator'] == 'NOT IN' && in_array( $compare, array( 'IN', 'LIKE' ) ) )
797 )
798 ) {
799 unset( $conditions[ $new_slug ] );
800 continue;
801 }
802
803 switch ( $val['field'] ) {
804
805 case 'agent_created':
806 if ( array_key_exists( 'submitted_by', $conditions ) ) {
807 unset( $conditions['submitted_by'] );
808 } else {
809 $value = $val['compare'] == 'not_match' && $val['cond_val'] == 'user' ? 'agent' : 'user';
810 $conditions['submitted_by'] = array(
811 'operator' => '=',
812 'operand_val_1' => $value,
813 );
814 }
815 break;
816
817 case 'user_type':
818 if ( array_key_exists( 'cf_user_type', $conditions ) ) {
819 unset( $conditions['cf_user_type'] );
820 } else {
821 $value = $val['cond_val'] == 'user' ? 'registered' : 'guest';
822 $value = $val['compare'] == 'not_match' && $val['cond_val'] == 'user' ? 'guest' : 'registered';
823 $conditions['cf_user_type'] = array(
824 'operator' => '=',
825 'operand_val_1' => $value,
826 );
827 }
828 break;
829
830 case 'user_wp_roles':
831 if ( array_key_exists( 'user_role', $conditions ) ) {
832 $conditions['user_role']['operand_val_1'][] = $val['cond_val'];
833 } else {
834
835 $conditions['user_role'] = array(
836 'operator' => $compare,
837 'operand_val_1' => array(
838 $val['cond_val'],
839 ),
840 );
841 }
842 break;
843
844 case 'ep_forwarded_from':
845 if ( array_key_exists( 'en_from', $conditions ) ) {
846 $prev_val = array( $conditions['en_from']['operand_val_1'] );
847 $prev_val[] = $val['cond_val'];
848 $conditions['en_from']['operand_val_1'] = implode( "\n", $prev_val );
849 } else {
850
851 $conditions['en_from'] = array(
852 'operator' => $compare,
853 'operand_val_1' => $val['cond_val'],
854 );
855 }
856 break;
857
858 case 'usergroup':
859 if ( array_key_exists( 'cf_usergroups', $conditions ) ) {
860
861 $conditions['cf_usergroups']['operand_val_1'][] = $usergroup_map[ $val['cond_val'] ];
862 } else {
863
864 $conditions['cf_usergroups'] = array(
865 'operator' => $compare,
866 'operand_val_1' => array(
867 $usergroup_map[ $val['cond_val'] ],
868 ),
869 );
870 }
871 break;
872 }
873 }
874 }
875
876 return wp_json_encode( $conditions );
877 }
878
879 /**
880 * Check whether given customer is an agent
881 *
882 * @param int $customer_id - customer id.
883 * @return boolean
884 */
885 function wpsc_is_agent( $customer_id ) {
886
887 global $wpdb;
888 $agent_id = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}psmsc_agents WHERE customer = " . $customer_id );
889 return $agent_id ? true : false;
890 }
891
892 /**
893 * Perform sum of date intervals
894 *
895 * @param array $arr - date interval array.
896 * @return DateInterval
897 */
898 function wpsc_date_interval_sum( $arr ) {
899
900 $response = $arr[0];
901 $arrau_count = count( $arr );
902 for ( $i = 1; $i < $arrau_count; $i++ ) {
903 $today = new DateTime();
904 $sum_today = clone $today;
905 $sum_today->add( $response );
906 $sum_today->add( $arr[ $i ] );
907 $response = $today->diff( $sum_today );
908 }
909 return $response;
910 }
911
912 /**
913 * Return string representation of date interval
914 *
915 * @param DateInterval $diff - date interval.
916 * @return string
917 */
918 function wpsc_date_interval_to_string( $diff ) {
919
920 $str = 'P';
921
922 if ( $diff->days ) {
923
924 $str .= $diff->format( '%aD' );
925
926 } elseif ( $diff->d ) {
927
928 $str .= $diff->format( '%dD' );
929
930 }
931
932 if ( $diff->h || $diff->i ) {
933
934 $str .= 'T';
935 $str .= $diff->h ? $diff->format( '%hH' ) : '';
936 $str .= $diff->i ? $diff->format( '%iM' ) : '';
937 }
938
939 if ( $str === 'P' ) {
940 $str = 'PT0M';
941 }
942
943 return $str;
944 }
945
946 /**
947 * Update total ticket count for the customer in database
948 *
949 * @param stdClass $customer - customer db object.
950 * @return void
951 */
952 function wpsc_update_customer_ticket_count( $customer ) {
953
954 global $wpdb;
955 $count = $wpdb->get_var( "SELECT count(id) from {$wpdb->prefix}psmsc_tickets WHERE is_active=1 AND customer=" . $customer->id );
956 $wpdb->update(
957 $wpdb->prefix . 'psmsc_customers',
958 array( 'ticket_count' => $count ),
959 array( 'id' => $customer->id )
960 );
961 }
962
963 /**
964 * Update customer usergroups
965 *
966 * @param stdClass $customer - customer db object.
967 * @return void
968 */
969 function wpsc_update_customer_usergroups( $customer ) {
970
971 global $wpdb;
972 $sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_usergroups WHERE members RLIKE \'(^|[|])' . $customer->id . '($|[|])\'';
973 $usergroups = array_map(
974 function ( $group ) {
975 return $group->id;
976 },
977 $wpdb->get_results( $sql )
978 );
979 if ( $usergroups ) {
980 $wpdb->update(
981 $wpdb->prefix . 'psmsc_customers',
982 array( 'usergroups' => implode( '|', $usergroups ) ),
983 array( 'id' => $customer->id )
984 );
985 }
986 }
987
988 /**
989 * Return whether or not agent has given capability
990 *
991 * @param stdClass $agent - agent db object.
992 * @param string $cap - capability slug.
993 * @return boolean
994 */
995 function wpsc_agent_has_cap( $agent, $cap ) {
996
997 $roles = get_option( 'wpsc-agent-roles', array() );
998 if ( ! isset( $roles[ $agent->role ] ) ) {
999 return false;
1000 }
1001 $role = $roles[ $agent->role ];
1002 return isset( $role['caps'][ $cap ] ) && $role['caps'][ $cap ] ? true : false;
1003 }
1004
1005 /**
1006 * Reset unresoved count for given agent
1007 *
1008 * @param stdClass $agent - agent db object.
1009 * @return void
1010 */
1011 function wpsc_agent_reset_unresolved_count( $agent ) {
1012
1013 global $wpdb;
1014 $more_settings = get_option( 'wpsc-tl-ms-agent-view' );
1015 if ( ! $more_settings['unresolved-ticket-statuses'] ) {
1016
1017 $wpdb->update(
1018 $wpdb->prefix . 'psmsc_agents',
1019 array( 'unresolved_count' => 0 ),
1020 array( 'id' => $agent->id )
1021 );
1022 } else {
1023
1024 $sql = "SELECT count(id) FROM {$wpdb->prefix}psmsc_tickets WHERE is_active=1 AND status IN(" . implode( ',', $more_settings['unresolved-ticket-statuses'] ) . ') AND ';
1025 // system query.
1026 $where = array( 'customer=' . $agent->customer );
1027 if ( wpsc_agent_has_cap( $agent, 'view-assigned-me' ) ) {
1028 $where[] = 'assigned_agent=' . $agent->id;
1029 }
1030 if ( wpsc_agent_has_cap( $agent, 'view-unassigned' ) ) {
1031 $where[] = "assigned_agent=''";
1032 }
1033 if ( wpsc_agent_has_cap( $agent, 'view-assigned-others' ) ) {
1034 $where[] = "assigned_agent NOT IN( '', " . $agent->id . ' )';
1035 }
1036
1037 // merge system query.
1038 $sql = $sql . '( ' . implode( ' OR ', $where ) . ' )';
1039 $count = $wpdb->get_var( $sql );
1040
1041 // update in db.
1042 $wpdb->update(
1043 $wpdb->prefix . 'psmsc_agents',
1044 array( 'unresolved_count' => $count ),
1045 array( 'id' => $agent->id )
1046 );
1047 }
1048 }
1049
1050 /**
1051 * Reset wordkload for given agent
1052 *
1053 * @param stdClass $agent - agent db object.
1054 * @return void
1055 */
1056 function wpsc_agent_reset_workload( $agent ) {
1057
1058 global $wpdb;
1059 $more_settings = get_option( 'wpsc-tl-ms-agent-view' );
1060 if ( ! $more_settings['unresolved-ticket-statuses'] ) {
1061 $wpdb->update(
1062 $wpdb->prefix . 'psmsc_agents',
1063 array( 'workload' => 0 ),
1064 array( 'id' => $agent->id )
1065 );
1066 } else {
1067 $sql = "SELECT count(id) FROM {$wpdb->prefix}psmsc_tickets WHERE is_active=1 AND status IN(" . implode( ',', $more_settings['unresolved-ticket-statuses'] ) . ') AND assigned_agent=' . $agent->id;
1068 $count = $wpdb->get_var( $sql );
1069
1070 // update in db.
1071 $wpdb->update(
1072 $wpdb->prefix . 'psmsc_agents',
1073 array( 'workload' => $count ),
1074 array( 'id' => $agent->id )
1075 );
1076 }
1077 }
1078
1079 /**
1080 * Destroy existing ticket
1081 *
1082 * @param int $ticket_id - ticket id to destroy.
1083 * @return void
1084 */
1085 function wpsc_destroy_existing_ticket( $ticket_id ) {
1086
1087 global $wpdb;
1088
1089 // delete ticket record.
1090 $wpdb->delete(
1091 $wpdb->prefix . 'psmsc_tickets',
1092 array( 'id' => $ticket_id )
1093 );
1094
1095 // delete threads.
1096 $wpdb->delete(
1097 $wpdb->prefix . 'psmsc_threads',
1098 array( 'ticket' => $ticket_id )
1099 );
1100
1101 // delete attachments.
1102 $wpdb->delete(
1103 $wpdb->prefix . 'psmsc_attachments',
1104 array( 'ticket_id' => $ticket_id )
1105 );
1106 }
1107