Skip to content

Commit 9ce7380

Browse files
committed
Query subscibers by email address, instead of using inefficient paging method
1 parent 878634d commit 9ce7380

File tree

2 files changed

+64
-43
lines changed

2 files changed

+64
-43
lines changed

src/ConvertKit_API.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ public function get_subscriber_id( $email_address ) {
349349
$options = array(
350350
'api_secret' => $this->api_secret,
351351
'status' => 'all',
352+
'email_address' => $email_address,
352353
);
353354

354355
$this->create_log(sprintf("GET subscriber id from all subscribers: %s, %s, %s", $request, json_encode($options), $email_address));
@@ -366,27 +367,7 @@ public function get_subscriber_id( $email_address ) {
366367
return $subscriber_id;
367368
}
368369

369-
$total_pages = $subscribers->total_pages;
370-
371-
$this->create_log(sprintf("Total number of pages is %s", $total_pages));
372-
373-
for ( $i = 2; $i <= $total_pages; $i++ ) {
374-
$options['page'] = $i;
375-
$this->create_log(sprintf("Go to page %s", $i));
376-
$subscribers = $this->make_request( $request, 'GET', $options );
377-
378-
if( !$subscribers ) {
379-
return false;
380-
}
381-
382-
$subscriber_id = $this::check_if_subscriber_in_array($email_address, $subscribers->subscribers);
383-
384-
if($subscriber_id) {
385-
return $subscriber_id;
386-
}
387-
}
388-
389-
$this->create_log("Subscriber not found anywhere");
370+
$this->create_log("Subscriber not found");
390371

391372
return false;
392373

tests/ConvertKitAPITest.php

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testGetAccount()
6868
/**
6969
* Test that get_sequences() returns the expected data.
7070
*
71-
* @since 1.0.
71+
* @since 1.0.0
7272
*
7373
* @return void
7474
*/
@@ -89,7 +89,7 @@ public function testGetSequences()
8989
/**
9090
* Test that get_sequence_subscriptions() returns the expected data.
9191
*
92-
* @since 1.0.
92+
* @since 1.0.0
9393
*
9494
* @return void
9595
*/
@@ -115,7 +115,7 @@ public function testGetSequenceSubscriptions()
115115
/**
116116
* Test that get_sequence_subscriptions() returns the expected data in descending order.
117117
*
118-
* @since 1.0.
118+
* @since 1.0.0
119119
*
120120
* @return void
121121
*/
@@ -141,7 +141,7 @@ public function testGetSequenceSubscriptionsWithDescSortOrder()
141141
* Test that get_sequence_subscriptions() throws a ClientException when an invalid
142142
* sequence ID is specified.
143143
*
144-
* @since 1.0.
144+
* @since 1.0.0
145145
*
146146
* @return void
147147
*/
@@ -155,7 +155,7 @@ public function testGetSequenceSubscriptionsWithInvalidSequenceID()
155155
* Test that get_sequence_subscriptions() throws a ClientException when an invalid
156156
* sort order is specified.
157157
*
158-
* @since 1.0.
158+
* @since 1.0.0
159159
*
160160
* @return void
161161
*/
@@ -168,7 +168,7 @@ public function testGetSequenceSubscriptionsWithInvalidSortOrder()
168168
/**
169169
* Test that add_subscriber_to_sequence() returns the expected data.
170170
*
171-
* @since 1.0.
171+
* @since 1.0.0
172172
*
173173
* @return void
174174
*/
@@ -186,7 +186,7 @@ public function testAddSubscriberToSequence()
186186
* Test that add_subscriber_to_sequence() throws a ClientException when an invalid
187187
* sequence is specified.
188188
*
189-
* @since 1.0.
189+
* @since 1.0.0
190190
*
191191
* @return void
192192
*/
@@ -200,7 +200,7 @@ public function testAddSubscriberToSequenceWithInvalidSequenceID()
200200
* Test that add_subscriber_to_sequence() throws a ClientException when an invalid
201201
* email address is specified.
202202
*
203-
* @since 1.0.
203+
* @since 1.0.0
204204
*
205205
* @return void
206206
*/
@@ -213,7 +213,7 @@ public function testAddSubscriberToSequenceWithInvalidEmailAddress()
213213
/**
214214
* Test that add_tag() returns the expected data.
215215
*
216-
* @since 1.0.
216+
* @since 1.0.0
217217
*
218218
* @return void
219219
*/
@@ -229,7 +229,7 @@ public function testAddTag()
229229
/**
230230
* Test that get_resources() for Forms returns the expected data.
231231
*
232-
* @since 1.0.
232+
* @since 1.0.0
233233
*
234234
* @return void
235235
*/
@@ -242,7 +242,7 @@ public function testGetResourcesForms()
242242
/**
243243
* Test that get_resources() for Landing Pages returns the expected data.
244244
*
245-
* @since 1.0.
245+
* @since 1.0.0
246246
*
247247
* @return void
248248
*/
@@ -255,7 +255,7 @@ public function testGetResourcesLandingPages()
255255
/**
256256
* Test that get_resources() for Subscription Forms returns the expected data.
257257
*
258-
* @since 1.0.
258+
* @since 1.0.0
259259
*
260260
* @return void
261261
*/
@@ -268,7 +268,7 @@ public function testGetResourcesSubscriptionForms()
268268
/**
269269
* Test that get_resources() for Tags returns the expected data.
270270
*
271-
* @since 1.0.
271+
* @since 1.0.0
272272
*
273273
* @return void
274274
*/
@@ -282,7 +282,7 @@ public function testGetResourcesTags()
282282
* Test that get_resources() throws a ClientException when an invalid
283283
* resource type is specified.
284284
*
285-
* @since 1.0.
285+
* @since 1.0.0
286286
*
287287
* @return void
288288
*/
@@ -296,7 +296,7 @@ public function testGetResourcesInvalidResourceType()
296296
/**
297297
* Test that form_subscribe() and form_unsubscribe() returns the expected data.
298298
*
299-
* @since 1.0.
299+
* @since 1.0.0
300300
*
301301
* @return void
302302
*/
@@ -326,7 +326,7 @@ public function testFormSubscribe()
326326
* Test that form_subscribe() throws a ClientException when an invalid
327327
* form ID is specified.
328328
*
329-
* @since 1.0.
329+
* @since 1.0.0
330330
*
331331
* @return void
332332
*/
@@ -338,12 +338,52 @@ public function testFormSubscribeWithInvalidFormID()
338338
]);
339339
}
340340

341+
/**
342+
* Test that get_subscriber_id() returns the expected data.
343+
*
344+
* @since 1.0.0
345+
*
346+
* @return void
347+
*/
348+
public function testGetSubscriberID()
349+
{
350+
$subscriber_id = $this->api->get_subscriber_id($_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL']);
351+
$this->assertIsInt($subscriber_id);
352+
$this->assertEquals($subscriber_id, (int) $_ENV['CONVERTKIT_API_SUBSCRIBER_ID']);
353+
}
354+
355+
/**
356+
* Test that get_subscriber_id() throws a ClientException when an invalid
357+
* email address is specified.
358+
*
359+
* @since 1.0.0
360+
*
361+
* @return void
362+
*/
363+
public function testGetSubscriberIDWithInvalidEmailAddress()
364+
{
365+
$this->expectException(\InvalidArgumentException::class);
366+
$result = $this->api->get_subscriber_id('not-an-email-address');
367+
}
341368

369+
/**
370+
* Test that get_subscriber_id() return false when no subscriber found
371+
* matching the given email address.
372+
*
373+
* @since 1.0.0
374+
*
375+
* @return void
376+
*/
377+
public function testGetSubscriberIDWithNotSubscribedEmailAddress()
378+
{
379+
$result = $this->api->get_subscriber_id('[email protected]');
380+
$this->assertFalse($result);
381+
}
342382

343383
/**
344384
* Test that get_subscriber() returns the expected data.
345385
*
346-
* @since 1.0.
386+
* @since 1.0.0
347387
*
348388
* @return void
349389
*/
@@ -363,7 +403,7 @@ public function testGetSubscriber()
363403
* Test that get_subscriber() throws a ClientException when an invalid
364404
* subscriber ID is specified.
365405
*
366-
* @since 1.0.
406+
* @since 1.0.0
367407
*
368408
* @return void
369409
*/
@@ -376,7 +416,7 @@ public function testGetSubscriberWithInvalidSubscriberID()
376416
/**
377417
* Test that get_subscriber_tags() returns the expected data.
378418
*
379-
* @since 1.0.
419+
* @since 1.0.0
380420
*
381421
* @return void
382422
*/
@@ -391,7 +431,7 @@ public function testGetSubscriberTags()
391431
* Test that get_subscriber_tags() throws a ClientException when an invalid
392432
* subscriber ID is specified.
393433
*
394-
* @since 1.0.
434+
* @since 1.0.0
395435
*
396436
* @return void
397437
*/
@@ -404,7 +444,7 @@ public function testGetSubscriberTagsWithInvalidSubscriberID()
404444
/**
405445
* Test that list_purchases() returns the expected data.
406446
*
407-
* @since 1.0.
447+
* @since 1.0.0
408448
*
409449
* @return void
410450
*/
@@ -423,7 +463,7 @@ public function testListPurchases()
423463
/**
424464
* Test that create_purchase() returns the expected data.
425465
*
426-
* @since 1.0.
466+
* @since 1.0.0
427467
*
428468
* @return void
429469
*/

0 commit comments

Comments
 (0)