Skip to content

Commit 760df32

Browse files
authored
Merge pull request #62 from ConvertKit/create-multiple-tags
Support creating multiple tags
2 parents 7e6d394 + 037a16a commit 760df32

File tree

5 files changed

+108
-6
lines changed

5 files changed

+108
-6
lines changed

.env.dist.testing

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages
66
CONVERTKIT_API_SEQUENCE_ID="1030824"
77
CONVERTKIT_API_TAG_NAME="wordpress"
88
CONVERTKIT_API_TAG_ID="2744672"
9+
CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1"
10+
CONVERTKIT_API_TAG_ID_2="2907192"
911
CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com"
1012
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages
1010
CONVERTKIT_API_SEQUENCE_ID="1030824"
1111
CONVERTKIT_API_TAG_NAME="wordpress"
1212
CONVERTKIT_API_TAG_ID="2744672"
13+
CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1"
14+
CONVERTKIT_API_TAG_ID_2="2907192"
1315
CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com"
1416
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"

phpcs.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,12 @@
6868
<!-- Allow use of is_null() -->
6969
<exclude name="Generic.PHP.ForbiddenFunctions" />
7070
</rule>
71+
72+
<!-- Permit slightly longer line lengths -->
73+
<rule ref="Generic.Files.LineLength">
74+
<properties>
75+
<property name="lineLimit" value="130"/>
76+
<property name="absoluteLineLimit" value="0"/>
77+
</properties>
78+
</rule>
7179
</ruleset>

src/ConvertKit_API.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ConvertKit_API
2222
*
2323
* @var string
2424
*/
25-
public const VERSION = '1.0.0';
25+
public const VERSION = '1.1.0';
2626

2727
/**
2828
* ConvertKit API Key
@@ -393,6 +393,36 @@ public function create_tag(string $tag)
393393
);
394394
}
395395

396+
/**
397+
* Creates multiple tags.
398+
*
399+
* @param array<int,string> $tags Tag Names.
400+
*
401+
* @since 1.1.0
402+
*
403+
* @see https://developers.convertkit.com/#create-a-tag
404+
*
405+
* @return false|mixed
406+
*/
407+
public function create_tags(array $tags)
408+
{
409+
// Build API compatible array of tags.
410+
$apiTags = [];
411+
foreach ($tags as $i => $tag) {
412+
$apiTags[] = [
413+
'name' => (string) $tag,
414+
];
415+
}
416+
417+
return $this->post(
418+
'tags',
419+
[
420+
'api_key' => $this->api_key,
421+
'tag' => $apiTags,
422+
]
423+
);
424+
}
425+
396426
/**
397427
* Tags a subscriber with the given existing Tag.
398428
*
@@ -1449,8 +1479,8 @@ public function get(string $endpoint, array $args = [])
14491479
/**
14501480
* Performs a POST request to the API.
14511481
*
1452-
* @param string $endpoint API Endpoint.
1453-
* @param array<string, bool|integer|string|array<int|string, int|string>|string> $args Request arguments.
1482+
* @param string $endpoint API Endpoint.
1483+
* @param array<string, bool|integer|string|array<int|string, int|string|array<string|string>>> $args Request arguments.
14541484
*
14551485
* @return false|mixed
14561486
*/
@@ -1500,9 +1530,9 @@ public function delete(string $endpoint, array $args = [])
15001530
/**
15011531
* Performs an API request using Guzzle.
15021532
*
1503-
* @param string $endpoint API Endpoint.
1504-
* @param string $method Request method.
1505-
* @param array<string, bool|integer|string|array<int|string, int|string>|string> $args Request arguments.
1533+
* @param string $endpoint API Endpoint.
1534+
* @param string $method Request method.
1535+
* @param array<string, bool|integer|string|array<int|string, int|string|array<string|string>>> $args Request arguments.
15061536
*
15071537
* @throws \Exception If JSON encoding arguments failed.
15081538
*

tests/ConvertKitAPITest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,66 @@ public function testCreateTagThatExists()
529529
$result = $this->api->create_tag($_ENV['CONVERTKIT_API_TAG_NAME']);
530530
}
531531

532+
/**
533+
* Test that create_tags() returns the expected data.
534+
*
535+
* @since 1.1.0
536+
*
537+
* @return void
538+
*/
539+
public function testCreateTags()
540+
{
541+
$tagNames = [
542+
'Tag Test ' . mt_rand(),
543+
'Tag Test ' . mt_rand(),
544+
];
545+
$result = $this->api->create_tags($tagNames);
546+
547+
// Iterate through the results to confirm the tags were created.
548+
foreach ($result as $i => $tag) {
549+
// Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10.
550+
$tag = get_object_vars($tag);
551+
$this->assertArrayHasKey('id', $tag);
552+
$this->assertArrayHasKey('name', $tag);
553+
$this->assertArrayHasKey('created_at', $tag);
554+
$this->assertEquals($tag['name'], $tagNames[$i]);
555+
}
556+
}
557+
558+
/**
559+
* Test that create_tags() throws a ClientException when creating
560+
* blank tags.
561+
*
562+
* @since 1.1.0
563+
*
564+
* @return void
565+
*/
566+
public function testCreateTagsBlank()
567+
{
568+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
569+
$result = $this->api->create_tags([
570+
'',
571+
'',
572+
]);
573+
}
574+
575+
/**
576+
* Test that create_tags() throws a ClientException when creating
577+
* tags that already exists.
578+
*
579+
* @since 1.1.0
580+
*
581+
* @return void
582+
*/
583+
public function testCreateTagsThatExist()
584+
{
585+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
586+
$result = $this->api->create_tags([
587+
$_ENV['CONVERTKIT_API_TAG_NAME'],
588+
$_ENV['CONVERTKIT_API_TAG_NAME_2'],
589+
]);
590+
}
591+
532592
/**
533593
* Test that tag_subscriber() returns the expected data.
534594
*

0 commit comments

Comments
 (0)