Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions includes/admin/templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
</p>
<form method="post" action="<?php echo esc_url( add_query_arg( array( 'page' => 'mailchimp_sf_options' ), admin_url( 'admin.php' ) ) ); ?>">
<?php
// 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 = mailchimp_sf_get_lists();
if ( is_wp_error( $lists ) ) {
Comment thread
iamdharmesh marked this conversation as resolved.
$msg = sprintf(
/* translators: %s: error message */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-mailchimp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
33 changes: 26 additions & 7 deletions mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 ) ) );

Comment thread
iamdharmesh marked this conversation as resolved.
$api = mailchimp_sf_get_api();
if ( ! $api ) {
return array();
}

return $api->get( 'lists', $limit, array( 'fields' => 'lists.id,lists.name,lists.email_type_option' ) );
Comment thread
iamdharmesh marked this conversation as resolved.
}
Loading