Skip to content
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
117 changes: 112 additions & 5 deletions src/ConvertKit_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,113 @@ public function get_subscriber_tags(int $subscriber_id)
);
}

/**
* List custom fields.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#list-fields
*
* @return false|object
*/
public function get_custom_fields()
{
return $this->get(
'custom_fields',
[
'api_key' => $this->api_key,
]
);
}

/**
* Creates a custom field.
*
* @param string $label Custom Field label.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#create-field
*
* @return false|object
*/
public function create_custom_field(string $label)
{
return $this->post(
'custom_fields',
[
'api_secret' => $this->api_secret,
'label' => [$label],
]
);
}

/**
* Creates multiple custom fields.
*
* @param array<string> $labels Custom Fields labels.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#create-field
*
* @return false|object
*/
public function create_custom_fields(array $labels)
{
return $this->post(
'custom_fields',
[
'api_secret' => $this->api_secret,
'label' => $labels,
]
);
}

/**
* Updates an existing custom field.
*
* @param integer $id Custom Field ID.
* @param string $label Updated Custom Field label.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#update-field
*
* @return false|object
*/
public function update_custom_field(int $id, string $label)
{
return $this->put(
sprintf('custom_fields/%s', $id),
[
'api_secret' => $this->api_secret,
'label' => $label,
]
);
}

/**
* Deletes an existing custom field.
*
* @param integer $id Custom Field ID.
*
* @since 1.0.0
*
* @see https://developers.convertkit.com/#destroy-field
*
* @return false|object
*/
public function delete_custom_field(int $id)
{
return $this->delete(
sprintf('custom_fields/%s', $id),
[
'api_secret' => $this->api_secret,
]
);
}

/**
* List purchases.
*
Expand Down Expand Up @@ -802,8 +909,8 @@ public function get(string $endpoint, array $args = [])
/**
* Performs a POST 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, int|string|array<int|string, int|string>|string> $args Request arguments.
*
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
*
Expand Down Expand Up @@ -859,9 +966,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, int|string|array<string, int|string>|string> $args Request arguments.
* @param string $endpoint API Endpoint.
* @param string $method Request method.
* @param array<string, int|string|array<int|string, int|string>|string> $args Request arguments.
*
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
* @throws \Exception If JSON encoding arguments failed.
Expand Down
173 changes: 173 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,179 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
$subscriber = $this->api->get_subscriber_tags(12345);
}

/**
* Test that get_custom_fields() returns the expected data.
*
* @since 1.0.0
*
* @return void
*/
public function testGetCustomFields()
{
$result = $this->api->get_custom_fields();
$this->assertInstanceOf('stdClass', $result);
$this->assertArrayHasKey('custom_fields', get_object_vars($result));

// Inspect first custom field.
$customField = get_object_vars($result->custom_fields[0]);
$this->assertArrayHasKey('id', $customField);
$this->assertArrayHasKey('name', $customField);
$this->assertArrayHasKey('key', $customField);
$this->assertArrayHasKey('label', $customField);
}

/**
* Test that create_custom_field() works.
*
* @since 1.0.0
*
* @return void
*/
public function testCreateCustomField()
{
$label = 'Custom Field ' . mt_rand();
$result = $this->api->create_custom_field($label);

$result = get_object_vars($result);
$this->assertArrayHasKey('id', $result);
$this->assertArrayHasKey('name', $result);
$this->assertArrayHasKey('key', $result);
$this->assertArrayHasKey('label', $result);
$this->assertEquals($result['label'], $label);

// Delete custom field.
$this->api->delete_custom_field($result['id']);
}

/**
* Test that create_custom_field() throws a ClientException when a blank
* label is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testCreateCustomFieldWithBlankLabel()
{
$this->expectException(GuzzleHttp\Exception\ClientException::class);
$this->api->create_custom_field('');
}

/**
* Test that create_custom_fields() works.
*
* @since 1.0.0
*
* @return void
*/
public function testCreateCustomFields()
{
$labels = [
'Custom Field ' . mt_rand(),
'Custom Field ' . mt_rand(),
];
$result = $this->api->create_custom_fields($labels);

// Confirm result is an array comprising of each custom field that was created.
$this->assertIsArray($result);
foreach ($result as $index => $customField) {
// Confirm individual custom field.
$customField = get_object_vars($customField);
$this->assertArrayHasKey('id', $customField);
$this->assertArrayHasKey('name', $customField);
$this->assertArrayHasKey('key', $customField);
$this->assertArrayHasKey('label', $customField);

// Confirm label is correct.
$this->assertEquals($labels[$index], $customField['label']);

// Delete custom field as tests passed.
$this->api->delete_custom_field($customField['id']);
}
}

/**
* Test that update_custom_field() works.
*
* @since 1.0.0
*
* @return void
*/
public function testUpdateCustomField()
{
// Create custom field.
$label = 'Custom Field ' . mt_rand();
$result = $this->api->create_custom_field($label);
$id = $result->id;

// Change label.
$newLabel = 'Custom Field ' . mt_rand();
$this->api->update_custom_field($id, $newLabel);

// Confirm label changed.
$customFields = $this->api->get_custom_fields();
foreach ($customFields->custom_fields as $customField) {
if ($customField->id === $id) {
$this->assertEquals($customField->label, $newLabel);
}
}

// Delete custom field as tests passed.
$this->api->delete_custom_field($id);
}

/**
* Test that update_custom_field() throws a ClientException when an
* invalid custom field ID is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testUpdateCustomFieldWithInvalidID()
{
$this->expectException(GuzzleHttp\Exception\ClientException::class);
$this->api->update_custom_field(12345, 'Something');
}

/**
* Test that delete_custom_field() works.
*
* @since 1.0.0
*
* @return void
*/
public function testDeleteCustomField()
{
// Create custom field.
$label = 'Custom Field ' . mt_rand();
$result = $this->api->create_custom_field($label);
$id = $result->id;

// Delete custom field as tests passed.
$this->api->delete_custom_field($id);

// Confirm custom field no longer exists.
$customFields = $this->api->get_custom_fields();
foreach ($customFields->custom_fields as $customField) {
$this->assertNotEquals($customField->id, $id);
}
}

/**
* Test that delete_custom_field() throws a ClientException when an
* invalid custom field ID is specified.
*
* @since 1.0.0
*
* @return void
*/
public function testDeleteCustomFieldWithInvalidID()
{
$this->expectException(GuzzleHttp\Exception\ClientException::class);
$this->api->delete_custom_field(12345);
}

/**
* Test that list_purchases() returns the expected data.
*
Expand Down