Filter
uncanny-automator
uap_option_get_list_groups
Filters the list of Mailchimp groups before they are retrieved for integration.
add_filter( 'uap_option_get_list_groups', $callback, 10, 1 );
Description
Fires after Mailchimp list group options are fetched. Developers can modify the group options array, for example, to filter out specific groups or add custom data before they are displayed in the UI. This filter ensures flexibility in how Mailchimp list groups are presented to users.
Usage
add_filter( 'uap_option_get_list_groups', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter represents the option value being filtered, which is typically an array of group data or a single group identifier.
Return Value
The filtered value.
Examples
/**
* Example of filtering the 'uap_option_get_list_groups' hook.
* This example demonstrates how to conditionally modify the list of groups
* available for a Mailchimp integration based on user role.
*
* @param array $option The original array of group options.
* @return array The modified array of group options.
*/
add_filter( 'uap_option_get_list_groups', function( $option ) {
// Check if the current user has the 'administrator' role.
if ( current_user_can( 'administrator' ) ) {
// If the user is an administrator, return the original options.
// This means all available groups will be displayed.
return $option;
} else {
// If the user is not an administrator, we might want to show a limited set
// of groups or perhaps none at all, depending on requirements.
// For this example, let's assume we want to show only a specific group
// if they are a 'subscriber' role, otherwise return an empty array.
if ( current_user_can( 'subscriber' ) ) {
// If the user is a subscriber, filter the options to include only 'New Subscribers' group.
// This is a hypothetical group ID. You'd replace 'your_subscriber_group_id'
// with the actual ID from your Mailchimp setup.
$subscriber_group_id = 'your_subscriber_group_id';
if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
$filtered_options = array_filter( $option['options'], function( $group ) use ( $subscriber_group_id ) {
// Assuming the group structure has an 'id' and 'name' key.
// Adjust this logic based on the actual structure of $option['options'].
return ( $group['id'] === $subscriber_group_id );
} );
// Update the 'options' key with the filtered results.
$option['options'] = $filtered_options;
// Optionally, disable multiple selections if only one group is shown.
$option['supports_multiple_values'] = false;
}
} else {
// If the user is neither an administrator nor a subscriber,
// return an empty array to indicate no groups are available.
$option['options'] = array();
$option['supports_multiple_values'] = false;
}
return $option;
}
}, 10, 1 ); // Priority 10, 1 accepted argument.
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:249
public function get_list_groups( $label = null, $option_code = 'MCLISTGROUPS', $args = array() ) {
if ( ! $label ) {
$label = esc_html_x( 'Groups', 'Mailchimp', 'uncanny-automator' );
}
$args = wp_parse_args(
$args,
array(
'uo_include_any' => false,
'uo_any_label' => esc_html_x( 'Any group', 'Mailchimp', 'uncanny-automator' ),
)
);
$token = key_exists( 'token', $args ) ? $args['token'] : false;
$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'] : '';
$options = array();
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => false,
'supports_tokens' => $token,
'is_ajax' => $is_ajax,
'fill_values_in' => $target_field,
'endpoint' => $end_point,
'custom_value_description' => '',
'supports_custom_value' => false,
'supports_multiple_values' => true,
'options' => $options,
);
return apply_filters( 'uap_option_get_list_groups', $option );
}