-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailchimp_rules.module
171 lines (149 loc) · 4.87 KB
/
mailchimp_rules.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @file
* Mailchimp rules module.
*/
use \Mailchimp\MailchimpLists;
use \Mailchimp\MailchimpAPIException;
/**
* Implements hook_libraries_info().
*/
function mailchimp_rules_libraries_info() {
$libraries['mailchimp'] = array(
'name' => 'MailChimp API',
'vendor url' => 'https://github.com/thinkshout/mailchimp-api-php',
'download url' => 'https://github.com/thinkshout/mailchimp-api-php/archive/v1.0.5.zip',
'version arguments' => array(
'file' => 'composer.json',
'pattern' => '/"version": "([0-9a-zA-Z.-]+)"/',
),
'files' => array(
'php' => array(
'src/Mailchimp.php',
'src/MailchimpAPIException',
'src/MailchimpCampaigns.php',
'src/MailchimpLists.php',
'src/MailchimpReports.php',
'src/MailchimpTemplates.php',
'vendor/autoload.php',
),
),
);
return $libraries;
}
/**
* Returns a new instance of an MailChimp API class.
*
* @param string $class_name
* The class name you want an instance from.
* @param string $api_key
* The MailChimp api key to use if not the default, which is retrieved from
* the variable mailchimp_api_key.
*
* @return object||null
* Either an instance of an MailChimp API class or NULL, if the library is
* missing or could not be loaded.
*/
function mailchimp_rules_get_api_object($class_name = 'Mailchimp', $api_key = NULL) {
$class_name = 'Mailchimp\\' . $class_name;
// Try to load the library via the libraries module.
if (module_exists('libraries')) {
$library = libraries_load('mailchimp');
}
// Check if the composer autoload file loaded the mailchimp library.
if (!class_exists($class_name)) {
watchdog('mailchimp_rules', 'Failed to load MailChimp PHP library. Please refer to the installation requirements.', array(), WATCHDOG_ERROR);
return NULL;
}
if (!$api_key) {
$api_key = variable_get('mailchimp_api_key');
}
if (empty($api_key)) {
watchdog('mailchimp_rules', 'Please set an API key for the MailChimp integration.', array(), WATCHDOG_ERROR);
}
// Set the timeout to something that won't take down the Drupal site:
$timeout = 60;
return new $class_name($api_key, 'apikey', $timeout);
}
/**
* Retrieves the lists for the set API key.
*
* @return array
* A list of MailChimp lists with the id as key and name as value.
*/
function mailchimp_rules_retrieve_lists() {
$lists_api = mailchimp_rules_get_api_object('MailchimpLists');
$lists = array();
try {
// Only get the first 500 for now.
$res = $lists_api->getLists(array('count' => 500));
foreach ($res->lists as $list) {
$lists[$list->id] = $list->name;
}
}
catch (MailchimpAPIException $exception) {
watchdog('mailchimp_rules',
'Could not retrieve the mailchimp lists due to @exception.',
array('@exception' => $exception->getMessage()), WATCHDOG_ERROR);
}
return $lists;
}
/**
* Subscribes the given user to the given list.
*
* @param string $list_id
* The ID for the list to which the mail should be subscribed.
* @param string $mail
* The mail address the should be subscribed.
* @param array $merge_fields
* An associative array of merge fields to be synced, using the key for the
* tag/name and the value for the merge fields value.
* @param bool $double_optin
* Set to true if the the subscription sould require a double opt in.
*/
function mailchimp_rules_subscribe_mail($list_id, $mail, $merge_fields = array(), $double_optin = FALSE) {
$lists_api = mailchimp_rules_get_api_object('MailchimpLists');
try {
$parameters = array(
'status' => ($double_optin) ? MailchimpLists::MEMBER_STATUS_PENDING : MailchimpLists::MEMBER_STATUS_SUBSCRIBED,
);
if (!empty($merge_fields)) {
$parameters['merge_fields'] = $merge_fields;
}
$lists_api->addOrUpdateMember($list_id, $mail, $parameters);
}
catch (MailchimpAPIException $exception) {
watchdog('mailchimp_rules',
'Could not subscribe mail @mail to mailchimp list with id @list_id due to @exception.',
array(
'@mail' => $mail,
'@list_id' => $list_id,
'@exception' => $exception->getMessage(),
),
WATCHDOG_ERROR);
}
}
/**
* Unsubscribe the given user from the given list.
*
* @param string $list_id
* The ID for the list to which the mail should be subscribed.
* @param string $mail
* The mail address the should be subscribed.
*/
function mailchimp_rules_unsubscribe_mail($list_id, $mail) {
$lists_api = mailchimp_rules_get_api_object('MailchimpLists');
try {
$lists_api->removeMember($list_id, $mail);
}
catch (MailchimpAPIException $exception) {
watchdog('mailchimp_rules',
'Could not subscribe mail @mail to mailchimp list with id @list_id due to @exception.',
array(
'@mail' => $mail,
'@list_id' => $list_id,
'@exception' => $exception->getMessage(),
),
WATCHDOG_ERROR);
}
}