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/class-mailchimp.php b/includes/class-mailchimp.php index ce3dd76a..34f2a285 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); @@ -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 { 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..e3d94e65 100755 --- a/includes/forms/views/tabs/form-settings.php +++ b/includes/forms/views/tabs/form-settings.php @@ -95,9 +95,9 @@
@@ -106,6 +106,18 @@
+ + +
+ +