From 48e27ccb3b37a33467500a8180464da27010d5c7 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 15:33:33 +0000 Subject: [PATCH 1/3] Added custom field API functions --- src/ConvertKit_API.php | 107 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 5 deletions(-) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index 1921d42..ebc3b87 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -589,6 +589,103 @@ public function get_subscriber_tags(int $subscriber_id) ); } + /** + * List custom fields. + * + * @since 1.0.0 + * + * @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 + * + * @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 $labels Custom Fields labels. + * + * @since 1.0.0 + * + * @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 + * + * @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 + * + * @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. * @@ -802,8 +899,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|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @@ -859,9 +956,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|string> $args Request arguments. * * @throws \InvalidArgumentException If the provided arguments are not of the expected type. * @throws \Exception If JSON encoding arguments failed. From 51f24d7a0b3e5c224afb0449923652d25084508e Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 16:10:06 +0000 Subject: [PATCH 2/3] Added tests --- tests/ConvertKitAPITest.php | 173 ++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index b0dda34..3b8ed88 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -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. * From 854c727b562829e427bc08f9f6c2db83ca440893 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 13 Mar 2023 16:23:49 +0000 Subject: [PATCH 3/3] Add missing API doc links --- src/ConvertKit_API.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/ConvertKit_API.php b/src/ConvertKit_API.php index ebc3b87..267fe45 100644 --- a/src/ConvertKit_API.php +++ b/src/ConvertKit_API.php @@ -594,6 +594,8 @@ public function get_subscriber_tags(int $subscriber_id) * * @since 1.0.0 * + * @see https://developers.convertkit.com/#list-fields + * * @return false|object */ public function get_custom_fields() @@ -613,6 +615,8 @@ public function get_custom_fields() * * @since 1.0.0 * + * @see https://developers.convertkit.com/#create-field + * * @return false|object */ public function create_custom_field(string $label) @@ -633,6 +637,8 @@ public function create_custom_field(string $label) * * @since 1.0.0 * + * @see https://developers.convertkit.com/#create-field + * * @return false|object */ public function create_custom_fields(array $labels) @@ -654,6 +660,8 @@ public function create_custom_fields(array $labels) * * @since 1.0.0 * + * @see https://developers.convertkit.com/#update-field + * * @return false|object */ public function update_custom_field(int $id, string $label) @@ -674,6 +682,8 @@ public function update_custom_field(int $id, string $label) * * @since 1.0.0 * + * @see https://developers.convertkit.com/#destroy-field + * * @return false|object */ public function delete_custom_field(int $id)