Skip to content

Commit 2e76f98

Browse files
committed
Added missing sequence API calls
1 parent c60e6bf commit 2e76f98

File tree

2 files changed

+179
-68
lines changed

2 files changed

+179
-68
lines changed

src/ConvertKit_API.php

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ public function get_account()
152152

153153
/**
154154
* Gets all sequences
155+
*
156+
* @see https://developers.convertkit.com/#list-sequences
155157
*
156158
* @return false|mixed
157159
*/
@@ -166,39 +168,68 @@ public function get_sequences()
166168
}
167169

168170
/**
169-
* Gets subscribers to a sequence
171+
* Adds a subscriber to a sequence by email address
170172
*
171-
* @param integer $sequence_id Sequence ID.
172-
* @param string $sort_order Sort Order (asc|desc).
173+
* @param integer $sequence_id Sequence ID.
174+
* @param string $email Email Address.
175+
* @param string $first_name First Name.
176+
* @param array<string, string> $fields Custom Fields.
177+
* @param array<string, int> $tag_ids Tag ID(s) to subscribe to.
178+
*
179+
* @see https://developers.convertkit.com/#add-subscriber-to-a-sequence
173180
*
174181
* @return false|mixed
175182
*/
176-
public function get_sequence_subscriptions(int $sequence_id, string $sort_order = 'asc')
183+
public function add_subscriber_to_sequence(int $sequence_id, string $email, string $first_name = '', array $fields = [], array $tag_ids = [])
177184
{
178-
return $this->get(
179-
sprintf('sequences/%s/subscriptions', $sequence_id),
180-
[
181-
'api_secret' => $this->api_secret,
182-
'sort_order' => $sort_order,
183-
]
185+
// Build parameters.
186+
$options = [
187+
'api_key' => $this->api_key,
188+
'email' => $email,
189+
];
190+
191+
if (!empty($first_name)) {
192+
$options['first_name'] = $first_name;
193+
}
194+
if (!empty($fields)) {
195+
$options['fields'] = $fields;
196+
}
197+
if (!empty($tags)) {
198+
$options['tags'] = $tag_ids;
199+
}
200+
201+
// Send request.
202+
return $this->post(
203+
sprintf('sequences/%s/subscribe', $sequence_id),
204+
$options
184205
);
185206
}
186207

187208
/**
188-
* Adds a subscriber to a sequence by email address
209+
* Gets subscribers to a sequence
189210
*
190-
* @param integer $sequence_id Sequence ID.
191-
* @param string $email Email Address.
211+
* @param integer $sequence_id Sequence ID.
212+
* @param string $sort_order Sort Order (asc|desc).
213+
* @param string $subscriber_state Subscriber State (active,cancelled).
214+
* @param integer $page Page.
215+
*
216+
* @see https://developers.convertkit.com/#list-subscriptions-to-a-sequence
192217
*
193218
* @return false|mixed
194219
*/
195-
public function add_subscriber_to_sequence(int $sequence_id, string $email)
196-
{
197-
return $this->post(
198-
sprintf('courses/%s/subscribe', $sequence_id),
220+
public function get_sequence_subscriptions(
221+
int $sequence_id,
222+
string $sort_order = 'asc',
223+
string $subscriber_state = 'active',
224+
int $page = 1
225+
){
226+
return $this->get(
227+
sprintf('sequences/%s/subscriptions', $sequence_id),
199228
[
200-
'api_key' => $this->api_key,
201-
'email' => $email,
229+
'api_secret' => $this->api_secret,
230+
'sort_order' => $sort_order,
231+
'subscriber_state' => $subscriber_state,
232+
'page' => $page,
202233
]
203234
);
204235
}

tests/ConvertKitAPITest.php

Lines changed: 129 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,131 @@ public function testGetSequences()
8686
$this->assertArrayHasKey('created_at', $sequence);
8787
}
8888

89+
/**
90+
* Test that add_subscriber_to_sequence() returns the expected data.
91+
*
92+
* @since 1.0.0
93+
*
94+
* @return void
95+
*/
96+
public function testAddSubscriberToSequence()
97+
{
98+
$result = $this->api->add_subscriber_to_sequence(
99+
$_ENV['CONVERTKIT_API_SEQUENCE_ID'],
100+
$this->generateEmailAddress()
101+
);
102+
$this->assertInstanceOf('stdClass', $result);
103+
$this->assertArrayHasKey('subscription', get_object_vars($result));
104+
}
105+
106+
/**
107+
* Test that add_subscriber_to_sequence() throws a ClientException when an invalid
108+
* sequence is specified.
109+
*
110+
* @since 1.0.0
111+
*
112+
* @return void
113+
*/
114+
public function testAddSubscriberToSequenceWithInvalidSequenceID()
115+
{
116+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
117+
$result = $this->api->add_subscriber_to_sequence(12345, $this->generateEmailAddress());
118+
}
119+
120+
/**
121+
* Test that add_subscriber_to_sequence() throws a ClientException when an invalid
122+
* email address is specified.
123+
*
124+
* @since 1.0.0
125+
*
126+
* @return void
127+
*/
128+
public function testAddSubscriberToSequenceWithInvalidEmailAddress()
129+
{
130+
$this->expectException(GuzzleHttp\Exception\ClientException::class);
131+
$result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], 'not-an-email-address');
132+
}
133+
134+
/**
135+
* Test that add_subscriber_to_sequence() returns the expected data
136+
* when a first_name parameter is included.
137+
*
138+
* @since 1.0.0
139+
*
140+
* @return void
141+
*/
142+
public function testAddSubscriberToSequenceWithFirstName()
143+
{
144+
$emailAddress = $this->generateEmailAddress();
145+
$firstName = 'First Name';
146+
$result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], $emailAddress, $firstName);
147+
148+
$this->assertInstanceOf('stdClass', $result);
149+
$this->assertArrayHasKey('subscription', get_object_vars($result));
150+
151+
// Fetch subscriber from API to confirm the first name was saved.
152+
$subscriber = $this->api->get_subscriber($result->subscription->subscriber->id);
153+
$this->assertEquals($subscriber->subscriber->email_address, $emailAddress);
154+
$this->assertEquals($subscriber->subscriber->first_name, $firstName);
155+
}
156+
157+
/**
158+
* Test that add_subscriber_to_sequence() returns the expected data
159+
* when custom field data is included.
160+
*
161+
* @since 1.0.0
162+
*
163+
* @return void
164+
*/
165+
public function testAddSubscriberToSequenceWithCustomFields()
166+
{
167+
$result = $this->api->add_subscriber_to_sequence(
168+
$_ENV['CONVERTKIT_API_SEQUENCE_ID'],
169+
$this->generateEmailAddress(),
170+
'First Name',
171+
[
172+
'last_name' => 'Last Name',
173+
]
174+
);
175+
176+
// Check subscription object returned.
177+
$this->assertInstanceOf('stdClass', $result);
178+
$this->assertArrayHasKey('subscription', get_object_vars($result));
179+
180+
// Fetch subscriber from API to confirm the custom fields were saved.
181+
$subscriber = $this->api->get_subscriber($result->subscription->subscriber->id);
182+
$this->assertEquals($subscriber->subscriber->fields->last_name, 'Last Name');
183+
}
184+
185+
/**
186+
* Test that add_subscriber_to_sequence() returns the expected data
187+
* when custom field data is included.
188+
*
189+
* @since 1.0.0
190+
*
191+
* @return void
192+
*/
193+
public function testAddSubscriberToSequenceWithTagID()
194+
{
195+
$result = $this->api->add_subscriber_to_sequence(
196+
$_ENV['CONVERTKIT_API_SEQUENCE_ID'],
197+
$this->generateEmailAddress(),
198+
'First Name',
199+
[],
200+
[
201+
(int) $_ENV['CONVERTKIT_API_TAG_ID']
202+
]
203+
);
204+
205+
// Check subscription object returned.
206+
$this->assertInstanceOf('stdClass', $result);
207+
$this->assertArrayHasKey('subscription', get_object_vars($result));
208+
209+
// Fetch subscriber tags from API to confirm the tag saved.
210+
$subscriberTags = $this->api->get_subscriber_tags($result->subscription->subscriber->id);
211+
// @TODO.
212+
}
213+
89214
/**
90215
* Test that get_sequence_subscriptions() returns the expected data.
91216
*
@@ -143,20 +268,6 @@ public function testGetSequenceSubscriptionsWithDescSortOrder()
143268
);
144269
}
145270

146-
/**
147-
* Test that get_sequence_subscriptions() throws a ClientException when an invalid
148-
* sequence ID is specified.
149-
*
150-
* @since 1.0.0
151-
*
152-
* @return void
153-
*/
154-
public function testGetSequenceSubscriptionsWithInvalidSequenceID()
155-
{
156-
$this->expectException(GuzzleHttp\Exception\ClientException::class);
157-
$result = $this->api->get_sequence_subscriptions(12345);
158-
}
159-
160271
/**
161272
* Test that get_sequence_subscriptions() throws a ClientException when an invalid
162273
* sort order is specified.
@@ -172,48 +283,17 @@ public function testGetSequenceSubscriptionsWithInvalidSortOrder()
172283
}
173284

174285
/**
175-
* Test that add_subscriber_to_sequence() returns the expected data.
176-
*
177-
* @since 1.0.0
178-
*
179-
* @return void
180-
*/
181-
public function testAddSubscriberToSequence()
182-
{
183-
$result = $this->api->add_subscriber_to_sequence(
184-
$_ENV['CONVERTKIT_API_SEQUENCE_ID'],
185-
$this->generateEmailAddress()
186-
);
187-
$this->assertInstanceOf('stdClass', $result);
188-
$this->assertArrayHasKey('subscription', get_object_vars($result));
189-
}
190-
191-
/**
192-
* Test that add_subscriber_to_sequence() throws a ClientException when an invalid
193-
* sequence is specified.
194-
*
195-
* @since 1.0.0
196-
*
197-
* @return void
198-
*/
199-
public function testAddSubscriberToSequenceWithInvalidSequenceID()
200-
{
201-
$this->expectException(GuzzleHttp\Exception\ClientException::class);
202-
$result = $this->api->add_subscriber_to_sequence(12345, $this->generateEmailAddress());
203-
}
204-
205-
/**
206-
* Test that add_subscriber_to_sequence() throws a ClientException when an invalid
207-
* email address is specified.
286+
* Test that get_sequence_subscriptions() throws a ClientException when an invalid
287+
* sequence ID is specified.
208288
*
209289
* @since 1.0.0
210290
*
211291
* @return void
212292
*/
213-
public function testAddSubscriberToSequenceWithInvalidEmailAddress()
293+
public function testGetSequenceSubscriptionsWithInvalidSequenceID()
214294
{
215295
$this->expectException(GuzzleHttp\Exception\ClientException::class);
216-
$result = $this->api->add_subscriber_to_sequence($_ENV['CONVERTKIT_API_SEQUENCE_ID'], 'not-an-email-address');
296+
$result = $this->api->get_sequence_subscriptions(12345);
217297
}
218298

219299
/**

0 commit comments

Comments
 (0)