MAILPOET_ADDUSER_A::mailpoet_add_user_to_list( string $user_id, array $action_data, string $recipe_id,  $args )

Validation function when the action is hit.


Parameters Parameters

$user_id

(string) (Required) user id.

$action_data

(array) (Required) action data.

$recipe_id

(string) (Required) recipe id.


Source Source

File: src/integrations/mailpoet/actions/mailpoet-adduser-a.php

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


		if ( ! class_exists( '\MailPoet\API\API' ) ) {
			$error_message = 'The class \MailPoet\API\API does not exist';
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

		$list_id = $action_data['meta'][ $this->action_meta ];

		// add user to a list.
		$mailpoet = \MailPoet\API\API::MP( 'v1' );

		$userdata = get_userdata( $user_id );

		$subscriber = $mailpoet->getSubscriber( $userdata->user_email );
		$disable_confirmation_email = true;
		if( isset( $action_data['meta']['USERADDEDTOLIST_CONFIRMATIONEMAIL'] ) ) {
			$disable_confirmation_email = Automator()->parse->text( $action_data['meta']['USERADDEDTOLIST_CONFIRMATIONEMAIL'], $recipe_id, $user_id, $args );
			$disable_confirmation_email = "true" === $disable_confirmation_email ? false : true;
		}

		try {
			$mailpoet->subscribeToList( $subscriber['id'], $list_id, [ 'send_confirmation_email' => $disable_confirmation_email ] );
			Automator()->complete_action( $user_id, $action_data, $recipe_id );
		} catch ( \MailPoet\API\MP\v1\APIException $e ) {
			$error_message                       = $e->getMessage();
			$recipe_log_id                       = $action_data['recipe_log_id'];
			$args['do-nothing']                  = true;
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message, $recipe_log_id, $args );
		}
	}