Skip to content
Draft
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
39 changes: 37 additions & 2 deletions src/ConvertKit_API_Traits.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,41 @@ public function create_tags(array $tags, string $callback_url = '')
);
}

/**
* Bulk delete tags.
*
* @param array<int> $tag_ids Tag IDs.
* @param string $callback_url URL to notify for large batch size when async processing complete.
*
* @since 2.6.0
*
* @see https://developers.kit.com/api-reference/tags/bulk-delete-tags
*
* @return false|mixed
*/
public function delete_tags(array $tag_ids, string $callback_url = '')
{
// Build parameters.
$options = [
'tags' => [],
];
foreach ($tag_ids as $i => $tag_id) {
$options['tags'][] = [
'id' => (int) $tag_id,
];
}

if (!empty($callback_url)) {
$options['callback_url'] = $callback_url;
}

// Send request.
return $this->delete(
'bulk/tags',
$options
);
}

/**
* Updates the name of a tag.
*
Expand Down Expand Up @@ -2688,8 +2723,8 @@ public function put(string $endpoint, array $args = [])
/**
* Performs a DELETE request to the API.
*
* @param string $endpoint API Endpoint.
* @param array<string, int|string|array<string, int|string>|string> $args Request arguments.
* @param string $endpoint API Endpoint.
* @param array<string, bool|integer|float|string|null|array<int|string, array<string, int|string>|boolean|integer|float|string>> $args Request arguments.
*
* @return false|mixed
*/
Expand Down
8 changes: 7 additions & 1 deletion tests/ConvertKitAPIKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,19 @@ public function testRequestHeadersMethodWithTypeAndAuthDisabled()
*
* @return void
*/
public function testCreateTags()
public function testCreateAndDeleteTags()
{
$this->expectException(ClientException::class);
$result = $this->api->create_tags([
'Tag Test ' . mt_rand(),
'Tag Test ' . mt_rand(),
]);

$this->expectException(ClientException::class);
$result = $this->api->delete_tags([
12345,
23456,
]);
}

/**
Expand Down
38 changes: 15 additions & 23 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2410,44 +2410,36 @@ public function testCreateTagThatExists()
}

/**
* Test that create_tags() returns the expected data.
* Test that create_tags() and delete_tags() returns the expected data.
*
* @since 1.1.0
*
* @return void
*/
public function testCreateTags()
public function testCreateAndDeleteTags()
{
$tagNames = [
'Tag Test ' . mt_rand(),
'Tag Test ' . mt_rand(),
];

// Add mock handler for this API request, as the API doesn't provide
// a method to delete tags to cleanup the test.
$this->api = $this->mockResponse(
api: $this->api,
responseBody: [
'tags' => [
[
'id' => 12345,
'name' => $tagNames[0],
'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z',
],
[
'id' => 23456,
'name' => $tagNames[1],
'created_at' => date('Y-m-d') . 'T' . date('H:i:s') . 'Z',
],
],
'failures' => [],
]
);

// Create tags.
$result = $this->api->create_tags($tagNames);

// Assert no failures.
$this->assertCount(0, $result->failures);

// Build tag IDs array.
$ids = [];
foreach ($result->tags as $tag) {
$ids[] = $tag->id;
}

// Delete tags.
$result = $this->api->delete_tags($ids);

// Assert no failures.
$this->assertCount(0, $result->failures);
}

/**
Expand Down
Loading