Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/MegaChriz/lists_add_tags_member'
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenson committed Oct 4, 2019
2 parents b4637c3 + 48d45c2 commit 8644b0d
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 2 deletions.
68 changes: 66 additions & 2 deletions src/MailchimpLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/MailchimpListsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';
$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 = '[email protected]';
$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);
}

}

0 comments on commit 8644b0d

Please sign in to comment.