Skip to content
Draft
1 change: 1 addition & 0 deletions config/default-form-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
'required_fields' => '',
'update_existing' => 0,
'subscriber_tags' => '',
'remove_subscriber_tags' => '',
'email_typo_check' => 0,
];
53 changes: 26 additions & 27 deletions includes/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda
$data->was_already_on_list = $existing_member_data->status === 'subscribed';

if (isset($args['tags']) && is_array($args['tags'])) {
$this->list_add_tags_to_subscriber($list_id, $data, $args['tags']);
$this->list_tags_to_subscriber($list_id, $data, $args['tags']);
}
} else {
$data = $api->add_new_list_member($list_id, $args);
Expand All @@ -110,54 +110,53 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda
* Format tags to send to Mailchimp.
*
* @param $mailchimp_tags array existent user tags
* @param $new_tags array new tags to add
* @param $tags array new tags to add
*
* @return array
* @since 4.7.9
*/
private function merge_and_format_member_tags($mailchimp_tags, $new_tags)
private function merge_and_format_member_tags($mailchimp_tags, $tags)
{
$mailchimp_tags = array_map(
function ($tag) {
return $tag->name;
},
$mailchimp_tags
);

$tags = array_unique(array_merge($mailchimp_tags, $new_tags), SORT_REGULAR);

return array_map(
function ($tag) {
return [
'name' => $tag,
'status' => 'active',
$formatted_tags = [];
foreach ($tags as $tag) {
if (is_string($tag)) {
$formatted_tags[] = [
'name' => $tag,
'status' => 'active'
];
},
$tags
);
} elseif (is_array($tag) && isset($tag['name'])) {
$formatted_tags[] = [
'name' => $tag['name'],
'status' => isset($tag['status']) ? $tag['status'] : 'active'
];
}
}

return $formatted_tags;
}

/**
* Post the tags on a list member.
* Post the tags on a list member.
*
* @param $mailchimp_list_id string The list id to subscribe to
* @param $mailchimp_member stdClass mailchimp user informations
* @param $new_tags array tags to add to the user
* @param $tags array tags to set for the user (can include 'status' key)
*
* @return bool
* @throws Exception
* @since 4.7.9
* @since 4.10.10
*/
private function list_add_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, array $new_tags)
private function list_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, array $tags)
{
// do nothing if no tags given
if (count($new_tags) === 0) {
if (count($tags) === 0) {
return true;
}

$api = $this->get_api();
$api = $this->get_api();

$data = [
'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $new_tags),
'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $tags),
];

try {
Expand Down
25 changes: 23 additions & 2 deletions includes/forms/class-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,12 +773,33 @@ public function get_message($key)
public function get_subscriber_tags()
{
$tags = [];
foreach (explode(',', $this->settings['subscriber_tags']) as $v) {

// Add active tags
$tags = array_merge($tags, $this->parse_tags_from_setting($this->settings['subscriber_tags'], 'active'));

// Add inactive (remove) tags
$tags = array_merge($tags, $this->parse_tags_from_setting($this->settings['remove_subscriber_tags'], 'inactive'));

return $tags;
}

/**
* Parse comma-separated tags from a setting into Mailchimp API format
*
* @since 4.10.10
* @param string $setting_value
* @param string $status
* @return array
*/
private function parse_tags_from_setting($setting_value, $status)
{
$tags = [];
foreach (explode(',', $setting_value) as $v) {
$v = trim($v);
if ($v == '') {
continue;
}
$tags[] = $v;
$tags[] = ['name' => $v, 'status' => $status];
}
return $tags;
}
Expand Down
16 changes: 14 additions & 2 deletions includes/forms/views/tabs/form-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@
</tr>

<tr valign="top">
<th scope="row"><label for="mc4wp_form_subscriber_tags"><?php echo esc_html__('Subscriber tags', 'mailchimp-for-wp'); ?></label></th>
<th scope="row"><label for="mc4wp_form_add_tags"><?php echo esc_html__('Add tags', 'mailchimp-for-wp'); ?></label></th>
<td>
<input type="text" class="widefat" name="mc4wp_form[settings][subscriber_tags]" id="mc4wp_form_subscriber_tags" placeholder="<?php echo esc_attr__('Example: My tag, another tag', 'mailchimp-for-wp'); ?>" value="<?php echo esc_attr($opts['subscriber_tags']); ?>" />
<input type="text" class="widefat" name="mc4wp_form[settings][subscriber_tags]" id="mc4wp_form_add_tags" placeholder="<?php echo esc_attr__('Example: My tag, another tag', 'mailchimp-for-wp'); ?>" value="<?php echo esc_attr($opts['subscriber_tags']); ?>" />
<p class="description">
<?php echo esc_html__('The listed tags will be applied to all subscribers added or updated by this form.', 'mailchimp-for-wp'); ?>
<?php echo esc_html__('Separate multiple values with a comma.', 'mailchimp-for-wp'); ?>
Expand All @@ -106,6 +106,18 @@
</td>
</tr>

<tr valign="top">
<th scope="row"><label for="mc4wp_form_remove_tags"><?php echo esc_html__('Remove tags', 'mailchimp-for-wp'); ?></label></th>
<td>
<input type="text" class="widefat" name="mc4wp_form[settings][remove_subscriber_tags]" id="mc4wp_form_remove_tags" placeholder="<?php echo esc_attr__('Example: My tag, another tag', 'mailchimp-for-wp'); ?>" value="<?php echo esc_attr($opts['remove_subscriber_tags']); ?>" />
<p class="description">
<?php echo esc_html__('The listed tags will be removed from all subscribers updated by this form.', 'mailchimp-for-wp'); ?>
<?php echo esc_html__('Separate multiple values with a comma.', 'mailchimp-for-wp'); ?>
</p>

</td>
</tr>

<?php do_action('mc4wp_admin_form_after_mailchimp_settings_rows', $opts, $form); ?>
</table>

Expand Down
18 changes: 16 additions & 2 deletions sample-code-snippets/forms/subscriber-tags.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<?php

/**
* This adds the "My tag" tag to all new subscribers added by the plugin.
*
* The "mc4wp_subscriber_form_data" filter only runs for form requests.
* Use "mc4wp_subscriber_data" to hook into both form & integration requests.
*/

//This will add the tag "My tag" to all new subscribers added by the plugin.
add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
$subscriber->tags[] = 'My tag';
return $subscriber;
});

//This will remove the tag "My tag" from all new subscribers added by the plugin.
add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
$subscriber->tags[] = ['name' => 'My Tag', 'status' => 'inactive'];
return $subscriber;
});


//You can add and remove multiple tags at once.
add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
$subscriber->tags[] = ['name' => 'My Tag', 'status' => 'active'];
$subscriber->tags[] = ['name' => 'Another tag', 'status' => 'active'];
$subscriber->tags[] = ['name' => 'Remove this tag', 'status' => 'inactive'];
return $subscriber;
});
11 changes: 11 additions & 0 deletions sample-code-snippets/integrations/contact-form-7/different-tag.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php

/**
* Add a different tag to the subscriber based on the CF7 form ID.
* In this example CF7 form with ID 500 will get the tag ES.
* The form ID 510 will get the tag NL
*
* NOTE: The ID in this case is the POST ID that you see in the URL when you edit a form.
* That means the ID is always a number and not the ID you see in the CF shortcode.
*
* It is also possible to remove tags, see the subscriber-tags.php snippet under /forms/
*/

add_filter('mc4wp_integration_contact-form-7_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber, $cf7_form_id) {
if ($cf7_form_id == 500) {
$subscriber->tags[] = 'ES';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?php

/**
* Add a different tag to the subscriber based on the Ninja Forms form ID.
* In this example Ninja Forms form with ID 500 will get the tag ES.
* The form ID 510 will get the tag NL
*
* It is also possible to remove tags, see the subscriber-tags.php snippet under /forms/
*/

add_filter('mc4wp_integration_ninja-forms_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber, $form_id) {
if ($form_id == 500) {
$subscriber->tags[] = 'ES';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<?php

/**
* This adds the "My tag" tag to all new subscribers added using WooCommerce Checkout integration.
* Add and or remove tags from subscribers added using WooCommerce Checkout integration.
*
*/

//Add the tag "My tag" to all new subscribers added using WooCommerce Checkout integration.
add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
$subscriber->tags[] = 'My tag';
return $subscriber;
});


//Remove the tag "My tag" from all new subscribers added using WooCommerce Checkout integration.
add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
$subscriber->tags[] = ['name' => 'My tag', 'status' => 'inactive'];
return $subscriber;
});

//Add the tag "My tag" to all new subscribers added using WooCommerce Checkout integration while removing the "Remove me" tag.
add_filter('mc4wp_integration_woocommerce_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) {
$subscriber->tags[] = ['name' => 'My tag', 'status' => 'active'];
$subscriber->tags[] = ['name' => 'Remove me', 'status' => 'inactive'];
return $subscriber;
});
10 changes: 8 additions & 2 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,15 @@ public function test_get_subscriber_tags()
$post = get_post(1);
$form = new MC4WP_Form(1, $post);
$form->settings = [
'subscriber_tags' => 'foo,,bar'
'subscriber_tags' => 'foo,bar',
'remove_subscriber_tags' => 'old,,tag'
];

$this->assertEquals($form->get_subscriber_tags(), ['foo', 'bar']);
$this->assertEquals($form->get_subscriber_tags(), [
['name' => 'foo', 'status' => 'active'],
['name' => 'bar', 'status' => 'active'],
['name' => 'old', 'status' => 'inactive'],
['name' => 'tag', 'status' => 'inactive']
]);
}
}