is_active() public method

Determines if a member has an active subscription, or is cancelled but has not reached EOT
Since: 2.1
public is_active ( )
/**
 * Determines if a member can cancel their subscription on site
 *
 * @access      public
 * @since       2.1
 */
function rcp_can_member_cancel( $user_id = 0 ) {

	if( empty( $user_id ) ) {
		$user_id = get_current_user_id();
	}

	$ret    = false;
	$member = new RCP_Member( $user_id );

	if( $member->is_recurring() && $member->is_active() && 'cancelled' !== $member->get_status() ) {

		$profile_id = $member->get_payment_profile_id();

		// Check if the member is a Stripe customer
		if( false !== strpos( $profile_id, 'cus_' ) ) {

			$ret = true;

		} elseif ( rcp_is_paypal_subscriber( $user_id ) && rcp_has_paypal_api_access() ) {

			$ret = true;

		}

	}

	return apply_filters( 'rcp_member_can_cancel', $ret, $user_id );
}
/**
 * Displays stripe checkout form
 *
 * @since 2.5
 * @access public
 *
 * @param $atts
 * @return mixed|void
 */
function rcp_register_form_stripe_checkout($atts)
{
    global $rcp_options;
    if (empty($atts['id'])) {
        return '';
    }
    // button is an alias for data-label
    if (isset($atts['button'])) {
        $atts['data-label'] = $atts['button'];
    }
    $key = rcp_is_sandbox() ? $rcp_options['stripe_test_publishable'] : $rcp_options['stripe_live_publishable'];
    $member = new RCP_Member(wp_get_current_user()->ID);
    $subscription = rcp_get_subscription_details($atts['id']);
    $amount = $subscription->price + $subscription->fee;
    if ($member->ID > 0) {
        $amount -= $member->get_prorate_credit_amount();
    }
    if ($amount < 0) {
        $amount = 0;
    }
    $data = wp_parse_args($atts, array('id' => 0, 'data-key' => $key, 'data-name' => get_option('blogname'), 'data-description' => $subscription->description, 'data-label' => sprintf(__('Join %s', 'rcp'), $subscription->name), 'data-panel-label' => __('Register - {{amount}}', 'rcp'), 'data-amount' => $amount * rcp_stripe_get_currency_multiplier(), 'data-locale' => 'auto', 'data-allow-remember-me' => true, 'data-currency' => rcp_get_currency(), 'data-alipay' => isset($rcp_options['stripe_alipay']) && '1' === $rcp_options['stripe_alipay'] && 'USD' === rcp_get_currency() ? 'true' : 'false'));
    if (empty($data['data-email']) && !empty($member->user_email)) {
        $data['data-email'] = $member->user_email;
    }
    if (empty($data['data-image']) && ($image = get_site_icon_url())) {
        $data['data-image'] = $image;
    }
    $data = apply_filters('rcp_stripe_checkout_data', $data);
    if ('USD' !== rcp_get_currency()) {
        unset($data['data-alipay']);
    }
    ob_start();
    if ($member->ID > 0 && $member->get_subscription_id() == $subscription->id && $member->is_active()) {
        ?>

		<div class="rcp-stripe-checkout-notice"><?php 
        _e('You are already subscribed.', 'rcp');
        ?>
</div>

	<?php 
    } else {
        ?>
		<form action="" method="post">
			<?php 
        do_action('register_form_stripe_fields', $data);
        ?>
			<script src="https://checkout.stripe.com/checkout.js" class="stripe-button" <?php 
        foreach ($data as $label => $value) {
            printf(' %s="%s" ', esc_attr($label), esc_attr($value));
        }
        ?>
 ></script>
			<input type="hidden" name="rcp_level" value="<?php 
        echo $subscription->id;
        ?>
" />
			<input type="hidden" name="rcp_register_nonce" value="<?php 
        echo wp_create_nonce('rcp-register-nonce');
        ?>
"/>
			<input type="hidden" name="rcp_gateway" value="stripe_checkout"/>
			<input type="hidden" name="rcp_stripe_checkout" value="1"/>
		</form>
	<?php 
    }
    return apply_filters('register_form_stripe', ob_get_clean(), $atts);
}
/**
 * Determines if a member can cancel their subscription on site
 *
 * @access      public
 * @since       2.1
 */
function rcp_can_member_cancel($user_id = 0)
{
    if (empty($user_id)) {
        $user_id = get_current_user_id();
    }
    $ret = false;
    $member = new RCP_Member($user_id);
    if ($member->is_recurring() && $member->is_active() && 'cancelled' !== $member->get_status()) {
        $profile_id = $member->get_payment_profile_id();
        // Check if the member is a Stripe customer
        if (rcp_is_stripe_subscriber($user_id)) {
            $ret = true;
        } elseif (rcp_is_paypal_subscriber($user_id) && rcp_has_paypal_api_access()) {
            $ret = true;
        } elseif (rcp_is_2checkout_subscriber($user_id) && defined('TWOCHECKOUT_ADMIN_USER') && defined('TWOCHECKOUT_ADMIN_PASSWORD')) {
            $ret = true;
        }
    }
    return apply_filters('rcp_member_can_cancel', $ret, $user_id);
}
/**
 * Register a new user
 *
 * @access      public
 * @since       1.0
 */
function rcp_process_registration()
{
    // check nonce
    if (!(isset($_POST["rcp_register_nonce"]) && wp_verify_nonce($_POST['rcp_register_nonce'], 'rcp-register-nonce'))) {
        return;
    }
    global $rcp_options, $rcp_levels_db;
    $subscription_id = rcp_get_registration()->get_subscription();
    $discount = isset($_POST['rcp_discount']) ? sanitize_text_field($_POST['rcp_discount']) : '';
    $price = number_format((double) $rcp_levels_db->get_level_field($subscription_id, 'price'), 2);
    $price = str_replace(',', '', $price);
    $subscription = $rcp_levels_db->get_level($subscription_id);
    $auto_renew = rcp_registration_is_recurring();
    // if both today's total and the recurring total are 0, the there is a full discount
    // if this is not a recurring subscription only check today's total
    $full_discount = $auto_renew ? rcp_get_registration()->get_total() == 0 && rcp_get_registration()->get_recurring_total() == 0 : rcp_get_registration()->get_total() == 0;
    // get the selected payment method/gateway
    if (!isset($_POST['rcp_gateway'])) {
        $gateway = 'paypal';
    } else {
        $gateway = sanitize_text_field($_POST['rcp_gateway']);
    }
    /***********************
     * validate the form
     ***********************/
    do_action('rcp_before_form_errors', $_POST);
    $is_ajax = isset($_POST['rcp_ajax']);
    $user_data = rcp_validate_user_data();
    if (!rcp_is_registration()) {
        // no subscription level was chosen
        rcp_errors()->add('no_level', __('Please choose a subscription level', 'rcp'), 'register');
    }
    if ($subscription_id && $price == 0 && $subscription->duration > 0 && rcp_has_used_trial($user_data['id'])) {
        // this ensures that users only sign up for a free trial once
        rcp_errors()->add('free_trial_used', __('You may only sign up for a free trial once', 'rcp'), 'register');
    }
    if (!empty($discount)) {
        // make sure we have a valid discount
        if (rcp_validate_discount($discount, $subscription_id)) {
            // check if the user has already used this discount
            if ($price > 0 && !$user_data['need_new'] && rcp_user_has_used_discount($user_data['id'], $discount) && apply_filters('rcp_discounts_once_per_user', false)) {
                rcp_errors()->add('discount_already_used', __('You can only use the discount code once', 'rcp'), 'register');
            }
        } else {
            // the entered discount code is incorrect
            rcp_errors()->add('invalid_discount', __('The discount you entered is invalid', 'rcp'), 'register');
        }
    }
    // Validate extra fields in gateways with the 2.1+ gateway API
    if (!has_action('rcp_gateway_' . $gateway) && $price > 0 && !$full_discount) {
        $gateways = new RCP_Payment_Gateways();
        $gateway_var = $gateways->get_gateway($gateway);
        $gateway_obj = new $gateway_var['class']();
        $gateway_obj->validate_fields();
    }
    do_action('rcp_form_errors', $_POST);
    // retrieve all error messages, if any
    $errors = rcp_errors()->get_error_messages();
    if (!empty($errors) && $is_ajax) {
        wp_send_json_error(array('success' => false, 'errors' => rcp_get_error_messages_html('register'), 'nonce' => wp_create_nonce('rcp-register-nonce')));
    } elseif ($is_ajax) {
        wp_send_json_success(array('success' => true));
    }
    // only create the user if there are no errors
    if (!empty($errors)) {
        return;
    }
    if ($user_data['need_new']) {
        $user_data['id'] = wp_insert_user(array('user_login' => $user_data['login'], 'user_pass' => $user_data['password'], 'user_email' => $user_data['email'], 'first_name' => $user_data['first_name'], 'last_name' => $user_data['last_name'], 'display_name' => $user_data['first_name'] . ' ' . $user_data['last_name'], 'user_registered' => date('Y-m-d H:i:s')));
    }
    if (empty($user_data['id'])) {
        return;
    }
    // Setup the member object
    $member = new RCP_Member($user_data['id']);
    update_user_meta($user_data['id'], '_rcp_new_subscription', '1');
    $subscription_key = rcp_generate_subscription_key();
    $old_subscription_id = $member->get_subscription_id();
    if ($old_subscription_id) {
        update_user_meta($user_data['id'], '_rcp_old_subscription_id', $old_subscription_id);
    }
    if (!$member->is_active()) {
        update_user_meta($user_data['id'], 'rcp_subscription_level', $subscription_id);
        update_user_meta($user_data['id'], 'rcp_subscription_key', $subscription_key);
        // Ensure no pending level details are set
        delete_user_meta($user_data['id'], 'rcp_pending_subscription_level');
        delete_user_meta($user_data['id'], 'rcp_pending_subscription_key');
        $member->set_status('pending');
    } else {
        // If the member is already active, we need to set these as pending changes
        update_user_meta($user_data['id'], 'rcp_pending_subscription_level', $subscription_id);
        update_user_meta($user_data['id'], 'rcp_pending_subscription_key', $subscription_key);
        // Flag the member as having just upgraded
        update_user_meta($user_data['id'], '_rcp_just_upgraded', current_time('timestamp'));
    }
    $member->set_joined_date('', $subscription_id);
    // Calculate the expiration date for the member
    $member_expires = $member->calculate_expiration($auto_renew);
    update_user_meta($user_data['id'], 'rcp_pending_expiration_date', $member_expires);
    // remove the user's old role, if this is a new user, we need to replace the default role
    $old_role = get_option('default_role', 'subscriber');
    if ($old_subscription_id) {
        $old_level = $rcp_levels_db->get_level($old_subscription_id);
        $old_role = !empty($old_level->role) ? $old_level->role : $old_role;
    }
    $member->remove_role($old_role);
    // Set the user's role
    $role = !empty($subscription->role) ? $subscription->role : 'subscriber';
    $user = new WP_User($user_data['id']);
    $user->add_role(apply_filters('rcp_default_user_level', $role, $subscription_id));
    do_action('rcp_form_processing', $_POST, $user_data['id'], $price);
    // process a paid subscription
    if ($price > '0') {
        if (!empty($discount)) {
            $discounts = new RCP_Discounts();
            $discount_obj = $discounts->get_by('code', $discount);
            // record the usage of this discount code
            $discounts->add_to_user($user_data['id'], $discount);
            // increase the usage count for the code
            $discounts->increase_uses($discount_obj->id);
            // if the discount is 100%, log the user in and redirect to success page
            if ($full_discount) {
                $member->set_expiration_date($member_expires);
                $member->set_status('active');
                rcp_login_user_in($user_data['id'], $user_data['login']);
                wp_redirect(rcp_get_return_url($user_data['id']));
                exit;
            }
        }
        // Remove trialing status, if it exists
        delete_user_meta($user_data['id'], 'rcp_is_trialing');
        // log the new user in
        rcp_login_user_in($user_data['id'], $user_data['login']);
        $redirect = rcp_get_return_url($user_data['id']);
        $subscription_data = array('price' => rcp_get_registration()->get_total(true, false), 'discount' => rcp_get_registration()->get_total_discounts(), 'discount_code' => $discount, 'fee' => rcp_get_registration()->get_total_fees(), 'length' => $subscription->duration, 'length_unit' => strtolower($subscription->duration_unit), 'subscription_id' => $subscription->id, 'subscription_name' => $subscription->name, 'key' => $subscription_key, 'user_id' => $user_data['id'], 'user_name' => $user_data['login'], 'user_email' => $user_data['email'], 'currency' => $rcp_options['currency'], 'auto_renew' => $auto_renew, 'return_url' => $redirect, 'new_user' => $user_data['need_new'], 'post_data' => $_POST);
        // if giving the user a credit, make sure the credit does not exceed the first payment
        if ($subscription_data['fee'] < 0 && abs($subscription_data['fee']) > $subscription_data['price']) {
            $subscription_data['fee'] = -1 * $subscription_data['price'];
        }
        update_user_meta($user_data['id'], 'rcp_pending_subscription_amount', $subscription_data['price'] + $subscription_data['fee']);
        // send all of the subscription data off for processing by the gateway
        rcp_send_to_gateway($gateway, apply_filters('rcp_subscription_data', $subscription_data));
        // process a free or trial subscription
    } else {
        // This is a free user registration or trial
        $member->set_expiration_date($member_expires);
        // if the subscription is a free trial, we need to record it in the user meta
        if ($member_expires != 'none') {
            // activate the user's trial subscription
            $member->set_status('active');
            // this is so that users can only sign up for one trial
            update_user_meta($user_data['id'], 'rcp_has_trialed', 'yes');
            update_user_meta($user_data['id'], 'rcp_is_trialing', 'yes');
            rcp_email_subscription_status($user_data['id'], 'trial');
        } else {
            update_user_meta($user_data['id'], 'rcp_subscription_level', $subscription_id);
            update_user_meta($user_data['id'], 'rcp_subscription_key', $subscription_key);
            // Ensure no pending level details are set
            delete_user_meta($user_data['id'], 'rcp_pending_subscription_level');
            delete_user_meta($user_data['id'], 'rcp_pending_subscription_key');
            // set the user's status to free
            $member->set_status('free');
            rcp_email_subscription_status($user_data['id'], 'free');
        }
        if ($user_data['need_new']) {
            if (!isset($rcp_options['disable_new_user_notices'])) {
                // send an email to the admin alerting them of the registration
                wp_new_user_notification($user_data['id']);
            }
            // log the new user in
            rcp_login_user_in($user_data['id'], $user_data['login']);
        }
        // send the newly created user to the redirect page after logging them in
        wp_redirect(rcp_get_return_url($user_data['id']));
        exit;
    }
    // end price check
}