Mailchimp_Helpers::select_mcgroupslist_from_mclist()


Source

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

	public function select_mcgroupslist_from_mclist() {
		global $uncanny_automator;
		// Nonce and post object validation
		$uncanny_automator->utilities->ajax_auth_check( $_POST );
		$fields = array();
		if ( ! isset( $_POST ) ) {
			echo wp_json_encode( $fields );
			die();
		}
		$list_id = sanitize_text_field( $_POST['values']['MCLIST'] );
		if ( empty( $list_id ) ) {
			echo wp_json_encode( $fields );
			die();
		}
		$request_params = array(
			'action'  => 'get_list_categories',
			'list_id' => $list_id,
		);
		try {
			$categories_response = $this->api_request( $request_params );
			if ( $categories_response->statusCode !== 200 || empty( $categories_response->data->categories ) ) {
				echo wp_json_encode( $fields );
				die();
			}
			foreach ( $categories_response->data->categories as $category ) {
				$request_params = array(
					'action'      => 'get_interests',
					'list_id'     => $list_id,
					'category_id' => $category->id,
				);
				$interests_response = $this->api_request( $request_params );
				if ( $interests_response->statusCode === 200 ) {
					if ( ! empty( $interests_response->data->interests ) ) {
						foreach ( $interests_response->data->interests as $interest ) {
							$fields[] = array(
								'value' => $interest->id,
								'text'  => $category->title . ' > ' . $interest->name,
							);
						}
					}
				}
			}
		} catch ( \Exception $e ) {
			// Do nothing
		}
		echo wp_json_encode( $fields );
		die();
	}