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 / upgrade / class-wpsc-upgrade-db-v1.php

class-wpsc-upgrade-db-v1.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.3.9, at upgrade/class-wpsc-upgrade-db-v1.php

4,126 lines 127.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly!
5 }
6
7 if ( ! class_exists( 'WPSC_Upgrade_DB_V1' ) ) :
8
9 final class WPSC_Upgrade_DB_V1 {
10
11 /**
12 * Number of posts to be processed per page
13 *
14 * @var integer
15 */
16 public static $posts_per_page = 20;
17
18 /**
19 * Initialize this class
20 *
21 * @return void
22 */
23 public static function init() {
24
25 add_action( 'init', 'wpsc_upgrade_send_admin_email' );
26 add_action( 'admin_menu', array( __CLASS__, 'load_admin_menus' ), 11 );
27 add_action( 'admin_enqueue_scripts', 'wpsc_upgrade_enqueue_scripts' );
28
29 // register custom post typs for upgrade.
30 add_action( 'init', 'wpsc_upgrade_register_post_type' );
31
32 // check compatibility.
33 add_action( 'wp_ajax_wpsc_authorize_v1_upgrade', array( __CLASS__, 'authorize' ) );
34 add_action( 'wp_ajax_wpsc_upgrade_v1_check_compatibility', array( __CLASS__, 'check_compatibility' ) );
35
36 // cron manager.
37 add_action( 'init', array( __CLASS__, 'cron_manager' ) );
38 add_action( 'wp_ajax_wpsc_v1_upgrade_status', array( __CLASS__, 'get_v1_upgrade_cron_status' ) );
39 add_action( 'wpsc_upgrade_v1', array( __CLASS__, 'upgrade' ) );
40 }
41
42 /**
43 * Load upgrade screen for admin menu
44 *
45 * @return void
46 */
47 public static function load_admin_menus() {
48
49 add_menu_page(
50 esc_attr__( 'Support', 'supportcandy' ),
51 esc_attr__( 'Support', 'supportcandy' ),
52 'manage_options',
53 'wpsc-tickets',
54 array( __CLASS__, 'upgrade_layout' ),
55 'dashicons-sos',
56 25
57 );
58 }
59
60 /**
61 * Authorize upgrade by an administrator
62 *
63 * @return void
64 */
65 public static function authorize() {
66
67 if ( ! wpsc_upgrade_is_site_admin() ) {
68 wp_send_json_error( 'Unauthorised request!', 401 );
69 }
70
71 if ( check_ajax_referer( 'wpsc_authorize_v1_upgrade', '_ajax_nonce', false ) != 1 ) {
72 wp_send_json_error( 'Unauthorised request!', 401 );
73 }
74
75 update_option( 'wpsc_upgrade_permission_v1', 1 );
76 wp_send_json( array( 'success' => true ) );
77 }
78
79 /**
80 * Upgrade layout caller
81 *
82 * @return void
83 */
84 public static function upgrade_layout() {
85
86 $upgrade_permission = get_option( 'wpsc_upgrade_permission_v1', 0 );
87 ?>
88 <div class="wrap">
89 <hr class="wp-header-end">
90 <h1>SupportCandy v3.0</h1>
91 <div class="wpsc-upgrade-container">
92 <?php
93 if ( ! $upgrade_permission ) {
94 ?>
95 <p>SupportCandy v3.0 is a major release. This version has significant changes that may break your current workflow. Please review the below changes that may be responsible for breaking your current workflow if it is dependent on them. <strong>We strongly recommend you check this on your test or staging site before the live site.</strong></p>
96 <ol>
97 <li>You must update all premium add-ons (if any).</li>
98 <li>If you took any paid customizations from us, it will not work. Please contact us on <a href="mailto:[email protected]">support@supportcandy.net</a> to know when you will get your customization delivered. Until then you must revert to the previous version.</li>
99 <li>All hooks and filters are changed.</li>
100 <li>Translations are subject to availability. <a href="https://wordpress.org/plugins/supportcandy/">Click here</a> to see list of translations available.</li>
101 <li>User interface design is changed. Previous appearance settings will not work on the new interface.</li>
102 </ol>
103 <p>It will migrate your tickets to custom database tables to improve performance and efficiency. This process may take a while and will make <strong>irreversible</strong> changes to the database. Please create a <strong>complete backup of your website</strong> before proceeding.</p>
104 <p><input type="checkbox" class="wpsc-confirm-backup"> I have secured a backup of my website data.</p>
105 <p><input type="checkbox" class="wpsc-confirm-read"> I have read and understood all the points above.</p>
106 <p>
107 <button disabled class="button action proceed">Proceed to upgrade</button>
108 <script>
109 jQuery('.wpsc-confirm-backup, .wpsc-confirm-read').change(function(){
110 let cb1 = jQuery('.wpsc-confirm-backup');
111 let cb2 = jQuery('.wpsc-confirm-read');
112 if ( cb1.is(":checked") && cb2.is(":checked") ) {
113 jQuery('button.proceed').attr( 'disabled', false );
114 } else {
115 jQuery('button.proceed').attr( 'disabled', true );
116 }
117 });
118 </script>
119 </p>
120 <h3 style="margin-top: 50px;">How do I revert this to the previous version?</h3>
121 <p>If you are not ready to upgrade yet, you can revert to the previous version of SupportCandy and its add-ons with the help of the below steps:</p>
122 <h4 style="margin-top: 30px;">Revert core plugin</h4>
123 <p>
124 <ol>
125 <li><a href="https://downloads.wordpress.org/plugin/supportcandy.2.3.1.zip">Click here</a> to download the previous version, v2.3.1</li>
126 <li>Go to the Plugins page in your WordPress dashboard.</li>
127 <li>Deactivate and delete SupportCandy.</li>
128 <li>Click Add New plugin.</li>
129 <li>Click Upload Plugin.</li>
130 <li>Upload the downloaded zip file in step 1.</li>
131 <li>Activate</li>
132 </ol>
133 </p>
134 <h4 style="margin-top: 30px;">Revert add-ons (if any)</h4>
135 <p>
136 If you have premium add-ons installed for SupportCandy, you can follow the below steps to revert them:
137 <ol>
138 <li><a href="https://supportcandy.net/account/" target="_blank">Click here</a> to go to the My Account page on our website.</li>
139 <li>Login to your account.</li>
140 <li>Click on View Details and Downloads for your recent order.</li>
141 <li>Download previous versions for each add-on ( less than 3.0.0 ).</li>
142 <li>Go to the Plugins page in your WordPress dashboard.</li>
143 <li>Deactivate and delete each SupportCandy add-on.</li>
144 <li>Click Add New and then install each add-on by uploading it.</li>
145 <li>Activate each add-on.</li>
146 </ol>
147 </p>
148 <?php
149 }
150 ?>
151 </div>
152 </div>
153 <script>
154 <?php
155 if ( $upgrade_permission ) {
156 ?>
157 jQuery(document).ready(function(){
158 wpsc_upgrade_v1_load_compatibilty();
159 });
160 <?php
161 } else {
162 ?>
163 jQuery('button.proceed').click(function(){
164 jQuery('div.wpsc-upgrade-container').html(supportcandy.loader_html);
165 var data = {
166 action: 'wpsc_authorize_v1_upgrade',
167 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_authorize_v1_upgrade' ) ); ?>'
168 };
169 jQuery.post(supportcandy.ajax_url, data, function (response) {
170 if ( response.success ) {
171 wpsc_upgrade_v1_load_compatibilty();
172 }
173 });
174 });
175 <?php
176 }
177 ?>
178 /**
179 * Load compatibility section
180 *
181 * @return void
182 */
183 function wpsc_upgrade_v1_load_compatibilty() {
184 jQuery('div.wpsc-upgrade-container').html(supportcandy.loader_html);
185 var data = {
186 action: 'wpsc_upgrade_v1_check_compatibility',
187 _ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_upgrade_v1_check_compatibility' ) ); ?>'
188 };
189 jQuery.post(supportcandy.ajax_url, data, function (response) {
190 jQuery('div.wpsc-upgrade-container').html(response);
191 });
192 }
193 </script>
194 <style>
195 body {
196 background-color: #fff;
197 }
198 p, ol {
199 font-size: 15px;
200 }
201 </style>
202 <?php
203 }
204
205 /**
206 * Check v3 compatiblity of current installation
207 *
208 * @return void
209 */
210 public static function check_compatibility() {
211
212 global $wpdb, $current_user;
213
214 $upgrade_permission = get_option( 'wpsc_upgrade_permission_v1', 0 );
215
216 if ( ! wpsc_upgrade_is_site_admin() || ! $upgrade_permission ) {
217 wp_send_json_error( 'Unauthorised request!', 401 );
218 }
219
220 if ( check_ajax_referer( 'wpsc_upgrade_v1_check_compatibility', '_ajax_nonce', false ) != 1 ) {
221 wp_send_json_error( 'Unauthorised request!', 401 );
222 }
223
224 $errors = array();
225
226 // check php version.
227 if ( version_compare( PHP_VERSION, '7.4.0', '<' ) ) {
228 $errors[] = 'PHP version must be 7.4 and above, current version is ' . PHP_VERSION;
229 }
230
231 // check WordPress version.
232 $wp_version = get_bloginfo( 'version' );
233 if ( $wp_version < '5.6' ) {
234 $errors[] = 'WordPress version must be 5.2 and above, current version is ' . $wp_version;
235 }
236
237 // check add-on versions.
238 $installed_addons = wpsc_upgrade_get_installed_plugin_info();
239 update_option( 'wpsc_upgrade_installed_addons', $installed_addons );
240
241 $plugins_url = admin_url( 'plugins.php' );
242 foreach ( $installed_addons as $addon ) {
243 if ( $addon['is_installed'] && $addon['version'] < '3.0.0' ) {
244
245 $errors[] = '<strong>' . $addon['name'] . '</strong> must be v3.0 and above. <a href="' . $plugins_url . '">Click here</a> to see updates.';
246
247 } elseif ( $addon['is_installed'] && $addon['version'] == '3.0.0' && ! $addon['is_active'] ) {
248
249 $errors[] = '<strong>' . $addon['name'] . '</strong> must be active in order to upgrade data related to it.';
250 }
251 }
252
253 // check for customizations.
254 if ( class_exists( 'WPSC_CUST' ) ) {
255 $errors[] = 'Customization detected! Please contact us on <a href="mailto:[email protected]">[email protected]</a> from your registered email address in order to plan re-development of your customization. Until then, you need to use previous version of SupportCandy.';
256 }
257
258 if ( $errors ) {
259
260 delete_option( 'wpsc_upgrade_permission_v1' );
261
262 ?>
263 <p>Please fix below problems:</p>
264 <ol>
265 <?php
266 foreach ( $errors as $error ) {
267 echo '<li>' . wp_kses_post( $error ) . '</li>';
268 }
269 ?>
270 </ol>
271 <?php
272
273 } else {
274
275 $cron_status = get_option( 'wpsc_v1_upgrade_cron_status' );
276 $percentage = 100;
277 if ( $cron_status['total_pages'] ) {
278 $percentage = round( ( $cron_status['page'] / $cron_status['total_pages'] ) * 100, 2 );
279 }
280 ?>
281 <p>This process can take several hours to complete depending your database size.</p>
282 <p>Please keep this tab open for faster upgrade. If you close this tab, you need server scheduled cron job for the site.</p>
283 <i class="wpsc-progressbar-title"><?php echo esc_html( $cron_status['title'] ); ?></i>
284 <div id="wpsc-progressbar"><div class="progress-label"><?php echo esc_html( $percentage ) . '%'; ?></div></div>
285
286 <!-- Server cron setup instructions -->
287 <h3 style="margin-top: 50px;">Why server scheduled cron job?</h3>
288 <p>WordPress has the default WP-Cron enabled for you, which only gets executed when the website has a visitor. Therefore, it is not suitable for time-critical events like sending an email, pulling emails for email piping, scheduled backups, scheduled upgrade (like this) etc. Also, visitors may experience slowness in page loading because WP-Cron has to finish its job before it renders output for the visitor.</p>
289 <h3 style="margin-top: 50px;">How to setup server scheduled cron job?</h3>
290 <h4>Step 1: Disable WP-Cron</h4>
291 <p>You can disable WP-Cron by adding the below line to your wp-config.php file.</p>
292 <code>define('DISABLE_WP_CRON', true);</code>
293 <h4>Step 2: Schedule cron on your server</h4>
294 <p>To schedule cron on your server, you must log in to cPanel (or your hosting interface). Then, add a new cron job with the below command, which will execute cron every minute.</p>
295 <code>*/1 * * * * wget -qO- <?php echo esc_url( site_url( '/wp-cron.php' ) ); ?> &> /dev/null</code>
296 <p>The reasonable time interval is 5-15 minutes. That is */5 * * * * or */15 * * * * for Cron interval setting. The longer the cron execution time, the longer it will take to complete this upgrade. You can change the interval from every minute to 5 or 15 minutes later.</p>
297
298
299 <style>
300 .ui-progressbar {
301 position: relative;
302 }
303 .progress-label {
304 position: absolute;
305 left: 50%;
306 top: 4px;
307 font-weight: bold;
308 text-shadow: 1px 1px 0 #fff;
309 }
310 </style>
311 <script>
312 var progressbar = jQuery( "#wpsc-progressbar" );
313 var progressLabel = jQuery( ".progress-label" );
314 var progressbarTitle = jQuery( ".wpsc-progressbar-title" );
315 progressbar.progressbar({ value: <?php echo intval( $percentage ); ?> });
316 wpsc_v1_upgrade_status();
317
318 /**
319 * Get cron status
320 *
321 * @return void
322 */
323 function wpsc_v1_upgrade_status() {
324 var data = { action: 'wpsc_v1_upgrade_status' };
325 jQuery.post(supportcandy.ajax_url, data, function (response) {
326 if ( response.success && response.status == 'running' ) {
327 let percentage = 100;
328 if ( response.total_pages ) {
329 percentage = ( ( response.page/response.total_pages ) * 100 ).toFixed(2);
330 }
331 progressbar.progressbar({ value: parseInt( percentage ) });
332 progressLabel.text( percentage + '%' );
333 progressbarTitle.text( response.title );
334 wpsc_v1_upgrade_status();
335 } else if ( response.success && response.status == 'complete' ) {
336 window.location.reload();
337 }
338 }).fail( function( xhr, status, error ) {
339 window.location.reload();
340 });
341 }
342 </script>
343 <?php
344 }
345
346 wp_die();
347 }
348
349 /**
350 * Cron manager for upgrade
351 *
352 * @return void
353 */
354 public static function cron_manager() {
355
356 $cron_status = get_option( 'wpsc_v1_upgrade_cron_status', array() );
357 if ( ! $cron_status ) {
358 $cron_status = array(
359 'status' => 'running',
360 'title' => 'Installing database tables',
361 'resource' => 'db_tables',
362 'page' => 0,
363 'total_pages' => 1,
364 );
365 update_option( 'wpsc_v1_upgrade_cron_status', $cron_status );
366 }
367
368 // Do not proceed for the first iteration via cron.
369 if ( $cron_status['resource'] == 'db_tables' ) {
370 return;
371 }
372
373 // Do not allow parallel process to run.
374 if ( 'yes' === get_transient( 'wpsc_ui_upgrading' ) ) {
375 return;
376 }
377
378 $upgrade_permission = get_option( 'wpsc_upgrade_permission_v1', 0 );
379 if ( $upgrade_permission && $cron_status['status'] == 'running' && ! wp_next_scheduled( 'wpsc_upgrade_v1' ) ) {
380 wp_schedule_single_event( time(), 'wpsc_upgrade_v1' );
381 }
382 }
383
384 /**
385 * Return cron status for v1 upgrade
386 *
387 * @return void
388 */
389 public static function get_v1_upgrade_cron_status() {
390
391 if ( ! wpsc_upgrade_is_site_admin() ) {
392 wp_send_json_error( 'Unauthorised request!', 401 );
393 }
394
395 // Set transient.
396 set_transient( 'wpsc_ui_upgrading', 'yes', MINUTE_IN_SECONDS * 2 );
397
398 do_action( 'wpsc_upgrade_v1' );
399
400 $cron_status = get_option( 'wpsc_v1_upgrade_cron_status' );
401 $cron_status['success'] = true;
402 wp_send_json( $cron_status );
403 }
404
405 /**
406 * Upgrade the resource
407 *
408 * @return void
409 */
410 public static function upgrade() {
411
412 global $wpdb;
413
414 // Do not allow parallel process to run.
415 if ( 'yes' === get_transient( 'wpsc_upgrading' ) ) {
416 return;
417 }
418
419 // Set transient.
420 set_transient( 'wpsc_upgrading', 'yes', MINUTE_IN_SECONDS * 2 );
421
422 $cron_status = get_option( 'wpsc_v1_upgrade_cron_status' );
423 switch ( $cron_status['resource'] ) {
424
425 case 'db_tables':
426 if ( $cron_status['page'] == $cron_status['total_pages'] ) {
427 $total_fields = count(
428 get_terms(
429 array(
430 'taxonomy' => 'wpsc_ticket_custom_fields',
431 'hide_empty' => false,
432 )
433 )
434 );
435
436 $total_pages = $total_fields ? ceil( $total_fields / 10 ) : 0;
437 $cron_status = array(
438 'status' => 'running',
439 'title' => 'Importing custom fields',
440 'resource' => 'custom_fields',
441 'page' => 0,
442 'total_pages' => $total_pages,
443 );
444 }
445 break;
446
447 case 'custom_fields':
448 if ( $cron_status['page'] == $cron_status['total_pages'] ) {
449 $cron_status = array(
450 'status' => 'running',
451 'title' => 'Importing settings',
452 'resource' => 'settings',
453 'page' => 0,
454 'total_pages' => 6,
455 );
456 }
457 break;
458
459 case 'settings':
460 if ( $cron_status['page'] == $cron_status['total_pages'] ) {
461 $results = new WP_Query(
462 array(
463 'post_type' => 'wpsc_ticket',
464 'post_status' => array( 'publish', 'trash' ),
465 )
466 );
467 $total_pages = $results->found_posts ? ceil( $results->found_posts / 5 ) : 0;
468 $cron_status = array(
469 'status' => 'running',
470 'title' => 'Importing tickets',
471 'resource' => 'tickets',
472 'page' => 0,
473 'total_pages' => $total_pages,
474 );
475 }
476 break;
477
478 case 'tickets':
479 if ( $cron_status['page'] == $cron_status['total_pages'] ) {
480 $total_customers = $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $wpdb->prefix . 'psmsc_customers' );
481 $total_pages = $total_customers ? ceil( $total_customers / 40 ) : 0;
482 $cron_status = array(
483 'status' => 'running',
484 'title' => 'Importing customers',
485 'resource' => 'customers',
486 'page' => 0,
487 'total_pages' => $total_pages,
488 );
489 }
490 break;
491
492 case 'customers':
493 if ( $cron_status['page'] == $cron_status['total_pages'] ) {
494 $total_agents = $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $wpdb->prefix . 'psmsc_agents' );
495 $total_pages = $total_agents ? ceil( $total_agents / 20 ) : 0;
496 $cron_status = array(
497 'status' => 'running',
498 'title' => 'Importing agents',
499 'resource' => 'agents',
500 'page' => 0,
501 'total_pages' => $total_pages,
502 );
503 }
504 break;
505
506 case 'agents':
507 if ( $cron_status['page'] == $cron_status['total_pages'] ) {
508 WPSC_Installation::set_upgrade_complete();
509 update_option(
510 'wpsc_upgrade_cleanup',
511 array(
512 'version' => 1,
513 'status' => 'texonomy',
514 )
515 );
516 $cron_status = array(
517 'status' => 'complete',
518 );
519 update_option( 'wpsc_v1_upgrade_cron_status', $cron_status );
520 }
521 break;
522 }
523
524 if ( $cron_status['status'] == 'complete' ) {
525 return;
526 }
527
528 if ( $cron_status['total_pages'] == 0 ) {
529 update_option( 'wpsc_v1_upgrade_cron_status', $cron_status );
530 delete_transient( 'wpsc_upgrading' );
531 return;
532 }
533
534 // execute the cron.
535 $page = intval( $cron_status['page'] ) + 1;
536
537 switch ( $cron_status['resource'] ) {
538
539 case 'db_tables':
540 WPSC_Installation::create_db_tables();
541 WPSC_Installation::initial_setup();
542 do_action( 'wpsc_upgrade_install_addons' );
543 break;
544
545 case 'custom_fields':
546 self::import_custom_fields( $page );
547 break;
548
549 case 'settings':
550 self::upgrade_settings( $page );
551 break;
552
553 case 'tickets':
554 self::upgrade_tickets( $page );
555 break;
556
557 case 'customers':
558 self::customer_ticket_count( $page );
559 break;
560
561 case 'agents':
562 self::agent_ticket_count( $page );
563 break;
564 }
565
566 $cron_status['page'] = $page;
567 update_option( 'wpsc_v1_upgrade_cron_status', $cron_status );
568
569 // Delete transient.
570 delete_transient( 'wpsc_upgrading' );
571 }
572
573 /**
574 * Upgrade settings
575 *
576 * @param int $page - page number.
577 * @return void
578 */
579 public static function upgrade_settings( $page ) {
580
581 switch ( $page ) {
582
583 case 1:
584 self::import_statuses();
585 self::import_categories();
586 self::import_priorities();
587 self::import_agents();
588 break;
589
590 case 2:
591 self::import_ticket_form_fields();
592 self::import_ticket_list_items();
593 self::import_tl_more_settings();
594 self::import_en_general();
595 self::import_en_ticket_notifications();
596 break;
597
598 case 3:
599 self::import_general_settings();
600 self::reorder_widgets();
601 self::import_thankyou_settings();
602 self::import_terms_and_conditions();
603 self::import_advanced_settings();
604 break;
605
606 case 4:
607 self::import_captcha();
608 self::import_assign_agent_rules();
609 self::import_automatic_close_tickets();
610 self::import_canned_reply();
611 self::import_report_settings();
612 break;
613
614 case 5:
615 self::import_satisfaction_survey();
616 self::import_email_piping();
617 self::import_faq_integration();
618 self::import_knowledgebase_integration();
619 self::import_export_addon();
620 break;
621
622 case 6:
623 self::import_woocommerce_addon();
624 self::import_shedule_tickets();
625 self::import_sla_settings();
626 self::import_usergroup_settings();
627 break;
628 }
629 }
630
631 /**
632 * Upgrade tickets
633 *
634 * @param int $page - page number.
635 * @return void
636 */
637 public static function upgrade_tickets( $page ) {
638
639 $posts_per_page = 5;
640 $results = get_posts(
641 array(
642 'post_type' => 'wpsc_ticket',
643 'post_status' => array( 'publish', 'trash' ),
644 'posts_per_page' => $posts_per_page,
645 'offset' => ( $page - 1 ) * $posts_per_page,
646 'orderby' => 'ID',
647 'order' => 'ASC',
648 )
649 );
650
651 foreach ( $results as $ticket ) {
652 self::import_individual_ticket( $ticket );
653 }
654 }
655
656 /**
657 * Customer ticket count
658 *
659 * @param int $page - page number.
660 * @return void
661 */
662 public static function customer_ticket_count( $page ) {
663
664 global $wpdb;
665 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
666 $offset = ( $page - 1 ) * 40;
667 $sql = "SELECT * FROM {$wpdb->prefix}psmsc_customers ORDER BY id ASC LIMIT 40 OFFSET " . $offset;
668 $customers = $wpdb->get_results( $sql );
669 foreach ( $customers as $customer ) {
670 wpsc_update_customer_ticket_count( $customer );
671 if ( $installed_addons[7]['is_installed'] ) {
672 wpsc_update_customer_usergroups( $customer );
673 }
674 self::import_saved_filters( $customer );
675 }
676 }
677
678 /**
679 * Agent ticket count
680 *
681 * @param int $page - page number.
682 * @return void
683 */
684 public static function agent_ticket_count( $page ) {
685
686 global $wpdb;
687 $offset = ( $page - 1 ) * 20;
688 $sql = "SELECT * FROM {$wpdb->prefix}psmsc_agents WHERE is_agentgroup = 0 ORDER BY id ASC LIMIT 20 OFFSET " . $offset;
689 $agents = $wpdb->get_results( $sql );
690 foreach ( $agents as $agent ) {
691 wpsc_agent_reset_unresolved_count( $agent );
692 wpsc_agent_reset_workload( $agent );
693 self::import_saved_filters( wpsc_get_customer_by( 'id', $agent->customer ) );
694 self::import_agent_settings( $agent );
695 }
696 }
697
698 /**
699 * Import saved ticket filters
700 *
701 * @param stdClass $customer - customer object.
702 * @return void
703 */
704 public static function import_saved_filters( $customer ) {
705
706 global $wpdb;
707
708 // return if customer is guest.
709 if ( ! $customer->user ) {
710 return;
711 }
712
713 $cf_slug_map = get_option( 'wpsc_upgrade_cf_slug_map' );
714 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
715 $status_map = get_option( 'wpsc_upgrade_status_map' );
716 $category_map = get_option( 'wpsc_upgrade_category_map' );
717 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
718 $rating_map = get_option( 'wpsc_upgrade_sf_rating_map' );
719 $agent_map = get_option( 'wpsc_upgrade_agent_map' );
720 $old_date_format = get_option( 'wpsc_calender_date_format' );
721 $parent_filter_map = array(
722 'all' => 'all',
723 'unresolved_agent' => 'unresolved',
724 'unresolved_customer' => 'unresolved',
725 'unassigned' => 'unassigned',
726 'mine' => 'mine',
727 'closed' => 'closed',
728 'deleted' => 'deleted',
729 );
730
731 $date_formats = array(
732 'dd-mm-yy' => 'd-m-Y',
733 'dd-yy-mm' => 'd-Y-m',
734 'mm-dd-yy' => 'm-d-Y',
735 'mm-yy-dd' => 'm-Y-d',
736 'yy-mm-dd' => 'Y-m-d',
737 'yy-dd-mm' => 'Y-d-m',
738 );
739
740 // previous filters.
741 $prev_filters = get_user_meta( $customer->user, get_current_blog_id() . '_wpsc_filter', true );
742 if ( ! $prev_filters ) {
743 return;
744 }
745
746 // new filters.
747 $index = 1;
748 $new_filters = array();
749
750 // import filters.
751 foreach ( $prev_filters as $prev ) {
752
753 $new = array(
754 'label' => $prev['save_label'],
755 'parent-filter' => $parent_filter_map[ $prev['label'] ],
756 'sort-by' => $cf_slug_map[ $prev['orderby'] ],
757 'sort-order' => $prev['order'],
758 );
759
760 $filters = array();
761 foreach ( $prev['custom_filter'] as $slug => $filter ) {
762
763 if ( $slug == 's' ) {
764 continue;
765 }
766
767 if ( $slug == 'id' ) {
768 $slug = 'ticket_id';
769 }
770
771 $cf = wpsc_get_cf_by( 'slug', $cf_slug_map[ $slug ] );
772 if ( ! $cf ) {
773 continue;
774 }
775
776 switch ( $cf->type ) {
777
778 case 'df_category':
779 $filters[ $cf->slug ] = array(
780 'operator' => 'IN',
781 'operand_val_1' => array_map(
782 function ( $prev_category ) use ( $category_map ) {
783 return $category_map[ $prev_category ];
784 },
785 $filter
786 ),
787 );
788 break;
789
790 case 'df_status':
791 $filters[ $cf->slug ] = array(
792 'operator' => 'IN',
793 'operand_val_1' => array_map(
794 function ( $prev_status ) use ( $status_map ) {
795 return $status_map[ $prev_status ];
796 },
797 $filter
798 ),
799 );
800 break;
801
802 case 'df_priority':
803 $filters[ $cf->slug ] = array(
804 'operator' => 'IN',
805 'operand_val_1' => array_map(
806 function ( $prev_priority ) use ( $priority_map ) {
807 return $priority_map[ $prev_priority ];
808 },
809 $filter
810 ),
811 );
812 break;
813
814 case 'df_customer_name':
815 case 'df_customer_email':
816 $slug = $cf->type == 'df_customer_name' ? 'name' : 'email';
817 $sql = "SELECT * FROM {$wpdb->prefix}psmsc_customers WHERE " . $slug . " IN( '" . implode( "', '", $filter ) . "' )";
818 $customers = $wpdb->get_results( $sql );
819 if ( ! $customers ) {
820 break;
821 }
822 if ( isset( $filters['customer'] ) ) {
823
824 $filters['customer']['operand_val_1'] = array_unique(
825 array_merge(
826 $filters['customer']['operand_val_1'],
827 array_map(
828 function ( $cust ) {
829 return $cust->id;
830 },
831 $customers
832 )
833 )
834 );
835
836 } else {
837
838 $filters['customer'] = array(
839 'operator' => 'IN',
840 'operand_val_1' => array_map(
841 function ( $cust ) {
842 return $cust->id;
843 },
844 $customers
845 ),
846 );
847 }
848 break;
849
850 case 'df_assigned_agent':
851 case 'df_agent_created':
852 $filters[ $cf->slug ] = array(
853 'operator' => 'IN',
854 'operand_val_1' => array_map(
855 function ( $prev_agent ) use ( $agent_map ) {
856 return $agent_map[ $prev_agent ];
857 },
858 $filter
859 ),
860 );
861 break;
862
863 case 'df_date_created':
864 case 'cf_date':
865 if ( ! ( $filter['from'] && $filter['to'] ) ) {
866 break;
867 }
868 $from = DateTime::createFromFormat( $date_formats[ $old_date_format ], $filter['from'] );
869 $to = DateTime::createFromFormat( $date_formats[ $old_date_format ], $filter['to'] );
870 if ( ! ( $from && $to ) ) {
871 break;
872 }
873 $filters[ $cf->slug ] = array(
874 'operator' => 'BETWEEN',
875 'operand_val_1' => $from->format( 'Y-m-d' ),
876 'operand_val_2' => $to->format( 'Y-m-d' ),
877 );
878 break;
879
880 case 'df_sf_rating':
881 $filters[ $cf->slug ] = array(
882 'operator' => 'IN',
883 'operand_val_1' => array_map(
884 function ( $prev_rating ) use ( $rating_map ) {
885 return $rating_map[ $prev_rating ];
886 },
887 $filter
888 ),
889 );
890 break;
891
892 case 'df_id':
893 case 'cf_textfield':
894 case 'cf_email':
895 case 'cf_number':
896 case 'cf_url':
897 $filters[ $cf->slug ] = array(
898 'operator' => 'IN',
899 'operand_val_1' => implode( '^^', $filter ),
900 );
901 break;
902
903 case 'cf_checkbox':
904 case 'cf_multi_select':
905 case 'cf_radio_button':
906 case 'cf_single_select':
907 $filters[ $cf->slug ] = array(
908 'operator' => 'IN',
909 'operand_val_1' => array_map(
910 function ( $prev_option ) use ( $options_map, $cf ) {
911 return $options_map[ $cf->id ][ $prev_option ];
912 },
913 $filter
914 ),
915 );
916 break;
917
918 case 'cf_woo_product':
919 case 'cf_woo_order':
920 $filters[ $cf->slug ] = array(
921 'operator' => 'IN',
922 'operand_val_1' => $filter,
923 );
924 break;
925 }
926 }
927
928 $new['filters'] = wp_json_encode( $filters );
929
930 $new_filters[ $index++ ] = $new;
931 }
932
933 // import Visibility conditions.
934 foreach ( $new_filters as $slug => $properties ) {
935 if ( ! $properties['filters'] ) {
936 continue;
937 }
938 $new_filters[ $slug ]['filters'] = WPSC_SC_Upgrade::upgrade_condition( $properties['filters'], 'AND' );
939 }
940 update_user_meta( $customer->user, get_current_blog_id() . '-wpsc-tl-saved-filters', $new_filters );
941 update_user_meta( $customer->user, get_current_blog_id() . '-wpsc-tl-cf-auto-increament', ++$index );
942 }
943
944 /**
945 * Import agent settings
946 *
947 * @param stdClass $agent - agent object.
948 * @return void
949 */
950 public static function import_agent_settings( $agent ) {
951
952 $signature = get_user_meta( $agent->user, 'wpsc_agent_signature', true );
953 if ( ! $signature ) {
954 update_user_meta( $agent->user, get_current_blog_id() . '_wpsc_email_signature', $signature );
955 }
956 }
957
958 /**
959 * Import statuses
960 *
961 * @return void
962 */
963 public static function import_statuses() {
964
965 global $wpdb;
966 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_statuses' );
967 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_statuses AUTO_INCREMENT = 1' );
968
969 $statuses = get_terms(
970 array(
971 'taxonomy' => 'wpsc_statuses',
972 'hide_empty' => false,
973 'orderby' => 'meta_value_num',
974 'order' => 'ASC',
975 'meta_query' => array( 'order_clause' => array( 'key' => 'wpsc_status_load_order' ) ),
976 )
977 );
978
979 $string_translations = get_option( 'wpsc-string-translation' );
980 $map = array();
981 $load_order = 1;
982 foreach ( $statuses as $prev ) {
983
984 $wpdb->insert(
985 $wpdb->prefix . 'psmsc_statuses',
986 array(
987 'name' => $prev->name,
988 'color' => get_term_meta( $prev->term_id, 'wpsc_status_color', true ),
989 'bg_color' => get_term_meta( $prev->term_id, 'wpsc_status_background_color', true ),
990 'load_order' => $load_order++,
991 )
992 );
993
994 $string_translations[ 'wpsc-status-' . $wpdb->insert_id ] = $prev->name;
995 $map[ $prev->term_id ] = $wpdb->insert_id;
996 }
997
998 update_option( 'wpsc-string-translation', $string_translations );
999 update_option( 'wpsc_upgrade_status_map', $map );
1000 }
1001
1002 /**
1003 * Import categories
1004 *
1005 * @return void
1006 */
1007 public static function import_categories() {
1008
1009 global $wpdb;
1010 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_categories' );
1011 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_categories AUTO_INCREMENT = 1' );
1012
1013 $categories = get_terms(
1014 array(
1015 'taxonomy' => 'wpsc_categories',
1016 'hide_empty' => false,
1017 'orderby' => 'meta_value_num',
1018 'order' => 'ASC',
1019 'meta_query' => array( 'order_clause' => array( 'key' => 'wpsc_category_load_order' ) ),
1020 )
1021 );
1022
1023 $string_translations = get_option( 'wpsc-string-translation' );
1024 $map = array();
1025 $load_order = 1;
1026 foreach ( $categories as $prev ) {
1027
1028 $wpdb->insert(
1029 $wpdb->prefix . 'psmsc_categories',
1030 array(
1031 'name' => $prev->name,
1032 'load_order' => $load_order++,
1033 )
1034 );
1035
1036 $string_translations[ 'wpsc-category-' . $wpdb->insert_id ] = $prev->name;
1037 $map[ $prev->term_id ] = $wpdb->insert_id;
1038 }
1039
1040 update_option( 'wpsc-string-translation', $string_translations );
1041 update_option( 'wpsc_upgrade_category_map', $map );
1042 }
1043
1044 /**
1045 * Import priorities
1046 *
1047 * @return void
1048 */
1049 public static function import_priorities() {
1050
1051 global $wpdb;
1052 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_priorities' );
1053 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_priorities AUTO_INCREMENT = 1' );
1054
1055 $priorities = get_terms(
1056 array(
1057 'taxonomy' => 'wpsc_priorities',
1058 'hide_empty' => false,
1059 'orderby' => 'meta_value_num',
1060 'order' => 'ASC',
1061 'meta_query' => array( 'order_clause' => array( 'key' => 'wpsc_priority_load_order' ) ),
1062 )
1063 );
1064
1065 $string_translations = get_option( 'wpsc-string-translation' );
1066 $map = array();
1067 $load_order = 1;
1068 foreach ( $priorities as $prev ) {
1069
1070 $wpdb->insert(
1071 $wpdb->prefix . 'psmsc_priorities',
1072 array(
1073 'name' => $prev->name,
1074 'color' => get_term_meta( $prev->term_id, 'wpsc_priority_color', true ),
1075 'bg_color' => get_term_meta( $prev->term_id, 'wpsc_priority_background_color', true ),
1076 'load_order' => $load_order++,
1077 )
1078 );
1079
1080 $string_translations[ 'wpsc-priority-' . $wpdb->insert_id ] = $prev->name;
1081 $map[ $prev->term_id ] = $wpdb->insert_id;
1082 }
1083
1084 update_option( 'wpsc-string-translation', $string_translations );
1085 update_option( 'wpsc_upgrade_priority_map', $map );
1086 }
1087
1088 /**
1089 * Import custom fields
1090 *
1091 * @param int $page - current page number.
1092 * @return void
1093 */
1094 public static function import_custom_fields( $page ) {
1095
1096 global $wpdb;
1097
1098 // Disable InnoDB strict mode if enabled to avoid limit of number of columns in tables.
1099 $strict_mode = $wpdb->get_var( "SHOW VARIABLES LIKE 'innodb_strict_mode'", 1 );
1100 if ( $strict_mode == 'ON' ) {
1101 $wpdb->query( "SET innodb_strict_mode = 'OFF'" );
1102 }
1103
1104 // Change load order of all existing custom fields to higher number.
1105 if ( $page == 1 ) {
1106 $wpdb->query( 'UPDATE ' . $wpdb->prefix . 'psmsc_custom_fields SET load_order=1000 WHERE id NOT IN(1,2,7)' );
1107 }
1108
1109 $offset = ( $page - 1 ) * 10;
1110 $args = array(
1111 'taxonomy' => 'wpsc_ticket_custom_fields',
1112 'hide_empty' => false,
1113 'number' => 10,
1114 'offset' => $offset,
1115 'orderby' => 'term_id',
1116 'order' => 'ASC',
1117 );
1118
1119 $fields = get_terms( $args );
1120 $string_translations = get_option( 'wpsc-string-translation' );
1121 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
1122 $term_id_map = get_option( 'wpsc_upgrade_cf_term_id_map', array() );
1123 $slug_map = get_option( 'wpsc_upgrade_cf_slug_map', array() );
1124 $load_order = get_option( 'wpsc_upgrade_cf_load_order', 8 );
1125 foreach ( $fields as $prev ) {
1126
1127 $label = get_term_meta( $prev->term_id, 'wpsc_tf_label', true );
1128 $extra_info = get_term_meta( $prev->term_id, 'wpsc_tf_extra_info', true );
1129 $type = get_term_meta( $prev->term_id, 'wpsc_tf_type', true );
1130
1131 if ( intval( $type ) === 0 ) {
1132
1133 switch ( $prev->slug ) {
1134
1135 case 'ticket_id':
1136 $term_id_map[ $prev->term_id ] = 1;
1137 $slug_map[ $prev->slug ] = 'id';
1138 break;
1139
1140 case 'ticket_status':
1141 $term_id_map[ $prev->term_id ] = 7;
1142 $slug_map[ $prev->slug ] = 'status';
1143 break;
1144
1145 case 'customer_name':
1146 $wpdb->update(
1147 $wpdb->prefix . 'psmsc_custom_fields',
1148 array(
1149 'name' => $label,
1150 'extra_info' => $extra_info,
1151 'load_order' => $load_order++,
1152 ),
1153 array( 'id' => 3 )
1154 );
1155 $term_id_map[ $prev->term_id ] = 3;
1156 $slug_map[ $prev->slug ] = 'name';
1157 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1158 break;
1159
1160 case 'customer_email':
1161 $wpdb->update(
1162 $wpdb->prefix . 'psmsc_custom_fields',
1163 array(
1164 'name' => $label,
1165 'extra_info' => $extra_info,
1166 'load_order' => $load_order++,
1167 ),
1168 array( 'id' => 4 )
1169 );
1170 $term_id_map[ $prev->term_id ] = 4;
1171 $slug_map[ $prev->slug ] = 'email';
1172 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1173 break;
1174
1175 case 'ticket_subject':
1176 $wpdb->update(
1177 $wpdb->prefix . 'psmsc_custom_fields',
1178 array(
1179 'name' => $label,
1180 'extra_info' => $extra_info,
1181 'load_order' => $load_order++,
1182 ),
1183 array( 'id' => 5 )
1184 );
1185 $term_id_map[ $prev->term_id ] = 5;
1186 $slug_map[ $prev->slug ] = 'subject';
1187 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1188 break;
1189
1190 case 'ticket_description':
1191 $wpdb->update(
1192 $wpdb->prefix . 'psmsc_custom_fields',
1193 array(
1194 'name' => $label,
1195 'extra_info' => $extra_info,
1196 'load_order' => $load_order++,
1197 ),
1198 array( 'id' => 6 )
1199 );
1200 $term_id_map[ $prev->term_id ] = 6;
1201 $slug_map[ $prev->slug ] = 'description';
1202 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1203 break;
1204
1205 case 'ticket_category':
1206 $wpdb->update(
1207 $wpdb->prefix . 'psmsc_custom_fields',
1208 array(
1209 'name' => $label,
1210 'extra_info' => $extra_info,
1211 'load_order' => $load_order++,
1212 ),
1213 array( 'id' => 9 )
1214 );
1215 $term_id_map[ $prev->term_id ] = 9;
1216 $slug_map[ $prev->slug ] = 'category';
1217 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1218 break;
1219
1220 case 'ticket_priority':
1221 $wpdb->update(
1222 $wpdb->prefix . 'psmsc_custom_fields',
1223 array(
1224 'name' => $label,
1225 'extra_info' => $extra_info,
1226 'load_order' => $load_order++,
1227 ),
1228 array( 'id' => 8 )
1229 );
1230 $term_id_map[ $prev->term_id ] = 8;
1231 $slug_map[ $prev->slug ] = 'priority';
1232 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1233 break;
1234
1235 case 'assigned_agent':
1236 $wpdb->update(
1237 $wpdb->prefix . 'psmsc_custom_fields',
1238 array(
1239 'name' => $label,
1240 'load_order' => $load_order++,
1241 ),
1242 array( 'id' => 10 )
1243 );
1244 $term_id_map[ $prev->term_id ] = 10;
1245 $slug_map[ $prev->slug ] = 'assigned_agent';
1246 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1247 break;
1248
1249 case 'date_created':
1250 $wpdb->update(
1251 $wpdb->prefix . 'psmsc_custom_fields',
1252 array(
1253 'name' => $label,
1254 'load_order' => $load_order++,
1255 ),
1256 array( 'id' => 11 )
1257 );
1258 $term_id_map[ $prev->term_id ] = 11;
1259 $slug_map[ $prev->slug ] = 'date_created';
1260 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1261 break;
1262
1263 case 'date_updated':
1264 $wpdb->update(
1265 $wpdb->prefix . 'psmsc_custom_fields',
1266 array(
1267 'name' => $label,
1268 'load_order' => $load_order++,
1269 ),
1270 array( 'id' => 12 )
1271 );
1272 $term_id_map[ $prev->term_id ] = 12;
1273 $slug_map[ $prev->slug ] = 'date_updated';
1274 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1275 break;
1276
1277 case 'agent_created':
1278 $wpdb->update(
1279 $wpdb->prefix . 'psmsc_custom_fields',
1280 array(
1281 'name' => $label,
1282 'load_order' => $load_order++,
1283 ),
1284 array( 'id' => 13 )
1285 );
1286 $term_id_map[ $prev->term_id ] = 13;
1287 $slug_map[ $prev->slug ] = 'agent_created';
1288 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1289 break;
1290
1291 case 'sf_rating':
1292 $id = $wpdb->get_var( "SELECT id from {$wpdb->prefix}psmsc_custom_fields WHERE slug = 'rating'" );
1293 if ( $id ) {
1294 $wpdb->update(
1295 $wpdb->prefix . 'psmsc_custom_fields',
1296 array(
1297 'name' => $label,
1298 'load_order' => $load_order++,
1299 ),
1300 array( 'id' => $id )
1301 );
1302 $term_id_map[ $prev->term_id ] = $id;
1303 $slug_map[ $prev->slug ] = 'rating';
1304 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1305 }
1306 break;
1307
1308 case 'sla':
1309 $id = $wpdb->get_var( "SELECT id from {$wpdb->prefix}psmsc_custom_fields WHERE slug = 'sla'" );
1310 if ( $id ) {
1311 $wpdb->update(
1312 $wpdb->prefix . 'psmsc_custom_fields',
1313 array(
1314 'name' => $label,
1315 'load_order' => $load_order++,
1316 ),
1317 array( 'id' => $id )
1318 );
1319 $term_id_map[ $prev->term_id ] = $id;
1320 $slug_map[ $prev->slug ] = 'sla';
1321 $string_translations[ 'wpsc-cf-name-' . $wpdb->insert_id ] = $label;
1322 }
1323 break;
1324 }
1325 } else {
1326
1327 $personal_info = get_term_meta( $prev->term_id, 'wpsc_tf_personal_info', true );
1328 $field = get_term_meta( $prev->term_id, 'agentonly', true ) == 1 ? 'agentonly' : 'ticket';
1329
1330 switch ( intval( $type ) ) {
1331
1332 case 1:
1333 $response = wpsc_upgrade_insert_custom_field(
1334 array(
1335 'name' => $label,
1336 'extra_info' => $extra_info,
1337 'field' => $field,
1338 'type' => 'cf_textfield',
1339 'is_personal_info' => $personal_info,
1340 'load_order' => $load_order++,
1341 )
1342 );
1343 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1344 $term_id_map[ $prev->term_id ] = $response['id'];
1345 $slug_map[ $prev->slug ] = $response['slug'];
1346 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1347 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1348 break;
1349
1350 case 2:
1351 $response = wpsc_upgrade_insert_custom_field(
1352 array(
1353 'name' => $label,
1354 'extra_info' => $extra_info,
1355 'field' => $field,
1356 'type' => 'cf_single_select',
1357 'is_personal_info' => $personal_info,
1358 'load_order' => $load_order++,
1359 )
1360 );
1361 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1362 $option_strings = self::import_options( $prev, $response['id'] );
1363 $string_translations = array_merge( $string_translations, $option_strings );
1364 $term_id_map[ $prev->term_id ] = $response['id'];
1365 $slug_map[ $prev->slug ] = $response['slug'];
1366 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1367 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1368 break;
1369
1370 case 3:
1371 $response = wpsc_upgrade_insert_custom_field(
1372 array(
1373 'name' => $label,
1374 'extra_info' => $extra_info,
1375 'field' => $field,
1376 'type' => 'cf_checkbox',
1377 'is_personal_info' => $personal_info,
1378 'load_order' => $load_order++,
1379 )
1380 );
1381 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1382 $option_strings = self::import_options( $prev, $response['id'] );
1383 $string_translations = array_merge( $string_translations, $option_strings );
1384 $term_id_map[ $prev->term_id ] = $response['id'];
1385 $slug_map[ $prev->slug ] = $response['slug'];
1386 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1387 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1388 break;
1389
1390 case 4:
1391 $response = wpsc_upgrade_insert_custom_field(
1392 array(
1393 'name' => $label,
1394 'extra_info' => $extra_info,
1395 'field' => $field,
1396 'type' => 'cf_radio_button',
1397 'is_personal_info' => $personal_info,
1398 'load_order' => $load_order++,
1399 )
1400 );
1401 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1402 $option_strings = self::import_options( $prev, $response['id'] );
1403 $string_translations = array_merge( $string_translations, $option_strings );
1404 $term_id_map[ $prev->term_id ] = $response['id'];
1405 $slug_map[ $prev->slug ] = $response['slug'];
1406 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1407 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1408 break;
1409
1410 case 5:
1411 $response = wpsc_upgrade_insert_custom_field(
1412 array(
1413 'name' => $label,
1414 'extra_info' => $extra_info,
1415 'field' => $field,
1416 'type' => 'cf_textarea',
1417 'is_personal_info' => $personal_info,
1418 'load_order' => $load_order++,
1419 )
1420 );
1421 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' LONGTEXT NULL' );
1422 $term_id_map[ $prev->term_id ] = $response['id'];
1423 $slug_map[ $prev->slug ] = $response['slug'];
1424 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1425 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1426 break;
1427
1428 case 6:
1429 $response = wpsc_upgrade_insert_custom_field(
1430 array(
1431 'name' => $label,
1432 'extra_info' => $extra_info,
1433 'field' => $field,
1434 'type' => 'cf_date',
1435 'is_personal_info' => $personal_info,
1436 'load_order' => $load_order++,
1437 )
1438 );
1439 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' DATETIME NULL DEFAULT NULL' );
1440 $term_id_map[ $prev->term_id ] = $response['id'];
1441 $slug_map[ $prev->slug ] = $response['slug'];
1442 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1443 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1444 break;
1445
1446 case 7:
1447 $response = wpsc_upgrade_insert_custom_field(
1448 array(
1449 'name' => $label,
1450 'extra_info' => $extra_info,
1451 'field' => $field,
1452 'type' => 'cf_url',
1453 'is_personal_info' => $personal_info,
1454 'load_order' => $load_order++,
1455 )
1456 );
1457 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1458 $term_id_map[ $prev->term_id ] = $response['id'];
1459 $slug_map[ $prev->slug ] = $response['slug'];
1460 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1461 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1462 break;
1463
1464 case 8:
1465 $response = wpsc_upgrade_insert_custom_field(
1466 array(
1467 'name' => $label,
1468 'extra_info' => $extra_info,
1469 'field' => $field,
1470 'type' => 'cf_email',
1471 'is_personal_info' => $personal_info,
1472 'load_order' => $load_order++,
1473 )
1474 );
1475 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1476 $term_id_map[ $prev->term_id ] = $response['id'];
1477 $slug_map[ $prev->slug ] = $response['slug'];
1478 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1479 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1480 break;
1481
1482 case 9:
1483 $response = wpsc_upgrade_insert_custom_field(
1484 array(
1485 'name' => $label,
1486 'extra_info' => $extra_info,
1487 'field' => $field,
1488 'type' => 'cf_number',
1489 'is_personal_info' => $personal_info,
1490 'load_order' => $load_order++,
1491 )
1492 );
1493 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1494 $term_id_map[ $prev->term_id ] = $response['id'];
1495 $slug_map[ $prev->slug ] = $response['slug'];
1496 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1497 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1498 break;
1499
1500 case 10:
1501 $response = wpsc_upgrade_insert_custom_field(
1502 array(
1503 'name' => $label,
1504 'extra_info' => $extra_info,
1505 'field' => $field,
1506 'type' => 'cf_file_attachment_multiple',
1507 'is_personal_info' => $personal_info,
1508 'load_order' => $load_order++,
1509 )
1510 );
1511 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' TINYTEXT NULL' );
1512 $term_id_map[ $prev->term_id ] = $response['id'];
1513 $slug_map[ $prev->slug ] = $response['slug'];
1514 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1515 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1516 break;
1517
1518 case 11:
1519 if ( $installed_addons[5]['is_installed'] ) {
1520 $response = wpsc_upgrade_insert_custom_field(
1521 array(
1522 'name' => $label,
1523 'extra_info' => $extra_info,
1524 'field' => $field,
1525 'type' => 'cf_woo_product',
1526 'is_personal_info' => $personal_info,
1527 'load_order' => $load_order++,
1528 )
1529 );
1530 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' BIGINT NULL DEFAULT 0' );
1531 $term_id_map[ $prev->term_id ] = $response['id'];
1532 $slug_map[ $prev->slug ] = $response['slug'];
1533 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1534 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1535 }
1536 break;
1537
1538 case 12:
1539 if ( $installed_addons[5]['is_installed'] ) {
1540 $response = wpsc_upgrade_insert_custom_field(
1541 array(
1542 'name' => $label,
1543 'extra_info' => $extra_info,
1544 'field' => $field,
1545 'type' => 'cf_woo_order',
1546 'is_personal_info' => $personal_info,
1547 'load_order' => $load_order++,
1548 )
1549 );
1550 $wpdb->query( "ALTER TABLE {$wpdb->prefix}psmsc_tickets ADD " . $response['slug'] . ' BIGINT NULL DEFAULT 0' );
1551 $term_id_map[ $prev->term_id ] = $response['id'];
1552 $slug_map[ $prev->slug ] = $response['slug'];
1553 $string_translations[ 'wpsc-cf-name-' . $response['id'] ] = $label;
1554 $string_translations[ 'wpsc-cf-exi-' . $response['id'] ] = $extra_info;
1555 }
1556 break;
1557 }
1558 }
1559 }
1560
1561 update_option( 'wpsc-string-translation', $string_translations );
1562 update_option( 'wpsc_upgrade_cf_term_id_map', $term_id_map );
1563 update_option( 'wpsc_upgrade_cf_slug_map', $slug_map );
1564 update_option( 'wpsc_upgrade_cf_load_order', $load_order );
1565 }
1566
1567 /**
1568 * Import options for custom field
1569 *
1570 * @param mixed $prev - previous custom field object.
1571 * @param int $id - new custom field id.
1572 * @return void
1573 */
1574 public static function import_options( $prev, $id ) {
1575
1576 global $wpdb;
1577 $string_translations = get_option( 'wpsc-string-translation' );
1578 $options = get_term_meta( $prev->term_id, 'wpsc_tf_options', true );
1579 $load_order = 1;
1580 $options_map = get_option( 'wpsc_upgrade_cf_options_map', array() );
1581 foreach ( $options as $name ) {
1582 $wpdb->insert(
1583 $wpdb->prefix . 'psmsc_options',
1584 array(
1585 'name' => $name,
1586 'date_created' => ( new DateTime( 'now' ) )->format( 'Y-m-d H:m:s' ),
1587 'custom_field' => $id,
1588 'load_order' => $load_order++,
1589 )
1590 );
1591 $string_translations[ 'wpsc-option-' . $wpdb->insert_id ] = $name;
1592 $options_map[ $id ][ $name ] = $wpdb->insert_id;
1593 }
1594 update_option( 'wpsc-string-translation', $string_translations );
1595 update_option( 'wpsc_upgrade_cf_options_map', $options_map );
1596 }
1597
1598 /**
1599 * Import agent and agentgroups
1600 *
1601 * @return void
1602 */
1603 public static function import_agents() {
1604
1605 global $wpdb;
1606
1607 // removing agent role cap.
1608 $agents = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}psmsc_agents" );
1609 foreach ( $agents as $agent ) {
1610 $user = get_user_by( 'id', $agent->user );
1611 if ( $user ) {
1612 $user->remove_cap( 'wpsc_agent' );
1613 }
1614 }
1615
1616 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_agents' );
1617 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_agents AUTO_INCREMENT = 1' );
1618 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_customers' );
1619 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_customers AUTO_INCREMENT = 1' );
1620
1621 // import agent roles.
1622 $new = array();
1623 $prev = get_option( 'wpsc_agent_role' );
1624 foreach ( $prev as $key => $role ) {
1625 $new[ $key ] = array(
1626 'label' => $role['label'],
1627 'caps' => array(
1628 'view-unassigned' => $role['view_unassigned'] == 1 ? true : false, // View ticket.
1629 'view-assigned-me' => $role['view_assigned_me'] == 1 ? true : false,
1630 'view-assigned-others' => $role['view_assigned_others'] == 1 ? true : false,
1631 'reply-unassigned' => $role['reply_unassigned'] == 1 ? true : false, // Reply ticket.
1632 'reply-assigned-me' => $role['reply_assigned_me'] == 1 ? true : false,
1633 'reply-assigned-others' => $role['reply_assigned_others'] == 1 ? true : false,
1634 'pn-unassigned' => true, // Private notes.
1635 'pn-assigned-me' => true,
1636 'pn-assigned-others' => true,
1637 'aa-unassigned' => $role['assign_unassigned'] == 1 ? true : false, // Assignee.
1638 'aa-assigned-me' => $role['assign_assigned_me'] == 1 ? true : false,
1639 'aa-assigned-others' => $role['assign_assigned_others'] == 1 ? true : false,
1640 'cs-unassigned' => $role['cng_tkt_sts_unassigned'] == 1 ? true : false, // Change status.
1641 'cs-assigned-me' => $role['cng_tkt_sts_assigned_me'] == 1 ? true : false,
1642 'cs-assigned-others' => $role['cng_tkt_sts_assigned_others'] == 1 ? true : false,
1643 'ctf-unassigned' => $role['cng_tkt_field_unassigned'] == 1 ? true : false, // Change ticket fields.
1644 'ctf-assigned-me' => $role['cng_tkt_field_assigned_me'] == 1 ? true : false,
1645 'ctf-assigned-others' => $role['cng_tkt_field_assigned_others'] == 1 ? true : false,
1646 'caof-unassigned' => $role['cng_tkt_ao_unassigned'] == 1 ? true : false, // Change agent only fields.
1647 'caof-assigned-me' => $role['cng_tkt_ao_assigned_me'] == 1 ? true : false,
1648 'caof-assigned-others' => $role['cng_tkt_ao_assigned_others'] == 1 ? true : false,
1649 'crb-unassigned' => $role['cng_tkt_rb_unassigned'] == 1 ? true : false, // Change raised by.
1650 'crb-assigned-me' => $role['cng_tkt_rb_assigned_me'] == 1 ? true : false,
1651 'crb-assigned-others' => $role['cng_tkt_rb_assigned_others'] == 1 ? true : false,
1652 'eth-unassigned' => $role['delete_unassigned'] == 1 ? true : false, // Edit thread.
1653 'eth-assigned-me' => $role['delete_assigned_me'] == 1 ? true : false,
1654 'eth-assigned-others' => $role['delete_assigned_others'] == 1 ? true : false,
1655 'dth-unassigned' => $role['delete_unassigned'] == 1 ? true : false, // Delete thread.
1656 'dth-assigned-me' => $role['delete_assigned_me'] == 1 ? true : false,
1657 'dth-assigned-others' => $role['delete_assigned_others'] == 1 ? true : false,
1658 'vl-unassigned' => true, // View logs.
1659 'vl-assigned-me' => true,
1660 'vl-assigned-others' => true,
1661 'dtt-unassigned' => $role['delete_unassigned'] == 1 ? true : false, // Delete ticket.
1662 'dtt-assigned-me' => $role['delete_assigned_me'] == 1 ? true : false,
1663 'dtt-assigned-others' => $role['delete_assigned_others'] == 1 ? true : false,
1664 'ar-unassigned' => true, // Additional recipients.
1665 'ar-assigned-me' => true,
1666 'ar-assigned-others' => true,
1667 'dt-unassigned' => true, // Duplicate ticket.
1668 'dt-assigned-me' => true,
1669 'dt-assigned-others' => true,
1670 'backend-access' => true, // Dashboard support menu access.
1671 'create-as' => true, // Create ticket on others behalf.
1672 'dtt-access' => true, // Deleted ticket access.
1673 'eci-access' => true, // Edit customer info.
1674 'dash-access' => true, // Dashboard access.
1675 ),
1676 );
1677 }
1678 update_option( 'wpsc-agent-roles', $new );
1679
1680 $map = array();
1681 $agents = get_terms(
1682 array(
1683 'taxonomy' => 'wpsc_agents',
1684 'hide_empty' => false,
1685 )
1686 );
1687
1688 // import agents.
1689 foreach ( $agents as $prev ) {
1690
1691 $is_agentgroup = get_term_meta( $prev->term_id, 'agentgroup', true );
1692 if ( $is_agentgroup ) {
1693 continue;
1694 }
1695
1696 $user_id = get_term_meta( $prev->term_id, 'user_id', true );
1697 $user = get_user_by( 'id', $user_id );
1698 if ( ! $user->ID ) {
1699 continue;
1700 }
1701
1702 // create customer record.
1703 $wpdb->insert(
1704 $wpdb->prefix . 'psmsc_customers',
1705 array(
1706 'user' => $user_id,
1707 'name' => $user->display_name,
1708 'email' => $user->user_email,
1709 )
1710 );
1711
1712 // insert agent record.
1713 $wpdb->insert(
1714 $wpdb->prefix . 'psmsc_agents',
1715 array(
1716 'user' => $user_id,
1717 'customer' => $wpdb->insert_id,
1718 'role' => get_term_meta( $prev->term_id, 'role', true ),
1719 'name' => $user->display_name,
1720 'is_active' => 1,
1721 )
1722 );
1723
1724 // adding agent role cap.
1725 $user->add_cap( 'wpsc_agent' );
1726
1727 // agent working hrs.
1728 $agent_id = $wpdb->insert_id;
1729 for ( $i = 1; $i <= 7; $i++ ) {
1730 $wpdb->insert(
1731 $wpdb->prefix . 'psmsc_working_hrs',
1732 array(
1733 'agent' => $agent_id,
1734 'day' => $i,
1735 'start_time' => '00:00:00',
1736 'end_time' => '23:59:59',
1737 )
1738 );
1739 }
1740
1741 $map[ $prev->term_id ] = $agent_id;
1742 }
1743
1744 // import agentgroups.
1745 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
1746 if ( $installed_addons[16]['is_installed'] ) {
1747
1748 foreach ( $agents as $prev ) {
1749
1750 $is_agentgroup = get_term_meta( $prev->term_id, 'agentgroup', true );
1751 if ( ! $is_agentgroup ) {
1752 continue;
1753 }
1754
1755 // insert agent record for the group.
1756 $wpdb->insert(
1757 $wpdb->prefix . 'psmsc_agents',
1758 array(
1759 'name' => $prev->name,
1760 'is_agentgroup' => 1,
1761 'is_active' => 1,
1762 )
1763 );
1764 $map[ $prev->term_id ] = $wpdb->insert_id;
1765
1766 // get members of the group.
1767 $group_user_ids = get_term_meta( $prev->term_id, 'agentgroup_user_id', false );
1768 $member_ids = array();
1769 foreach ( $group_user_ids as $user_id ) {
1770 $member_ids[] = $map[ $user_id ];
1771 }
1772
1773 // create agentgroup record.
1774 $wpdb->insert(
1775 $wpdb->prefix . 'psmsc_agentgroups',
1776 array(
1777 'agent_id' => $wpdb->insert_id,
1778 'name' => $prev->name,
1779 'agents' => implode( '|', $member_ids ),
1780 'supervisors' => implode( '|', $member_ids ),
1781 )
1782 );
1783 }
1784 }
1785
1786 update_option( 'wpsc_upgrade_agent_map', $map );
1787 }
1788
1789 /**
1790 * Import ticket form field settings
1791 *
1792 * @return void
1793 */
1794 public static function import_ticket_form_fields() {
1795
1796 $form_fields = array();
1797 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
1798 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
1799 $category_map = get_option( 'wpsc_upgrade_category_map' );
1800 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
1801 $fields = get_terms(
1802 array(
1803 'taxonomy' => 'wpsc_ticket_custom_fields',
1804 'hide_empty' => false,
1805 'orderby' => 'meta_value_num',
1806 'meta_key' => 'wpsc_tf_load_order',
1807 'order' => 'ASC',
1808 'meta_query' => array(
1809 array(
1810 'key' => 'agentonly',
1811 'value' => '0',
1812 'compare' => '=',
1813 ),
1814 ),
1815 )
1816 );
1817
1818 foreach ( $fields as $field ) {
1819
1820 $status = get_term_meta( $field->term_id, 'wpsc_tf_status', true );
1821 if ( $status == 0 ) {
1822 continue;
1823 }
1824
1825 $is_required = get_term_meta( $field->term_id, 'wpsc_tf_required', true );
1826
1827 $width = get_term_meta( $field->term_id, 'wpsc_tf_width', true );
1828 switch ( $width ) {
1829
1830 case '1/2':
1831 $width = 'half';
1832 break;
1833
1834 case '1':
1835 $width = 'full';
1836 }
1837
1838 $visibility = array();
1839 $prev_visibility = get_term_meta( $field->term_id, 'wpsc_tf_visibility', true );
1840 if ( $prev_visibility ) {
1841 foreach ( $prev_visibility as $condition ) {
1842 $condition = explode( '--', $condition );
1843 $cf = wpsc_get_cf_by( 'id', $cf_map[ $condition[0] ] );
1844 switch ( $cf->slug ) {
1845
1846 case 'category':
1847 if ( isset( $visibility[ $cf->slug ] ) ) {
1848 $visibility[ $cf->slug ]['operand_val_1'][] = $category_map[ $condition[1] ];
1849 } else {
1850 $visibility[ $cf->slug ] = array(
1851 'operator' => 'IN',
1852 'operand_val_1' => array(
1853 $category_map[ $condition[1] ],
1854 ),
1855 );
1856 }
1857 break;
1858
1859 case 'priority':
1860 if ( isset( $visibility[ $cf->slug ] ) ) {
1861 $visibility[ $cf->slug ]['operand_val_1'][] = $priority_map[ $condition[1] ];
1862 } else {
1863 $visibility[ $cf->slug ] = array(
1864 'operator' => 'IN',
1865 'operand_val_1' => array(
1866 $priority_map[ $condition[1] ],
1867 ),
1868 );
1869 }
1870 break;
1871
1872 default:
1873 if ( isset( $visibility[ $cf->slug ] ) ) {
1874 $visibility[ $cf->slug ]['operand_val_1'][] = $options_map[ $cf->id ][ $condition[1] ];
1875 } else {
1876 $visibility[ $cf->slug ] = array(
1877 'operator' => 'IN',
1878 'operand_val_1' => array(
1879 $options_map[ $cf->id ][ $condition[1] ],
1880 ),
1881 );
1882 }
1883 }
1884 }
1885 }
1886
1887 $cf = wpsc_get_cf_by( 'id', $cf_map[ $field->term_id ] );
1888 $form_fields[ $cf->slug ] = array(
1889 'is-required' => $is_required,
1890 'width' => $width,
1891 'relation' => 'OR',
1892 'visibility' => $visibility ? wp_json_encode( $visibility ) : '{}',
1893 );
1894 }
1895
1896 // import tff conditions.
1897 foreach ( $form_fields as $slug => $properties ) {
1898 if ( ! $properties['visibility'] ) {
1899 continue;
1900 }
1901 $form_fields[ $slug ]['visibility'] = WPSC_SC_Upgrade::upgrade_condition( $properties['visibility'], $properties['relation'] );
1902 }
1903 update_option( 'wpsc-tff', $form_fields );
1904 }
1905
1906 /**
1907 * Import agent and customer ticket list and filter items
1908 *
1909 * @return void
1910 */
1911 public static function import_ticket_list_items() {
1912
1913 $map = get_option( 'wpsc_upgrade_cf_term_id_map' );
1914 // import agent ticket list.
1915 $items = array();
1916 $fields = get_terms(
1917 array(
1918 'taxonomy' => 'wpsc_ticket_custom_fields',
1919 'hide_empty' => false,
1920 'orderby' => 'meta_value_num',
1921 'meta_key' => 'wpsc_tl_agent_load_order',
1922 'order' => 'ASC',
1923 'meta_query' => array(
1924 'relation' => 'AND',
1925 array(
1926 'key' => 'wpsc_allow_ticket_list',
1927 'value' => '1',
1928 'compare' => '=',
1929 ),
1930 array(
1931 'key' => 'wpsc_agent_ticket_list_status',
1932 'value' => '1',
1933 'compare' => '=',
1934 ),
1935 ),
1936 )
1937 );
1938 foreach ( $fields as $field ) {
1939 $cf = wpsc_get_cf_by( 'id', $map[ $field->term_id ] );
1940 $items[] = $cf->slug;
1941 }
1942 update_option( 'wpsc-atl-list-items', $items );
1943 // import agent filter items.
1944 $items = array();
1945 $fields = get_terms(
1946 array(
1947 'taxonomy' => 'wpsc_ticket_custom_fields',
1948 'hide_empty' => false,
1949 'orderby' => 'meta_value_num',
1950 'meta_key' => 'wpsc_filter_agent_load_order',
1951 'order' => 'ASC',
1952 'meta_query' => array(
1953 'relation' => 'AND',
1954 array(
1955 'key' => 'wpsc_allow_ticket_filter',
1956 'value' => '1',
1957 'compare' => '=',
1958 ),
1959 array(
1960 'key' => 'wpsc_agent_ticket_filter_status',
1961 'value' => '1',
1962 'compare' => '=',
1963 ),
1964 ),
1965 )
1966 );
1967 foreach ( $fields as $field ) {
1968 $cf = wpsc_get_cf_by( 'id', $map[ $field->term_id ] );
1969 if ( $cf->slug == 'name' ) {
1970 $items[] = 'customer';
1971 } else {
1972 $items[] = $cf->slug;
1973 }
1974 }
1975 update_option( 'wpsc-atl-filter-items', $items );
1976 // import customer list items.
1977 $items = array();
1978 $fields = get_terms(
1979 array(
1980 'taxonomy' => 'wpsc_ticket_custom_fields',
1981 'hide_empty' => false,
1982 'orderby' => 'meta_value_num',
1983 'meta_key' => 'wpsc_tl_customer_load_order',
1984 'order' => 'ASC',
1985 'meta_query' => array(
1986 'relation' => 'AND',
1987 array(
1988 'key' => 'wpsc_allow_ticket_list',
1989 'value' => '1',
1990 'compare' => '=',
1991 ),
1992 array(
1993 'key' => 'wpsc_customer_ticket_list_status',
1994 'value' => '1',
1995 'compare' => '=',
1996 ),
1997 ),
1998 )
1999 );
2000 foreach ( $fields as $field ) {
2001 $cf = wpsc_get_cf_by( 'id', $map[ $field->term_id ] );
2002 $items[] = $cf->slug;
2003 }
2004 update_option( 'wpsc-ctl-list-items', $items );
2005 // import customer filter items.
2006 $items = array();
2007 $fields = get_terms(
2008 array(
2009 'taxonomy' => 'wpsc_ticket_custom_fields',
2010 'hide_empty' => false,
2011 'orderby' => 'meta_value_num',
2012 'meta_key' => 'wpsc_filter_customer_load_order',
2013 'order' => 'ASC',
2014 'meta_query' => array(
2015 'relation' => 'AND',
2016 array(
2017 'key' => 'wpsc_allow_ticket_filter',
2018 'value' => '1',
2019 'compare' => '=',
2020 ),
2021 array(
2022 'key' => 'wpsc_customer_ticket_filter_status',
2023 'value' => '1',
2024 'compare' => '=',
2025 ),
2026 ),
2027 )
2028 );
2029 foreach ( $fields as $field ) {
2030 $cf = wpsc_get_cf_by( 'id', $map[ $field->term_id ] );
2031 if ( $cf->slug == 'name' ) {
2032 $items[] = 'customer';
2033 } else {
2034 $items[] = $cf->slug;
2035 }
2036 }
2037 update_option( 'wpsc-ctl-filter-items', $items );
2038 }
2039
2040 /**
2041 * Import ticket list more settings
2042 *
2043 * @return void
2044 */
2045 public static function import_tl_more_settings() {
2046
2047 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
2048 $status_map = get_option( 'wpsc_upgrade_status_map' );
2049 // agent view.
2050 $aview = get_option( 'wpsc-tl-ms-agent-view' );
2051 $orderby = get_option( 'wpsc_tl_agent_orderby' );
2052 $orderby = get_term_by( 'slug', $orderby, 'wpsc_ticket_custom_fields' );
2053 $orderby = wpsc_get_cf_by( 'id', $cf_map[ $orderby->term_id ] );
2054 $aview['default-sort-by'] = $orderby->slug;
2055 $aview['default-sort-order'] = get_option( 'wpsc_tl_agent_orderby_order' );
2056 $aview['number-of-tickets'] = get_option( 'wpsc_tl_agent_no_of_tickets' );
2057 $aview['unresolved-ticket-statuses'] = array_filter(
2058 array_unique(
2059 array_map(
2060 function ( $status_id ) use ( $status_map ) {
2061 return $status_map[ $status_id ];
2062 },
2063 get_option( 'wpsc_tl_agent_unresolve_statuses', array() )
2064 )
2065 )
2066 );
2067 update_option( 'wpsc-tl-ms-agent-view', $aview );
2068 // customer view.
2069 $cview = get_option( 'wpsc-tl-ms-customer-view' );
2070 $orderby = get_option( 'wpsc_tl_customer_orderby' );
2071 $orderby = get_term_by( 'slug', $orderby, 'wpsc_ticket_custom_fields' );
2072 $orderby = wpsc_get_cf_by( 'id', $cf_map[ $orderby->term_id ] );
2073 $cview['default-sort-by'] = $orderby->slug;
2074 $cview['default-sort-order'] = get_option( 'wpsc_tl_customer_orderby_order' );
2075 $cview['number-of-tickets'] = get_option( 'wpsc_tl_customer_no_of_tickets' );
2076 $cview['unresolved-ticket-statuses'] = array_filter(
2077 array_unique(
2078 array_map(
2079 function ( $status_id ) use ( $status_map ) {
2080 return $status_map[ $status_id ];
2081 },
2082 get_option( 'wpsc_tl_customer_unresolve_statuses', array() )
2083 )
2084 )
2085 );
2086 update_option( 'wpsc-tl-ms-customer-view', $cview );
2087 }
2088
2089 /**
2090 * Import email notifications general settings
2091 *
2092 * @return void
2093 */
2094 public static function import_en_general() {
2095
2096 $settings = get_option( 'wpsc-en-general', array() );
2097 $settings['from-name'] = get_option( 'wpsc_en_from_name' );
2098 $settings['from-email'] = get_option( 'wpsc_en_from_email' );
2099 $settings['reply-to'] = get_option( 'wpsc_en_reply_to' );
2100 $settings['blocked-emails'] = get_option( 'wpsc_en_ignore_emails', array() );
2101 update_option( 'wpsc-en-general', $settings );
2102 }
2103
2104 /**
2105 * Import ticket notifications
2106 *
2107 * @return void
2108 */
2109 public static function import_en_ticket_notifications() {
2110
2111 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
2112 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
2113 $status_map = get_option( 'wpsc_upgrade_status_map' );
2114 $category_map = get_option( 'wpsc_upgrade_category_map' );
2115 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
2116
2117 $new_templates = array();
2118 $event_map = wpsc_get_en_event_map();
2119 $email_templates = get_terms(
2120 array(
2121 'taxonomy' => 'wpsc_en',
2122 'hide_empty' => false,
2123 'orderby' => 'ID',
2124 'order' => 'ASC',
2125 )
2126 );
2127
2128 $string_translations = get_option( 'wpsc-string-translation' );
2129 foreach ( $email_templates as $template ) {
2130
2131 $type = get_term_meta( $template->term_id, 'type', true );
2132 $subject = get_term_meta( $template->term_id, 'subject', true );
2133 $body = get_term_meta( $template->term_id, 'body', true );
2134
2135 // conditions.
2136 $conditions = array();
2137 $prev_conditions = get_term_meta( $template->term_id, 'conditions', true );
2138 if ( $prev_conditions ) {
2139 foreach ( $prev_conditions as $key => $val ) {
2140 $cf = wpsc_get_cf_by( 'id', $cf_map[ $key ] );
2141 switch ( $cf->slug ) {
2142
2143 case 'status':
2144 $conditions['cf_status'] = array(
2145 'operator' => 'IN',
2146 'operand_val_1' => array_map(
2147 function ( $status ) use ( $status_map ) {
2148 return $status_map[ $status ];
2149 },
2150 $val
2151 ),
2152 );
2153 break;
2154
2155 case 'category':
2156 $conditions['cf_category'] = array(
2157 'operator' => 'IN',
2158 'operand_val_1' => array_map(
2159 function ( $category ) use ( $category_map ) {
2160 return $category_map[ $category ];
2161 },
2162 $val
2163 ),
2164 );
2165 break;
2166
2167 case 'priority':
2168 $conditions['cf_priority'] = array(
2169 'operator' => 'IN',
2170 'operand_val_1' => array_map(
2171 function ( $priority ) use ( $priority_map ) {
2172 return $priority_map[ $priority ];
2173 },
2174 $val
2175 ),
2176 );
2177 break;
2178
2179 default:
2180 $conditions[ 'cf_' . $cf->slug ] = array(
2181 'operator' => 'IN',
2182 'operand_val_1' => array_map(
2183 function ( $option ) use ( $options_map, $cf ) {
2184 return $options_map[ $cf->id ][ $option ];
2185 },
2186 $val
2187 ),
2188 );
2189 }
2190 }
2191 }
2192
2193 $subject = wpsc_upgrade_macros( $subject );
2194 $body = wpsc_upgrade_macros( $body );
2195 $et = array(
2196 'title' => $template->name,
2197 'event' => $event_map[ $type ],
2198 'is_enable' => 1,
2199 'subject' => $subject,
2200 'body' => array(
2201 'text' => $body,
2202 'editor' => 'html',
2203 ),
2204 'to' => array(
2205 'general-recipients' => array(),
2206 'agent-roles' => array(),
2207 'custom' => array(),
2208 ),
2209 'cc' => array(
2210 'general-recipients' => array(),
2211 'agent-roles' => array(),
2212 'custom' => array(),
2213 ),
2214 'bcc' => wpsc_en_extract_bcc( $template ),
2215 'relation' => 'AND',
2216 'conditions' => $conditions ? wp_json_encode( $conditions ) : '{}',
2217 );
2218
2219 if ( count( $new_templates ) == 0 ) {
2220 $new_templates[1] = $et;
2221 } else {
2222 $new_templates[] = $et;
2223 }
2224 $template_id = array_key_last( $new_templates );
2225 $string_translations[ 'wpsc-en-tn-subject-' . $template_id ] = $subject;
2226 $string_translations[ 'wpsc-en-tn-body-' . $template_id ] = $body;
2227 }
2228
2229 update_option( 'wpsc-string-translation', $string_translations );
2230
2231 // Email notifications.
2232 foreach ( $new_templates as $index => $properties ) {
2233 if ( ! $properties['conditions'] ) {
2234 continue;
2235 }
2236 $new_templates[ $index ]['conditions'] = WPSC_SC_Upgrade::upgrade_condition( $properties['conditions'], $properties['relation'] );
2237 }
2238 update_option( 'wpsc-email-templates', $new_templates );
2239 }
2240
2241 /**
2242 * Import general settings
2243 *
2244 * @return void
2245 */
2246 public static function import_general_settings() {
2247
2248 global $wpdb;
2249 $gs = get_option( 'wpsc-gs-general' );
2250 $page_settings = get_option( 'wpsc-gs-page-settings' );
2251 $file_attachments = get_option( 'wpsc-gs-file-attachments' );
2252 $guest_rich_text = get_option( 'wpsc-te-guest-user' );
2253 $advanced = get_option( 'wpsc-ms-advanced-settings', array() );
2254 $roles = get_option( 'wpsc-agent-roles' );
2255 $status_map = get_option( 'wpsc_upgrade_status_map' );
2256 $category_map = get_option( 'wpsc_upgrade_category_map' );
2257 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
2258 // support page.
2259 $page_settings['support-page'] = get_option( 'wpsc_support_page_id' );
2260 // default ticket status.
2261 $default_value = get_option( 'wpsc_default_ticket_status' );
2262 $default_value = $default_value ? $status_map[ $default_value ] : '1';
2263 $wpdb->update(
2264 $wpdb->prefix . 'psmsc_custom_fields',
2265 array( 'default_value' => $default_value ),
2266 array( 'id' => 7 )
2267 );
2268 // default ticket category.
2269 $default_value = get_option( 'wpsc_default_ticket_category' );
2270 $default_value = $default_value ? $category_map[ $default_value ] : '';
2271 $wpdb->update(
2272 $wpdb->prefix . 'psmsc_custom_fields',
2273 array( 'default_value' => $default_value ),
2274 array( 'id' => 9 )
2275 );
2276 // default ticket priority.
2277 $default_value = get_option( 'wpsc_default_ticket_priority' );
2278 $default_value = $default_value ? $priority_map[ $default_value ] : '';
2279 $wpdb->update(
2280 $wpdb->prefix . 'psmsc_custom_fields',
2281 array( 'default_value' => $default_value ),
2282 array( 'id' => 8 )
2283 );
2284 // ticket status after customer reply.
2285 $status = get_option( 'wpsc_ticket_status_after_customer_reply' );
2286 $gs['ticket-status-after-customer-reply'] = $status ? $status_map[ $status ] : '';
2287 // ticket status after agent reply.
2288 $status = get_option( 'wpsc_ticket_status_after_agent_reply' );
2289 $gs['ticket-status-after-agent-reply'] = $status ? $status_map[ $status ] : '';
2290 // close ticket status.
2291 $status = get_option( 'wpsc_close_ticket_status' );
2292 $gs['close-ticket-status'] = $status ? $status_map[ $status ] : '';
2293 // allow customer to close ticket.
2294 $customer = get_option( 'wpsc_allow_customer_close_ticket', 1 );
2295 $allow_close_ticket = $customer ? array( 'customer' ) : array();
2296 foreach ( $roles as $key => $role ) {
2297 $allow_close_ticket[] = $key;
2298 }
2299 $gs['allow-close-ticket'] = $allow_close_ticket;
2300 // reply form position.
2301 $position = get_option( 'wpsc_reply_form_position' );
2302 $gs['reply-form-position'] = $position == 1 ? 'top' : 'bottom';
2303 // ticket alice.
2304 $gs['ticket-alice'] = get_option( 'wpsc_ticket_alice' );
2305 // Attachment max filesize.
2306 $file_attachments['attachments-max-filesize'] = get_option( 'wpsc_attachment_max_filesize' );
2307 // allow guest tickets.
2308 $allow_create_ticket = array( 'registered-user' );
2309 if ( get_option( 'wpsc_allow_guest_ticket' ) == 1 ) {
2310 $allow_create_ticket[] = 'guest';
2311 }
2312 foreach ( $roles as $key => $role ) {
2313 $allow_create_ticket[] = $key;
2314 }
2315 $gs['allow-create-ticket'] = $allow_create_ticket;
2316 // enable otp login if guest tickets are allowed.
2317 if ( in_array( 'guest', $gs['allow-create-ticket'] ) ) {
2318 $page_settings['otp-login'] = 1;
2319 }
2320 // allow rich text editor for guest user.
2321 $guest_rich_text['enable'] = get_option( 'wpsc_allow_tinymce_in_guest_ticket', 0 );
2322 // allow reply to closed ticket after SUP-483.
2323 $allow_reply_to_closed = intval( get_option( 'wpsc_reply_to_close_ticket', 1 ) );
2324 if ( ! $allow_reply_to_closed ) {
2325 $options = $advanced['allow-reply-to-close-ticket'];
2326 unset( $options[0] );
2327 $advanced['allow-reply-to-close-ticket'] = $options;
2328 }
2329 // default login.
2330 $default_login = get_option( 'wpsc_default_login_setting', 1 );
2331 switch ( intval( $default_login ) ) {
2332 case 1:
2333 $page_settings['user-login'] = 'default';
2334 break;
2335 case 2:
2336 $page_settings['user-login'] = 'wp-default';
2337 break;
2338 case 3:
2339 $page_settings['user-login'] = 'custom';
2340 $page_settings['custom-login-url'] = get_option( 'wpsc_custom_login_url' );
2341 break;
2342 }
2343 // user registration.
2344 $user_registration = get_option( 'wpsc_user_registration', 0 );
2345 if ( $user_registration == 0 ) {
2346 $page_settings['user-registration'] = 'disable';
2347 } else {
2348 $registration_method = get_option( 'wpsc_user_registration_method', 1 );
2349 switch ( intval( $registration_method ) ) {
2350 case 1:
2351 $page_settings['user-registration'] = 'default';
2352 break;
2353 case 2:
2354 $page_settings['user-registration'] = 'wp-default';
2355 break;
2356 case 3:
2357 $page_settings['user-registration'] = 'custom';
2358 $page_settings['custom-registration-url'] = get_option( 'wpsc_custom_registration_url' );
2359 break;
2360 }
2361 }
2362 // update options.
2363 update_option( 'wpsc-gs-general', $gs );
2364 update_option( 'wpsc-gs-page-settings', $page_settings );
2365 update_option( 'wpsc-gs-file-attachments', $file_attachments );
2366 update_option( 'wpsc-te-guest-user', $guest_rich_text );
2367 update_option( 'wpsc-ms-advanced-settings', $advanced );
2368 }
2369
2370 /**
2371 * Re-order widgets as per old records
2372 *
2373 * @return void
2374 */
2375 public static function reorder_widgets() {
2376
2377 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2378 $new = get_option( 'wpsc-ticket-widget', array() );
2379 $prev = get_terms(
2380 array(
2381 'taxonomy' => 'wpsc_ticket_widget',
2382 'hide_empty' => false,
2383 'orderby' => 'meta_value_num',
2384 'order' => 'ASC',
2385 'meta_query' => array( 'order_clause' => array( 'key' => 'wpsc_ticket_widget_load_order' ) ),
2386 )
2387 );
2388 $map = array(
2389 'status' => 'change-status',
2390 'raised-by' => 'raised-by',
2391 'assign-agent' => 'assignee',
2392 'ticket-fields' => 'ticket-fields',
2393 'agent-only-fields' => 'agentonly-fields',
2394 'rating' => 'rating',
2395 'woo-order' => 'woo-order',
2396 );
2397
2398 $string_translations = get_option( 'wpsc-string-translation' );
2399 $reordered = array();
2400 foreach ( $prev as $widget ) {
2401 $flag = true;
2402 switch ( $widget->slug ) {
2403 case 'rating':
2404 if ( ! $installed_addons[15]['is_installed'] ) {
2405 $flag = false;
2406 }
2407 break;
2408 case 'woo-order':
2409 if ( ! $installed_addons[5]['is_installed'] ) {
2410 $flag = false;
2411 }
2412 break;
2413 }
2414 if ( $flag ) {
2415 $reordered[ $map[ $widget->slug ] ] = $new[ $map[ $widget->slug ] ];
2416 unset( $new[ $map[ $widget->slug ] ] );
2417 }
2418 }
2419
2420 foreach ( $reordered as $key => $widget ) {
2421 $string_translations[ 'wpsc-twt-' . $key ] = $widget['title'];
2422 }
2423
2424 foreach ( $new as $key => $widget ) {
2425 $reordered[ $key ] = $widget;
2426 }
2427 update_option( 'wpsc-string-translation', $string_translations );
2428 update_option( 'wpsc-ticket-widget', $reordered );
2429 }
2430
2431 /**
2432 * Thank you page import
2433 *
2434 * @return void
2435 */
2436 public static function import_thankyou_settings() {
2437
2438 $string_translations = get_option( 'wpsc-string-translation' );
2439
2440 $thankyou_html = wpsc_upgrade_macros( get_option( 'wpsc_thankyou_html' ) );
2441 $thankyou_url = get_option( 'wpsc_thankyou_url' );
2442 if ( $thankyou_url ) {
2443 $action = 'url';
2444 } else {
2445 $action = 'text';
2446 }
2447
2448 update_option(
2449 'wpsc-gs-thankyou-page-settings',
2450 array(
2451 'action-agent' => $action,
2452 'action-customer' => $action,
2453 'html-agent' => $thankyou_html,
2454 'html-customer' => $thankyou_html,
2455 'page-url-agent' => $thankyou_url,
2456 'page-url-customer' => $thankyou_url,
2457 'editor-agent' => 'html',
2458 'editor-customer' => 'html',
2459 )
2460 );
2461
2462 $string_translations['wpsc-thankyou-html'] = $thankyou_html;
2463 $string_translations['wpsc-thankyou-html-agent'] = $thankyou_html;
2464
2465 update_option( 'wpsc-string-translation', $string_translations );
2466 }
2467
2468 /**
2469 * Import terms and conditions
2470 *
2471 * @return void
2472 */
2473 public static function import_terms_and_conditions() {
2474
2475 $string_translations = get_option( 'wpsc-string-translation' );
2476 $tc = get_option( 'wpsc-term-and-conditions' );
2477 $tandc_text = get_option( 'wpsc_terms_and_conditions_html', $tc['tandc-text'] );
2478 $tc['allow-term-and-conditions'] = intval( get_option( 'wpsc_terms_and_conditions', $tc['allow-term-and-conditions'] ) );
2479 $tc['tandc-text'] = $tandc_text;
2480 $string_translations['wpsc-term-and-conditions'] = $tandc_text;
2481 update_option( 'wpsc-term-and-conditions', $tc );
2482
2483 $gdpr = get_option( 'wpsc-gdpr-settings' );
2484 $gdpr_text = get_option( 'wpsc_gdpr_html', $gdpr['gdpr-text'] );
2485 $gdpr['allow-gdpr'] = intval( get_option( 'wpsc_set_in_gdpr', $gdpr['allow-gdpr'] ) );
2486 $gdpr['gdpr-text'] = $gdpr_text;
2487 $gdpr['personal-data-retention-time'] = intval( get_option( 'wpsc_personal_data_retention_period_time', $gdpr['personal-data-retention-time'] ) );
2488 $unit_map = array(
2489 'days' => 'days',
2490 'months' => 'month',
2491 'years' => 'year',
2492 );
2493 $unit = get_option( 'wpsc_personal_data_retention_period_unit' );
2494 $gdpr['personal-data-retention-unit'] = isset( $unit_map[ $unit ] ) ? $unit_map[ $unit ] : 'days';
2495 $string_translations['wpsc-gdpr'] = $gdpr_text;
2496
2497 update_option( 'wpsc-string-translation', $string_translations );
2498 update_option( 'wpsc-gdpr-settings', $gdpr );
2499 }
2500
2501 /**
2502 * Import advanced settings
2503 *
2504 * @return void
2505 */
2506 public static function import_advanced_settings() {
2507
2508 $advanced = get_option( 'wpsc-ms-advanced-settings' );
2509 $agent_editor = get_option( 'wpsc-te-agent' );
2510 $reg_user_editor = get_option( 'wpsc-te-registered-user' );
2511 $guest_editor = get_option( 'wpsc-te-guest-user' );
2512 // Guest can upload files.
2513 $guest_editor['allow-attachments'] = intval( get_option( 'wpsc_guest_can_upload_files', $guest_editor['allow-attachments'] ) );
2514 // public mode.
2515 $advanced['public-mode'] = intval( get_option( 'wpsc_ticket_public_mode', $advanced['public-mode'] ) );
2516 // Allow Reply Confirmation.
2517 $advanced['reply-confirmation'] = intval( get_option( 'wpsc_allow_reply_confirmation', $advanced['reply-confirmation'] ) );
2518 // Tinymce toolbar.
2519 $wpsc_toolbar = get_option( 'wpsc_tinymce_toolbar' );
2520 if ( $wpsc_toolbar !== false ) {
2521 $old = get_option( 'wpsc_tinymce_toolbar_active' );
2522 $new = array();
2523 foreach ( $old as $key ) {
2524 $new[] = $wpsc_toolbar[ $key ]['value'];
2525 }
2526 $agent_editor['toolbar'] = $new;
2527 $reg_user_editor['toolbar'] = $new;
2528 $guest_editor['toolbar'] = $new;
2529 }
2530 // Thread Date Format.
2531 $display_as = get_option( 'wpsc_thread_date_format' );
2532 $advanced['thread-date-display-as'] = $display_as == 'string' ? 'diff' : 'date';
2533 $advanced['thread-date-format'] = get_option( 'wpsc_thread_date_time_format', $advanced['thread-date-format'] );
2534 // Do not notify owner.
2535 $advanced['do-not-notify-owner'] = get_option( 'wpsc_do_not_notify_setting', $advanced['do-not-notify-owner'] );
2536 // update options.
2537 update_option( 'wpsc-ms-advanced-settings', $advanced );
2538 update_option( 'wpsc-te-agent', $agent_editor );
2539 update_option( 'wpsc-te-registered-user', $reg_user_editor );
2540 update_option( 'wpsc-te-guest-user', $guest_editor );
2541 }
2542
2543 /**
2544 * Import captcha
2545 *
2546 * @return void
2547 */
2548 public static function import_captcha() {
2549
2550 if (
2551 get_option( 'wpsc_captcha' ) == 1 &&
2552 get_option( 'wpsc_recaptcha_type', 1 ) == 0
2553 ) {
2554 $recaptcha = get_option( 'wpsc-recaptcha-settings' );
2555 $recaptcha['allow-recaptcha'] = 1;
2556 $recaptcha['recaptcha-version'] = 2;
2557 $recaptcha['recaptcha-site-key'] = get_option( 'wpsc_get_site_key', '' );
2558 $recaptcha['recaptcha-secret-key'] = get_option( 'wpsc_get_secret_key', '' );
2559 update_option( 'wpsc-recaptcha-settings', $recaptcha );
2560 }
2561 }
2562
2563 /**
2564 * Import assign agent rules.
2565 *
2566 * @return void
2567 */
2568 public static function import_assign_agent_rules() {
2569
2570 global $wpdb;
2571 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2572 if ( ! $installed_addons[10]['is_installed'] ) {
2573 return;
2574 }
2575
2576 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
2577 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
2578 $category_map = get_option( 'wpsc_upgrade_category_map' );
2579 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
2580 $status_map = get_option( 'wpsc_upgrade_status_map' );
2581
2582 // import settings.
2583 $general = get_option( 'wpsc-aar-general-settings' );
2584 $general['auto-assign-agent'] = get_option( 'wpsc_assign_auto_responder', 0 );
2585 update_option( 'wpsc-aar-general-settings', $general );
2586
2587 // import rules.
2588 $agent_map = get_option( 'wpsc_upgrade_agent_map' );
2589 $new_rules = array();
2590 $prev_rules = get_terms(
2591 array(
2592 'taxonomy' => 'wpsc_caa',
2593 'hide_empty' => false,
2594 )
2595 );
2596
2597 foreach ( $prev_rules as $prev ) {
2598
2599 $prev_agents = get_term_meta( $prev->term_id, 'agent_ids', true );
2600 $new_agents = array_map(
2601 function ( $old_id ) use ( $wpdb, $agent_map ) {
2602 return $wpdb->get_row( "SELECT * from {$wpdb->prefix}psmsc_agents WHERE id = " . $agent_map[ $old_id ] );
2603 },
2604 $prev_agents
2605 );
2606 $agents = array();
2607 $agentgroups = array();
2608 foreach ( $new_agents as $agent ) {
2609 if ( $agent->is_agentgroup ) {
2610 $agentgroups[] = $agent->id;
2611 } else {
2612 $agents[] = $agent->id;
2613 }
2614 }
2615
2616 $conditions = array();
2617 $prev_conditions = get_term_meta( $prev->term_id, 'conditions', true );
2618 if ( $prev_conditions ) {
2619
2620 foreach ( $prev_conditions as $key => $val ) {
2621
2622 $cf = wpsc_get_cf_by( 'id', $cf_map[ $key ] );
2623 switch ( $cf->slug ) {
2624
2625 case 'status':
2626 $conditions['cf_status'] = array(
2627 'operator' => 'IN',
2628 'operand_val_1' => array_map(
2629 function ( $status ) use ( $status_map ) {
2630 return $status_map[ $status ];
2631 },
2632 $val
2633 ),
2634 );
2635 break;
2636
2637 case 'category':
2638 $conditions['cf_category'] = array(
2639 'operator' => 'IN',
2640 'operand_val_1' => array_map(
2641 function ( $category ) use ( $category_map ) {
2642 return $category_map[ $category ];
2643 },
2644 $val
2645 ),
2646 );
2647 break;
2648
2649 case 'priority':
2650 $conditions['cf_priority'] = array(
2651 'operator' => 'IN',
2652 'operand_val_1' => array_map(
2653 function ( $priority ) use ( $priority_map ) {
2654 return $priority_map[ $priority ];
2655 },
2656 $val
2657 ),
2658 );
2659 break;
2660
2661 default:
2662 $conditions[ 'cf_' . $cf->slug ] = array(
2663 'operator' => 'IN',
2664 'operand_val_1' => array_map(
2665 function ( $option ) use ( $options_map, $cf ) {
2666 return $options_map[ $cf->id ][ $option ];
2667 },
2668 $val
2669 ),
2670 );
2671 }
2672 }
2673 }
2674
2675 $rule = array(
2676 'title' => $prev->name,
2677 'agents' => implode( '|', $agents ),
2678 'assign_method' => 'assign_all',
2679 'agentgroups' => implode( '|', $agentgroups ),
2680 'assign_group_method' => 'assign_all_groups',
2681 'relation' => 'AND',
2682 'conditions' => $conditions ? wp_json_encode( $conditions ) : '{}',
2683 );
2684
2685 if ( $new_rules ) {
2686 $new_rules[] = $rule;
2687 } else {
2688 $new_rules[1] = $rule;
2689 }
2690 }
2691
2692 // Assigned agent rules filters.
2693 foreach ( $new_rules as $index => $properties ) {
2694 $new_rules[ $index ]['conditions'] = WPSC_SC_Upgrade::upgrade_condition( $properties['conditions'], $properties['relation'] );
2695 }
2696
2697 update_option( 'wpsc-aar-rules', $new_rules );
2698 }
2699
2700 /**
2701 * Import automatic close tickets add-on
2702 *
2703 * @return void
2704 */
2705 public static function import_automatic_close_tickets() {
2706
2707 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2708 if ( ! $installed_addons[0]['is_installed'] ) {
2709 return;
2710 }
2711
2712 // import settings.
2713 $string_translations = get_option( 'wpsc-string-translation' );
2714 $gs = get_option( 'wpsc-gs-general' );
2715 $status_map = get_option( 'wpsc_upgrade_status_map' );
2716 $settings = get_option( 'wpsc-atc-settings' );
2717 $settings['statuses-enabled'] = array_map(
2718 function ( $status_id ) use ( $status_map ) {
2719 return $status_map[ $status_id ];
2720 },
2721 get_option( 'wpsc_tl_statuses', array() )
2722 );
2723 $settings['age'] = get_option( 'wpsc_atc_age', '0' );
2724 $settings['close-status'] = $gs['close-ticket-status'];
2725 update_option( 'wpsc-atc-settings', $settings );
2726
2727 // import warning email.
2728 $subject = wpsc_upgrade_macros( get_option( 'wpsc_atc_subject', '' ) );
2729 $body = wpsc_upgrade_macros( get_option( 'wpsc_atc_email_body', '' ) );
2730 $templates = array(
2731 array(
2732 'title' => 'Default template',
2733 'days-before' => get_option( 'wpsc_atc_waring_email_age', 3 ),
2734 'subject' => $subject,
2735 'body' => $body,
2736 'editor' => 'html',
2737 ),
2738 );
2739 $string_translations['wpsc-act-subject-0'] = $subject;
2740 $string_translations['wpsc-act-body-0'] = $body;
2741 update_option( 'wpsc-string-translation', $string_translations );
2742 update_option( 'wpsc-atc-et', $templates );
2743 }
2744
2745 /**
2746 * Import canned reply addon
2747 *
2748 * @return void
2749 */
2750 public static function import_canned_reply() {
2751
2752 global $wpdb;
2753
2754 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2755 if ( ! $installed_addons[1]['is_installed'] ) {
2756 return;
2757 }
2758
2759 // reset data.
2760 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_cr_categories' );
2761 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_cr_categories AUTO_INCREMENT = 1' );
2762 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_canned_reply' );
2763 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_canned_reply AUTO_INCREMENT = 1' );
2764
2765 // import canned reply categories.
2766 $wpdb->insert(
2767 $wpdb->prefix . 'psmsc_cr_categories',
2768 array(
2769 'name' => 'Uncategorized',
2770 )
2771 );
2772 $categories = get_terms(
2773 array(
2774 'taxonomy' => 'wpsc_canned_reply_categories',
2775 'hide_empty' => false,
2776 )
2777 );
2778
2779 $string_translations = get_option( 'wpsc-string-translation' );
2780 $map = array();
2781 foreach ( $categories as $prev ) {
2782 $wpdb->insert(
2783 $wpdb->prefix . 'psmsc_cr_categories',
2784 array(
2785 'name' => $prev->name,
2786 )
2787 );
2788 $string_translations[ 'wpsc-cr-category-' . $wpdb->insert_id ] = $prev->name;
2789 $map[ $prev->term_id ] = $wpdb->insert_id;
2790 }
2791
2792 // import canned reply.
2793 $response = get_posts(
2794 array(
2795 'post_type' => 'wpsc_canned_reply',
2796 'post_status' => 'publish',
2797 'orderby' => 'date',
2798 'order' => 'ASC',
2799 'posts_per_page' => -1,
2800 )
2801 );
2802 foreach ( $response as $prev ) {
2803 $prev_categories = get_the_terms( $prev->ID, 'wpsc_canned_reply_categories' );
2804 $categories = '1';
2805 if ( is_array( $prev_categories ) ) {
2806 $categories = implode(
2807 '|',
2808 array_map(
2809 function ( $category ) use ( $map ) {
2810 return $map[ $category->term_id ];
2811 },
2812 $prev_categories
2813 ),
2814 );
2815 }
2816 $author = wpsc_get_customer_by( 'user_id', $prev->post_author );
2817 $visibility = get_post_meta( $prev->ID, 'wpsc_agent_visibility', true );
2818 $wpdb->insert(
2819 $wpdb->prefix . 'psmsc_canned_reply',
2820 array(
2821 'title' => $prev->post_title,
2822 'author' => $author->id,
2823 'body' => wpautop( wpsc_upgrade_macros( $prev->post_content ) ),
2824 'categories' => $categories,
2825 'visibility' => $visibility ? 'public' : 'private',
2826 'date_created' => $prev->post_date,
2827 )
2828 );
2829 }
2830 update_option( 'wpsc-string-translation', $string_translations );
2831 }
2832
2833 /**
2834 * Import report settings
2835 *
2836 * @return void
2837 */
2838 public static function import_report_settings() {
2839
2840 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2841 if ( ! $installed_addons[6]['is_installed'] ) {
2842 return;
2843 }
2844
2845 $agent_roles = get_option( 'wpsc-agent-roles' );
2846 foreach ( $agent_roles as $index => $role ) {
2847 $role['caps']['view-reports'] = $index == 1 ? true : false;
2848 $agent_roles[ $index ] = $role;
2849 }
2850 update_option( 'wpsc-agent-roles', $agent_roles );
2851 }
2852
2853 /**
2854 * Import satisfaction survey
2855 *
2856 * @return void
2857 */
2858 public static function import_satisfaction_survey() {
2859
2860 global $wpdb;
2861
2862 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2863 if ( ! $installed_addons[15]['is_installed'] ) {
2864 return;
2865 }
2866
2867 // reset data.
2868 $wpdb->query( 'DELETE FROM ' . $wpdb->prefix . 'psmsc_sf_ratings' );
2869 $wpdb->query( 'ALTER TABLE ' . $wpdb->prefix . 'psmsc_sf_ratings AUTO_INCREMENT = 1' );
2870
2871 // import ratings.
2872 $ratings = get_terms(
2873 array(
2874 'taxonomy' => 'wpsc_sf_rating',
2875 'hide_empty' => false,
2876 'orderby' => 'meta_value_num',
2877 'order' => 'ASC',
2878 'meta_query' => array( 'order_clause' => array( 'key' => 'load_order' ) ),
2879 )
2880 );
2881
2882 $string_translations = get_option( 'wpsc-string-translation' );
2883 $map = array();
2884 $load_order = 1;
2885 $confirmation_text = get_option( 'wpsc_sf_thankyou_text', '' );
2886 foreach ( $ratings as $prev ) {
2887 $bg_color = get_term_meta( $prev->term_id, 'color', true );
2888 $wpdb->insert(
2889 $wpdb->prefix . 'psmsc_sf_ratings',
2890 array(
2891 'name' => $prev->name,
2892 'color' => '#FFFFFF',
2893 'bg_color' => $bg_color,
2894 'confirmation_text' => $confirmation_text,
2895 'load_order' => $load_order++,
2896 )
2897 );
2898
2899 $string_translations[ 'wpsc-rating-name-' . $wpdb->insert_id ] = $prev->name;
2900 $string_translations[ 'wpsc-rating-ct-' . $wpdb->insert_id ] = $confirmation_text;
2901 $map[ $prev->term_id ] = $wpdb->insert_id;
2902 }
2903 update_option( 'wpsc_upgrade_sf_rating_map', $map );
2904
2905 // import settings.
2906 $settings = get_option( 'wpsc-sf-general-setting' );
2907 $settings['survey-page'] = get_option( 'wpsc_sf_page', 0 );
2908 update_option( 'wpsc-sf-general-setting', $settings );
2909
2910 // import email template.
2911 $subject = wpsc_upgrade_macros( get_option( 'wpsc_sf_subject', '' ) );
2912 $body = wpsc_upgrade_macros( get_option( 'wpsc_sf_email_body', '' ) );
2913 $days_after = get_option( 'wpsc_sf_age', '0' );
2914 if ( get_option( 'wpsc_sf_age_unit', 'd' ) == 'h' ) {
2915 $days_after = 1;
2916 }
2917 $new = array(
2918 array(
2919 'title' => 'Default template',
2920 'days-after' => $days_after,
2921 'subject' => $subject,
2922 'body' => $body,
2923 'editor' => 'html',
2924 ),
2925 );
2926 $string_translations['wpsc-sf-et-subject-0'] = $subject;
2927 $string_translations['wpsc-sf-et-body-0'] = $body;
2928 update_option( 'wpsc-string-translation', $string_translations );
2929 update_option( 'wpsc-sf-et', $new );
2930 }
2931
2932 /**
2933 * Import email piping add-on settings
2934 *
2935 * @return void
2936 */
2937 public static function import_email_piping() {
2938
2939 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
2940 if ( ! $installed_addons[3]['is_installed'] ) {
2941 return;
2942 }
2943
2944 // import settings.
2945 $general = get_option( 'wpsc-ep-general-settings' );
2946 $general['connection'] = get_option( 'wpsc_ep_piping_type', 'imap' );
2947
2948 $block_emails = get_option( 'wpsc_ep_block_emails', array() );
2949 $general['block-emails'] = array_filter( array_map( 'trim', $block_emails ) );
2950
2951 $block_subject = explode( PHP_EOL, get_option( 'wpsc_ep_block_subject', '' ) );
2952 $block_subject = $block_subject ? $block_subject : array();
2953 $general['block-subject'] = array_filter( array_map( 'trim', $block_subject ) );
2954
2955 $allowed_users = get_option( 'wpsc_ep_allowed_user', 0 );
2956 $general['allowed-users'] = $allowed_users ? 'anyone' : 'registered';
2957 $general['time-frequency'] = get_option( 'wpsc_ep_cron_execution_time', 5 );
2958 $general['body-reference'] = get_option( 'wpsc_ep_email_type', 'text' );
2959 update_option( 'wpsc-ep-general-settings', $general );
2960
2961 // import connection settings.
2962 if ( $general['connection'] == 'imap' ) {
2963
2964 $imap = array(
2965 'email-address' => get_option( 'wpsc_ep_imap_email_address', '' ),
2966 'password' => get_option( 'wpsc_ep_imap_email_password', '' ),
2967 'encryption' => get_option( 'wpsc_ep_imap_encryption', 'ssl' ),
2968 'incoming-mail-server' => get_option( 'wpsc_ep_imap_incoming_mail_server', '' ),
2969 'port' => get_option( 'wpsc_ep_imap_port', '' ),
2970 'is_active' => 0,
2971 'last-error' => '',
2972 );
2973
2974 // check connection.
2975 $encryption_text = '';
2976 if ( $imap['encryption'] == 'none' ) {
2977 $encryption_text = 'novalidate-cert';
2978 } elseif ( $imap['encryption'] == 'ssl' ) {
2979 $encryption_text = 'imap/ssl/novalidate-cert';
2980 }
2981 $conn = @imap_open( '{' . $imap['incoming-mail-server'] . ':' . $imap['port'] . '/' . $encryption_text . '}INBOX', $imap['email-address'], $imap['password'] ); // phpcs:ignore
2982 if ( $conn ) {
2983 $imap['is_active'] = 1;
2984 } else {
2985 $imap['last-error'] = imap_last_error();
2986 }
2987
2988 update_option( 'wpsc-ep-imap-settings', $imap );
2989
2990 } else { // gmail.
2991
2992 $gmail = array(
2993 'email-address' => get_option( 'wpsc_ep_email_address', '' ),
2994 'client-id' => get_option( 'wpsc_ep_client_id', '' ),
2995 'client-secret' => get_option( 'wpsc_ep_client_secret', '' ),
2996 'is-active' => 0,
2997 'last-error' => '',
2998 'refresh-token' => get_option( 'wpsc_ep_refresh_token', '' ),
2999 'history-id' => get_option( 'wpsc_ep_historyId', '' ),
3000 );
3001 if (
3002 $gmail['email-address'] &&
3003 $gmail['client-id'] &&
3004 $gmail['client-secret'] &&
3005 $gmail['refresh-token'] &&
3006 $gmail['history-id']
3007 ) {
3008 $gmail['is-active'] = 1;
3009 }
3010 update_option( 'wpsc-ep-gmail-settings', $gmail );
3011 }
3012
3013 // import email piping rules.
3014 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
3015 $category_map = get_option( 'wpsc_upgrade_category_map' );
3016 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
3017 $new_rules = array();
3018
3019 $fields = get_terms(
3020 array(
3021 'taxonomy' => 'wpsc_ticket_custom_fields',
3022 'hide_empty' => false,
3023 'meta_query' => array(
3024 'relation' => 'AND',
3025 array(
3026 'key' => 'agentonly',
3027 'value' => array( 0, 1 ),
3028 'compare' => 'IN',
3029 ),
3030 array(
3031 'key' => 'wpsc_tf_type',
3032 'value' => '0',
3033 'compare' => '>',
3034 ),
3035 ),
3036 )
3037 );
3038
3039 $email_piping_rules = get_terms(
3040 array(
3041 'taxonomy' => 'wpsc_ep_rules',
3042 'hide_empty' => false,
3043 'orderby' => 'meta_value_num',
3044 'order' => 'ASC',
3045 'meta_query' => array( 'order_clause' => array( 'key' => 'wpsc_en_rule_load_order' ) ),
3046 )
3047 );
3048
3049 foreach ( $email_piping_rules as $prev ) {
3050
3051 $rule = array(
3052 'title' => $prev->name,
3053 );
3054
3055 // condition.
3056 $forwarding_address = get_term_meta( $prev->term_id, 'wpsc_ep_to_address', true );
3057 $forwarding_address = $forwarding_address ? $forwarding_address : array();
3058 $rule['forwarding-address'] = array_filter( array_map( 'trim', $forwarding_address ) );
3059
3060 $has_words = get_term_meta( $prev->term_id, 'wpsc_ep_has_words', true );
3061 $has_words = $has_words ? $has_words : array();
3062 $rule['has-words'] = array_filter( array_map( 'trim', $has_words ) );
3063
3064 // category and priority.
3065 $category = get_term_meta( $prev->term_id, 'ticket_category', true );
3066 if ( $category ) {
3067 $rule['category'] = $category_map[ $category ];
3068 }
3069 $priority = get_term_meta( $prev->term_id, 'ticket_priority', true );
3070 if ( $priority ) {
3071 $rule['priority'] = $priority_map[ $priority ];
3072 }
3073
3074 // custom fields.
3075 foreach ( $fields as $field ) {
3076
3077 $type = get_term_meta( $field->term_id, 'wpsc_tf_type', true );
3078 $cf = wpsc_get_cf_by( 'id', $cf_map[ $field->term_id ] );
3079
3080 switch ( $type ) {
3081
3082 case 1:
3083 case 5:
3084 case 7:
3085 case 8:
3086 case 9:
3087 $val = get_term_meta( $prev->term_id, $field->slug, true );
3088 if ( $val ) {
3089 $rule[ $cf->slug ] = $val ? $val : '';
3090 }
3091 break;
3092
3093 case 2:
3094 case 4:
3095 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
3096 $val = get_term_meta( $prev->term_id, $field->slug, true );
3097 if ( $val ) {
3098 $rule[ $cf->slug ] = $options_map[ $cf->id ][ $val ];
3099 }
3100 break;
3101
3102 case 3:
3103 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
3104 $val = get_term_meta( $prev->term_id, $field->slug, false );
3105 if ( $val ) {
3106 $rule[ $cf->slug ] = implode(
3107 '|',
3108 array_filter(
3109 array_map(
3110 function ( $name ) use ( $options_map, $cf ) {
3111 return $name ? $options_map[ $cf->id ][ $name ] : false;
3112 },
3113 $val
3114 )
3115 )
3116 );
3117 }
3118 break;
3119
3120 case 6:
3121 $val = get_term_meta( $prev->term_id, $field->slug, true );
3122 if ( $val ) {
3123 $val = new DateTime( $val );
3124 $rule[ $cf->slug ] = $val->format( 'Y-m-d' );
3125 }
3126 }
3127 }
3128
3129 if ( $new_rules ) {
3130 $new_rules[] = $rule;
3131 } else {
3132 $new_rules[1] = $rule;
3133 }
3134 }
3135
3136 update_option( 'wpsc-ep-pipe-rules', $new_rules );
3137 }
3138
3139 /**
3140 * Import FAQ integration add-on settings
3141 *
3142 * @return void
3143 */
3144 public static function import_faq_integration() {
3145
3146 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3147 if ( ! $installed_addons[11]['is_installed'] ) {
3148 return;
3149 }
3150
3151 $map = array(
3152 'WPSC_Ultimate_FAQ' => 'ultimate_faq',
3153 'WPSC_Acronix_FAQ' => 'acronix_faq',
3154 );
3155 $prev = get_option( 'wpsc_select_faq_set', '' );
3156 if ( $prev ) {
3157 update_option(
3158 'wpsc-faq-settings',
3159 array(
3160 'faq' => $map[ $prev ],
3161 )
3162 );
3163 }
3164 }
3165
3166 /**
3167 * Import knowledgebase integration
3168 *
3169 * @return void
3170 */
3171 public static function import_knowledgebase_integration() {
3172
3173 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3174 if ( ! $installed_addons[12]['is_installed'] ) {
3175 return;
3176 }
3177
3178 $map = array(
3179 'WPSC_PrassApp_Knowledgbase' => 'pressapp_kb',
3180 'WPSC_WP_Knowledgbase' => 'wp_kb',
3181 'WPSC_HelpGuru_Knowledgbase' => 'ht_kb',
3182 'WPSC_BasePress_Knowledgbase' => 'basepress_kb',
3183 'WPSC_BWL_Knowledgbase' => 'bwl_kb',
3184 'WPSC_Echo_Knowledgbase' => 'echo_kb',
3185 );
3186 $prev = get_option( 'wpsc_select_knowledgbase_set', '' );
3187 if ( $prev ) {
3188 update_option(
3189 'wpsc-kb-settings',
3190 array(
3191 'knowledgebase' => $map[ $prev ],
3192 )
3193 );
3194 }
3195 }
3196
3197 /**
3198 * Import export add-on settings
3199 *
3200 * @return void
3201 */
3202 public static function import_export_addon() {
3203
3204 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3205 if ( ! $installed_addons[2]['is_installed'] ) {
3206 return;
3207 }
3208
3209 // visibility.
3210 $new_visibility = array();
3211 $prev_visibility = get_option( 'wpsc_selected_user_roll_data', array() );
3212 foreach ( $prev_visibility as $visibility ) {
3213 if ( $visibility == 'customer' ) {
3214 $new_visibility[] = 'registered-user';
3215 $new_visibility[] = 'guest';
3216 continue;
3217 }
3218 $new_visibility[] = $visibility;
3219 }
3220
3221 update_option(
3222 'wpsc-export-roles',
3223 array(
3224 'allow-export-ticket' => $new_visibility,
3225 )
3226 );
3227
3228 // export fields.
3229 $cf_slug_map = get_option( 'wpsc_upgrade_cf_slug_map' );
3230 $prev_items = get_option( 'wpsc_export_ticket_list', array() );
3231 $new_items = array();
3232 foreach ( $prev_items as $slug ) {
3233 $new_items[] = $cf_slug_map[ $slug ];
3234 }
3235 update_option( 'wpsc-agent-export-settings', $new_items );
3236 update_option( 'wpsc-register-export-settings', $new_items );
3237 }
3238
3239 /**
3240 * Import woocommerce settings
3241 *
3242 * @return void
3243 */
3244 public static function import_woocommerce_addon() {
3245
3246 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3247 if ( ! $installed_addons[5]['is_installed'] ) {
3248 return;
3249 }
3250
3251 $string_translations = get_option( 'wpsc-string-translation' );
3252 // settings.
3253 $tab_lable = get_option( 'wpsc_dashboard_support_tab_label', '' );
3254 $help_lable = get_option( 'wpsc_order_help_button_label', '' );
3255 $settings = get_option( 'wpsc-woo-settings' );
3256 $settings['dashboard-support-tab'] = intval( get_option( 'wpsc_dashboard_support_tab', 1 ) );
3257 $settings['dashboard-support-tab-label'] = $tab_lable;
3258 $settings['order-help-button'] = intval( get_option( 'wpsc_order_help_button', 0 ) );
3259 $settings['order-help-button-label'] = $help_lable;
3260 update_option( 'wpsc-woo-settings', $settings );
3261
3262 $string_translations['wpsc-woo-dashboard-tab-label'] = $tab_lable;
3263 $string_translations['wpsc-woo-order-help-button-label'] = $help_lable;
3264 update_option( 'wpsc-string-translation', $string_translations );
3265 }
3266
3267 /**
3268 * Import schedule tickets
3269 *
3270 * @return void
3271 */
3272 public static function import_shedule_tickets() {
3273
3274 global $wpdb;
3275 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3276 if ( ! $installed_addons[14]['is_installed'] ) {
3277 return;
3278 }
3279
3280 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
3281 $category_map = get_option( 'wpsc_upgrade_category_map' );
3282 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
3283 $new_rules = array();
3284
3285 $fields = get_terms(
3286 array(
3287 'taxonomy' => 'wpsc_ticket_custom_fields',
3288 'hide_empty' => false,
3289 'meta_query' => array(
3290 'relation' => 'AND',
3291 array(
3292 'key' => 'agentonly',
3293 'value' => array( 0, 1 ),
3294 'compare' => 'IN',
3295 ),
3296 array(
3297 'key' => 'wpsc_tf_type',
3298 'value' => '0',
3299 'compare' => '>',
3300 ),
3301 ),
3302 )
3303 );
3304
3305 $scheule_tickets = get_terms(
3306 array(
3307 'taxonomy' => 'wpsc_schedule_tickets',
3308 'hide_empty' => false,
3309 )
3310 );
3311
3312 foreach ( $scheule_tickets as $prev ) {
3313
3314 $time_unit = get_term_meta( $prev->term_id, 'repeat_every_time_unit', true );
3315 $recurrence = get_term_meta( $prev->term_id, 'no_of_recurrence', true );
3316 $start_date = new DateTime( get_term_meta( $prev->term_id, 'start_date', true ) . ' 00:00:00' );
3317
3318 $rule = array(
3319 'title' => $prev->name,
3320 'recurrence-period' => 'daily',
3321 'daily-recurrence-type' => 'daily-every-day',
3322 'daily-x-days' => 1,
3323 'daily-x-work-days' => 1,
3324 'weekly-x-weeks' => 1,
3325 'weekly-days' => array( 1, 2, 3, 4, 5 ),
3326 'monthly-recurrence-type' => 'monthly-day-number',
3327 'monthly-day-number-x-months' => 1,
3328 'monthly-day-number-day' => 1,
3329 'monthly-week-number-x-months' => 1,
3330 'monthly-week-number-occurrence' => 1,
3331 'monthly-week-number-day' => 1,
3332 'yearly-recurrence-type' => 'yearly-day-number',
3333 'yearly-day-number-x-years' => 1,
3334 'yearly-day-number-day' => 1,
3335 'yearly-day-number-month' => 1,
3336 'yearly-week-number-x-years' => 1,
3337 'yearly-week-number-occurrence' => 1,
3338 'yearly-week-number-day' => 1,
3339 'yearly-week-number-month' => 1,
3340 'starts-on' => ( new DateTime() )->format( 'Y-m-d' ),
3341 'ends-on' => 'no-end-date',
3342 'ends-after-times' => 10,
3343 'end-date' => '',
3344 'ticket-count' => 0,
3345 );
3346
3347 if ( $time_unit == 'days' ) {
3348
3349 $rule['recurrence-period'] = 'daily';
3350 $rule['daily-recurrence-type'] = 'daily-every-day';
3351 $rule['daily-x-days'] = intval( $recurrence );
3352
3353 } else {
3354
3355 $rule['recurrence-period'] = 'monthly';
3356 $rule['monthly-recurrence-type'] = 'monthly-day-number';
3357 $rule['monthly-day-number-x-months'] = intval( $recurrence );
3358 $rule['monthly-day-number-day'] = $start_date->format( 'd' );
3359 }
3360
3361 $rule['starts-on'] = $start_date->format( 'Y-m-d' );
3362 $rule['ends-on'] = 'no-end-date';
3363
3364 // customer.
3365 $customer_name = get_term_meta( $prev->term_id, 'customer_name', true );
3366 $customer_email = get_term_meta( $prev->term_id, 'customer_email', true );
3367 $customer_id = $wpdb->get_var( "SELECT * FROM {$wpdb->prefix}psmsc_customers WHERE email = '" . $customer_email . "'" );
3368 if ( ! $customer_id ) {
3369 $user = get_user_by( 'email', $customer_email );
3370 if ( $user ) {
3371 $wpdb->insert(
3372 $wpdb->prefix . 'psmsc_customers',
3373 array(
3374 'user' => $user->ID,
3375 'name' => $user->display_name,
3376 'email' => $user->user_email,
3377 )
3378 );
3379 } else {
3380 $wpdb->insert(
3381 $wpdb->prefix . 'psmsc_customers',
3382 array(
3383 'user' => 0,
3384 'name' => $customer_name,
3385 'email' => $customer_email,
3386 )
3387 );
3388 }
3389 $customer_id = $wpdb->insert_id;
3390 }
3391 $rule['customer'] = $customer_id;
3392
3393 // subject, description, category & priority.
3394 $rule['subject'] = get_term_meta( $prev->term_id, 'ticket_subject', true );
3395 $rule['description'] = get_term_meta( $prev->term_id, 'ticket_description', true );
3396 $category = get_term_meta( $prev->term_id, 'ticket_category', true );
3397 if ( $category ) {
3398 $rule['category'] = $category_map[ $category ];
3399 }
3400 $priority = get_term_meta( $prev->term_id, 'ticket_priority', true );
3401 if ( $priority ) {
3402 $rule['priority'] = $priority_map[ $priority ];
3403 }
3404
3405 // custom fields.
3406 foreach ( $fields as $field ) {
3407
3408 $type = get_term_meta( $field->term_id, 'wpsc_tf_type', true );
3409 $cf = wpsc_get_cf_by( 'id', $cf_map[ $field->term_id ] );
3410
3411 switch ( $type ) {
3412
3413 case 1:
3414 case 5:
3415 case 7:
3416 case 8:
3417 case 9:
3418 $val = get_term_meta( $prev->term_id, $field->slug, true );
3419 if ( $val ) {
3420 $rule[ $cf->slug ] = $val ? $val : '';
3421 }
3422 break;
3423
3424 case 2:
3425 case 4:
3426 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
3427 $val = get_term_meta( $prev->term_id, $field->slug, true );
3428 if ( $val ) {
3429 $rule[ $cf->slug ] = $options_map[ $cf->id ][ $val ];
3430 }
3431 break;
3432
3433 case 3:
3434 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
3435 $val = get_term_meta( $prev->term_id, $field->slug, false );
3436 if ( $val ) {
3437 $rule[ $cf->slug ] = implode(
3438 '|',
3439 array_filter(
3440 array_map(
3441 function ( $name ) use ( $options_map, $cf ) {
3442 return $name ? $options_map[ $cf->id ][ $name ] : false;
3443 },
3444 $val
3445 )
3446 )
3447 );
3448 }
3449 break;
3450
3451 case 6:
3452 $val = get_term_meta( $prev->term_id, $field->slug, true );
3453 if ( $val ) {
3454 $val = new DateTime( $val );
3455 $rule[ $cf->slug ] = $val->format( 'Y-m-d' );
3456 }
3457 }
3458 }
3459
3460 if ( ! $new_rules ) {
3461 $new_rules[1] = $rule;
3462 } else {
3463 $new_rules[] = $rule;
3464 }
3465 }
3466
3467 update_option( 'wpsc-st-rules', $new_rules );
3468 }
3469
3470 /**
3471 * Import SLA settings
3472 *
3473 * @return void
3474 */
3475 public static function import_sla_settings() {
3476
3477 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3478 if ( ! $installed_addons[4]['is_installed'] ) {
3479 return;
3480 }
3481
3482 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
3483 $options_map = get_option( 'wpsc_upgrade_cf_options_map' );
3484 $category_map = get_option( 'wpsc_upgrade_category_map' );
3485 $priority_map = get_option( 'wpsc_upgrade_priority_map' );
3486 $status_map = get_option( 'wpsc_upgrade_status_map' );
3487
3488 // settings.
3489 $settings = get_option( 'wpsc-sla-general-settings' );
3490 $settings['out-sla-bg-color'] = get_option( 'wpsc_out_sla_color', $settings['out-sla-bg-color'] );
3491 $settings['out-sla-text-color'] = '#ffffff';
3492 $settings['in-sla-bg-color'] = get_option( 'wpsc_in_sla_color', $settings['in-sla-bg-color'] );
3493 $settings['in-sla-text-color'] = '#ffffff';
3494 update_option( 'wpsc-sla-general-settings', $settings );
3495
3496 // sla policies.
3497 $policies = get_terms(
3498 array(
3499 'taxonomy' => 'wpsc_sla',
3500 'hide_empty' => false,
3501 'orderby' => 'meta_value_num',
3502 'order' => 'ASC',
3503 'meta_query' => array( 'order_clause' => array( 'key' => 'load_order' ) ),
3504 )
3505 );
3506
3507 $new_policies = array();
3508 $count = 0;
3509 $map = array();
3510
3511 foreach ( $policies as $prev ) {
3512
3513 $time_unit_map = array(
3514 'minutes' => 'minute',
3515 'hours' => 'hour',
3516 'days' => 'day',
3517 'months' => 'month',
3518 'years' => 'year',
3519 );
3520 $time = get_term_meta( $prev->term_id, 'time', true );
3521 $time_unit = get_term_meta( $prev->term_id, 'time_unit', true );
3522
3523 $conditions = array();
3524 $prev_conditions = get_term_meta( $prev->term_id, 'conditions', true );
3525 if ( $prev_conditions ) {
3526
3527 foreach ( $prev_conditions as $key => $val ) {
3528
3529 $cf = wpsc_get_cf_by( 'id', $cf_map[ $key ] );
3530 switch ( $cf->slug ) {
3531
3532 case 'status':
3533 $conditions['cf_status'] = array(
3534 'operator' => 'IN',
3535 'operand_val_1' => array_map(
3536 function ( $status ) use ( $status_map ) {
3537 return $status_map[ $status ];
3538 },
3539 $val
3540 ),
3541 );
3542 break;
3543
3544 case 'category':
3545 $conditions['cf_category'] = array(
3546 'operator' => 'IN',
3547 'operand_val_1' => array_map(
3548 function ( $category ) use ( $category_map ) {
3549 return $category_map[ $category ];
3550 },
3551 $val
3552 ),
3553 );
3554 break;
3555
3556 case 'priority':
3557 $conditions['cf_priority'] = array(
3558 'operator' => 'IN',
3559 'operand_val_1' => array_map(
3560 function ( $priority ) use ( $priority_map ) {
3561 return $priority_map[ $priority ];
3562 },
3563 $val
3564 ),
3565 );
3566 break;
3567
3568 default:
3569 $conditions[ 'cf_' . $cf->slug ] = array(
3570 'operator' => 'IN',
3571 'operand_val_1' => array_map(
3572 function ( $option ) use ( $options_map, $cf ) {
3573 return $options_map[ $cf->id ][ $option ];
3574 },
3575 $val
3576 ),
3577 );
3578 }
3579 }
3580 }
3581
3582 $policy = array(
3583 'title' => $prev->name,
3584 'time' => $time,
3585 'time-unit' => $time_unit_map[ $time_unit ],
3586 'calculate-from' => 'date_updated',
3587 'relation' => 'AND',
3588 'conditions' => $conditions ? wp_json_encode( $conditions ) : '{}',
3589 );
3590
3591 $new_policies[ ++$count ] = $policy;
3592 $map[ $prev->term_id ] = $count;
3593 }
3594
3595 // SLA policy filters.
3596 foreach ( $new_policies as $index => $properties ) {
3597 $new_policies[ $index ]['conditions'] = WPSC_SC_Upgrade::upgrade_condition( $properties['conditions'], $properties['relation'] );
3598 }
3599
3600 update_option( 'wpsc-sla-policies', $new_policies );
3601 update_option( 'wpsc_upgrade_sla_policy_map', $map );
3602 }
3603
3604 /**
3605 * Import usergroup settings
3606 *
3607 * @return void
3608 */
3609 public static function import_usergroup_settings() {
3610
3611 global $wpdb;
3612 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3613 if ( ! $installed_addons[7]['is_installed'] ) {
3614 return;
3615 }
3616
3617 $category_map = get_option( 'wpsc_upgrade_category_map' );
3618 $usergroups = get_terms(
3619 array(
3620 'taxonomy' => 'wpsc_usergroup_data',
3621 'hide_empty' => false,
3622 )
3623 );
3624
3625 foreach ( $usergroups as $prev ) {
3626
3627 $members = array();
3628 $prev_members = get_term_meta( $prev->term_id, 'wpsc_usergroup_userid', false );
3629 foreach ( $prev_members as $user_id ) {
3630 $customer_id = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}psmsc_customers WHERE user = " . $user_id );
3631 if ( ! $customer_id ) {
3632 $user = get_user_by( 'id', $user_id );
3633 $wpdb->insert(
3634 $wpdb->prefix . 'psmsc_customers',
3635 array(
3636 'user' => $user_id,
3637 'name' => $user->display_name,
3638 'email' => $user->user_email,
3639 )
3640 );
3641 $customer_id = $wpdb->insert_id;
3642 }
3643 $members[] = $customer_id;
3644 }
3645
3646 $supervisors = array();
3647 $prev_supervisors = get_term_meta( $prev->term_id, 'wpsc_usergroup_supervisor_id', false );
3648 foreach ( $prev_supervisors as $user_id ) {
3649 $supervisors[] = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}psmsc_customers WHERE user = " . $user_id );
3650 }
3651
3652 $category = get_term_meta( $prev->term_id, 'wpsc_usergroup_category', true );
3653
3654 $wpdb->insert(
3655 $wpdb->prefix . 'psmsc_usergroups',
3656 array(
3657 'name' => $prev->name,
3658 'members' => implode( '|', $members ),
3659 'supervisors' => implode( '|', $supervisors ),
3660 'category' => $category ? $category_map[ $category ] : '',
3661 )
3662 );
3663 }
3664 }
3665
3666 /**
3667 * Import single ticket
3668 *
3669 * @param WP_Post $ticket - ticket post object.
3670 * @return void
3671 */
3672 public static function import_individual_ticket( $ticket ) {
3673
3674 global $wpdb;
3675
3676 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3677
3678 // destroy existing ticket if already imported.
3679 $ticket_id = $wpdb->get_var( "SELECT id FROM {$wpdb->prefix}psmsc_tickets WHERE id = " . get_post_meta( $ticket->ID, 'ticket_id', true ) );
3680 if ( $ticket_id ) {
3681 wpsc_destroy_existing_ticket( $ticket_id );
3682 }
3683
3684 $fields = get_terms(
3685 array(
3686 'taxonomy' => 'wpsc_ticket_custom_fields',
3687 'hide_empty' => false,
3688 'orderby' => 'term_id',
3689 'order' => 'ASC',
3690 )
3691 );
3692
3693 $cf_map = get_option( 'wpsc_upgrade_cf_term_id_map' );
3694 $data = array();
3695
3696 foreach ( $fields as $prev ) {
3697
3698 $type = get_term_meta( $prev->term_id, 'wpsc_tf_type', true );
3699
3700 if ( ! isset( $cf_map[ $prev->term_id ] ) || $prev->slug == 'ticket_description' ) {
3701 continue;
3702 }
3703
3704 $cf = wpsc_get_cf_by( 'id', $cf_map[ $prev->term_id ] );
3705
3706 if ( intval( $type ) === 0 ) {
3707
3708 switch ( $prev->slug ) {
3709
3710 case 'ticket_id':
3711 case 'customer_name':
3712 case 'customer_email':
3713 case 'ticket_subject':
3714 case 'date_created':
3715 case 'date_updated':
3716 $data[ $cf->slug ] = get_post_meta( $ticket->ID, $prev->slug, true );
3717 break;
3718
3719 case 'ticket_status':
3720 $map = get_option( 'wpsc_upgrade_status_map' );
3721 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3722 $data[ $cf->slug ] = $map[ $val ];
3723 break;
3724
3725 case 'ticket_category':
3726 $map = get_option( 'wpsc_upgrade_category_map' );
3727 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3728 $data[ $cf->slug ] = $map[ $val ];
3729 break;
3730
3731 case 'ticket_priority':
3732 $map = get_option( 'wpsc_upgrade_priority_map' );
3733 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3734 $data[ $cf->slug ] = $map[ $val ];
3735 break;
3736
3737 case 'agent_created':
3738 $map = get_option( 'wpsc_upgrade_agent_map' );
3739 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3740 $data[ $cf->slug ] = $val ? $map[ $val ] : 0;
3741 break;
3742
3743 case 'assigned_agent':
3744 $map = get_option( 'wpsc_upgrade_agent_map' );
3745 $val = array_filter(
3746 array_map(
3747 function ( $agent_id ) use ( $map ) {
3748 return $agent_id ? $map[ $agent_id ] : '';
3749 },
3750 get_post_meta( $ticket->ID, $prev->slug, false )
3751 )
3752 );
3753 $data[ $cf->slug ] = implode( '|', $val );
3754 break;
3755
3756 case 'sf_rating':
3757 $map = get_option( 'wpsc_upgrade_sf_rating_map' );
3758 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3759 if ( is_numeric( $val ) ) {
3760 $data[ $cf->slug ] = $map[ $val ];
3761 }
3762 break;
3763
3764 case 'sla':
3765 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3766 if ( $val && $val != '3099-01-01 00:00:00' ) {
3767 $policy_map = get_option( 'wpsc_upgrade_sla_policy_map' );
3768 $data[ $cf->slug ] = $val;
3769 $data['sla_policy'] = $policy_map[ get_post_meta( $ticket->ID, 'sla_term', true ) ];
3770 $data['od_email'] = get_post_meta( $ticket->ID, 'wpsp_out_of_sla_email_send', true );
3771 }
3772 break;
3773 }
3774 } else {
3775
3776 switch ( intval( $type ) ) {
3777
3778 case 1:
3779 case 5:
3780 case 6:
3781 case 7:
3782 case 8:
3783 case 9:
3784 case 11:
3785 case 12:
3786 $data[ $cf->slug ] = get_post_meta( $ticket->ID, $prev->slug, true );
3787 break;
3788
3789 case 2:
3790 case 4:
3791 $options = wpsc_get_cf_options( $cf->id );
3792 $val = get_post_meta( $ticket->ID, $prev->slug, true );
3793 foreach ( $options as $option ) {
3794 if ( $option->name == $val ) {
3795 $data[ $cf->slug ] = $option->id;
3796 break;
3797 }
3798 }
3799 break;
3800
3801 case 3:
3802 $options = wpsc_get_cf_options( $cf->id );
3803 $prev_val = get_post_meta( $ticket->ID, $prev->slug, false );
3804 $new_val = array();
3805 foreach ( $options as $option ) {
3806 if ( in_array( $option->name, $prev_val ) ) {
3807 $new_val[] = $option->id;
3808 }
3809 }
3810 $data[ $cf->slug ] = implode( '|', $new_val );
3811 break;
3812
3813 case 10:
3814 $prev_val = array_filter( get_post_meta( $ticket->ID, $prev->slug, false ) );
3815 $new_val = array();
3816 $upload_dir = wp_upload_dir();
3817 foreach ( $prev_val as $item ) {
3818 $term_meta = get_term_meta( $item );
3819 if ( ! $term_meta ) {
3820 continue;
3821 }
3822 $wpdb->insert(
3823 $wpdb->prefix . 'psmsc_attachments',
3824 array(
3825 'name' => $term_meta['filename'][0],
3826 'file_path' => '/wpsc/' . $term_meta['save_file_name'][0],
3827 'is_image' => $term_meta['is_image'][0],
3828 'is_active' => 1,
3829 'date_created' => $term_meta['time_uploaded'][0],
3830 'source' => 'cf',
3831 'source_id' => $cf->id,
3832 'ticket_id' => $data['id'],
3833 )
3834 );
3835 $new_val[] = $wpdb->insert_id;
3836 }
3837 $data[ $cf->slug ] = implode( '|', $new_val );
3838 break;
3839 }
3840 }
3841 }
3842
3843 // customer record if not exists.
3844 $customer = wpsc_import_customer( $data['name'], $data['email'] );
3845 $data['customer'] = $customer->id;
3846 unset( $data['name'] );
3847 unset( $data['email'] );
3848
3849 // user type.
3850 $data['user_type'] = $customer->user ? 'registered' : 'guest';
3851
3852 // deleted.
3853 $data['is_active'] = $ticket->post_status == 'publish' ? 1 : 0;
3854
3855 // Auth code.
3856 $data['auth_code'] = get_post_meta( $ticket->ID, 'ticket_auth_code', true );
3857
3858 // create ticket record.
3859 $wpdb->insert(
3860 $wpdb->prefix . 'psmsc_tickets',
3861 $data
3862 );
3863
3864 $ticket = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_tickets WHERE id = " . $wpdb->insert_id );
3865
3866 // import ticket threads.
3867 self::import_ticket_threads( $ticket );
3868 }
3869
3870 /**
3871 * Import ticket threads
3872 *
3873 * @param stdClass $ticket - ticket object.
3874 * @return void
3875 */
3876 public static function import_ticket_threads( $ticket ) {
3877
3878 global $wpdb;
3879 $gs = get_option( 'wpsc-gs-general' );
3880 $tl_advanced = get_option( 'wpsc-tl-ms-advanced' );
3881 $installed_addons = get_option( 'wpsc_upgrade_installed_addons' );
3882
3883 $threads = get_posts(
3884 array(
3885 'post_type' => 'wpsc_ticket_thread',
3886 'post_status' => 'publish',
3887 'orderby' => 'date',
3888 'order' => 'ASC',
3889 'posts_per_page' => -1,
3890 'meta_query' => array(
3891 'relation' => 'AND',
3892 array(
3893 'key' => 'ticket_id',
3894 'value' => $ticket->id,
3895 'compare' => '=',
3896 ),
3897 ),
3898 )
3899 );
3900
3901 // ticket update data.
3902 $ticket_data = array();
3903
3904 // last reply thread.
3905 $last_thread = null;
3906
3907 // first response delay.
3908 $frd = 0;
3909
3910 // average response delay.
3911 $ard = 0;
3912
3913 // communication gap.
3914 $cg = 1;
3915
3916 // report temp variables.
3917 $count = 0;
3918 $last_reply_by = 'customer';
3919 $last_reply_time = new DateTime( $ticket->date_created );
3920 $delay = 0;
3921
3922 foreach ( $threads as $thread ) {
3923
3924 $thread_type = get_post_meta( $thread->ID, 'thread_type', true );
3925
3926 if ( in_array( $thread_type, array( 'report', 'reply', 'note' ) ) ) {
3927
3928 // thread_customer import.
3929 $customer_name = get_post_meta( $thread->ID, 'customer_name', true );
3930 $customer_email = get_post_meta( $thread->ID, 'customer_email', true );
3931 $customer = wpsc_import_customer( $customer_name, $customer_email );
3932
3933 // create thread record.
3934 $wpdb->insert(
3935 $wpdb->prefix . 'psmsc_threads',
3936 array(
3937 'ticket' => $ticket->id,
3938 'customer' => $customer->id,
3939 'type' => $thread_type,
3940 'body' => $thread->post_content,
3941 'date_created' => $thread->post_date,
3942 'date_updated' => $thread->post_date,
3943 )
3944 );
3945 $new_thread = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_threads WHERE id = " . $wpdb->insert_id );
3946
3947 // Calculate response delays.
3948 if ( $new_thread->type == 'reply' ) {
3949 ++$cg;
3950 $current_reply_by = 'customer';
3951 if ( $ticket->customer != $new_thread->customer && wpsc_is_agent( $new_thread->customer ) ) {
3952 $current_reply_by = 'agent';
3953 }
3954 if ( $last_reply_by == 'customer' && $current_reply_by == 'agent' ) {
3955 ++$count;
3956 $diff = ceil( abs( $last_reply_time->getTimestamp() - ( new DateTime( $new_thread->date_created ) )->getTimestamp() ) / 60 );
3957 if ( $count == 1 ) {
3958 $frd = $diff;
3959 }
3960 $delay += $diff;
3961 }
3962 if ( $last_reply_by == 'agent' && $current_reply_by == 'customer' ) {
3963 $last_reply_time = new DateTime( $new_thread->date_created );
3964 }
3965 $last_reply_by = $current_reply_by;
3966 }
3967
3968 if ( $new_thread->type == 'report' || $new_thread->type == 'reply' ) {
3969 $last_thread = $new_thread;
3970 }
3971
3972 // Handle attachments.
3973 $prev = get_post_meta( $thread->ID, 'attachments', true );
3974 $new = array();
3975 $upload_dir = wp_upload_dir();
3976 foreach ( $prev as $item ) {
3977
3978 $term_meta = get_term_meta( $item );
3979 if ( ! $term_meta ) {
3980 continue;
3981 }
3982
3983 $wpdb->insert(
3984 $wpdb->prefix . 'psmsc_attachments',
3985 array(
3986 'name' => $term_meta['filename'][0],
3987 'file_path' => '/wpsc/' . $term_meta['save_file_name'][0],
3988 'is_image' => $term_meta['is_image'][0],
3989 'is_active' => 1,
3990 'date_created' => $term_meta['time_uploaded'][0],
3991 'source' => $thread_type,
3992 'source_id' => $new_thread->id,
3993 'ticket_id' => $ticket->id,
3994 )
3995 );
3996 $new[] = $wpdb->insert_id;
3997 }
3998 // update attachments to db.
3999 $wpdb->update(
4000 $wpdb->prefix . 'psmsc_threads',
4001 array(
4002 'attachments' => implode( '|', $new ),
4003 'body' => self::import_uploaded_thread_images( $new_thread->body, $new_thread, $ticket ),
4004 ),
4005 array( 'id' => $new_thread->id )
4006 );
4007
4008 } elseif ( $thread_type == 'log' ) {
4009
4010 $wpdb->insert(
4011 $wpdb->prefix . 'psmsc_threads',
4012 array(
4013 'ticket' => $ticket->id,
4014 'customer' => 0,
4015 'type' => 'log',
4016 'body' => $thread->post_content,
4017 'date_created' => $thread->post_date,
4018 'date_updated' => $thread->post_date,
4019 )
4020 );
4021
4022 } elseif ( $installed_addons[15]['is_installed'] && $thread_type == 'feedback' ) {
4023 // Add ticket feedback (satisfaction survey).
4024 $ticket_data['sf_feedback'] = $thread->post_content;
4025 $ticket_data['sf_date'] = $thread->post_date;
4026 }
4027 }
4028
4029 // date_closed was not available in v1 so date_updated is considered as date closed for closed tickets.
4030 if ( $ticket->status == $gs['close-ticket-status'] || in_array( $ticket->status, $tl_advanced['closed-ticket-statuses'] ) ) {
4031 $ticket_data['date_closed'] = $ticket->date_updated;
4032 }
4033
4034 // last reply by and last reply on.
4035 if ( $last_thread ) {
4036 $ticket_data['last_reply_by'] = $last_thread->customer;
4037 $ticket_data['last_reply_on'] = $last_thread->date_created;
4038 }
4039
4040 // report add-on.
4041 if ( $installed_addons[6]['is_installed'] ) {
4042 $ticket_data['frd'] = $frd;
4043 if ( $count ) {
4044 $ticket_data['ard'] = ceil( $delay / $count );
4045 }
4046 $ticket_data['cg'] = $cg;
4047 if ( $ticket->status == $gs['close-ticket-status'] || in_array( $ticket->status, $tl_advanced['closed-ticket-statuses'] ) ) {
4048 $ticket_data['cd'] = ceil( abs( ( new DateTime( $ticket->date_created ) )->getTimestamp() - ( new DateTime( $ticket->date_closed ) )->getTimestamp() ) / 60 );
4049 }
4050 }
4051
4052 // insert sf_date if only rating is available.
4053 if ( $ticket->rating && ! isset( $ticket_data['sf_date'] ) ) {
4054 $date_closed = isset( $ticket->date_closed ) ? $ticket->date_closed : '';
4055 $date_closed = ! $date_closed && isset( $ticket_data['date_closed'] ) ? $ticket_data['date_closed'] : $date_closed;
4056 if ( $date_closed ) {
4057 $ticket_data['sf_date'] = $date_closed;
4058 }
4059 }
4060
4061 // update ticket data.
4062 $wpdb->update(
4063 $wpdb->prefix . 'psmsc_tickets',
4064 $ticket_data,
4065 array( 'id' => $ticket->id )
4066 );
4067 }
4068
4069 /**
4070 * Import uploaded thread images
4071 *
4072 * @param string $thread_content - thread html.
4073 * @param stdClass $thread - thread object.
4074 * @param stdClass $ticket - ticket object.
4075 * @return string
4076 */
4077 public static function import_uploaded_thread_images( $thread_content, $thread, $ticket ) {
4078
4079 global $wpdb;
4080 $upload_dir = wp_upload_dir();
4081 $regex = 'src="' . preg_quote( $upload_dir['baseurl'], '/' ) . '\/wpsc\/(.*?)"';
4082 preg_match_all( '/' . $regex . '/', $thread_content, $matches );
4083 if ( $matches[1] ) {
4084 $count = count( $matches[1] );
4085 for ( $i = 0; $i < $count; $i++ ) {
4086
4087 $file_path = $upload_dir['basedir'] . '/wpsc/' . $matches[1][ $i ];
4088 if ( ! file_exists( $file_path ) ) {
4089 continue;
4090 }
4091
4092 $file_path = str_replace( $upload_dir['basedir'], '', $file_path );
4093 $file_name = basename( $file_path );
4094 $file_url = $upload_dir['baseurl'] . '/wpsc/' . $matches[1][ $i ];
4095
4096 $wpdb->insert(
4097 $wpdb->prefix . 'psmsc_attachments',
4098 array(
4099 'name' => $file_name,
4100 'file_path' => $file_path,
4101 'is_image' => 1,
4102 'is_active' => 1,
4103 'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ),
4104 'source' => 'img_editor',
4105 'source_id' => $thread->id,
4106 'ticket_id' => $ticket->id,
4107 )
4108 );
4109
4110 $thread_content = str_replace(
4111 $file_url,
4112 home_url( '/' ) . '?wpsc_attachment=' . $wpdb->insert_id,
4113 $thread_content
4114 );
4115 }
4116 }
4117 return $thread_content;
4118 }
4119 }
4120
4121 endif;
4122
4123 WPSC_Upgrade_DB_V1::init();
4124
4125 require_once __DIR__ . '/functions.php';
4126