Mailchimp_Helpers::select_segments_from_list()

Ajax callback for loading segment IDS list.


Source Source

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

	public function select_segments_from_list() {
		global $uncanny_automator;

		// Nonce and post object validation
		$uncanny_automator->utilities->ajax_auth_check( $_POST );

		$fields = array();

		$fields[] = array(
			'value' => '-1',
			'text'  => __( 'Select a Segment or Tag', 'uncanny-automator' ),
		);

		if ( ! isset( $_POST ) ) {
			echo wp_json_encode( $fields );
			die();
		}

		$list_id = sanitize_text_field( $_POST['values']['MCLIST'] );

		$request_params = array(
			'action'  => 'get_segments',
			'list_id' => $list_id,
			'fields'  => 'segments.name,segments.id',
			'count'   => 1000,
		);
		try {
			$response = $this->api_request( $request_params );

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

				if ( ! empty( $response->data->segments ) ) {
					foreach ( $response->data->segments as $segment ) {
						$fields[] = array(
							'value' => $segment->id,
							'text'  => $segment->name,
						);
					}
				}
			}
		} catch ( \Exception $e ) {
			// Do nothing
		}

		echo wp_json_encode( $fields );
		die();
	}