RESTRICT_CONTENT_PURCHASESMEMBERSHIP::user_purchases_membership_level( int $membership_id,  $RCP_Membership )


Parameters Parameters

$membership_id

(int) (Required) ID of the membership.

$membership

(RCP_Membership) (Required) Membership object.


Source Source

File: src/integrations/restrict-content/triggers/restrict-content-purchasesmembership.php

	public function user_purchases_membership_level( $membership_id, \RCP_Membership $RCP_Membership ) {



		$user_id = $RCP_Membership->get_user_id();

		if ( ! $user_id ) {
			return;
		}

		$level_id = $RCP_Membership->get_object_id();

		$recipes            = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
		$required_level     = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
		$matched_recipe_ids = array();

		//Add where Membership Level is set for trigger
		foreach ( $recipes as $recipe_id => $recipe ) {
			foreach ( $recipe['triggers'] as $trigger ) {
				$trigger_id = $trigger['ID'];//return early for all products
				if ( $required_level[ $recipe_id ][ $trigger_id ] === '-1' || $required_level[ $recipe_id ][ $trigger_id ] === $level_id ) {
					$matched_recipe_ids[] = [
						'recipe_id'  => $recipe_id,
						'trigger_id' => $trigger_id,
					];
				}
			}
		}

		if ( ! empty( $matched_recipe_ids ) ) {
			foreach ( $matched_recipe_ids as $matched_recipe_id ) {

				$pass_args = [
					'code'             => $this->trigger_code,
					'meta'             => $this->trigger_meta,
					'user_id'          => $user_id,
					'recipe_to_match'  => $matched_recipe_id['recipe_id'],
					'trigger_to_match' => $matched_recipe_id['trigger_id'],
					'ignore_post_id'   => true,
				];

				$args = Automator()->maybe_add_trigger_entry( $pass_args, false );

				if ( $args ) {
					foreach ( $args as $result ) {
						if ( true === $result['result'] ) {

							// Add token for options
							Automator()->insert_trigger_meta(
								[
									'user_id'        => $user_id,
									'trigger_id'     => $result['args']['trigger_id'],
									'meta_key'       => $this->trigger_meta . '_MEMBERSHIPID ',
									'meta_value'     => $membership_id,
									'trigger_log_id' => $result['args']['get_trigger_id'],
									'run_number'     => $result['args']['run_number'],
								]
							);

							Automator()->maybe_trigger_complete( $result['args'] );
						}
					}
				}
			}
		}

		return;

	}