Skip to content

Commit 2d202a9

Browse files
committed
Merge branch 'add-tag-subscribers-method' into remove-unnecessary-tests
2 parents 011f516 + 8817aee commit 2d202a9

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

src/ConvertKit_API_Traits.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,43 @@ public function update_tag_name(int $tag_id, string $name)
602602
return $this->put(sprintf('tags/%s', $tag_id), ['name' => $name]);
603603
}
604604

605+
/**
606+
* Tags the given subscribers with the given existing Tags.
607+
*
608+
* @param array<int,array<string>> $taggings Taggings, in the format:
609+
* [
610+
* [
611+
* "tag_id" => 0,
612+
* "subscriber_id" => 0
613+
* ],
614+
* [
615+
* "tag_id" => 1,
616+
* "subscriber_id" => 1
617+
* ],
618+
* ].
619+
* @param string $callback_url URL to notify for large batch size when async processing complete.
620+
*
621+
* @since 2.2.1
622+
*
623+
* @see https://developers.kit.com/api-reference/tags/bulk-tag-subscribers
624+
*
625+
* @return false|mixed
626+
*/
627+
public function tag_subscribers(array $taggings, string $callback_url = '')
628+
{
629+
// Build parameters.
630+
$options = ['taggings' => $taggings];
631+
if (!empty($callback_url)) {
632+
$options['callback_url'] = $callback_url;
633+
}
634+
635+
// Send request.
636+
return $this->post(
637+
'bulk/tags/subscribers',
638+
$options
639+
);
640+
}
641+
605642
/**
606643
* Tags a subscriber with the given existing Tag.
607644
*

tests/ConvertKitAPIKeyTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public function testCreateTagsThatExist()
276276
* Test that tag_subscribers() throws a ClientException when attempting
277277
* to tag subscribers, as this is only supported using OAuth.
278278
*
279-
* @since 2.2.0
279+
* @since 2.2.1
280280
*
281281
* @return void
282282
*/
@@ -293,6 +293,32 @@ public function testTagSubscribers()
293293
);
294294
}
295295

296+
/**
297+
* Skip this test from ConvertKitAPITest, as testTagSubscribers() above
298+
* confirms a ClientException is thrown.
299+
*
300+
* @since 2.2.1
301+
*
302+
* @return void
303+
*/
304+
public function testTagSubscribersWithInvalidTagID()
305+
{
306+
$this->markTestSkipped('testTagSubscribers() above confirms a ClientException is thrown.');
307+
}
308+
309+
/**
310+
* Skip this test from ConvertKitAPITest, as testTagSubscribers() above
311+
* confirms a ClientException is thrown.
312+
*
313+
* @since 2.2.1
314+
*
315+
* @return void
316+
*/
317+
public function testTagSubscribersWithInvalidSubscriberID()
318+
{
319+
$this->markTestSkipped('testTagSubscribers() above confirms a ClientException is thrown.');
320+
}
321+
296322
/**
297323
* Test that add_subscribers_to_forms() throws a ClientException when
298324
* attempting to add subscribers to forms, as this is only supported

tests/ConvertKitAPITest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,125 @@ public function testGetSubscriberStatsWithInvalidSubscriberID()
18181818
$result = $this->api->get_subscriber_stats(12345);
18191819
}
18201820

1821+
/**
1822+
* Test that tag_subscribers() returns the expected data.
1823+
*
1824+
* @since 2.2.1
1825+
*
1826+
* @return void
1827+
*/
1828+
public function testTagSubscribers()
1829+
{
1830+
// Create subscribers.
1831+
$subscribers = [
1832+
[
1833+
'email_address' => str_replace('@kit.com', '[email protected]', $this->generateEmailAddress()),
1834+
],
1835+
[
1836+
'email_address' => str_replace('@kit.com', '[email protected]', $this->generateEmailAddress()),
1837+
],
1838+
];
1839+
$result = $this->api->create_subscribers($subscribers);
1840+
1841+
// Set subscriber_id to ensure subscriber is unsubscribed after test.
1842+
foreach ($result->subscribers as $i => $subscriber) {
1843+
$this->subscriber_ids[] = $subscriber->id;
1844+
}
1845+
1846+
// Tag subscribers.
1847+
$result = $this->api->tag_subscribers(
1848+
[
1849+
[
1850+
'tag_id' => (int) $_ENV['CONVERTKIT_API_TAG_ID'],
1851+
'subscriber_id' => $this->subscriber_ids[0]
1852+
],
1853+
[
1854+
'tag_id' => (int) $_ENV['CONVERTKIT_API_TAG_ID'],
1855+
'subscriber_id' => $this->subscriber_ids[1]
1856+
],
1857+
]
1858+
);
1859+
1860+
// Assert no failures.
1861+
$this->assertCount(0, $result->failures);
1862+
1863+
// Confirm result is an array comprising of each subscriber that was created.
1864+
$this->assertIsArray($result->subscribers);
1865+
$this->assertCount(2, $result->subscribers);
1866+
}
1867+
1868+
/**
1869+
* Test that tag_subscribers() returns failures when an invalid
1870+
* tag ID is specified.
1871+
*
1872+
* @since 2.2.1
1873+
*
1874+
* @return void
1875+
*/
1876+
public function testTagSubscribersWithInvalidTagID()
1877+
{
1878+
// Create subscribers.
1879+
$subscribers = [
1880+
[
1881+
'email_address' => str_replace('@kit.com', '[email protected]', $this->generateEmailAddress()),
1882+
],
1883+
[
1884+
'email_address' => str_replace('@kit.com', '[email protected]', $this->generateEmailAddress()),
1885+
],
1886+
];
1887+
$result = $this->api->create_subscribers($subscribers);
1888+
1889+
// Set subscriber_id to ensure subscriber is unsubscribed after test.
1890+
foreach ($result->subscribers as $i => $subscriber) {
1891+
$this->subscriber_ids[] = $subscriber->id;
1892+
}
1893+
1894+
// Tag subscribers.
1895+
$result = $this->api->tag_subscribers(
1896+
[
1897+
[
1898+
'tag_id' => 12345,
1899+
'subscriber_id' => $this->subscriber_ids[0]
1900+
],
1901+
[
1902+
'tag_id' => 12345,
1903+
'subscriber_id' => $this->subscriber_ids[1]
1904+
],
1905+
]
1906+
);
1907+
1908+
// Assert failures.
1909+
$this->assertCount(2, $result->failures);
1910+
}
1911+
1912+
/**
1913+
* Test that tag_subscribers() returns failures when an invalid
1914+
* subscriber ID is specified.
1915+
*
1916+
* @since 2.2.1
1917+
*
1918+
* @return void
1919+
*/
1920+
public function testTagSubscribersWithInvalidSubscriberID()
1921+
{
1922+
// Tag subscribers that do not exist.
1923+
$result = $this->api->tag_subscribers(
1924+
[
1925+
[
1926+
'tag_id' => (int) $_ENV['CONVERTKIT_API_TAG_ID'],
1927+
'subscriber_id' => 12345,
1928+
],
1929+
[
1930+
'tag_id' => (int) $_ENV['CONVERTKIT_API_TAG_ID'],
1931+
'subscriber_id' => 67890,
1932+
],
1933+
]
1934+
);
1935+
1936+
// Assert failures.
1937+
$this->assertCount(2, $result->failures);
1938+
}
1939+
18211940
/**
18221941
* Test that tag_subscriber_by_email() returns the expected data.
18231942
*

0 commit comments

Comments
 (0)