/**
  * There was a bug in the WCS_Upgrade_2_0::add_line_tax_data() method in Subscriptions 2.0.0 and 2.0.1 which
  * prevented recurring line tax data from being copied correctly to newly created subscriptions. This bug was
  * fixed in 2.0.2, so we can now use that method to make sure line tax data is set correctly. But to do that,
  * we first need to massage some of the deprecated line item meta to use the original meta keys.
  *
  * @param  int $subscription_line_item_id ID of the new subscription line item
  * @param  int $old_order_item_id ID of the old order line item
  * @param  array $old_order_item The old line item
  * @return bool|int the meta ID of the newly added '_line_tax_data' meta data row, or false if no line tax data was added.
  */
 protected static function maybe_repair_line_tax_data($subscription_line_item_id, $old_order_item_id, $old_order_item)
 {
     // we need item meta in the old format so that we can use the (now fixed) WCS_Upgrade_2_0::add_line_tax_data() method and save duplicating its code
     $old_order_item['item_meta']['_recurring_line_total'] = isset($old_order_item['item_meta']['_wcs_migrated_recurring_line_total']) ? $old_order_item['item_meta']['_wcs_migrated_recurring_line_total'] : 0;
     $old_order_item['item_meta']['_recurring_line_tax'] = isset($old_order_item['item_meta']['_wcs_migrated_recurring_line_tax']) ? $old_order_item['item_meta']['_wcs_migrated_recurring_line_tax'] : 0;
     $old_order_item['item_meta']['_recurring_line_subtotal_tax'] = isset($old_order_item['item_meta']['_wcs_migrated_recurring_line_subtotal_tax']) ? $old_order_item['item_meta']['_wcs_migrated_recurring_line_subtotal_tax'] : 0;
     if (isset($old_order_item['item_meta']['_wcs_migrated_recurring_line_tax_data'])) {
         $old_order_item['item_meta']['_recurring_line_tax_data'] = $old_order_item['item_meta']['_wcs_migrated_recurring_line_tax_data'];
     }
     return WCS_Upgrade_2_0::add_line_tax_data($subscription_line_item_id, $old_order_item_id, $old_order_item);
 }
 /**
  * Move scheduled subscription hooks out of wp-cron and into the new Action Scheduler.
  *
  * Also set all existing subscriptions to "sold individually" to maintain previous behavior
  * for existing subscription products before the subscription quantities feature was enabled..
  *
  * @since 1.5
  */
 public static function ajax_upgrade()
 {
     global $wpdb;
     check_admin_referer('wcs_upgrade_process', 'nonce');
     self::set_upgrade_limits();
     WCS_Upgrade_Logger::add(sprintf('Starting upgrade step: %s', $_POST['upgrade_step']));
     if (ini_get('max_execution_time') < 600) {
         @set_time_limit(600);
     }
     @ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
     update_option('wc_subscriptions_is_upgrading', gmdate('U') + 60 * 2);
     switch ($_POST['upgrade_step']) {
         case 'really_old_version':
             $upgraded_versions = self::upgrade_really_old_versions();
             $results = array('message' => sprintf(__('Database updated to version %s', 'woocommerce-subscriptions'), $upgraded_versions));
             break;
         case 'products':
             require_once 'class-wcs-upgrade-1-5.php';
             $upgraded_product_count = WCS_Upgrade_1_5::upgrade_products();
             $results = array('message' => sprintf(_x('Marked %s subscription products as "sold individually".', 'used in the subscriptions upgrader', 'woocommerce-subscriptions'), $upgraded_product_count));
             break;
         case 'hooks':
             require_once 'class-wcs-upgrade-1-5.php';
             $upgraded_hook_count = WCS_Upgrade_1_5::upgrade_hooks(self::$upgrade_limit_hooks);
             $results = array('upgraded_count' => $upgraded_hook_count, 'message' => sprintf(__('Migrated %s subscription related hooks to the new scheduler (in {execution_time} seconds).', 'woocommerce-subscriptions'), $upgraded_hook_count));
             break;
         case 'subscriptions':
             require_once 'class-wcs-repair-2-0.php';
             require_once 'class-wcs-upgrade-2-0.php';
             try {
                 $upgraded_subscriptions = WCS_Upgrade_2_0::upgrade_subscriptions(self::$upgrade_limit_subscriptions);
                 $results = array('upgraded_count' => $upgraded_subscriptions, 'message' => sprintf(__('Migrated %s subscriptions to the new structure (in {execution_time} seconds).', 'woocommerce-subscriptions'), $upgraded_subscriptions), 'status' => 'success', 'time_message' => __('Estimated time left (minutes:seconds): {time_left}', 'woocommerce-subscriptions'));
             } catch (Exception $e) {
                 WCS_Upgrade_Logger::add(sprintf('Error on upgrade step: %s. Error: %s', $_POST['upgrade_step'], $e->getMessage()));
                 $results = array('upgraded_count' => 0, 'message' => sprintf(__('Unable to upgrade subscriptions.<br/>Error: %1$s<br/>Please refresh the page and try again. If problem persists, %2$scontact support%3$s.', 'woocommerce-subscriptions'), '<code>' . $e->getMessage() . '</code>', '<a href="' . esc_url('https://woothemes.com/my-account/create-a-ticket/') . '">', '</a>'), 'status' => 'error');
             }
             break;
         case 'subscription_dates_repair':
             require_once 'class-wcs-upgrade-2-0.php';
             require_once 'class-wcs-repair-2-0-2.php';
             $subscription_ids_to_repair = WCS_Repair_2_0_2::get_subscriptions_to_repair(self::$upgrade_limit_subscriptions);
             try {
                 $subscription_counts = WCS_Repair_2_0_2::maybe_repair_subscriptions($subscription_ids_to_repair);
                 $repair_message = sprintf(__('Repaired %d subscriptions with incorrect dates, line tax data or missing customer notes.', 'woocommerce-subscriptions'), $subscription_counts['repaired_count']);
                 if ($subscription_counts['unrepaired_count'] > 0) {
                     $repair_message .= ' ' . sprintf(_n('%d other subscription was checked and did not need any repairs.', '%d other subscriptions were checked and did not need any repairs.', $subscription_counts['unrepaired_count'], 'woocommerce-subscriptions'), $subscription_counts['unrepaired_count']);
                 }
                 $repair_message .= ' ' . __('(in {execution_time} seconds)', 'woocommerce-subscriptions');
                 $results = array('repaired_count' => $subscription_counts['repaired_count'], 'unrepaired_count' => $subscription_counts['unrepaired_count'], 'message' => $repair_message, 'status' => 'success', 'time_message' => __('Estimated time left (minutes:seconds): {time_left}', 'woocommerce-subscriptions'));
             } catch (Exception $e) {
                 WCS_Upgrade_Logger::add(sprintf('Error on upgrade step: %s. Error: %s', $_POST['upgrade_step'], $e->getMessage()));
                 $results = array('repaired_count' => 0, 'unrepaired_count' => 0, 'message' => sprintf(__('Unable to repair subscriptions.<br/>Error: %1$s<br/>Please refresh the page and try again. If problem persists, %2$scontact support%3$s.', 'woocommerce-subscriptions'), '<code>' . $e->getMessage() . '</code>', '<a href="' . esc_url('https://woothemes.com/my-account/create-a-ticket/') . '">', '</a>'), 'status' => 'error');
             }
             break;
     }
     if ('subscriptions' == $_POST['upgrade_step'] && 0 === self::get_total_subscription_count_query()) {
         self::upgrade_complete();
     } elseif ('subscription_dates_repair' == $_POST['upgrade_step']) {
         $subscriptions_to_repair = WCS_Repair_2_0_2::get_subscriptions_to_repair(self::$upgrade_limit_subscriptions);
         if (empty($subscriptions_to_repair)) {
             self::upgrade_complete();
         }
     }
     WCS_Upgrade_Logger::add(sprintf('Completed upgrade step: %s', $_POST['upgrade_step']));
     header('Content-Type: application/json; charset=utf-8');
     echo json_encode($results);
     exit;
 }