Skip to content

Commit 22e59f1

Browse files
authored
Merge pull request #119 from Kit/add-bulk-update-subscriber-custom-field-values
Add `update_subscriber_custom_field_values` method
2 parents 7ac2f4f + 1ee2aca commit 22e59f1

5 files changed

Lines changed: 123 additions & 4 deletions

File tree

.env.dist.testing

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CONVERTKIT_API_BROADCAST_ID="8697158"
2+
CONVERTKIT_API_CUSTOM_FIELD_ID="264073"
23
CONVERTKIT_API_FORM_ID="2765139"
34
CONVERTKIT_API_FORM_ID_2="2780977"
45
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CONVERTKIT_OAUTH_CLIENT_ID=
1111
CONVERTKIT_OAUTH_CLIENT_SECRET=
1212
CONVERTKIT_OAUTH_REDIRECT_URI="https://convertkit-github.local/wp-admin/options-general.php?page=_wp_convertkit_settings"
1313
CONVERTKIT_API_BROADCAST_ID="8697158"
14+
CONVERTKIT_API_CUSTOM_FIELD_ID="264073"
1415
CONVERTKIT_API_FORM_ID="2765139"
1516
CONVERTKIT_API_FORM_ID_2="2780977"
1617
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"

src/ConvertKit_API_Traits.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,8 @@ public function create_webhook(string $url, string $event, string $parameter = '
15361536
case 'subscriber.subscriber_bounce':
15371537
case 'subscriber.subscriber_complain':
15381538
case 'purchase.purchase_create':
1539+
case 'custom_field.field_created':
1540+
case 'custom_field.field_deleted':
15391541
$eventData = ['name' => $event];
15401542
break;
15411543

@@ -1576,6 +1578,13 @@ public function create_webhook(string $url, string $event, string $parameter = '
15761578
];
15771579
break;
15781580

1581+
case 'custom_field.field_value_updated':
1582+
$eventData = [
1583+
'name' => $event,
1584+
'custom_field_id' => $parameter,
1585+
];
1586+
break;
1587+
15791588
default:
15801589
throw new \InvalidArgumentException(sprintf('The event %s is not supported', $event));
15811590
}//end switch
@@ -1685,6 +1694,36 @@ public function create_custom_fields(array $labels, string $callback_url = '')
16851694
);
16861695
}
16871696

1697+
/**
1698+
* Bulk update subscriber custom field values
1699+
*
1700+
* @param array<array<string,string|integer>> $custom_field_values Array of custom field values to update.
1701+
* - 'subscriber_id' (int) Subscriber ID.
1702+
* - 'subscriber_custom_field_id' (int) Custom Field ID.
1703+
* - 'value' (string|integer) Value to update.
1704+
* @param string $callback_url URL to notify for large batch size when async processing complete.
1705+
*
1706+
* @since 2.4.0
1707+
*
1708+
* @see https://developers.kit.com/api-reference/custom-fields/bulk-update-subscriber-custom-field-values
1709+
*
1710+
* @return mixed|object
1711+
*/
1712+
public function update_subscriber_custom_field_values(array $custom_field_values, string $callback_url = '')
1713+
{
1714+
// Build parameters.
1715+
$options = ['custom_field_values' => $custom_field_values];
1716+
if (!empty($callback_url)) {
1717+
$options['callback_url'] = $callback_url;
1718+
}
1719+
1720+
// Send request.
1721+
return $this->post(
1722+
'bulk/custom_fields/subscribers',
1723+
$options
1724+
);
1725+
}
1726+
16881727
/**
16891728
* Update a custom field.
16901729
*

tests/ConvertKitAPIKeyTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,33 @@ public function testCreateCustomFields()
475475
$result = $this->api->create_custom_fields($labels);
476476
}
477477

478+
/**
479+
* Test that update_subscriber_custom_field_values() throws a ClientException
480+
* as this is only supported using OAuth.
481+
*
482+
* @since 2.4.0
483+
*
484+
* @return void
485+
*/
486+
public function testUpdateSubscriberCustomFieldValues()
487+
{
488+
$this->expectException(ClientException::class);
489+
$result = $this->api->update_subscriber_custom_field_values(
490+
[
491+
[
492+
'subscriber_id' => 1,
493+
'subscriber_custom_field_id' => (int) $_ENV['CONVERTKIT_API_CUSTOM_FIELD_ID'],
494+
'value' => '100',
495+
],
496+
[
497+
'subscriber_id' => 2,
498+
'subscriber_custom_field_id' => (int) $_ENV['CONVERTKIT_API_CUSTOM_FIELD_ID'],
499+
'value' => '200',
500+
],
501+
]
502+
);
503+
}
504+
478505
/**
479506
* Test that get_purchases() throws a ClientException
480507
* as this is only supported using OAuth.

tests/ConvertKitAPITest.php

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4853,17 +4853,19 @@ public function testCreateWebhookWithEventParameter()
48534853
$url = 'https://webhook.site/' . str_shuffle('wfervdrtgsdewrafvwefds');
48544854
$result = $this->api->create_webhook(
48554855
url: $url,
4856-
event: 'subscriber.form_subscribe',
4857-
parameter: $_ENV['CONVERTKIT_API_FORM_ID']
4856+
event: 'custom_field.field_value_updated',
4857+
parameter: $_ENV['CONVERTKIT_API_CUSTOM_FIELD_ID']
48584858
);
48594859

48604860
// Confirm webhook created with correct data.
48614861
$this->assertArrayHasKey('webhook', get_object_vars($result));
48624862
$this->assertArrayHasKey('id', get_object_vars($result->webhook));
4863+
$this->assertArrayHasKey('account_id', get_object_vars($result->webhook));
4864+
$this->assertArrayHasKey('event', get_object_vars($result->webhook));
48634865
$this->assertArrayHasKey('target_url', get_object_vars($result->webhook));
48644866
$this->assertEquals($result->webhook->target_url, $url);
4865-
$this->assertEquals($result->webhook->event->name, 'form_subscribe');
4866-
$this->assertEquals($result->webhook->event->form_id, $_ENV['CONVERTKIT_API_FORM_ID']);
4867+
$this->assertEquals($result->webhook->event->name, 'field_value_updated');
4868+
$this->assertEquals($result->webhook->event->custom_field_id, $_ENV['CONVERTKIT_API_CUSTOM_FIELD_ID']);
48674869

48684870
// Delete the webhook.
48694871
$result = $this->api->delete_webhook($result->webhook->id);
@@ -5059,6 +5061,55 @@ public function testCreateCustomFields()
50595061
$this->assertIsArray($result->custom_fields);
50605062
}
50615063

5064+
/**
5065+
* Test that update_subscriber_custom_field_values() works.
5066+
*
5067+
* @since 2.4.0
5068+
*
5069+
* @return void
5070+
*/
5071+
public function testUpdateSubscriberCustomFieldValues()
5072+
{
5073+
// Create subscribers.
5074+
$subscribers = [
5075+
[
5076+
'email_address' => str_replace('@kit.com', '-1@kit.com', $this->generateEmailAddress()),
5077+
],
5078+
[
5079+
'email_address' => str_replace('@kit.com', '-2@kit.com', $this->generateEmailAddress()),
5080+
],
5081+
];
5082+
$result = $this->api->create_subscribers($subscribers);
5083+
5084+
// Set subscriber_id to ensure subscriber is unsubscribed after test.
5085+
foreach ($result->subscribers as $i => $subscriber) {
5086+
$this->subscriber_ids[] = $subscriber->id;
5087+
}
5088+
5089+
// Bulk update subscriber custom field values.
5090+
$result = $this->api->update_subscriber_custom_field_values(
5091+
[
5092+
[
5093+
'subscriber_id' => $this->subscriber_ids[0],
5094+
'subscriber_custom_field_id' => (int) $_ENV['CONVERTKIT_API_CUSTOM_FIELD_ID'],
5095+
'value' => '100',
5096+
],
5097+
[
5098+
'subscriber_id' => $this->subscriber_ids[1],
5099+
'subscriber_custom_field_id' => (int) $_ENV['CONVERTKIT_API_CUSTOM_FIELD_ID'],
5100+
'value' => '200',
5101+
],
5102+
]
5103+
);
5104+
5105+
// Assert no failures.
5106+
$this->assertCount(0, $result->failures);
5107+
5108+
// Confirm result is an array comprising of each custom field value that was updated.
5109+
$this->assertIsArray($result->custom_field_values);
5110+
$this->assertCount(2, $result->custom_field_values);
5111+
}
5112+
50625113
/**
50635114
* Test that update_custom_field() works.
50645115
*

0 commit comments

Comments
 (0)