MAILCHIMP_TOKENS::parse_tokens( mixed $value, mixed $pieces, mixed $recipe_id, mixed $trigger_data, mixed $user_id, mixed $replace_args )

Parsing the tokens.


Parameters Parameters

$value

(Required)

$pieces

(Required)

$recipe_id

(Required)

$trigger_data

(Required)

$user_id

(Required)

$replace_args

(Required)


Top ↑

Return Return

(void)


Source Source

File: src/integrations/mailchimp/tokens/mailchimp-tokens.php

	public function parse_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {

		$trigger_code = '';

		if ( isset( $trigger_data[0]['meta']['code'] ) ) {
			$trigger_code = $trigger_data[0]['meta']['code'];
		}

		$triggers = array( 'ANON_MAILCHIMP_CONTACT_UNSUBSCRIBED', 'ANON_MAILCHIMP_CONTACT_ADDED', 'ANON_MAILCHIMP_CONTACT_EMAIL_CHANGED' );

		if ( empty( $trigger_code ) || ! in_array( $trigger_code, $triggers, true ) ) {
			return $value;
		}

		if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
			return $value;
		}

		// Get the meta from database record.
		$mailchimp_webhook_data = json_decode( Automator()->db->token->get( 'MAILCHIMP_WEBHOOK_EVENT_DATA', $replace_args ), true );

		// The pieces[2] is equal to Mailchimp's key.
		if ( ! empty( $mailchimp_webhook_data[ $pieces[2] ] ) ) {

			$value = $mailchimp_webhook_data[ $pieces[2] ];

		}

		// New email or upemail event does not contain 'email' field. It contains 'new_email' instead.
		if ( isset( $mailchimp_webhook_data['new_email'] ) && ! empty( $mailchimp_webhook_data['new_email'] ) ) {

			$value = $mailchimp_webhook_data['new_email'];

		}

		// Handle merge fields.

		if ( false !== strpos( $pieces[1], 'MAILCHIMP_MERGEFIELD' ) ) {

			if ( isset( $mailchimp_webhook_data['merges'][ $pieces[2] ] ) && ! empty( $mailchimp_webhook_data['merges'][ $pieces[2] ] ) ) {

				$value = $mailchimp_webhook_data['merges'][ $pieces[2] ];

			}
		}

		// Handle array values such as address.
		if ( is_array( $value ) ) {
			$value = implode( ', ', $value );
		}

		return $value;

	}