Skip to content

Commit 9729e6d

Browse files
authored
Merge pull request #45 from ConvertKit/subscriber-functions
Add Subscriber API functions
2 parents 84e0fe6 + 9e61ac0 commit 9729e6d

File tree

2 files changed

+323
-58
lines changed

2 files changed

+323
-58
lines changed

src/ConvertKit_API.php

Lines changed: 123 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,34 @@ public function get_landing_pages()
174174
return $this->get_resources('landing_pages');
175175
}
176176

177+
/**
178+
* Adds a subscriber to a form.
179+
*
180+
* @param integer $form_id Form ID.
181+
* @param array<string, string> $options Array of user data (email, name).
182+
*
183+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
184+
*
185+
* @return false|object
186+
*/
187+
public function form_subscribe(int $form_id, array $options)
188+
{
189+
if (!is_int($form_id)) {
190+
throw new \InvalidArgumentException();
191+
}
192+
if (!is_array($options)) {
193+
throw new \InvalidArgumentException();
194+
}
195+
196+
// Add API Key to array of options.
197+
$options['api_key'] = $this->api_key;
198+
199+
return $this->post(
200+
sprintf('forms/%s/subscribe', $form_id),
201+
$options
202+
);
203+
}
204+
177205
/**
178206
* List subscriptions to a form
179207
*
@@ -452,55 +480,6 @@ public function get_resources(string $resource)
452480
return $this->resources[$resource];
453481
}
454482

455-
/**
456-
* Adds a subscriber to a form.
457-
*
458-
* @param integer $form_id Form ID.
459-
* @param array<string, string> $options Array of user data (email, name).
460-
*
461-
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
462-
*
463-
* @return false|object
464-
*/
465-
public function form_subscribe(int $form_id, array $options)
466-
{
467-
if (!is_int($form_id)) {
468-
throw new \InvalidArgumentException();
469-
}
470-
if (!is_array($options)) {
471-
throw new \InvalidArgumentException();
472-
}
473-
474-
// Add API Key to array of options.
475-
$options['api_key'] = $this->api_key;
476-
477-
return $this->post(
478-
sprintf('forms/%s/subscribe', $form_id),
479-
$options
480-
);
481-
}
482-
483-
/**
484-
* Remove subscription from a form
485-
*
486-
* @param array<string, string> $options Array of user data (email).
487-
*
488-
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
489-
*
490-
* @return false|object
491-
*/
492-
public function form_unsubscribe(array $options)
493-
{
494-
if (!is_array($options)) {
495-
throw new \InvalidArgumentException();
496-
}
497-
498-
// Add API Secret to array of options.
499-
$options['api_secret'] = $this->api_secret;
500-
501-
return $this->put('unsubscribe', $options);
502-
}
503-
504483
/**
505484
* Get the ConvertKit subscriber ID associated with email address if it exists.
506485
* Return false if subscriber not found.
@@ -524,7 +503,6 @@ public function get_subscriber_id(string $email_address)
524503
'subscribers',
525504
[
526505
'api_secret' => $this->api_secret,
527-
'status' => 'all',
528506
'email_address' => $email_address,
529507
]
530508
);
@@ -548,6 +526,8 @@ public function get_subscriber_id(string $email_address)
548526
*
549527
* @param integer $subscriber_id Subscriber ID.
550528
*
529+
* @see https://developers.convertkit.com/#view-a-single-subscriber
530+
*
551531
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
552532
*
553533
* @return false|integer
@@ -566,11 +546,104 @@ public function get_subscriber(int $subscriber_id)
566546
);
567547
}
568548

549+
/**
550+
* Updates the information for a single subscriber.
551+
*
552+
* @param integer $subscriber_id Existing Subscriber ID.
553+
* @param string $first_name New First Name.
554+
* @param string $email_address New Email Address.
555+
* @param array<string, string> $fields Updated Custom Fields.
556+
*
557+
* @see https://developers.convertkit.com/#update-subscriber
558+
*
559+
* @return false|mixed
560+
*/
561+
public function update_subscriber(
562+
int $subscriber_id,
563+
string $first_name = '',
564+
string $email_address = '',
565+
array $fields = []
566+
) {
567+
// Build parameters.
568+
$options = [
569+
'api_secret' => $this->api_secret,
570+
];
571+
572+
if (!empty($first_name)) {
573+
$options['first_name'] = $first_name;
574+
}
575+
if (!empty($email_address)) {
576+
$options['email_address'] = $email_address;
577+
}
578+
if (!empty($fields)) {
579+
$options['fields'] = $fields;
580+
}
581+
582+
// Send request.
583+
return $this->put(
584+
sprintf('subscribers/%s', $subscriber_id),
585+
$options
586+
);
587+
}
588+
589+
/**
590+
* Unsubscribe an email address from all forms and sequences.
591+
*
592+
* @param string $email Email Address.
593+
*
594+
* @see https://developers.convertkit.com/#unsubscribe-subscriber
595+
*
596+
* @return false|object
597+
*/
598+
public function unsubscribe(string $email)
599+
{
600+
return $this->put(
601+
'unsubscribe',
602+
[
603+
'api_secret' => $this->api_secret,
604+
'email' => $email,
605+
]
606+
);
607+
}
608+
609+
/**
610+
* Remove subscription from a form
611+
*
612+
* @param array<string, string> $options Array of user data (email).
613+
*
614+
* @see https://developers.convertkit.com/#unsubscribe-subscriber
615+
*
616+
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
617+
*
618+
* @return false|object
619+
*/
620+
public function form_unsubscribe(array $options)
621+
{
622+
// This function is deprecated in 1.0, as we prefer functions with structured arguments.
623+
// This function name is also misleading, as it doesn't just unsubscribe the email
624+
// address from forms.
625+
trigger_error(
626+
'form_unsubscribe() is deprecated in 1.0. Use unsubscribe($email) instead.',
627+
E_USER_NOTICE
628+
);
629+
630+
if (!is_array($options)) {
631+
throw new \InvalidArgumentException();
632+
}
633+
634+
// Add API Secret to array of options.
635+
$options['api_secret'] = $this->api_secret;
636+
637+
return $this->put('unsubscribe', $options);
638+
}
639+
569640
/**
570641
* Get a list of the tags for a subscriber.
571642
*
572643
* @param integer $subscriber_id Subscriber ID.
573644
*
645+
* @see https://developers.convertkit.com/#list-tags-for-a-subscriber
646+
*
574647
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
575648
*
576649
* @return false|array<int,\stdClass>

0 commit comments

Comments
 (0)