FCRM_USER_TO_LIST::list_to_user( $user_id,  $action_data,  $recipe_id )

Validation function when the trigger action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)


Source Source

File: src/integrations/fluent-crm/actions/fcrm-user-to-list.php

	public function list_to_user( $user_id, $action_data, $recipe_id ) {



		$lists     = array_map( 'intval', json_decode( $action_data['meta'][ $this->action_meta ] ) );
		$user_info = get_userdata( $user_id );

		if ( $user_info ) {
			$subscriber = Subscriber::where( 'email', $user_info->user_email )->first();

			if ( $subscriber ) {

				$existingLists   = $subscriber->lists;
				$existingListIds = array();
				foreach ( $existingLists as $list ) {
					if ( in_array( $list->id, $lists ) ) {
						$existingListIds[] = $list->title;
					}
				}

				$subscriber->attachLists( $lists );

				if ( empty( $existingListIds ) ) {
					Automator()->complete_action( $user_id, $action_data, $recipe_id );

					return;
				} else {

					if ( count( $existingListIds ) === count( $lists ) ) {
						// ALL lists were already assigned
						$action_data['do-nothing']           = true;
						$action_data['complete_with_errors'] = true;

						$message = sprintf(
						/* translators: 1. List of lists the user is in. */
							_x( 'User was already a member of %1$s', 'FluentCRM', 'uncanny-automator' ),
							implode(
							/* translators: Character to separate items */
								__( ',', 'uncanny-automator' ) . ' ',
								$existingListIds
							)
						);

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

						return;
					}

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

					return;


				}
			} else {
				// User is not a contact
				$args['do-nothing']                  = true;
				$action_data['do-nothing']           = true;
				$action_data['complete_with_errors'] = true;

				$message = sprintf(
				/* translators: 1. The user email */
					_x( 'User is not a contact: %1$s', 'FluentCRM', 'uncanny-automator' ),
					$user_info->user_email
				);

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

				return;
			}
		} else {
			// User does not exist
			$args['do-nothing']                  = true;
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;

			$message = sprintf(
			/* translators: 1. The user id */
				_x( 'User does not exist: %1$s', 'FluentCRM', 'uncanny-automator' ),
				$user_id
			);

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

			return;
		}
	}