From 045bf33506f77d9a06e38d818a7ac0f81334191a Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 10:19:09 +0100 Subject: [PATCH 1/8] Adding tag removal option add_filter('mc4wp_subscriber_data', function (MC4WP_MailChimp_Subscriber $subscriber) { $subscriber->tags[] = ['name' => 'removethis', 'status' => 'inactive']; return $subscriber; }); --- includes/class-mailchimp.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/includes/class-mailchimp.php b/includes/class-mailchimp.php index ce3dd76a..62496cff 100755 --- a/includes/class-mailchimp.php +++ b/includes/class-mailchimp.php @@ -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); @@ -138,26 +138,43 @@ function ($tag) { } /** - * 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(); + + // Format tags - handle both string tags (old style, default to active) and array tags with explicit status + $formatted_tags = []; + foreach ($tags as $tag) { + if (is_string($tag)) { + $formatted_tags[] = [ + 'name' => $tag, + 'status' => 'active' + ]; + } elseif (is_array($tag) && isset($tag['name'])) { + $formatted_tags[] = [ + 'name' => $tag['name'], + 'status' => isset($tag['status']) ? $tag['status'] : 'active' + ]; + } + } + $data = [ - 'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $new_tags), + 'tags' => $formatted_tags, ]; try { From 13099b11377973b9cd2b302e3ab3f57263971d6e Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 10:50:59 +0100 Subject: [PATCH 2/8] moved tag array formatting back to its own function --- includes/class-mailchimp.php | 54 ++++++++++++------------------------ 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/includes/class-mailchimp.php b/includes/class-mailchimp.php index 62496cff..34f2a285 100755 --- a/includes/class-mailchimp.php +++ b/includes/class-mailchimp.php @@ -110,31 +110,29 @@ 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; } /** @@ -157,24 +155,8 @@ private function list_tags_to_subscriber($mailchimp_list_id, $mailchimp_member, $api = $this->get_api(); - // Format tags - handle both string tags (old style, default to active) and array tags with explicit status - $formatted_tags = []; - foreach ($tags as $tag) { - if (is_string($tag)) { - $formatted_tags[] = [ - 'name' => $tag, - 'status' => 'active' - ]; - } elseif (is_array($tag) && isset($tag['name'])) { - $formatted_tags[] = [ - 'name' => $tag['name'], - 'status' => isset($tag['status']) ? $tag['status'] : 'active' - ]; - } - } - $data = [ - 'tags' => $formatted_tags, + 'tags' => $this->merge_and_format_member_tags($mailchimp_member->tags, $tags), ]; try { From ef9dc58471a14f60f0043f78de0fc3079039c79b Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 12:38:38 +0100 Subject: [PATCH 3/8] add form setting to remove tagas --- config/default-form-settings.php | 1 + includes/forms/class-form.php | 25 +++++++++++++++++++-- includes/forms/views/tabs/form-settings.php | 14 +++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/config/default-form-settings.php b/config/default-form-settings.php index b2a452a4..1bf4cb55 100755 --- a/config/default-form-settings.php +++ b/config/default-form-settings.php @@ -10,5 +10,6 @@ 'required_fields' => '', 'update_existing' => 0, 'subscriber_tags' => '', + 'remove_subscriber_tags' => '', 'email_typo_check' => 0, ]; diff --git a/includes/forms/class-form.php b/includes/forms/class-form.php index 275e2b7c..2fee9564 100755 --- a/includes/forms/class-form.php +++ b/includes/forms/class-form.php @@ -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; } diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index c6222290..ea3c79e7 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,7 +95,7 @@ - +

@@ -106,6 +106,18 @@ + + + + +

+ + +

+ + + + From d5b6813ea46c9ecc312bbce00ca4f04dc8c56061 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 12:49:48 +0100 Subject: [PATCH 4/8] testing 123 --- tests/FormTest.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/FormTest.php b/tests/FormTest.php index eaa34461..7202371c 100755 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -276,9 +276,17 @@ 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'] + ]); } + + } From 94dca0bf907c1378d108ad3a54a3a00a53b67d37 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 12:52:35 +0100 Subject: [PATCH 5/8] Update FormTest.php --- tests/FormTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/FormTest.php b/tests/FormTest.php index 7202371c..223b10aa 100755 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -287,6 +287,4 @@ public function test_get_subscriber_tags() ['name' => 'tag', 'status' => 'inactive'] ]); } - - } From 4bccf59d24b8150cf79f8949c981a279b8befc74 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 14:18:03 +0100 Subject: [PATCH 6/8] Textual updates for consistency --- includes/forms/views/tabs/form-settings.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index ea3c79e7..4c6f109a 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,7 +95,7 @@ - +

@@ -107,9 +107,9 @@ - + - +

From 94f87700410ff3167b0be54422c18ad7afc66a11 Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Sat, 13 Dec 2025 14:22:04 +0100 Subject: [PATCH 7/8] textual updates for consistency in form settings view --- includes/forms/views/tabs/form-settings.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/forms/views/tabs/form-settings.php b/includes/forms/views/tabs/form-settings.php index 4c6f109a..e3d94e65 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,9 +95,9 @@ - + - +

@@ -107,9 +107,9 @@ - + - +

From 0f875580b672032b551234f4c92408d02d3538fa Mon Sep 17 00:00:00 2001 From: Arne Lap Date: Tue, 16 Dec 2025 10:53:29 +0100 Subject: [PATCH 8/8] bugfix-remove-tags-new-subscriber When submitting a new email I would get an error - tags[n] : This value should be of type string. --- includes/class-mailchimp.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/includes/class-mailchimp.php b/includes/class-mailchimp.php index 34f2a285..b32e570a 100755 --- a/includes/class-mailchimp.php +++ b/includes/class-mailchimp.php @@ -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_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;