Skip to content

Commit

Permalink
pull-in last 10 lists too
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Apr 22, 2024
1 parent ca7ea8c commit 7cb8fde
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions includes/class-mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,19 +391,21 @@ private function fetch_lists() {
$lists_data = array();
$offset = 0;
$count = 10;
$exceptions_skipped = 0;

// increase time limits
@set_time_limit( 180 );
// increase total time limit to 3 minutes
@set_time_limit(180);

// increase HTTP timeout to 30s as MailChimp is super slow to calculate dynamic fields
add_filter(
'mc4wp_http_request_args',
function ( $args ) {
function ($args) {
$args['timeout'] = 30;

return $args;
}
);

// collect all lists in separate HTTP requests (batches of 5)
// collect all lists in separate HTTP requests
do {
try {
$data = $client->get(
Expand All @@ -414,15 +416,18 @@ function ( $args ) {
'fields' => 'total_items,lists.id,lists.name,lists.web_id,lists.stats.member_count,lists.marketing_permissions',
)
);

$lists_data = array_merge( $lists_data, $data->lists );
$offset += $count;
} catch ( MC4WP_API_Connection_Exception $e ) {
// ignore timeout errors as this is likely due to mailchimp being slow to calculate the lists.stats.member_count property
// keep going so we can at least pull-in all other lists
$offset += $count;
$exceptions_skipped++;

// failsafe against infinite loop
if ( $offset > 300 ) {
// bail after 5 skipped exceptions
if ($exceptions_skipped >= 5) {
break;
}

Expand All @@ -431,7 +436,7 @@ function ( $args ) {
// break on other errors, like "API key missing"etc.
break;
}
} while ( $data->total_items > $offset );
} while ($data->total_items >= $offset);

// key by list ID
$lists = array();
Expand Down

0 comments on commit 7cb8fde

Please sign in to comment.