Skip to content

Commit 376fcfe

Browse files
authored
Merge pull request #76 from ConvertKit/v4-api-forms
v4 API: Forms
2 parents e628055 + 02e471e commit 376fcfe

File tree

2 files changed

+286
-200
lines changed

2 files changed

+286
-200
lines changed

src/ConvertKit_API.php

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -407,99 +407,99 @@ public function get_landing_pages()
407407
}
408408

409409
/**
410-
* Adds a subscriber to a form.
411-
*
412-
* @param integer $form_id Form ID.
413-
* @param array<string, string> $options Array of user data (email, name).
414-
*
415-
* @deprecated 1.0.0 Use add_subscriber_to_form($form_id, $email, $first_name, $fields, $tag_ids).
410+
* Adds a subscriber to a form by email address
416411
*
417-
* @throws \InvalidArgumentException If the provided arguments are not of the expected type.
412+
* @param integer $form_id Form ID.
413+
* @param string $email Email Address.
418414
*
419-
* @see https://developers.convertkit.com/#add-subscriber-to-a-form
415+
* @see https://developers.convertkit.com/v4.html#add-subscriber-to-form-by-email-address
420416
*
421-
* @return false|object
417+
* @return false|mixed
422418
*/
423-
public function form_subscribe(int $form_id, array $options)
419+
public function add_subscriber_to_form(int $form_id, string $email)
424420
{
425-
// This function is deprecated in 1.0, as we prefer functions with structured arguments.
426-
trigger_error(
427-
'form_subscribe() is deprecated in 1.0.
428-
Use add_subscriber_to_form($form_id, $email, $first_name, $fields, $tag_ids) instead.',
429-
E_USER_NOTICE
430-
);
431-
432421
return $this->post(
433-
sprintf('forms/%s/subscribe', $form_id),
434-
$options
422+
endpoint: sprintf('forms/%s/subscribers', $form_id),
423+
args: ['email_address' => $email]
435424
);
436425
}
437426

438427
/**
439-
* Adds a subscriber to a form by email address
428+
* Adds a subscriber to a form by subscriber ID
440429
*
441-
* @param integer $form_id Form ID.
442-
* @param string $email Email Address.
443-
* @param string $first_name First Name.
444-
* @param array<string, string> $fields Custom Fields.
445-
* @param array<string, int> $tag_ids Tag ID(s) to subscribe to.
430+
* @param integer $form_id Form ID.
431+
* @param integer $subscriber_id Subscriber ID.
432+
*
433+
* @see https://developers.convertkit.com/v4.html#add-subscriber-to-form
446434
*
447-
* @see https://developers.convertkit.com/#add-subscriber-to-a-form
435+
* @since 2.0.0
448436
*
449437
* @return false|mixed
450438
*/
451-
public function add_subscriber_to_form(
452-
int $form_id,
453-
string $email,
454-
string $first_name = '',
455-
array $fields = [],
456-
array $tag_ids = []
457-
) {
458-
// Build parameters.
459-
$options = ['email' => $email];
460-
461-
if (!empty($first_name)) {
462-
$options['first_name'] = $first_name;
463-
}
464-
if (!empty($fields)) {
465-
$options['fields'] = $fields;
466-
}
467-
if (!empty($tag_ids)) {
468-
$options['tags'] = $tag_ids;
469-
}
470-
471-
// Send request.
472-
return $this->post(
473-
sprintf('forms/%s/subscribe', $form_id),
474-
$options
475-
);
439+
public function add_subscriber_to_form_by_subscriber_id(int $form_id, int $subscriber_id)
440+
{
441+
return $this->post(sprintf('forms/%s/subscribers/%s', $form_id, $subscriber_id));
476442
}
477443

478444
/**
479-
* List subscriptions to a form
445+
* List subscribers for a form
480446
*
481-
* @param integer $form_id Form ID.
482-
* @param string $sort_order Sort Order (asc|desc).
483-
* @param string $subscriber_state Subscriber State (active,cancelled).
484-
* @param integer $page Page.
447+
* @param integer $form_id Form ID.
448+
* @param string $subscriber_state Subscriber State (active|bounced|cancelled|complained|inactive).
449+
* @param \DateTime $created_after Filter subscribers who have been created after this date.
450+
* @param \DateTime $created_before Filter subscribers who have been created before this date.
451+
* @param \DateTime $added_after Filter subscribers who have been added to the form after this date.
452+
* @param \DateTime $added_before Filter subscribers who have been added to the form before this date.
453+
* @param string $after_cursor Return results after the given pagination cursor.
454+
* @param string $before_cursor Return results before the given pagination cursor.
455+
* @param integer $per_page Number of results to return.
485456
*
486-
* @see https://developers.convertkit.com/#list-subscriptions-to-a-form
457+
* @see https://developers.convertkit.com/v4.html#list-subscribers-for-a-form
487458
*
488459
* @return false|mixed
489460
*/
490461
public function get_form_subscriptions(
491462
int $form_id,
492-
string $sort_order = 'asc',
493463
string $subscriber_state = 'active',
494-
int $page = 1
464+
\DateTime $created_after = null,
465+
\DateTime $created_before = null,
466+
\DateTime $added_after = null,
467+
\DateTime $added_before = null,
468+
string $after_cursor = '',
469+
string $before_cursor = '',
470+
int $per_page = 100
495471
) {
472+
// Build parameters.
473+
$options = [];
474+
475+
if (!empty($subscriber_state)) {
476+
$options['status'] = $subscriber_state;
477+
}
478+
if (!is_null($created_after)) {
479+
$options['created_after'] = $created_after->format('Y-m-d');
480+
}
481+
if (!is_null($created_before)) {
482+
$options['created_before'] = $created_before->format('Y-m-d');
483+
}
484+
if (!is_null($added_after)) {
485+
$options['added_after'] = $added_after->format('Y-m-d');
486+
}
487+
if (!is_null($added_before)) {
488+
$options['added_before'] = $added_before->format('Y-m-d');
489+
}
490+
491+
// Build pagination parameters.
492+
$options = $this->build_pagination_params(
493+
params: $options,
494+
after_cursor: $after_cursor,
495+
before_cursor: $before_cursor,
496+
per_page: $per_page
497+
);
498+
499+
// Send request.
496500
return $this->get(
497-
sprintf('forms/%s/subscriptions', $form_id),
498-
[
499-
'sort_order' => $sort_order,
500-
'subscriber_state' => $subscriber_state,
501-
'page' => $page,
502-
]
501+
endpoint: sprintf('forms/%s/subscribers', $form_id),
502+
args: $options
503503
);
504504
}
505505

0 commit comments

Comments
 (0)