diff --git a/includes/admin/templates/settings.php b/includes/admin/templates/settings.php index b657841..bd4593d 100644 --- a/includes/admin/templates/settings.php +++ b/includes/admin/templates/settings.php @@ -51,8 +51,7 @@

get( 'lists', 100, array( 'fields' => 'lists.id,lists.name' ) ); + $lists = mailchimp_sf_get_lists(); if ( is_wp_error( $lists ) ) { $msg = sprintf( /* translators: %s: error message */ diff --git a/includes/blocks/class-mailchimp-list-subscribe-form-blocks.php b/includes/blocks/class-mailchimp-list-subscribe-form-blocks.php index 945863f..8488c7d 100644 --- a/includes/blocks/class-mailchimp-list-subscribe-form-blocks.php +++ b/includes/blocks/class-mailchimp-list-subscribe-form-blocks.php @@ -103,14 +103,7 @@ public function get_lists() { return $lists; } - // If we don't have any lists, get them from the API. - $api = mailchimp_sf_get_api(); - if ( ! $api ) { - return array(); - } - - // we *could* support paging, but 100 is more than enough for now. - $lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) ); + $lists = mailchimp_sf_get_lists(); if ( is_wp_error( $lists ) ) { return array(); } diff --git a/includes/class-mailchimp-admin.php b/includes/class-mailchimp-admin.php index 3805336..43e2fd1 100644 --- a/includes/class-mailchimp-admin.php +++ b/includes/class-mailchimp-admin.php @@ -372,7 +372,7 @@ public function verify_and_save_oauth_token( $access_token, $data_center ) { update_option( 'mc_user', $this->sanitize_data( $user ) ); // Clear Mailchimp List ID if saved list is not available. - $lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) ); + $lists = mailchimp_sf_get_lists(); if ( ! is_wp_error( $lists ) ) { $lists = $lists['lists'] ?? array(); $saved_list_id = get_option( 'mc_list_id' ); diff --git a/mailchimp.php b/mailchimp.php index caf1cd2..19f97be 100644 --- a/mailchimp.php +++ b/mailchimp.php @@ -546,13 +546,8 @@ function mailchimp_sf_change_list_if_necessary() { return; } - $api = mailchimp_sf_get_api(); - if ( ! $api ) { return; } - - // we *could* support paging, but few users have that many lists (and shouldn't) - $lists = $api->get( 'lists', 100, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) ); - - if ( ! isset( $lists['lists'] ) || is_wp_error( $lists['lists'] ) ) { + $lists = mailchimp_sf_get_lists(); + if ( is_wp_error( $lists ) || ! isset( $lists['lists'] ) ) { return; } @@ -998,3 +993,27 @@ function mailchimp_sf_get_access_token() { function mailchimp_sf_should_display_form() { return mailchimp_sf_get_api() && ! get_option( 'mailchimp_sf_auth_error' ) && get_option( 'mc_list_id' ); } + +/** + * Get Mailchimp Lists. + * + * @since x.x.x + * @return array|WP_Error|false List of Mailchimp lists, or an error/false from the API request. + */ +function mailchimp_sf_get_lists() { + /** + * Filter the limit of lists to fetch. + * + * This value is sanitized to a positive integer and clamped before the API request. + * Defaults to 100. 1000 is the maximum allowed by the API. 1 is the minimum allowed. + */ + $limit = apply_filters( 'mailchimp_sf_list_limit', 100 ); // Default to 100. + $limit = max( 1, min( 1000, absint( $limit ) ) ); + + $api = mailchimp_sf_get_api(); + if ( ! $api ) { + return array(); + } + + return $api->get( 'lists', $limit, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) ); +}