/**
  * Check if the old subscription meta had an end date recorded and make sure that end date is now being used for the new subscription.
  *
  * In Subscriptions prior to 2.0 a subscription could have both an end date and an expiration date. The end date represented a date in the past
  * on which the subscription expired or was cancelled. The expiration date represented a date on which the subscription was set to expire (this
  * could be in the past or future and could be the same as the end date or different). Because the end date is a definitive even, in this function
  * we first check if it exists before falling back to the expiration date to check against.
  *
  * @param  WC_Subscription $subscription the subscription to check
  * @param  array $former_order_item_meta the order item meta data for the line item on the original order that formerly represented the subscription
  * @return string|bool false if the date does not need to be repaired or the new date if it should be repaired
  */
 protected static function check_end_date($subscription, $former_order_item_meta)
 {
     $new_end_time = $subscription->get_time('end');
     if ($new_end_time > 0) {
         $old_end_date = isset($former_order_item_meta['_wcs_migrated_subscription_end_date'][0]) ? $former_order_item_meta['_wcs_migrated_subscription_end_date'][0] : 0;
         // if the subscription hadn't expired or been cancelled yet, it wouldn't have an end date, but it may still have had an expiry date, so use that instead
         if (0 == $old_end_date) {
             $old_end_date = isset($former_order_item_meta['_wcs_migrated_subscription_expiry_date'][0]) ? $former_order_item_meta['_wcs_migrated_subscription_expiry_date'][0] : 0;
         }
         WCS_Upgrade_Logger::add(sprintf('For subscription %d: new end date = %s.', $subscription->id, var_export($subscription->get_date('end'), true)));
         WCS_Upgrade_Logger::add(sprintf('For subscription %d: old end date = %s.', $subscription->id, var_export($old_end_date, true)));
         // if the subscription has an end time whereas previously it didn't, we need it to be deleted so set it 0
         if (0 == $old_end_date) {
             $repair_date = 0;
         } else {
             $repair_date = false;
         }
     } else {
         $repair_date = false;
     }
     WCS_Upgrade_Logger::add(sprintf('For subscription %d: repair end date = %s.', $subscription->id, var_export($repair_date, true)));
     return $repair_date;
 }
예제 #2
0
/**
 * Return an associative array of a given subscriptions details (if it exists) in the pre v2.0 data structure.
 *
 * @param WC_Subscription $subscription An instance of WC_Subscription
 * @return array Subscription details
 * @since 2.0
 */
function wcs_get_subscription_in_deprecated_structure(WC_Subscription $subscription)
{
    $completed_payments = array();
    if ($subscription->get_completed_payment_count()) {
        if (!empty($subscription->order) && $subscription->order->has_status($subscription->get_paid_order_statuses())) {
            $completed_payments[] = $subscription->order->post->post_date_gmt;
        }
        $paid_renewal_order_ids = get_posts(array('posts_per_page' => -1, 'post_status' => $subscription->get_paid_order_statuses(), 'post_type' => 'shop_order', 'orderby' => 'date', 'order' => 'desc', 'fields' => 'ids', 'meta_query' => array(array('key' => '_subscription_renewal', 'compare' => '=', 'value' => $subscription->id, 'type' => 'numeric'))));
        foreach ($paid_renewal_order_ids as $paid_renewal_order_id) {
            $completed_payments[] = get_post_field('post_date_gmt', $paid_renewal_order_id);
        }
    }
    $items = $subscription->get_items();
    $item = array_pop($items);
    if (!empty($item)) {
        $deprecated_subscription_object = array('order_id' => $subscription->order->id, 'product_id' => isset($item['product_id']) ? $item['product_id'] : 0, 'variation_id' => isset($item['variation_id']) ? $item['variation_id'] : 0, 'status' => $subscription->get_status(), 'period' => $subscription->billing_period, 'interval' => $subscription->billing_interval, 'length' => wcs_estimate_periods_between(0 == $subscription->get_time('trial_end') ? $subscription->get_time('start') : $subscription->get_time('trial_end'), $subscription->get_time('end') + 120, $subscription->billing_period, 'floor') / $subscription->billing_interval, 'start_date' => $subscription->get_date('start'), 'expiry_date' => $subscription->get_date('end'), 'end_date' => $subscription->has_status(wcs_get_subscription_ended_statuses()) ? $subscription->get_date('end') : 0, 'trial_expiry_date' => $subscription->get_date('trial_end'), 'failed_payments' => $subscription->failed_payment_count, 'completed_payments' => $completed_payments, 'suspension_count' => $subscription->suspension_count, 'last_payment_date' => $subscription->get_date('last_payment'));
    } else {
        $deprecated_subscription_object = array();
    }
    return $deprecated_subscription_object;
}