Skip to content

Support creating multiple tags #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]"
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]"
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
8 changes: 8 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@
<!-- Allow use of is_null() -->
<exclude name="Generic.PHP.ForbiddenFunctions" />
</rule>

<!-- Permit slightly longer line lengths -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="130"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
</ruleset>
42 changes: 36 additions & 6 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ConvertKit_API
*
* @var string
*/
public const VERSION = '1.0.0';
public const VERSION = '1.1.0';

/**
* ConvertKit API Key
Expand Down Expand Up @@ -393,6 +393,36 @@ public function create_tag(string $tag)
);
}

/**
* Creates multiple tags.
*
* @param array<int,string> $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.
*
Expand Down Expand Up @@ -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, bool|integer|string|array<int|string, int|string>|string> $args Request arguments.
* @param string $endpoint API Endpoint.
* @param array<string, bool|integer|string|array<int|string, int|string|array<string|string>>> $args Request arguments.
*
* @return false|mixed
*/
Expand Down Expand Up @@ -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, bool|integer|string|array<int|string, int|string>|string> $args Request arguments.
* @param string $endpoint API Endpoint.
* @param string $method Request method.
* @param array<string, bool|integer|string|array<int|string, int|string|array<string|string>>> $args Request arguments.
*
* @throws \Exception If JSON encoding arguments failed.
*
Expand Down
60 changes: 60 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down