/**
 * Return an instance of a WC_Subscription object for the given subscription key (if one exists).
 *
 * @param string $subscription_key A subscription key in the deprecated form created by @see self::get_subscription_key()
 * @return WC_Subscription|null The subscription object if it can be found (i.e. an order exists) or null if no order exists for the subscription (i.e. it was manually created).
 * @since 2.0
 */
function wcs_get_subscription_from_key($subscription_key)
{
    $subscription_id = wcs_get_subscription_id_from_key($subscription_key);
    if (null !== $subscription_id && is_numeric($subscription_id)) {
        $subscription = wcs_get_subscription($subscription_id);
    }
    if (!is_object($subscription)) {
        // translators: placeholder is either subscription key or a subscription id, or, failing that, empty (e.g. "145_21" or "145")
        throw new InvalidArgumentException(sprintf(__('Could not get subscription. Most likely the subscription key does not refer to a subscription. The key was: "%s".', 'woocommerce-subscriptions'), $subscription_key));
    }
    return $subscription;
}
 /**
  * When a scheduled subscription payment hook is fired, automatically process the subscription payment
  * if the amount is for $0 (and therefore, there is no payment to be processed by a gateway, and likely
  * no gateway used on the initial order).
  *
  * If a subscription has a $0 recurring total and is not already active (after being actived by something else
  * handling the 'scheduled_subscription_payment' with the default priority of 10), then this function will call
  * @see self::process_subscription_payment() to reactive the subscription, generate a renewal order etc.
  *
  * @param int $user_id The id of the user who the subscription belongs to
  * @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
  * @since 1.3.2
  * @deprecated 2.0
  */
 public static function maybe_process_subscription_payment($user_id, $subscription_key)
 {
     _deprecated_function(__METHOD__, '2.0', __CLASS__ . '::prepare_renewal( $subscription_id )');
     self::prepare_renewal(wcs_get_subscription_id_from_key($subscription_key));
 }
 /**
  * Handle subscriptions upgrade
  *
  * This method runs on each admin page load and checks the current
  * Subscriptions version against our record of Subscriptions version.
  * We can't rely on the `woocommerce_subscriptions_upgraded` hook because
  * that cannot be caught when Memberships is deactivated during upgrade.
  *
  * This solution catches all upgrades, even if they were done while Memberships
  * was not active.
  *
  * @since 1.0.0
  */
 public function handle_upgrade()
 {
     $subscriptions_version = get_option('wc_memberships_subscriptions_version');
     // Versions match, we don't need to do anything
     if ($subscriptions_version == WC_Subscriptions::$version) {
         return;
     }
     // Upgrade routine from Subscriptions pre-2.0 to 2.0
     if (version_compare(WC_Subscriptions::$version, '2.0', '>=') && version_compare($subscriptions_version, '2.0', '<')) {
         global $wpdb;
         // Upgrade user memberships to use Subscription IDs instead of keys.
         $results = $wpdb->get_results("\n\t\t\t\tSELECT pm.post_id as ID, pm.meta_value as subscription_key\n\t\t\t\tFROM {$wpdb->postmeta} pm\n\t\t\t\tLEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id\n\t\t\t\tWHERE pm.meta_key = '_subscription_key'\n\t\t\t\tAND p.post_type = 'wc_user_membership'\n\t\t\t");
         // Bail out if there are no memberships with subscription keys
         if (empty($results)) {
             return;
         }
         foreach ($results as $result) {
             $subscription_id = wcs_get_subscription_id_from_key($result->subscription_key);
             if ($subscription_key) {
                 update_post_meta($result->ID, '_subscription_id', $subscription_id);
                 delete_post_meta($result->ID, '_subscription_key');
             }
         }
     }
     // Update our record of Subscriptions version
     update_option('wc_memberships_subscriptions_version', WC_Subscriptions::$version);
 }
 /**
  * Obtain subscription by subscriptions < 2.x subscription key without
  * use of deprecated methods when using subscriptions >= 2.x
  * 
  * @param string $subscription_key
  * @return array subscription
  */
 private static function get_subscription_by_subscription_key($subscription_key)
 {
     $subscription = array();
     if (function_exists('wcs_get_subscription_from_key') && function_exists('wcs_get_subscription_in_deprecated_structure')) {
         try {
             $subscription_id = wcs_get_subscription_id_from_key($subscription_key);
             if (null !== $subscription_id && is_numeric($subscription_id)) {
                 if ($subscription = wcs_get_subscription_from_key($subscription_key)) {
                     $subscription = wcs_get_subscription_in_deprecated_structure($subscription);
                 }
             }
         } catch (Exception $e) {
             $subscription = array();
         }
     } else {
         $subscription = WC_Subscriptions_Manager::get_subscription($subscription_key);
     }
     return $subscription;
 }