/**
  * Hooks upgrade function to init.
  * 
  * @since 1.2
  */
 public static function init()
 {
     self::$active_version = get_option(WC_Subscriptions_Admin::$option_prefix . '_active_version', '0');
     if (isset($_GET['wcs_upgrade_step']) || version_compare(self::$active_version, WC_Subscriptions::$version, '<') && 'true' != get_transient('wc_subscriptions_is_upgrading')) {
         // If an admin hits the front of the site, run the updates, otherwise, run it as soon as someone hits the backend
         if (current_user_can('update_plugins')) {
             add_action('init', __CLASS__ . '::upgrade', 1);
         } else {
             add_action('admin_init', __CLASS__ . '::upgrade', 1);
         }
     }
 }
 /**
  * Hooks upgrade function to init.
  *
  * @since 1.2
  */
 public static function init()
 {
     self::$active_version = get_option(WC_Subscriptions_Admin::$option_prefix . '_active_version', '0');
     if (isset($_GET['wcs_upgrade_step']) || version_compare(self::$active_version, WC_Subscriptions::$version, '<') && 'true' != get_transient('wc_subscriptions_is_upgrading')) {
         // If an admin hits the front of the site, run the updates, otherwise, run it as soon as someone hits the backend
         if (current_user_can('update_plugins')) {
             add_action('init', __CLASS__ . '::upgrade', 1);
         } else {
             add_action('admin_init', __CLASS__ . '::upgrade', 1);
         }
     }
     // Maybe upgrade to WC 2.0 data structure (after WC has run it's upgrader)
     self::$is_wc_version_2 = version_compare(get_option('woocommerce_db_version'), '2.0', '>=');
     self::$updated_to_wc_2_0 = get_option('wcs_updated_to_wc_2_0', 'false');
     if (self::$is_wc_version_2 && 'true' != self::$updated_to_wc_2_0) {
         if (current_user_can('update_plugins')) {
             add_action('init', __CLASS__ . '::upgrade_to_latest_wc', 2);
         } else {
             add_action('admin_init', __CLASS__ . '::upgrade_to_latest_wc', 2);
         }
     }
 }
 /**
  * Gets an array of subscriptions from the v1.5 database structure and returns them in the in the v1.5 structure of
  * 'order_item_id' => subscription details array().
  *
  * The subscription will be orders from oldest to newest, which is important because self::migrate_resubscribe_orders()
  * method expects a subscription to exist in order to migrate the resubscribe meta data correctly.
  *
  * @param int $batch_size The number of subscriptions to return.
  * @return array Subscription details in the v1.5 structure of 'order_item_id' => array()
  * @since 2.0
  */
 private static function get_subscriptions($batch_size)
 {
     global $wpdb;
     $query = WC_Subscriptions_Upgrader::get_subscription_query($batch_size);
     $wpdb->query('SET SQL_BIG_SELECTS = 1;');
     $raw_subscriptions = $wpdb->get_results($query);
     $subscriptions = array();
     // Create a backward compatible structure
     foreach ($raw_subscriptions as $raw_subscription) {
         if (!isset($raw_subscription->order_item_id)) {
             continue;
         }
         if (!array_key_exists($raw_subscription->order_item_id, $subscriptions)) {
             $subscriptions[$raw_subscription->order_item_id] = array('order_id' => $raw_subscription->order_id, 'name' => $raw_subscription->order_item_name);
             $subscriptions[$raw_subscription->order_item_id]['user_id'] = (int) get_post_meta($raw_subscription->order_id, '_customer_user', true);
         }
         $meta_key = str_replace('_subscription', '', $raw_subscription->meta_key);
         $meta_key = substr($meta_key, 0, 1) == '_' ? substr($meta_key, 1) : $meta_key;
         if ('product_id' === $meta_key) {
             $subscriptions[$raw_subscription->order_item_id]['subscription_key'] = $subscriptions[$raw_subscription->order_item_id]['order_id'] . '_' . $raw_subscription->meta_value;
         }
         $subscriptions[$raw_subscription->order_item_id][$meta_key] = maybe_unserialize($raw_subscription->meta_value);
     }
     return $subscriptions;
 }
 /**
  * Used to check if a user ID is greater than the last user upgraded to version 1.4.
  *
  * Needs to be a separate function so that it can use a static variable (and therefore avoid calling get_option() thousands
  * of times when iterating over thousands of users).
  *
  * @since 1.4
  */
 public static function is_user_upgraded_to_1_4($user_id)
 {
     if (false === self::$last_upgraded_user_id) {
         self::$last_upgraded_user_id = get_option("wcs_1_4_last_upgraded_user_id", 0);
     }
     return $user_id > self::$last_upgraded_user_id ? true : false;
 }
 /**
  * In v2.0 and newer, it's possible to simply use wp_count_posts( 'shop_subscription' ) to count subscriptions,
  * but not in v1.5, because a subscription data is still stored in order item meta. This function queries the
  * v1.5 database structure.
  *
  * @since 2.0
  */
 private static function get_total_subscription_count($initial = false)
 {
     if ($initial) {
         $subscription_count = get_option('wcs_upgrade_initial_total_subscription_count', false);
         if (false === $subscription_count) {
             $subscription_count = self::get_total_subscription_count();
             update_option('wcs_upgrade_initial_total_subscription_count', $subscription_count);
         }
     } else {
         if (null === self::$old_subscription_count) {
             self::$old_subscription_count = self::get_total_subscription_count_query();
         }
         $subscription_count = self::$old_subscription_count;
     }
     return $subscription_count;
 }