AUDIENCE_UNSUBSCRIBEAUSER::unsubscribe_audience_member( $user_id,  $action_data,  $recipe_id,  $args )

Validation function when the action is hit


Parameters Parameters

$user_id

(Required)

$action_data

(Required)

$recipe_id

(Required)


Source Source

File: src/integrations/mailchimp/actions/audience-unsubscribeauser.php

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

		global $uncanny_automator;

		try {
			// Here unsubscribe
			$list_id      = $action_data['meta']['MCLIST'];
			$deleteMember = $action_data['meta']['MCDELETEMEMBER'];

			// get current user email
			$user      = get_userdata( $user_id );
			$user_hash = md5( strtolower( trim( $user->user_email ) ) );

			$mc_client = $uncanny_automator->helpers->recipe->mailchimp->options->get_mailchimp_client();
			if ( $mc_client ) {

				if ( 'no' === $deleteMember ) {
					$user_data = array(
						'status' => 'unsubscribed',
					);

					$request_params = array(
						'action'    => 'update_subscriber',
						'list_id'   => $list_id,
						'user_hash' => $user_hash,
						'user_data' => json_encode( $user_data ),
					);

				} else {
					$request_params = array(
						'action'    => 'delete_subscriber',
						'list_id'   => $list_id,
						'user_hash' => $user_hash,
					);

				}

				$response = $uncanny_automator->helpers->recipe->mailchimp->options->api_request( $request_params );

				// prepare meeting lists
				if ( $response->statusCode === 200 ) {

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

					return;
				} else {

					$uncanny_automator->helpers->recipe->mailchimp->options->log_action_error( $response, $user_id, $action_data, $recipe_id );

					return;
				}
			} else {
				// log error when no token found.
				$error_msg                           = __( 'Mailchimp account is not connected.', 'uncanny-automator' );
				$action_data['do-nothing']           = true;
				$action_data['complete_with_errors'] = true;
				$uncanny_automator->complete_action( $user_id, $action_data, $recipe_id, $error_msg );

				return;
			}
		} catch ( \Exception $e ) {
			$error_msg = $e->getMessage();
			if ( $json = json_decode( $error_msg ) ) {
				if ( isset( $json->error ) && isset( $json->error->message ) ) {
					$error_msg = $json->error->message;
				}
			}
			$action_data['do-nothing']           = true;
			$action_data['complete_with_errors'] = true;
			$uncanny_automator->complete_action( $user_id, $action_data, $recipe_id, $error_msg );

			return;
		}
	}