AUDIENCE_REMOVEUSERTAG::remove_tag_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-removeusertag.php

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

		global $uncanny_automator;

		try {
			// Here add note
			$list_id = $action_data['meta']['MCLIST'];
			$tag     = $action_data['meta']['MCLISTTAGS'];

			if ( empty( $tag ) ) {
				// log error when no token found.
				$error_msg                           = __( 'No tag selected.', '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;
			}

			// 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 ) {

				$tags_body = array(
					'tags' => array(
						array(
							'name'   => $tag,
							'status' => 'inactive',
						),
					),
				);

				$request_params = array(
					'action'    => 'update_subscriber_tags',
					'list_id'   => $list_id,
					'user_hash' => $user_hash,
					'tags'      => json_encode( $tags_body ),
				);

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

				// prepare meeting lists
				if ( $response === null ) {

					$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;
		}
	}