Filter uncanny-automator

uap_option_get_all_lists

Filters all Mailchimp lists before they are retrieved by the UAP integration.

add_filter( 'uap_option_get_all_lists', $callback, 10, 1 );

Description

Allows developers to filter the list of Mailchimp options. Modify this filter to alter or extend the data returned for Mailchimp list options, influencing how they are displayed or processed within the plugin. Fires when retrieving all Mailchimp lists.


Usage

add_filter( 'uap_option_get_all_lists', 'your_function_name', 10, 1 );

Parameters

$option (mixed)
This parameter is the option code for the Mailchimp list, defaulting to 'MCLIST'.

Return Value

The filtered value.


Examples

/**
 * Modify the Mailchimp list options to add custom mapping for a specific list.
 *
 * This example demonstrates how to hook into 'uap_option_get_all_lists' to
 * dynamically alter the data associated with Mailchimp lists, such as adding
 * custom tokens or modifying descriptions based on certain conditions.
 *
 * @param array $option The original array of Mailchimp list options.
 * @return array The modified array of Mailchimp list options.
 */
add_filter( 'uap_option_get_all_lists', function( $option ) {
	// Let's assume we have a specific Mailchimp list ID that requires special handling.
	$specific_list_id = 'your_mailchimp_list_id_here';

	// Check if the current list being processed is our specific list.
	// We're assuming $option is an array where each element represents a list,
	// and it contains a key like 'list_id' or similar that identifies the Mailchimp list.
	// In the provided source context, it seems $option is likely a single list's data being built.
	// For this example, let's assume $option is the array representing a single list's configuration.

	// We need to check if the option represents the specific list we want to modify.
	// Let's assume the list ID is stored in a key like 'id' within the $option array.
	// This is an assumption based on common WordPress plugin patterns.
	if ( isset( $option['id'] ) && $option['id'] === $specific_list_id ) {
		// If it's our specific list, let's add a custom token or modify a description.
		// For instance, we might want to map a custom user field to a specific Mailchimp merge tag.
		$option['relevant_tokens'][] = '{custom_user_field_token}'; // Add a custom token
		$option['custom_value_description'] = 'Enter a value to be mapped to a custom Mailchimp merge tag.'; // Modify description
		$option['supports_custom_value'] = true; // Ensure custom values are supported for this list.

		// We can also dynamically change the placeholder based on the list.
		$option['placeholder'] = 'Map your custom data here';
	}

	// If it's not our specific list, we don't make any changes and return the original option.
	// This ensures other lists are processed normally.
	return $option;
}, 10, 1 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/mailchimp/helpers/mailchimp-helpers.php:202
src/integrations/mailchimp/helpers/mailchimp-helpers.php:733

public function get_all_lists( $label = null, $option_code = 'MCLIST', $args = array() ) {

		if ( ! $label ) {
			$label = esc_html_x( 'Audience', 'Mailchimp', 'uncanny-automator' );
		}

		$is_ajax                  = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
		$target_field             = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
		$end_point                = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
		$description              = key_exists( 'description', $args ) ? $args['description'] : null;
		$custom_value_description = key_exists( 'custom_value_description', $args ) ? $args['custom_value_description'] : null;
		$supports_custom_value    = key_exists( 'supports_custom_value', $args ) ? $args['supports_custom_value'] : false;
		$supports_tokens          = key_exists( 'supports_tokens', $args ) ? $args['supports_tokens'] : null;
		$placeholder              = key_exists( 'placeholder', $args ) ? $args['placeholder'] : null;
		$has_any                  = key_exists( 'has_any', $args ) ? $args['has_any'] : null;
		$options                  = array();

		try {

			$body = array(
				'action' => 'get_lists',
			);

			$response = $this->api_request( $body );

			if ( 200 === intval( $response['statusCode'] ) ) {
				if ( ! empty( $response['data']['lists'] ) ) {
					if ( ! empty( $has_any ) && true === $has_any ) {
						$options[] = array(
							'value' => '-1',
							'text'  => esc_html_x( 'Any audience', 'Mailchimp', 'uncanny-automator' ),
						);
					}
					foreach ( $response['data']['lists'] as $list ) {
						$options[] = array(
							'value' => $list['id'],
							'text'  => $list['name'],
						);
					}
				}
			}
		} catch ( Exception $e ) {
			$options[] = array(
				'value' => '',
				'text'  => $e->getMessage(),
			);
		}

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'description'              => $description,
			'input_type'               => 'select',
			'required'                 => true,
			'is_ajax'                  => $is_ajax,
			'fill_values_in'           => $target_field,
			'endpoint'                 => $end_point,
			'options'                  => $options,
			'supports_tokens'          => apply_filters( 'uap_option_' . $option_code . '_select_field', $supports_tokens ),
			'custom_value_description' => $custom_value_description,
			'supports_custom_value'    => $supports_custom_value,
			'placeholder'              => $placeholder,
			'integration'              => 'MAILCHIMP',
			'relevant_tokens'          => array(),
		);

		return apply_filters( 'uap_option_get_all_lists', $option );
	}


Scroll to Top