Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
];
66 changes: 36 additions & 30 deletions includes/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,24 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda
}

try {
// Extract tags from args before subscriber creation/update
$tags = [];
if (isset($args['tags']) && is_array($args['tags'])) {
$tags = $args['tags'];
unset($args['tags']);
}

if ($existing_member_data) {
$data = $api->update_list_member($list_id, $email_address, $args);
$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']);
}
} else {
$data = $api->add_new_list_member($list_id, $args);
$data->was_already_on_list = false;
}

if (!empty($tags)) {
$this->list_tags_to_subscriber($list_id, $data, $tags);
}
} catch (MC4WP_API_Exception $e) {
$this->error_code = $e->getCode();
$this->error_message = $e;
Expand All @@ -110,54 +117,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
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']
]);
}
}