Mailchimp_Helpers::get_mailchimp_audience_fields()

Ajax callback for loading audience list related merge fields.

Contents


Return Return

(Uncanny_Automatorvoid.)


Source Source

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

	public function get_mailchimp_audience_fields() {
		global $uncanny_automator;
		// Nonce and post object validation
		$uncanny_automator->utilities->ajax_auth_check( $_POST );
		$fields   = array();
		$response = (object) array(
			'success' => false,
			'samples' => array(),
		);
		if ( ! isset( $_POST ) ) {
			echo wp_json_encode( $response );
			die();
		}
		$list_id = sanitize_text_field( $_POST['audience'] );
		$request_params = array(
			'action'  => 'get_list_fields',
			'list_id' => $list_id,
		);
		try {
			$response = $this->api_request( $request_params );
			// prepare meeting lists
			if ( $response->statusCode === 200 ) {
				if ( ! empty( $response->data->merge_fields ) ) {
					foreach ( $response->data->merge_fields as $field ) {
						$merge_order = $field->display_order * 10;
						if ( $field->type == 'address' ) {
							$merge_order           += 1;
							$fields[ $merge_order ] = array(
								'key'  => $field->tag . '_addr1',
								'type' => 'text',
								'data' => $field->name,
							);
							$merge_order           += 1;
							$fields[ $merge_order ] = array(
								'key'  => $field->tag . '_addr2',
								'type' => 'text',
								'data' => $field->name,
							);
							$merge_order           += 1;
							$fields[ $merge_order ] = array(
								'key'  => $field->tag . '_city',
								'type' => 'text',
								'data' => $field->name,
							);
							$merge_order           += 1;
							$fields[ $merge_order ] = array(
								'key'  => $field->tag . '_state',
								'type' => 'text',
								'data' => $field->name,
							);
							$merge_order           += 1;
							$fields[ $merge_order ] = array(
								'key'  => $field->tag . '_zip',
								'type' => 'text',
								'data' => $field->name,
							);
							$merge_order           += 1;
							$fields[ $merge_order ] = array(
								'key'  => $field->tag . '_country',
								'type' => 'text',
								'data' => $field->name,
							);
						} else {
							$fields[ $merge_order ] = array(
								'key'  => $field->tag,
								'type' => 'text',
								'data' => $field->name,
							);
						}
					}
					ksort( $fields );
					$ajax_response = (object) array(
						'success' => true,
						'samples' => array( $fields ),
					);
				}
			}
		} catch ( \Exception $e ) {
			$ajax_response = (object) array(
				'success' => false,
				'error'   => $e->getMessage(),
			);
		}
		echo wp_json_encode( $ajax_response );
		die();
	}