@@ -63,11 +63,11 @@ public function is_enabled(): bool {
63
63
*
64
64
* @see https://developers.activecampaign.com/reference/create-a-new-contact
65
65
*
66
- * @param string $email Email address for the contact
66
+ * @param string $email Email address for the contact
67
67
* @param string $first_name First name for the contact
68
- * @param string $last_name Last name for the contact
69
- * @param string $phone Phone number for the contact
70
- * @param string $company Company for the contact
68
+ * @param string $last_name Last name for the contact
69
+ * @param string $phone Phone number for the contact
70
+ * @param string $company Company for the contact
71
71
*
72
72
* @return bool|array An array containing contact details or false if unsuccessful
73
73
*/
@@ -110,9 +110,9 @@ public function sync_contact( $email, $first_name = false, $last_name = false, $
110
110
*
111
111
* @see https://developers.activecampaign.com/reference/create-an-account-1
112
112
*
113
- * @param string $contact_id ID of the contact to sync company data for
113
+ * @param string $contact_id ID of the contact to sync company data for
114
114
* @param string $current_company_id ID of the company currently associated with the contact ("0" = none)
115
- * @param string $company_name Name of the company to sync
115
+ * @param string $company_name Name of the company to sync
116
116
*
117
117
* @return bool
118
118
*/
@@ -128,6 +128,7 @@ public function sync_company( $contact_id, $current_company_id, $company_name ):
128
128
if ( $ current_company_id !== '0 ' ) {
129
129
if ( ! $ this ->delete_account_association ( $ contact_id ) ) {
130
130
$ this ->log ( esc_html__ ( 'There was an error when attempting to delete an association ' , 'automatewoo ' ) );
131
+
131
132
return false ;
132
133
}
133
134
}
@@ -154,7 +155,7 @@ public function sync_company( $contact_id, $current_company_id, $company_name ):
154
155
*
155
156
* @see https://developers.activecampaign.com/reference/list-all-accounts
156
157
*
157
- * @param string $account_name Name of the account to search for
158
+ * @param string $account_name Name of the account to search for
158
159
* @param bool $create_missing_account If true then the account will be created if not found
159
160
*
160
161
* @return bool|array
@@ -227,6 +228,7 @@ public function get_lists(): array {
227
228
228
229
if ( ! isset ( $ response ['lists ' ] ) ) {
229
230
$ this ->log ( __ ( 'Unexpected response when trying to retrieve all lists ' , 'automatewoo ' ) );
231
+
230
232
return [];
231
233
}
232
234
@@ -246,7 +248,7 @@ public function get_lists(): array {
246
248
* @see https://developers.activecampaign.com/reference/update-list-status-for-contact
247
249
*
248
250
* @param string $contact ID of the contact to add to a list
249
- * @param string $list ID of the list to add the contact to
251
+ * @param string $list ID of the list to add the contact to
250
252
*
251
253
* @return bool
252
254
*/
@@ -297,6 +299,7 @@ public function is_contact( $email ) {
297
299
public function get_contact ( $ email ) {
298
300
if ( ! is_email ( $ email ) ) {
299
301
$ this ->log ( __ ( 'Invalid email was supplied when checking if contact exists in ActiveCampaign ' , 'automatewoo ' ) );
302
+
300
303
return false ;
301
304
}
302
305
@@ -334,6 +337,7 @@ public function get_contact_custom_fields(): array {
334
337
335
338
if ( ! isset ( $ response ['fields ' ] ) ) {
336
339
$ this ->log ( __ ( 'Unexpected response when trying to retrieve all custom fields ' , 'automatewoo ' ) );
340
+
337
341
return [];
338
342
}
339
343
@@ -351,9 +355,9 @@ public function get_contact_custom_fields(): array {
351
355
/**
352
356
* Get a tag ID by its name
353
357
*
354
- * @param string $tag Name of the tag to get the ID for
358
+ * @param string $tag Name of the tag to get the ID for
355
359
* @param bool $create_missing_tag If true then the tag will be created if not found
356
- * @param bool $retry_on_error Whether to retry if an error occurs when creating a tag. It will clear the cache and try again.
360
+ * @param bool $retry_on_error Whether to retry if an error occurs when creating a tag. It will clear the cache and try again.
357
361
*
358
362
* @return string|bool
359
363
*/
@@ -365,7 +369,7 @@ public function get_tag_id( $tag, $create_missing_tag = false, $retry_on_error =
365
369
if ( $ cache ) {
366
370
$ this ->active_tags = $ cache ;
367
371
} else {
368
- $ response = $ this ->request ( 'tags ' )-> get_body ( );
372
+ $ response = $ this ->paginated_request ( 'tags ' );
369
373
$ this ->active_tags = $ response ['tags ' ];
370
374
371
375
Cache::set_transient ( $ transient , $ this ->active_tags , 0.15 );
@@ -395,10 +399,12 @@ public function get_tag_id( $tag, $create_missing_tag = false, $retry_on_error =
395
399
// Somehow the tag was created by another process in the meantime or externally so we clear the cache and try again.
396
400
Cache::delete_transient ( $ transient );
397
401
$ this ->active_tags = [];
402
+
398
403
return $ this ->get_tag_id ( $ tag , $ create_missing_tag , false );
399
404
}
400
405
// phpcs:disable WordPress.PHP.DevelopmentFunctions
401
406
$ this ->log ( 'Unexpected response when attempting to create a tag. Response: ' . print_r ( $ response , true ) . 'Tags: ' . print_r ( $ this ->active_tags , true ) );
407
+
402
408
// phpcs:enable
403
409
return false ;
404
410
}
@@ -417,7 +423,7 @@ public function get_tag_id( $tag, $create_missing_tag = false, $retry_on_error =
417
423
* Add tags to an existing contact
418
424
*
419
425
* @param string $contact ID of the contact to add tags to
420
- * @param array $tags An array of tags to add
426
+ * @param array $tags An array of tags to add
421
427
*
422
428
* @return bool
423
429
*/
@@ -453,8 +459,8 @@ public function add_tags( $contact, $tags ) {
453
459
*
454
460
* @see https://developers.activecampaign.com/reference/overview
455
461
*
456
- * @param string $path Path to the API endpoint
457
- * @param array $data Data to include with the request
462
+ * @param string $path Path to the API endpoint
463
+ * @param array $data Data to include with the request
458
464
* @param string $method The HTTP method for this request
459
465
*
460
466
* @return \ActiveCampaign|false
@@ -497,11 +503,43 @@ public function request( $path, $data = [], $method = 'GET' ) {
497
503
return $ response ;
498
504
}
499
505
500
-
501
506
/**
502
- * @deprecated
507
+ * Send a request to the ActiveCampaign API with pagination
503
508
*
509
+ * @see https://developers.activecampaign.com/reference/pagination
510
+ *
511
+ * @param string $path Path to the API endpoint
512
+ * @param array $data Data to include with the request
513
+ * @param string $method The HTTP method for this request
514
+ * @param int $page The page to fetch.
515
+ * @param array $results Accumulated results for all the pages.
516
+ *
517
+ * @return array
518
+ */
519
+ public function paginated_request ( $ path , $ data = [], $ method = 'GET ' , $ page = 0 , $ results = [] ) {
520
+
521
+ // Set offset based on the $page and the api_pagination_limit
522
+ $ data ['limit ' ] = $ this ->get_api_pagination_limit ();
523
+ $ data ['offset ' ] = $ page * $ this ->get_api_pagination_limit ();
524
+
525
+ // Get elements for the current page and add them to the results.
526
+ $ request = $ this ->request ( $ path , $ data , $ method )->get_body ();
527
+ $ results = array_merge ( $ results , $ request [ $ path ] );
528
+
529
+ // If there are more elements left. Fetch again with the next page.
530
+ if ( $ request ['meta ' ]['total ' ] > count ( $ results ) ) {
531
+ return $ this ->paginated_request ( $ path , $ data , $ method , ++$ page , $ results );
532
+ }
533
+
534
+ return [
535
+ $ path => $ results ,
536
+ 'meta ' => [ 'total ' => $ request ['meta ' ]['total ' ] ],
537
+ ];
538
+ }
539
+
540
+ /**
504
541
* @return void
542
+ * @deprecated
505
543
*/
506
544
protected function get_sdk () {
507
545
wc_deprecated_function ( __METHOD__ , '6.1.0 ' , 'request ' );
@@ -518,4 +556,21 @@ public function clear_cache_data() {
518
556
Cache::delete_transient ( 'ac_tags ' );
519
557
Cache::delete_transient ( 'ac_contact_fields ' );
520
558
}
559
+
560
+
561
+ /**
562
+ * Get the pagination limit.
563
+ *
564
+ * @return int
565
+ */
566
+ public function get_api_pagination_limit () {
567
+
568
+ /**
569
+ * Filter the pagination limit for Active Campaign REST API.
570
+ *
571
+ * @since 6.0.31
572
+ * @filter automatewoo/integrations/active_campaign/pagination
573
+ */
574
+ return apply_filters ( 'automatewoo/integrations/active_campaign/pagination ' , 100 );
575
+ }
521
576
}
0 commit comments