RESTRICT_CONTENT_ADD_MEMBERSHIP_LEVEL::add_rcp_membership( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the trigger action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)


Source Source

File: src/integrations/restrict-content/actions/restrict-content-add-membership-level.php

	public function add_rcp_membership( $user_id, $action_data, $recipe_id, $args ) {



		$level_id    = absint( $action_data['meta'][ $this->action_meta ] );
		$expiry_date = Automator()->parse->text( $action_data['meta']['RCMEMBERSHIPEXPIRY'], $recipe_id, $user_id, $args );
		// Get all the active membership level IDs.
		$level_ids = rcp_get_membership_levels( [ 'status' => 'active', 'fields' => 'id' ] );
		if ( empty( $level_ids ) ) {
			Automator()->complete_action( $user_id, $action_data, $recipe_id, __( 'You must have at least one active membership level.', 'rcp' ) );

			return;
		}

		$customer     = rcp_get_customer_by_user_id( $user_id );
		$newest_time  = current_time( 'timestamp' );
		$created_date = date( 'Y-m-d H:i:s', $newest_time );
		// Create a new customer record if one does not exist.
		if ( empty( $customer ) ) {
			$customer_id = rcp_add_customer( [
				'user_id'         => absint( $user_id ),
				'date_registered' => $created_date,
			] );
		} else {
			$customer_id = $customer->get_id();
		}

		// Now add the membership.

		/*
		 * For the time always active status.
		 */
		$status          = 'active';
		$membership_args = [
			'customer_id'      => absint( $customer_id ),
			'user_id'          => $user_id,
			'object_id'        => ! empty( $level_id ) ? $level_id : $level_ids[ array_rand( $level_ids ) ],
			// specified or random membership level ID
			'status'           => $status,
			'created_date'     => $created_date,
			'gateway'          => 'manual',
			'subscription_key' => rcp_generate_subscription_key(),
		];
		if ( ! empty( $expiry_date ) ) {
			$membership_args['expiration_date'] = date( 'Y-m-d H:i:s', strtotime( $expiry_date ) );
		}

		$membership_id = rcp_add_membership( $membership_args );

		// Add membership meta to designate this as a generated record so we can deleted it later.
		rcp_add_membership_meta( $membership_id, 'rcp_generated_via_UA', $recipe_id );

		$membership = rcp_get_membership( $membership_id );

		// Generate a transaction ID.
		$auth_key       = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
		$transaction_id = strtolower( md5( $membership_args['subscription_key'] . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'rcp', true ) ) );

		// Create a corresponding payment record.
		$payment_args = array(
			'subscription'     => rcp_get_subscription_name( $membership_args['object_id'] ),
			'object_id'        => $membership_args['object_id'],
			'date'             => $membership_args['created_date'],
			'amount'           => $membership->get_initial_amount(),
			'subtotal'         => $membership->get_initial_amount(),
			'user_id'          => $user_id,
			'subscription_key' => $membership_args['subscription_key'],
			'transaction_id'   => $transaction_id,
			'status'           => 'pending' == $membership_args['status'] ? 'pending' : 'complete',
			'gateway'          => 'manual',
			'customer_id'      => $customer_id,
			'membership_id'    => $membership_id,
		);

		$rcp_payments = new \RCP_Payments();
		$payment_id   = $rcp_payments->insert( $payment_args );

		// Add payment meta to designate this as a generated record so we can delete it later.
		$rcp_payments->add_meta( $payment_id, 'rcp_generated_via_UA', $recipe_id );

		Automator()->complete_action( $user_id, $action_data, $recipe_id );
	}