You are here

class MailchimpLists in Mailchimp 8

Same name in this branch
  1. 8 lib/mailchimp-api-php/src/MailchimpLists.php \Mailchimp\MailchimpLists
  2. 8 lib/mailchimp-api-php/tests/src/MailchimpLists.php \Mailchimp\Tests\MailchimpLists

Mailchimp Lists/Audiences library.

@package Mailchimp

Hierarchy

Expanded class hierarchy of MailchimpLists

1 file declares its use of MailchimpLists
mailchimp.module in ./mailchimp.module
Mailchimp module.
16 string references to 'MailchimpLists'
MailchimpListsWebhookSettingsForm::submitForm in modules/mailchimp_lists/src/Form/MailchimpListsWebhookSettingsForm.php
Form submission handler.
mailchimp_batch_update_members in ./mailchimp.module
Batch updates a number of Mailchimp list members.
mailchimp_campaign_get_list_segments in modules/mailchimp_campaign/mailchimp_campaign.module
Gets an array of list segments for a given list ID.
mailchimp_get_lists in ./mailchimp.module
Returns all Mailchimp lists for a given key. Lists are stored in the cache.
mailchimp_get_lists_for_email in ./mailchimp.module
Returns all lists a given email address is currently subscribed to.

... See full list

File

lib/mailchimp-api-php/src/MailchimpLists.php, line 10

Namespace

Mailchimp
View source
class MailchimpLists extends Mailchimp {
  const MEMBER_STATUS_SUBSCRIBED = 'subscribed';
  const MEMBER_STATUS_UNSUBSCRIBED = 'unsubscribed';
  const MEMBER_STATUS_CLEANED = 'cleaned';
  const MEMBER_STATUS_PENDING = 'pending';

  /**
   * Gets information about all lists/audiences owned by the authenticated account.
   *
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#read-get_lists
   */
  public function getLists($parameters = []) {
    return $this
      ->request('GET', '/lists', NULL, $parameters);
  }

  /**
   * Gets a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#read-get_lists_list_id
   */
  public function getList($list_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}', $tokens, $parameters);
  }

  /**
   * Gets information about all interest categories associated with a list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#read-get_lists_list_id_interest_categories
   */
  public function getInterestCategories($list_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/interest-categories', $tokens, $parameters);
  }

  /**
   * Gets information about all interests associated with an interest category.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $interest_category_id
   *   The ID of the interest category.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/interests/#read-get_lists_list_id_interest_categories_interest_category_id_interests
   */
  public function getInterests($list_id, $interest_category_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'interest_category_id' => $interest_category_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/interest-categories/{interest_category_id}/interests', $tokens, $parameters);
  }

  /**
   * Gets merge fields associated with a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/merge-fields/#read-get_lists_list_id_merge_fields
   */
  public function getMergeFields($list_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/merge-fields', $tokens, $parameters);
  }

  /**
   * Add merge field associated with a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $name
   *   The name of the merge field.
   * @param string $type
   *   The type for the merge field.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/merge-fields/#create-post_lists_list_id_merge_fields
   */
  public function addMergeField($list_id, $name, $type, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    $parameters += [
      'name' => $name,
      'type' => $type,
    ];
    return $this
      ->request('POST', '/lists/{list_id}/merge-fields', $tokens, $parameters);
  }

  /**
   * Gets information about all members of a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members
   */
  public function getMembers($list_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/members', $tokens, $parameters);
  }

  /**
   * Gets information about a member of a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $email
   *   The member's email address.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash
   */
  public function getMemberInfo($list_id, $email, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'subscriber_hash' => md5(strtolower($email)),
    ];
    return $this
      ->request('GET', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters);
  }

  /**
   * Gets information about a member of a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $mc_eid
   *   The member's unique ID.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see https://developer.mailchimp.com/documentation/mailchimp/guides/getting-started-with-ecommerce/
   */
  public function getMemberInfoById($list_id, $mc_eid, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    $parameters = [
      'unique_email_id' => $mc_eid,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/members/', $tokens, $parameters);
  }

  /**
   * Gets activity related to a member of a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $email
   *   The member's email address.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/activity/#read-get_lists_list_id_members_subscriber_hash_activity
   */
  public function getMemberActivity($list_id, $email, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'subscriber_hash' => md5(strtolower($email)),
    ];
    return $this
      ->request('GET', '/lists/{list_id}/members/{subscriber_hash}/activity', $tokens, $parameters);
  }

  /**
   * Adds a new member to a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $email
   *   The email address to add.
   * @param array $parameters
   *   Associative array of optional request parameters.
   * @param bool $batch
   *   TRUE to create a new pending batch operation.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members
   */
  public function addMember($list_id, $email, $parameters = [], $batch = FALSE) {
    $tokens = [
      'list_id' => $list_id,
    ];
    $parameters += [
      'email_address' => $email,
    ];
    return $this
      ->request('POST', '/lists/{list_id}/members', $tokens, $parameters, $batch);
  }

  /**
   * Removes a member from a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $email
   *   The member's email address.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#delete-delete_lists_list_id_members_subscriber_hash
   */
  public function removeMember($list_id, $email) {
    $tokens = [
      'list_id' => $list_id,
      'subscriber_hash' => md5(strtolower($email)),
    ];
    return $this
      ->request('DELETE', '/lists/{list_id}/members/{subscriber_hash}', $tokens);
  }

  /**
   * Updates a member of a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $email
   *   The member's email address.
   * @param array $parameters
   *   Associative array of optional request parameters.
   * @param bool $batch
   *   TRUE to create a new pending batch operation.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-patch_lists_list_id_members_subscriber_hash
   */
  public function updateMember($list_id, $email, $parameters = [], $batch = FALSE) {
    $tokens = [
      'list_id' => $list_id,
      'subscriber_hash' => md5(strtolower($email)),
    ];
    return $this
      ->request('PATCH', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters, $batch);
  }

  /**
   * Adds a new or update an existing member of a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $email
   *   The member's email address.
   * @param array $parameters
   *   Associative array of optional request parameters.
   * @param bool $batch
   *   TRUE to create a new pending batch operation.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash
   */
  public function addOrUpdateMember($list_id, $email, $parameters = [], $batch = FALSE) {
    $tokens = [
      'list_id' => $list_id,
      'subscriber_hash' => md5(strtolower($email)),
    ];
    $parameters += [
      'email_address' => $email,
    ];
    return $this
      ->request('PUT', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters, $batch);
  }

  /**
   * Gets information about segments associated with a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#read-get_lists_list_id_segments
   */
  public function getSegments($list_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/segments', $tokens, $parameters);
  }

  /**
   * Gets a Mailchimp list segment.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $segment_id
   *   The ID of the list segment.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#read-get_lists_list_id_segments_segment_id
   */
  public function getSegment($list_id, $segment_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'segment_id' => $segment_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters);
  }

  /**
   * Adds a new segment to a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $name
   *   The name of the segment.
   * @param array $parameters
   *   Associative array of optional request parameters.
   * @param bool $batch
   *   TRUE to create a new pending batch operation.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#create-post_lists_list_id_segments
   */
  public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) {
    $tokens = [
      'list_id' => $list_id,
    ];
    $parameters += [
      'name' => $name,
    ];
    return $this
      ->request('POST', '/lists/{list_id}/segments', $tokens, $parameters, $batch);
  }

  /**
   * Updates a segment associated with a Mailchimp list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param int $segment_id
   *   The ID of the segment.
   * @param string $name
   *   The name of the segment.
   * @param array $parameters
   *   Associative array of optional request parameters.
   * @param bool $batch
   *   TRUE to create a new pending batch operation.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#edit-patch_lists_list_id_segments_segment_id
   */
  public function updateSegment($list_id, $segment_id, $name, $parameters = [], $batch = FALSE) {
    $tokens = [
      'list_id' => $list_id,
      'segment_id' => $segment_id,
    ];
    $parameters += [
      'name' => $name,
    ];
    return $this
      ->request('PATCH', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch);
  }

  /**
   * Gets information about members of a list segment.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $segment_id
   *   The ID of the segment.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/members/#read-get_lists_list_id_segments_segment_id_members
   */
  public function getSegmentMembers($list_id, $segment_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'segment_id' => $segment_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/segments/{segment_id}/members', $tokens, $parameters);
  }

  /**
   * Adds a member to a list segment.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $segment_id
   *   The ID of the segment.
   * @param array $email
   *   The email address to add to the segment.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/members/
   */
  public function addSegmentMember($list_id, $segment_id, $email, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'segment_id' => $segment_id,
    ];
    $parameters += [
      'email_address' => $email,
    ];
    return $this
      ->request('POST', '/lists/{list_id}/segments/{segment_id}/members', $tokens, $parameters);
  }

  /**
   * Gets information about webhooks associated with a list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/webhooks/#read-get_lists_list_id_webhooks
   */
  public function getWebhooks($list_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/webhooks', $tokens, $parameters);
  }

  /**
   * Gets information about a specific webhook associated with a list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $webhook_id
   *   The ID of the webhook.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   *
   * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/webhooks/#read-get_lists_list_id_webhooks_webhook_id
   */
  public function getWebhook($list_id, $webhook_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'webhook_id' => $webhook_id,
    ];
    return $this
      ->request('GET', '/lists/{list_id}/webhooks/{webhook_id}', $tokens, $parameters);
  }

  /**
   * Adds a new webhook to a list.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $url
   *   The callback URL the webhook should notify of events.
   * @param array $parameters
   *   Associative array of optional request parameters.
   * @param bool $batch
   *   TRUE to create a new pending batch operation.
   *
   * @return object
   */
  public function addWebhook($list_id, $url, $parameters = [], $batch = FALSE) {
    $tokens = [
      'list_id' => $list_id,
    ];
    $parameters += [
      'url' => $url,
    ];
    return $this
      ->request('POST', '/lists/{list_id}/webhooks', $tokens, $parameters, $batch);
  }

  /**
   * Deletes a webhook.
   *
   * @param string $list_id
   *   The ID of the list.
   * @param string $webhook_id
   *   The ID of the webhook.
   * @param array $parameters
   *   Associative array of optional request parameters.
   *
   * @return object
   */
  public function deleteWebhook($list_id, $webhook_id, $parameters = []) {
    $tokens = [
      'list_id' => $list_id,
      'webhook_id' => $webhook_id,
    ];
    return $this
      ->request('DELETE', '/lists/{list_id}/webhooks/{webhook_id}', $tokens, $parameters);
  }

  /**
   * Gets all lists an email address is subscribed to.
   *
   * @param string $email
   *   The email address to get lists for.
   *
   * @return array
   *   Array of subscribed list objects.
   *
   * @throws MailchimpAPIException
   */
  public function getListsForEmail($email) {
    $list_data = $this
      ->getLists();
    $subscribed_lists = [];

    // Check each list for a subscriber matching the email address.
    if ($list_data->total_items > 0) {
      foreach ($list_data->lists as $list) {
        try {
          $member_data = $this
            ->getMemberInfo($list->id, $email);
          if (isset($member_data->id)) {
            $subscribed_lists[] = $list;
          }
        } catch (MailchimpAPIException $e) {
          if ($e
            ->getCode() !== 404) {

            // 404 indicates the email address is not subscribed to this list
            // and can be safely ignored. Surface all other exceptions.
            throw new MailchimpAPIException($e
              ->getMessage(), $e
              ->getCode(), $e);
          }
        }
      }
    }
    return $subscribed_lists;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Mailchimp::$api_key private property The Mailchimp API key to authenticate with.
Mailchimp::$api_user private property The Mailchimp API username to authenticate with.
Mailchimp::$batch_operations private property Array of pending batch operations.
Mailchimp::$client protected property The HTTP client.
Mailchimp::$debug_error_code private property A Mailchimp API error code to return with every API response.
Mailchimp::$endpoint protected property The REST API endpoint.
Mailchimp::$version public property API version.
Mailchimp::addBatchOperation protected function Adds a pending batch operation.
Mailchimp::DEFAULT_DATA_CENTER constant
Mailchimp::ERROR_CODE_API_KEY_INVALID constant
Mailchimp::ERROR_CODE_API_KEY_MISSING constant
Mailchimp::ERROR_CODE_BAD_REQUEST constant
Mailchimp::ERROR_CODE_COMPLIANCE_RELATED constant
Mailchimp::ERROR_CODE_FORBIDDEN constant
Mailchimp::ERROR_CODE_INTERNAL_SERVER_ERROR constant
Mailchimp::ERROR_CODE_INVALID_ACTION constant
Mailchimp::ERROR_CODE_INVALID_METHOD_OVERRIDE constant
Mailchimp::ERROR_CODE_INVALID_RESOURCE constant
Mailchimp::ERROR_CODE_JSON_PARSE_ERROR constant
Mailchimp::ERROR_CODE_METHOD_NOT_ALLOWED constant
Mailchimp::ERROR_CODE_REQUESTED_FIELDS_INVALID constant
Mailchimp::ERROR_CODE_RESOURCE_NESTING_TOO_DEEP constant
Mailchimp::ERROR_CODE_RESOURCE_NOT_FOUND constant
Mailchimp::ERROR_CODE_TOO_MANY_REQUESTS constant
Mailchimp::ERROR_CODE_USER_DISABLED constant
Mailchimp::ERROR_CODE_WRONG_DATACENTER constant
Mailchimp::getAccount public function Gets Mailchimp account information for the authenticated account.
Mailchimp::getBatchOperation public function Gets the status of a batch request.
Mailchimp::getDataCenter private function Gets the ID of the data center associated with an API key.
Mailchimp::getDefaultHttpClient private function Instantiates a default HTTP client based on the local environment.
Mailchimp::processBatchOperations public function Processes all pending batch operations.
Mailchimp::request public function Makes a request to the Mailchimp API.
Mailchimp::setClient public function Sets a custom HTTP client to be used for all API requests.
Mailchimp::setDebugErrorCode public function Sets a Mailchimp error code to be returned by all requests.
Mailchimp::VERSION constant
Mailchimp::__construct public function Mailchimp constructor. 7
MailchimpLists::addMember public function Adds a new member to a Mailchimp list. 1
MailchimpLists::addMergeField public function Add merge field associated with a Mailchimp list.
MailchimpLists::addOrUpdateMember public function Adds a new or update an existing member of a Mailchimp list. 1
MailchimpLists::addSegment public function Adds a new segment to a Mailchimp list. 1
MailchimpLists::addSegmentMember public function Adds a member to a list segment.
MailchimpLists::addWebhook public function Adds a new webhook to a list. 1
MailchimpLists::deleteWebhook public function Deletes a webhook. 1
MailchimpLists::getInterestCategories public function Gets information about all interest categories associated with a list. 1
MailchimpLists::getInterests public function Gets information about all interests associated with an interest category. 1
MailchimpLists::getList public function Gets a Mailchimp list. 1
MailchimpLists::getLists public function Gets information about all lists/audiences owned by the authenticated account. 1
MailchimpLists::getListsForEmail public function Gets all lists an email address is subscribed to.
MailchimpLists::getMemberActivity public function Gets activity related to a member of a Mailchimp list.
MailchimpLists::getMemberInfo public function Gets information about a member of a Mailchimp list. 1
MailchimpLists::getMemberInfoById public function Gets information about a member of a Mailchimp list.
MailchimpLists::getMembers public function Gets information about all members of a Mailchimp list.
MailchimpLists::getMergeFields public function Gets merge fields associated with a Mailchimp list. 1
MailchimpLists::getSegment public function Gets a Mailchimp list segment. 1
MailchimpLists::getSegmentMembers public function Gets information about members of a list segment.
MailchimpLists::getSegments public function Gets information about segments associated with a Mailchimp list. 1
MailchimpLists::getWebhook public function Gets information about a specific webhook associated with a list.
MailchimpLists::getWebhooks public function Gets information about webhooks associated with a list. 1
MailchimpLists::MEMBER_STATUS_CLEANED constant
MailchimpLists::MEMBER_STATUS_PENDING constant
MailchimpLists::MEMBER_STATUS_SUBSCRIBED constant
MailchimpLists::MEMBER_STATUS_UNSUBSCRIBED constant
MailchimpLists::removeMember public function Removes a member from a Mailchimp list. 1
MailchimpLists::updateMember public function Updates a member of a Mailchimp list. 1
MailchimpLists::updateSegment public function Updates a segment associated with a Mailchimp list. 1