diff --git a/.env.dist.testing b/.env.dist.testing index 9def6ca..48cc810 100644 --- a/.env.dist.testing +++ b/.env.dist.testing @@ -6,5 +6,7 @@ CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages CONVERTKIT_API_SEQUENCE_ID="1030824" CONVERTKIT_API_TAG_NAME="wordpress" CONVERTKIT_API_TAG_ID="2744672" +CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1" +CONVERTKIT_API_TAG_ID_2="2907192" CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com" CONVERTKIT_API_SUBSCRIBER_ID="1579118532" diff --git a/.env.example b/.env.example index 2ded829..f93060f 100644 --- a/.env.example +++ b/.env.example @@ -10,5 +10,7 @@ CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages CONVERTKIT_API_SEQUENCE_ID="1030824" CONVERTKIT_API_TAG_NAME="wordpress" CONVERTKIT_API_TAG_ID="2744672" +CONVERTKIT_API_TAG_NAME_2="gravityforms-tag-1" +CONVERTKIT_API_TAG_ID_2="2907192" CONVERTKIT_API_SUBSCRIBER_EMAIL="optin@n7studios.com" CONVERTKIT_API_SUBSCRIBER_ID="1579118532" diff --git a/phpcs.xml b/phpcs.xml index 95c3996..290616f 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -68,4 +68,12 @@ + + + + + + + + \ No newline at end of file diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 9adc8b1..84d1cc1 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -22,7 +22,7 @@ class ConvertKit_API * * @var string */ - public const VERSION = '1.0.0'; + public const VERSION = '1.1.0'; /** * ConvertKit API Key @@ -393,6 +393,36 @@ public function create_tag(string $tag) ); } + /** + * Creates multiple tags. + * + * @param array $tags Tag Names. + * + * @since 1.1.0 + * + * @see https://developers.convertkit.com/#create-a-tag + * + * @return false|mixed + */ + public function create_tags(array $tags) + { + // Build API compatible array of tags. + $apiTags = []; + foreach ($tags as $i => $tag) { + $apiTags[] = [ + 'name' => (string) $tag, + ]; + } + + return $this->post( + 'tags', + [ + 'api_key' => $this->api_key, + 'tag' => $apiTags, + ] + ); + } + /** * Tags a subscriber with the given existing Tag. * @@ -1449,8 +1479,8 @@ public function get(string $endpoint, array $args = []) /** * Performs a POST request to the API. * - * @param string $endpoint API Endpoint. - * @param array|string> $args Request arguments. + * @param string $endpoint API Endpoint. + * @param array>> $args Request arguments. * * @return false|mixed */ @@ -1500,9 +1530,9 @@ public function delete(string $endpoint, array $args = []) /** * Performs an API request using Guzzle. * - * @param string $endpoint API Endpoint. - * @param string $method Request method. - * @param array|string> $args Request arguments. + * @param string $endpoint API Endpoint. + * @param string $method Request method. + * @param array>> $args Request arguments. * * @throws \Exception If JSON encoding arguments failed. * diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index b97cc3b..e918d23 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -529,6 +529,66 @@ public function testCreateTagThatExists() $result = $this->api->create_tag($_ENV['CONVERTKIT_API_TAG_NAME']); } + /** + * Test that create_tags() returns the expected data. + * + * @since 1.1.0 + * + * @return void + */ + public function testCreateTags() + { + $tagNames = [ + 'Tag Test ' . mt_rand(), + 'Tag Test ' . mt_rand(), + ]; + $result = $this->api->create_tags($tagNames); + + // Iterate through the results to confirm the tags were created. + foreach ($result as $i => $tag) { + // Convert to array to check for keys, as assertObjectHasAttribute() will be deprecated in PHPUnit 10. + $tag = get_object_vars($tag); + $this->assertArrayHasKey('id', $tag); + $this->assertArrayHasKey('name', $tag); + $this->assertArrayHasKey('created_at', $tag); + $this->assertEquals($tag['name'], $tagNames[$i]); + } + } + + /** + * Test that create_tags() throws a ClientException when creating + * blank tags. + * + * @since 1.1.0 + * + * @return void + */ + public function testCreateTagsBlank() + { + $this->expectException(GuzzleHttp\Exception\ClientException::class); + $result = $this->api->create_tags([ + '', + '', + ]); + } + + /** + * Test that create_tags() throws a ClientException when creating + * tags that already exists. + * + * @since 1.1.0 + * + * @return void + */ + public function testCreateTagsThatExist() + { + $this->expectException(GuzzleHttp\Exception\ClientException::class); + $result = $this->api->create_tags([ + $_ENV['CONVERTKIT_API_TAG_NAME'], + $_ENV['CONVERTKIT_API_TAG_NAME_2'], + ]); + } + /** * Test that tag_subscriber() returns the expected data. *