Skip to content

Commit 53e8485

Browse files
authored
Merge pull request #46 from ConvertKit/custom-field-functions
Add Custom Field API functions
2 parents 774d670 + 854c727 commit 53e8485

File tree

2 files changed

+285
-5
lines changed

2 files changed

+285
-5
lines changed

src/ConvertKit_API.php

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,113 @@ public function get_subscriber_tags(int $subscriber_id)
820820
);
821821
}
822822

823+
/**
824+
* List custom fields.
825+
*
826+
* @since 1.0.0
827+
*
828+
* @see https://developers.convertkit.com/#list-fields
829+
*
830+
* @return false|object
831+
*/
832+
public function get_custom_fields()
833+
{
834+
return $this->get(
835+
'custom_fields',
836+
[
837+
'api_key' => $this->api_key,
838+
]
839+
);
840+
}
841+
842+
/**
843+
* Creates a custom field.
844+
*
845+
* @param string $label Custom Field label.
846+
*
847+
* @since 1.0.0
848+
*
849+
* @see https://developers.convertkit.com/#create-field
850+
*
851+
* @return false|object
852+
*/
853+
public function create_custom_field(string $label)
854+
{
855+
return $this->post(
856+
'custom_fields',
857+
[
858+
'api_secret' => $this->api_secret,
859+
'label' => [$label],
860+
]
861+
);
862+
}
863+
864+
/**
865+
* Creates multiple custom fields.
866+
*
867+
* @param array<string> $labels Custom Fields labels.
868+
*
869+
* @since 1.0.0
870+
*
871+
* @see https://developers.convertkit.com/#create-field
872+
*
873+
* @return false|object
874+
*/
875+
public function create_custom_fields(array $labels)
876+
{
877+
return $this->post(
878+
'custom_fields',
879+
[
880+
'api_secret' => $this->api_secret,
881+
'label' => $labels,
882+
]
883+
);
884+
}
885+
886+
/**
887+
* Updates an existing custom field.
888+
*
889+
* @param integer $id Custom Field ID.
890+
* @param string $label Updated Custom Field label.
891+
*
892+
* @since 1.0.0
893+
*
894+
* @see https://developers.convertkit.com/#update-field
895+
*
896+
* @return false|object
897+
*/
898+
public function update_custom_field(int $id, string $label)
899+
{
900+
return $this->put(
901+
sprintf('custom_fields/%s', $id),
902+
[
903+
'api_secret' => $this->api_secret,
904+
'label' => $label,
905+
]
906+
);
907+
}
908+
909+
/**
910+
* Deletes an existing custom field.
911+
*
912+
* @param integer $id Custom Field ID.
913+
*
914+
* @since 1.0.0
915+
*
916+
* @see https://developers.convertkit.com/#destroy-field
917+
*
918+
* @return false|object
919+
*/
920+
public function delete_custom_field(int $id)
921+
{
922+
return $this->delete(
923+
sprintf('custom_fields/%s', $id),
924+
[
925+
'api_secret' => $this->api_secret,
926+
]
927+
);
928+
}
929+
823930
/**
824931
* List purchases.
825932
*
@@ -1033,8 +1140,8 @@ public function get(string $endpoint, array $args = [])
10331140
/**
10341141
* Performs a POST request to the API.
10351142
*
1036-
* @param string $endpoint API Endpoint.
1037-
* @param array<string, int|string|array<string, int|string>|string> $args Request arguments.
1143+
* @param string $endpoint API Endpoint.
1144+
* @param array<string, int|string|array<int|string, int|string>|string> $args Request arguments.
10381145
*
10391146
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
10401147
*
@@ -1090,9 +1197,9 @@ public function delete(string $endpoint, array $args = [])
10901197
/**
10911198
* Performs an API request using Guzzle.
10921199
*
1093-
* @param string $endpoint API Endpoint.
1094-
* @param string $method Request method.
1095-
* @param array<string, int|string|array<string, int|string>|string> $args Request arguments.
1200+
* @param string $endpoint API Endpoint.
1201+
* @param string $method Request method.
1202+
* @param array<string, int|string|array<int|string, int|string>|string> $args Request arguments.
10961203
*
10971204
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
10981205
* @throws \Exception If JSON encoding arguments failed.

tests/ConvertKitAPITest.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,179 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
12371237
$subscriber = $this->api->get_subscriber_tags(12345);
12381238
}
12391239

1240+
/**
1241+
* Test that get_custom_fields() returns the expected data.
1242+
*
1243+
* @since 1.0.0
1244+
*
1245+
* @return void
1246+
*/
1247+
public function testGetCustomFields()
1248+
{
1249+
$result = $this->api->get_custom_fields();
1250+
$this->assertInstanceOf('stdClass', $result);
1251+
$this->assertArrayHasKey('custom_fields', get_object_vars($result));
1252+
1253+
// Inspect first custom field.
1254+
$customField = get_object_vars($result->custom_fields[0]);
1255+
$this->assertArrayHasKey('id', $customField);
1256+
$this->assertArrayHasKey('name', $customField);
1257+
$this->assertArrayHasKey('key', $customField);
1258+
$this->assertArrayHasKey('label', $customField);
1259+
}
1260+
1261+
/**
1262+
* Test that create_custom_field() works.
1263+
*
1264+
* @since 1.0.0
1265+
*
1266+
* @return void
1267+
*/
1268+
public function testCreateCustomField()
1269+
{
1270+
$label = 'Custom Field ' . mt_rand();
1271+
$result = $this->api->create_custom_field($label);
1272+
1273+
$result = get_object_vars($result);
1274+
$this->assertArrayHasKey('id', $result);
1275+
$this->assertArrayHasKey('name', $result);
1276+
$this->assertArrayHasKey('key', $result);
1277+
$this->assertArrayHasKey('label', $result);
1278+
$this->assertEquals($result['label'], $label);
1279+
1280+
// Delete custom field.
1281+
$this->api->delete_custom_field($result['id']);
1282+
}
1283+
1284+
/**
1285+
* Test that create_custom_field() throws a ClientException when a blank
1286+
* label is specified.
1287+
*
1288+
* @since 1.0.0
1289+
*
1290+
* @return void
1291+
*/
1292+
public function testCreateCustomFieldWithBlankLabel()
1293+
{
1294+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
1295+
$this->api->create_custom_field('');
1296+
}
1297+
1298+
/**
1299+
* Test that create_custom_fields() works.
1300+
*
1301+
* @since 1.0.0
1302+
*
1303+
* @return void
1304+
*/
1305+
public function testCreateCustomFields()
1306+
{
1307+
$labels = [
1308+
'Custom Field ' . mt_rand(),
1309+
'Custom Field ' . mt_rand(),
1310+
];
1311+
$result = $this->api->create_custom_fields($labels);
1312+
1313+
// Confirm result is an array comprising of each custom field that was created.
1314+
$this->assertIsArray($result);
1315+
foreach ($result as $index => $customField) {
1316+
// Confirm individual custom field.
1317+
$customField = get_object_vars($customField);
1318+
$this->assertArrayHasKey('id', $customField);
1319+
$this->assertArrayHasKey('name', $customField);
1320+
$this->assertArrayHasKey('key', $customField);
1321+
$this->assertArrayHasKey('label', $customField);
1322+
1323+
// Confirm label is correct.
1324+
$this->assertEquals($labels[$index], $customField['label']);
1325+
1326+
// Delete custom field as tests passed.
1327+
$this->api->delete_custom_field($customField['id']);
1328+
}
1329+
}
1330+
1331+
/**
1332+
* Test that update_custom_field() works.
1333+
*
1334+
* @since 1.0.0
1335+
*
1336+
* @return void
1337+
*/
1338+
public function testUpdateCustomField()
1339+
{
1340+
// Create custom field.
1341+
$label = 'Custom Field ' . mt_rand();
1342+
$result = $this->api->create_custom_field($label);
1343+
$id = $result->id;
1344+
1345+
// Change label.
1346+
$newLabel = 'Custom Field ' . mt_rand();
1347+
$this->api->update_custom_field($id, $newLabel);
1348+
1349+
// Confirm label changed.
1350+
$customFields = $this->api->get_custom_fields();
1351+
foreach ($customFields->custom_fields as $customField) {
1352+
if ($customField->id === $id) {
1353+
$this->assertEquals($customField->label, $newLabel);
1354+
}
1355+
}
1356+
1357+
// Delete custom field as tests passed.
1358+
$this->api->delete_custom_field($id);
1359+
}
1360+
1361+
/**
1362+
* Test that update_custom_field() throws a ClientException when an
1363+
* invalid custom field ID is specified.
1364+
*
1365+
* @since 1.0.0
1366+
*
1367+
* @return void
1368+
*/
1369+
public function testUpdateCustomFieldWithInvalidID()
1370+
{
1371+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
1372+
$this->api->update_custom_field(12345, 'Something');
1373+
}
1374+
1375+
/**
1376+
* Test that delete_custom_field() works.
1377+
*
1378+
* @since 1.0.0
1379+
*
1380+
* @return void
1381+
*/
1382+
public function testDeleteCustomField()
1383+
{
1384+
// Create custom field.
1385+
$label = 'Custom Field ' . mt_rand();
1386+
$result = $this->api->create_custom_field($label);
1387+
$id = $result->id;
1388+
1389+
// Delete custom field as tests passed.
1390+
$this->api->delete_custom_field($id);
1391+
1392+
// Confirm custom field no longer exists.
1393+
$customFields = $this->api->get_custom_fields();
1394+
foreach ($customFields->custom_fields as $customField) {
1395+
$this->assertNotEquals($customField->id, $id);
1396+
}
1397+
}
1398+
1399+
/**
1400+
* Test that delete_custom_field() throws a ClientException when an
1401+
* invalid custom field ID is specified.
1402+
*
1403+
* @since 1.0.0
1404+
*
1405+
* @return void
1406+
*/
1407+
public function testDeleteCustomFieldWithInvalidID()
1408+
{
1409+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
1410+
$this->api->delete_custom_field(12345);
1411+
}
1412+
12401413
/**
12411414
* Test that list_purchases() returns the expected data.
12421415
*

0 commit comments

Comments
 (0)