diff --git a/src/MailchimpLists.php b/src/MailchimpLists.php index f39027f..cb83b15 100644 --- a/src/MailchimpLists.php +++ b/src/MailchimpLists.php @@ -467,7 +467,7 @@ public function getSegmentMembers($list_id, $segment_id, $parameters = []) { * The ID of the list. * @param string $segment_id * The ID of the segment. - * @param array $email + * @param string $email * The email address to add to the segment. * @param array $parameters * Associative array of optional request parameters. @@ -496,7 +496,7 @@ public function addSegmentMember($list_id, $segment_id, $email, $parameters = [] * The ID of the list. * @param string $segment_id * The ID of the segment. - * @param array $email + * @param string $email * The email address to remove from the segment. * * @return object @@ -513,6 +513,70 @@ public function removeSegmentMember($list_id, $segment_id, $email) { return $this->request('DELETE', '/lists/{list_id}/segments/{segment_id}/members/{subscriber_hash}', $tokens); } + /** + * Adds tags to a member. + * + * @param string $list_id + * The ID of the list. + * @param string[] $tags + * A list of tags to add. + * @param string $email + * The email address to add the tag to. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/ + */ + public function addTagsMember($list_id, array $tags, $email, array $parameters = []) { + $tokens = [ + 'list_id' => $list_id, + 'subscriber_hash' => md5(strtolower($email)), + ]; + + foreach ($tags as $tag) { + $parameters['tags'][] = [ + 'name' => $tag, + 'status' => 'active', + ]; + } + + return $this->request('POST', '/lists/{list_id}/members/{subscriber_hash}/tags', $tokens, $parameters); + } + + /** + * Removes tags from a member. + * + * @param string $list_id + * The ID of the list. + * @param string[] $tags + * A list of tags to remove. + * @param string $email + * The email address to remove the tag from. + * @param array $parameters + * Associative array of optional request parameters. + * + * @return object + * + * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/ + */ + public function removeTagsMember($list_id, array $tags, $email, array $parameters = []) { + $tokens = [ + 'list_id' => $list_id, + 'subscriber_hash' => md5(strtolower($email)), + ]; + + foreach ($tags as $tag) { + $parameters['tags'][] = [ + 'name' => $tag, + 'status' => 'inactive', + ]; + } + + return $this->request('POST', '/lists/{list_id}/members/{subscriber_hash}/tags', $tokens, $parameters); + } + /** * Gets information about webhooks associated with a list. * diff --git a/tests/MailchimpListsTest.php b/tests/MailchimpListsTest.php index 0a57f71..0cdfe80 100644 --- a/tests/MailchimpListsTest.php +++ b/tests/MailchimpListsTest.php @@ -316,4 +316,64 @@ public function testRemoveSegmentMember() { $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id . '/members/' . md5($email), $mc->getClient()->uri); } + /** + * Tests library functionality for adding tags to a member. + */ + public function testAddTagsMember() { + $list_id = '205d96e6b4'; + $tags = ['Foo', 'Bar']; + $email = 'test@example.com'; + $mc = new MailchimpLists(); + $mc->addTagsMember($list_id, $tags, $email); + + $this->assertEquals('POST', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/tags', $mc->getClient()->uri); + + $this->assertNotEmpty($mc->getClient()->options['json']); + + $request_body = $mc->getClient()->options['json']; + + $expected = [ + [ + 'name' => 'Foo', + 'status' => 'active', + ], + [ + 'name' => 'Bar', + 'status' => 'active', + ], + ]; + $this->assertEquals($expected, $request_body->tags); + } + + /** + * Tests library functionality for removing tags from a member. + */ + public function testRemoveTagsMember() { + $list_id = '205d96e6b4'; + $tags = ['Foo', 'Bar']; + $email = 'test@example.com'; + $mc = new MailchimpLists(); + $mc->removeTagsMember($list_id, $tags, $email); + + $this->assertEquals('POST', $mc->getClient()->method); + $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/tags', $mc->getClient()->uri); + + $this->assertNotEmpty($mc->getClient()->options['json']); + + $request_body = $mc->getClient()->options['json']; + + $expected = [ + [ + 'name' => 'Foo', + 'status' => 'inactive', + ], + [ + 'name' => 'Bar', + 'status' => 'inactive', + ], + ]; + $this->assertEquals($expected, $request_body->tags); + } + }