/**
  * Change the status of a subscription and show a notice to the user if there was an issue.
  *
  * @since 2.0
  */
 public static function change_users_subscription($subscription, $new_status)
 {
     $subscription = !is_object($subscription) ? wcs_get_subscription($subscription) : $subscription;
     switch ($new_status) {
         case 'active':
             if ($subscription->needs_payment()) {
                 WC_Subscriptions::add_notice(sprintf(__('You can not reactivate that subscription until paying to renew it. Please contact us if you need assistance.', 'woocommerce-subscriptions'), $new_status), 'error');
             } else {
                 $subscription->update_status($new_status);
                 $status_message = __('reactivated', 'woocommerce-subscriptions');
             }
             break;
         case 'on-hold':
             if (wcs_can_user_put_subscription_on_hold($subscription)) {
                 $subscription->update_status($new_status);
                 $status_message = __('put on hold', 'woocommerce-subscriptions');
             } else {
                 WC_Subscriptions::add_notice(__('You can not suspend that subscription - the suspension limit has been reached. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error');
             }
             break;
         case 'cancelled':
             $subscription->cancel_order();
             $status_message = __('cancelled', 'woocommerce-subscriptions');
             break;
     }
     if (isset($status_message)) {
         // translators: placeholder is status (e.g. "reactivated", "cancelled")
         $subscription->add_order_note(sprintf(__('Subscription %s by the subscriber from their account page.', 'woocommerce-subscriptions'), $status_message));
         // translators: placeholder is status (e.g. "reactivated", "cancelled")
         WC_Subscriptions::add_notice(sprintf(__('Your subscription has been %s.', 'woocommerce-subscriptions'), $status_message), 'success');
         do_action('woocommerce_customer_changed_subscription_to_' . $new_status, $subscription);
     }
 }
 /**
  * Change the status of a subscription and show a notice to the user if there was an issue.
  *
  * @since 2.0
  */
 public static function change_users_subscription($subscription, $new_status)
 {
     $subscription = !is_object($subscription) ? wcs_get_subscription($subscription) : $subscription;
     $changed = false;
     switch ($new_status) {
         case 'active':
             if (!$subscription->needs_payment()) {
                 $subscription->update_status($new_status);
                 $subscription->add_order_note(_x('Subscription reactivated by the subscriber from their account page.', 'order note left on subscription after user action', 'woocommerce-subscriptions'));
                 WC_Subscriptions::add_notice(_x('Your subscription has been reactivated.', 'Notice displayed to user confirming their action.', 'woocommerce-subscriptions'), 'success');
                 $changed = true;
             } else {
                 WC_Subscriptions::add_notice(__('You can not reactivate that subscription until paying to renew it. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error');
             }
             break;
         case 'on-hold':
             if (wcs_can_user_put_subscription_on_hold($subscription)) {
                 $subscription->update_status($new_status);
                 $subscription->add_order_note(_x('Subscription put on hold by the subscriber from their account page.', 'order note left on subscription after user action', 'woocommerce-subscriptions'));
                 WC_Subscriptions::add_notice(_x('Your subscription has been put on hold.', 'Notice displayed to user confirming their action.', 'woocommerce-subscriptions'), 'success');
                 $changed = true;
             } else {
                 WC_Subscriptions::add_notice(__('You can not suspend that subscription - the suspension limit has been reached. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error');
             }
             break;
         case 'cancelled':
             $subscription->cancel_order();
             $subscription->add_order_note(_x('Subscription cancelled by the subscriber from their account page.', 'order note left on subscription after user action', 'woocommerce-subscriptions'));
             WC_Subscriptions::add_notice(_x('Your subscription has been cancelled.', 'Notice displayed to user confirming their action.', 'woocommerce-subscriptions'), 'success');
             $changed = true;
             break;
     }
     if ($changed) {
         do_action('woocommerce_customer_changed_subscription_to_' . $new_status, $subscription);
     }
 }
 /**
  * Return a link for subscribers to change the status of their subscription, as specified with $status parameter
  *
  * @param string $subscription_key A subscription key of the form created by @see self::get_subscription_key()
  * @since 1.0
  */
 public static function current_user_can_suspend_subscription($subscription_key)
 {
     _deprecated_function(__METHOD__, '2.0', 'wcs_can_user_put_subscription_on_hold( $subscription, $user )');
     return wcs_can_user_put_subscription_on_hold(wcs_get_subscription_from_key($subscription_key));
 }
/**
 * Retrieve available actions that a user can perform on the subscription
 *
 * @since 2.0
 */
function wcs_get_all_user_actions_for_subscription($subscription, $user_id)
{
    $actions = array();
    if (user_can($user_id, 'edit_shop_subscription_status', $subscription->id)) {
        $admin_with_suspension_disallowed = current_user_can('manage_woocommerce') && '0' === get_option(WC_Subscriptions_Admin::$option_prefix . '_max_customer_suspensions', '0') ? true : false;
        $current_status = $subscription->get_status();
        if ($subscription->can_be_updated_to('on-hold') && wcs_can_user_put_subscription_on_hold($subscription, $user_id) && !$admin_with_suspension_disallowed) {
            $actions['suspend'] = array('url' => wcs_get_users_change_status_link($subscription->id, 'on-hold', $current_status), 'name' => __('Suspend', 'woocommerce-subscriptions'));
        } elseif ($subscription->can_be_updated_to('active') && !$subscription->needs_payment()) {
            $actions['reactivate'] = array('url' => wcs_get_users_change_status_link($subscription->id, 'active', $current_status), 'name' => __('Reactivate', 'woocommerce-subscriptions'));
        }
        if (wcs_can_user_resubscribe_to($subscription, $user_id)) {
            $actions['resubscribe'] = array('url' => wcs_get_users_resubscribe_link($subscription), 'name' => __('Resubscribe', 'woocommerce-subscriptions'));
        }
        // Show button for subscriptions which can be cancelled and which may actually require cancellation (i.e. has a future payment)
        $next_payment = $subscription->get_time('next_payment');
        if ($subscription->can_be_updated_to('cancelled') && !$subscription->is_one_payment() && ($next_payment > 0 || $subscription->has_status('on-hold') && empty($next_payment))) {
            $actions['cancel'] = array('url' => wcs_get_users_change_status_link($subscription->id, 'cancelled', $current_status), 'name' => _x('Cancel', 'an action on a subscription', 'woocommerce-subscriptions'));
        }
    }
    return apply_filters('wcs_view_subscription_actions', $actions, $subscription);
}