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 @@ + + + + +

+ + +

+ + + + diff --git a/sample-code-snippets/forms/subscriber-tags.php b/sample-code-snippets/forms/subscriber-tags.php index 8c5946cb..b4241998 100755 --- a/sample-code-snippets/forms/subscriber-tags.php +++ b/sample-code-snippets/forms/subscriber-tags.php @@ -1,13 +1,27 @@ 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; +}); diff --git a/sample-code-snippets/integrations/contact-form-7/different-tag.php b/sample-code-snippets/integrations/contact-form-7/different-tag.php index 331c6226..bc7b55ab 100755 --- a/sample-code-snippets/integrations/contact-form-7/different-tag.php +++ b/sample-code-snippets/integrations/contact-form-7/different-tag.php @@ -1,5 +1,16 @@ tags[] = 'ES'; diff --git a/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php b/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php index 54716d65..669b4690 100755 --- a/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php +++ b/sample-code-snippets/integrations/ninja-forms/different-tag-per-form.php @@ -1,5 +1,13 @@ tags[] = 'ES'; diff --git a/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php b/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php index c927aa31..74a6f6dc 100755 --- a/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php +++ b/sample-code-snippets/integrations/woocommerce/woocommerce-subscriber-tag.php @@ -1,11 +1,26 @@ 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; +}); diff --git a/tests/FormTest.php b/tests/FormTest.php index eaa34461..223b10aa 100755 --- a/tests/FormTest.php +++ b/tests/FormTest.php @@ -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'] + ]); } }