Skip to content

Commit 9b6bda4

Browse files
Merge pull request #807 from arnelap/remove-tags-option
Ability to remove tags, closes #621, props @arnelap
2 parents 42ff9d2 + 94f8770 commit 9b6bda4

File tree

5 files changed

+72
-33
lines changed

5 files changed

+72
-33
lines changed

config/default-form-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
'required_fields' => '',
1111
'update_existing' => 0,
1212
'subscriber_tags' => '',
13+
'remove_subscriber_tags' => '',
1314
'email_typo_check' => 0,
1415
];

includes/class-mailchimp.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda
9191
$data->was_already_on_list = $existing_member_data->status === 'subscribed';
9292

9393
if (isset($args['tags']) && is_array($args['tags'])) {
94-
$this->list_add_tags_to_subscriber($list_id, $data, $args['tags']);
94+
$this->list_tags_to_subscriber($list_id, $data, $args['tags']);
9595
}
9696
} else {
9797
$data = $api->add_new_list_member($list_id, $args);
@@ -110,54 +110,53 @@ public function list_subscribe($list_id, $email_address, array $args = [], $upda
110110
* Format tags to send to Mailchimp.
111111
*
112112
* @param $mailchimp_tags array existent user tags
113-
* @param $new_tags array new tags to add
113+
* @param $tags array new tags to add
114114
*
115115
* @return array
116116
* @since 4.7.9
117117
*/
118-
private function merge_and_format_member_tags($mailchimp_tags, $new_tags)
118+
private function merge_and_format_member_tags($mailchimp_tags, $tags)
119119
{
120-
$mailchimp_tags = array_map(
121-
function ($tag) {
122-
return $tag->name;
123-
},
124-
$mailchimp_tags
125-
);
126-
127-
$tags = array_unique(array_merge($mailchimp_tags, $new_tags), SORT_REGULAR);
128-
129-
return array_map(
130-
function ($tag) {
131-
return [
132-
'name' => $tag,
133-
'status' => 'active',
120+
$formatted_tags = [];
121+
foreach ($tags as $tag) {
122+
if (is_string($tag)) {
123+
$formatted_tags[] = [
124+
'name' => $tag,
125+
'status' => 'active'
134126
];
135-
},
136-
$tags
137-
);
127+
} elseif (is_array($tag) && isset($tag['name'])) {
128+
$formatted_tags[] = [
129+
'name' => $tag['name'],
130+
'status' => isset($tag['status']) ? $tag['status'] : 'active'
131+
];
132+
}
133+
}
134+
135+
return $formatted_tags;
138136
}
139137

140138
/**
141-
* Post the tags on a list member.
139+
* Post the tags on a list member.
142140
*
143141
* @param $mailchimp_list_id string The list id to subscribe to
144142
* @param $mailchimp_member stdClass mailchimp user informations
145-
* @param $new_tags array tags to add to the user
143+
* @param $tags array tags to set for the user (can include 'status' key)
146144
*
147145
* @return bool
148146
* @throws Exception
149-
* @since 4.7.9
147+
* @since 4.10.10
150148
*/
151-
private function list_add_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, array $new_tags)
149+
private function list_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, array $tags)
152150
{
153151
// do nothing if no tags given
154-
if (count($new_tags) === 0) {
152+
if (count($tags) === 0) {
155153
return true;
156154
}
157155

158-
$api = $this->get_api();
156+
$api = $this->get_api();
157+
159158
$data = [
160-
'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $new_tags),
159+
'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $tags),
161160
];
162161

163162
try {

includes/forms/class-form.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,33 @@ public function get_message($key)
773773
public function get_subscriber_tags()
774774
{
775775
$tags = [];
776-
foreach (explode(',', $this->settings['subscriber_tags']) as $v) {
776+
777+
// Add active tags
778+
$tags = array_merge($tags, $this->parse_tags_from_setting($this->settings['subscriber_tags'], 'active'));
779+
780+
// Add inactive (remove) tags
781+
$tags = array_merge($tags, $this->parse_tags_from_setting($this->settings['remove_subscriber_tags'], 'inactive'));
782+
783+
return $tags;
784+
}
785+
786+
/**
787+
* Parse comma-separated tags from a setting into Mailchimp API format
788+
*
789+
* @since 4.10.10
790+
* @param string $setting_value
791+
* @param string $status
792+
* @return array
793+
*/
794+
private function parse_tags_from_setting($setting_value, $status)
795+
{
796+
$tags = [];
797+
foreach (explode(',', $setting_value) as $v) {
777798
$v = trim($v);
778799
if ($v == '') {
779800
continue;
780801
}
781-
$tags[] = $v;
802+
$tags[] = ['name' => $v, 'status' => $status];
782803
}
783804
return $tags;
784805
}

includes/forms/views/tabs/form-settings.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@
9595
</tr>
9696

9797
<tr valign="top">
98-
<th scope="row"><label for="mc4wp_form_subscriber_tags"><?php echo esc_html__('Subscriber tags', 'mailchimp-for-wp'); ?></label></th>
98+
<th scope="row"><label for="mc4wp_form_add_tags"><?php echo esc_html__('Add tags', 'mailchimp-for-wp'); ?></label></th>
9999
<td>
100-
<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']); ?>" />
100+
<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']); ?>" />
101101
<p class="description">
102102
<?php echo esc_html__('The listed tags will be applied to all subscribers added or updated by this form.', 'mailchimp-for-wp'); ?>
103103
<?php echo esc_html__('Separate multiple values with a comma.', 'mailchimp-for-wp'); ?>
@@ -106,6 +106,18 @@
106106
</td>
107107
</tr>
108108

109+
<tr valign="top">
110+
<th scope="row"><label for="mc4wp_form_remove_tags"><?php echo esc_html__('Remove tags', 'mailchimp-for-wp'); ?></label></th>
111+
<td>
112+
<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']); ?>" />
113+
<p class="description">
114+
<?php echo esc_html__('The listed tags will be removed from all subscribers updated by this form.', 'mailchimp-for-wp'); ?>
115+
<?php echo esc_html__('Separate multiple values with a comma.', 'mailchimp-for-wp'); ?>
116+
</p>
117+
118+
</td>
119+
</tr>
120+
109121
<?php do_action('mc4wp_admin_form_after_mailchimp_settings_rows', $opts, $form); ?>
110122
</table>
111123

tests/FormTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,15 @@ public function test_get_subscriber_tags()
276276
$post = get_post(1);
277277
$form = new MC4WP_Form(1, $post);
278278
$form->settings = [
279-
'subscriber_tags' => 'foo,,bar'
279+
'subscriber_tags' => 'foo,bar',
280+
'remove_subscriber_tags' => 'old,,tag'
280281
];
281282

282-
$this->assertEquals($form->get_subscriber_tags(), ['foo', 'bar']);
283+
$this->assertEquals($form->get_subscriber_tags(), [
284+
['name' => 'foo', 'status' => 'active'],
285+
['name' => 'bar', 'status' => 'active'],
286+
['name' => 'old', 'status' => 'inactive'],
287+
['name' => 'tag', 'status' => 'inactive']
288+
]);
283289
}
284290
}

0 commit comments

Comments
 (0)