PMP_MEMBERSHIPCANCEL::pmpro_subscription_cancelled( $level_id,  $user_id,  $cancel_level )

Validation function when the trigger action is hit


Parameters Parameters

$level_id

(Required)

$user_id

(Required)

$cancel_level

(Required)


Source Source

File: src/integrations/paid-memberships-pro/triggers/pmp-membershipcancel.php

	public function pmpro_subscription_cancelled( $level_id, $user_id, $cancel_level ) {

		if ( 0 !== absint( $level_id ) ) {
			return;
		}

		if ( 0 === absint( $level_id ) && is_numeric( $cancel_level ) ) {
			$recipes             = Automator()->get->recipes_from_trigger_code( $this->trigger_code );
			$required_memerbship = Automator()->get->meta_from_recipes( $recipes, $this->trigger_meta );
			$matched_recipe_ids  = array();

			//Add where option is set to Any membership
			foreach ( $recipes as $recipe_id => $recipe ) {
				foreach ( $recipe['triggers'] as $trigger ) {
					$trigger_id = $trigger['ID'];//return early for all memberships
					if ( - 1 === intval( $required_memerbship[ $recipe_id ][ $trigger_id ] ) ) {
						$matched_recipe_ids[] = [
							'recipe_id'  => $recipe_id,
							'trigger_id' => $trigger_id,
						];

						break;
					}
				}
			}

			//Add where Membership ID is set for trigger
			foreach ( $recipes as $recipe_id => $recipe ) {
				foreach ( $recipe['triggers'] as $trigger ) {
					$trigger_id = $trigger['ID'];//return early for all memberships
					if ( $required_memerbship[ $recipe_id ][ $trigger_id ] == $cancel_level ) {
						$matched_recipe_ids[] = [
							'recipe_id'  => $recipe_id,
							'trigger_id' => $trigger_id,
						];
					}
				}
			}

			if ( ! empty( $matched_recipe_ids ) ) {
				foreach ( $matched_recipe_ids as $matched_recipe_id ) {
					$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,
					];
					$result = Automator()->maybe_add_trigger_entry( $args, false );

					if ( $result ) {
						foreach ( $result as $r ) {
							if ( true === $r['result'] ) {
								do_action( 'uap_save_pmp_membership_level', $cancel_level, $r['args'], $user_id, $this->trigger_meta );
								Automator()->maybe_trigger_complete( $r['args'] );
							}
						}
					}
				}
			}
		}

		return;
	}