| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Updated class to Mailchimp API v3 |
| 5 |
Since v230530 |
| 6 |
*/ |
| 7 |
|
| 8 |
class Mailchimp |
| 9 |
{ |
| 10 |
public $lists; |
| 11 |
|
| 12 |
public function __construct($api_key, $timeout = 30) |
| 13 |
{ |
| 14 |
$this->lists = new MailchimpV3($api_key, $timeout); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
class MailchimpV3 |
| 19 |
{ |
| 20 |
private $api_key; |
| 21 |
private $server; |
| 22 |
private $timeout; |
| 23 |
|
| 24 |
public function __construct($api_key, $timeout = 30) |
| 25 |
{ |
| 26 |
$this->api_key = $api_key; |
| 27 |
$this->server = explode('-', $this->api_key)[1]; |
| 28 |
$this->timeout = $timeout; |
| 29 |
} |
| 30 |
|
| 31 |
public function subscribe($list_id, $email, $merge_fields = [], $email_type = 'html', $double_optin = true, $update_existing = true, $replace_interests = true, $send_welcome = false) |
| 32 |
{ |
| 33 |
$subscriber_hash = md5(strtolower($email['email'])); |
| 34 |
$url = "https://{$this->server}.api.mailchimp.com/3.0/lists/{$list_id}/members/{$subscriber_hash}"; |
| 35 |
|
| 36 |
//230708 Convert the groupings to interest IDs. |
| 37 |
$interests = []; |
| 38 |
if (isset($merge_fields['GROUPINGS'])) { |
| 39 |
$interests = $this->groupings_to_interests($list_id, $merge_fields['GROUPINGS']); |
| 40 |
unset($merge_fields['GROUPINGS']); |
| 41 |
} |
| 42 |
|
| 43 |
$merge_fields['FNAME'] = $merge_fields['MERGE1']; |
| 44 |
$merge_fields['LNAME'] = $merge_fields['MERGE2']; |
| 45 |
|
| 46 |
$data = [ |
| 47 |
'email_address' => $email['email'], |
| 48 |
'status' => 'subscribed', |
| 49 |
'status_if_new' => $double_optin ? 'pending' : 'subscribed', |
| 50 |
'email_type' => $email_type, |
| 51 |
'merge_fields' => (object)$merge_fields, |
| 52 |
]; |
| 53 |
//230925 |
| 54 |
if (!empty($interests)) |
| 55 |
$data['interests'] = (object)$interests; //231103 object |
| 56 |
|
| 57 |
$options = [ |
| 58 |
'http' => [ |
| 59 |
'header' => "Content-type: application/json\r\nAuthorization: Basic " . base64_encode('user:' . $this->api_key), |
| 60 |
'method' => $update_existing ? 'PUT' : 'POST', |
| 61 |
'content' => json_encode($data), |
| 62 |
'timeout' => $this->timeout, |
| 63 |
], |
| 64 |
]; |
| 65 |
|
| 66 |
$context = stream_context_create($options); |
| 67 |
$result = file_get_contents($url, false, $context); |
| 68 |
|
| 69 |
if ($result === FALSE) { |
| 70 |
c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', get_defined_vars()); |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
$result = json_decode($result, true); |
| 75 |
$result['email'] = $result['email_address']; |
| 76 |
c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', get_defined_vars()); |
| 77 |
|
| 78 |
return $result; |
| 79 |
} |
| 80 |
|
| 81 |
public function unsubscribe($list_id, $email, $delete_member = false, $send_goodbye = false, $send_notify = false, $groupings = []) |
| 82 |
{ |
| 83 |
$subscriber_hash = md5(strtolower($email['email'])); |
| 84 |
$url = "https://{$this->server}.api.mailchimp.com/3.0/lists/{$list_id}/members/{$subscriber_hash}"; |
| 85 |
|
| 86 |
//230714 |
| 87 |
if ($delete_member) { |
| 88 |
$data = []; |
| 89 |
$method = 'DELETE'; |
| 90 |
} else { |
| 91 |
$interests = []; |
| 92 |
if (isset($groupings['GROUPINGS'])) { |
| 93 |
$interests = $this->groupings_to_interests($list_id, $groupings['GROUPINGS']); |
| 94 |
// Set all to false for unsub. |
| 95 |
$interests = array_fill_keys(array_keys($interests), false); |
| 96 |
} |
| 97 |
|
| 98 |
$data = [ |
| 99 |
'status' => 'unsubscribed', |
| 100 |
]; |
| 101 |
//230925 |
| 102 |
if (!empty($interests)) |
| 103 |
$data['interests'] = (object)$interests; //231103 object |
| 104 |
|
| 105 |
$method = 'PATCH'; |
| 106 |
} |
| 107 |
|
| 108 |
$options = [ |
| 109 |
'http' => [ |
| 110 |
'header' => "Content-type: application/json\r\nAuthorization: Basic " . base64_encode('user:' . $this->api_key), |
| 111 |
'method' => $method, |
| 112 |
'content' => json_encode($data), |
| 113 |
'timeout' => $this->timeout, |
| 114 |
], |
| 115 |
]; |
| 116 |
|
| 117 |
$context = stream_context_create($options); |
| 118 |
$result = file_get_contents($url, false, $context); |
| 119 |
|
| 120 |
if ($result === FALSE) { |
| 121 |
c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', get_defined_vars()); |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
$result = json_decode($result, true); |
| 126 |
$result['complete'] = true; |
| 127 |
|
| 128 |
c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', get_defined_vars()); |
| 129 |
|
| 130 |
return $result; |
| 131 |
} |
| 132 |
|
| 133 |
//230708 |
| 134 |
public function groupings_to_interests($list_id, $groupings) |
| 135 |
{ |
| 136 |
$interests = []; |
| 137 |
|
| 138 |
// These are the group/interests for the list in s2's MC options |
| 139 |
// listid::groupcateg::group|group|group |
| 140 |
$grouping = $groupings[0]; |
| 141 |
$group_categ_name = $grouping['name']; |
| 142 |
$group_names = $grouping['groups']; |
| 143 |
|
| 144 |
// Get all interest categories for the list |
| 145 |
// Interest is the API name for Group |
| 146 |
$interest_categs_url = "https://{$this->server}.api.mailchimp.com/3.0/lists/{$list_id}/interest-categories?count=1000"; |
| 147 |
$context = stream_context_create([ |
| 148 |
'http' => [ |
| 149 |
'header' => "Authorization: Basic " . base64_encode('user:' . $this->api_key), |
| 150 |
], |
| 151 |
]); |
| 152 |
$all_interest_categs = json_decode(file_get_contents($interest_categs_url, false, $context), true)['categories']; |
| 153 |
|
| 154 |
// Find the interest category id that corresponds to the group category name |
| 155 |
foreach ($all_interest_categs as $interest_categ) { |
| 156 |
if ($interest_categ['title'] === $group_categ_name) { |
| 157 |
$interest_categ_id = $interest_categ['id']; |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (!empty($interest_categ_id)) { |
| 163 |
// Get all interests for the interest category |
| 164 |
$interests_url = "https://{$this->server}.api.mailchimp.com/3.0/lists/{$list_id}/interest-categories/{$interest_categ_id}/interests?count=1000"; |
| 165 |
$all_interests = json_decode(file_get_contents($interests_url, false, $context), true)['interests']; |
| 166 |
|
| 167 |
$interests_map = []; |
| 168 |
foreach ($all_interests as $interest) { |
| 169 |
$interests_map[$interest['name']] = $interest['id']; |
| 170 |
} |
| 171 |
// If the interest name is in the group names, add it to the interests array |
| 172 |
foreach ($group_names as $group_name) { |
| 173 |
if (isset($interests_map[$group_name])) { |
| 174 |
$interests[$interests_map[$group_name]] = true; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
} else { |
| 179 |
// Log an error message or throw an exception |
| 180 |
c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', "Group category not found: {$group_categ_name}"); |
| 181 |
} |
| 182 |
|
| 183 |
c_ws_plugin__s2member_utils_logs::log_entry('mailchimp-api', get_defined_vars()); |
| 184 |
|
| 185 |
return $interests; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
|