MAILPOET_ADDSUBSCRIBERTOLIST_A::mailpoet_add_subscriber_to_list( string $user_id, array $action_data, string $recipe_id, array $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.

$args

(array) (Required) arguments.


Source Source

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

	public function mailpoet_add_subscriber_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 subscriber to a list.
		$mailpoet = \MailPoet\API\API::MP( 'v1' );

		if ( isset( $action_data['meta']['ADDSUBSCRIBER'] ) && ! empty( $action_data['meta']['ADDSUBSCRIBER'] ) ) {
			$subscriber['email'] = Automator()->parse->text( $action_data['meta']['ADDSUBSCRIBER'], $recipe_id, $user_id, $args );
		}

		if ( isset( $action_data['meta']['ADDSUBSCRIBER_FIRSTNAME'] ) && ! empty( $action_data['meta']['ADDSUBSCRIBER_FIRSTNAME'] ) ) {
			$subscriber['first_name'] = Automator()->parse->text( $action_data['meta']['ADDSUBSCRIBER_FIRSTNAME'], $recipe_id, $user_id, $args );
		}

		if ( isset( $action_data['meta']['ADDSUBSCRIBER_LASTNAME'] ) && ! empty( $action_data['meta']['ADDSUBSCRIBER_LASTNAME'] ) ) {
			$subscriber['last_name'] = Automator()->parse->text( $action_data['meta']['ADDSUBSCRIBER_LASTNAME'], $recipe_id, $user_id, $args );
		}

		if ( isset( $action_data['meta']['ADDSUBSCRIBER_STATUS'] ) && ! empty( $action_data['meta']['ADDSUBSCRIBER_STATUS'] ) ) {
			$subscriber['status'] = Automator()->parse->text( $action_data['meta']['ADDSUBSCRIBER_STATUS'], $recipe_id, $user_id, $args );
		}

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

		try {
			// try to find if user is already a subscriber
			$existing_subscriber = \MailPoet\Models\Subscriber::findOne( $subscriber['email'] );
			if ( ! $existing_subscriber ) {
				$mailpoet->addSubscriber( $subscriber, json_decode( $list_id ), [ 'send_confirmation_email' => $disable_confirmation_email ] );
				Automator()->complete_action( $user_id, $action_data, $recipe_id );
			} else {
				$mailpoet->subscribeToLists( $existing_subscriber->id, json_decode( $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 );
		}

		return;

	}