Skip to content
Merged
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ If you want to permantly delete a user, you can call the delete method. Carefull
$mc->delete('john.doe@example.com'); // Permanently deletes john.doe@example.com from the default list
$mc->delete('john.doe@example.com', 'abcdef1356'); // Permanently deletes john.doe@example.com from the list abcdef1356
```

If you just want to get a subscribers status, you can use the getStatus method. I do not suggest to implement this in your forms like 'this user is already subscribed' to protect the anonymity of your users. You can learn more about status labels [here](http://developer.mailchimp.com/documentation/mailchimp/guides/manage-subscribers-with-the-mailchimp-api/#check-subscription-status)
```PHP
$mc->getStatus('john.doe@example.com'); // Returns the status of a user.
```
## Important Notes
* This module does not do any data validation. Use a sever-sided validation like [Valitron](https://github.com/vlucas/valitron)
* Make sure that you have set up your fields in your Mailchimp list. You can do it at `Settings > List fields and *|MERGE|* tags`
Expand Down Expand Up @@ -81,6 +84,12 @@ If you have enabled double opt-in (it is enabled by default) you will not see th
Go to [Mailchimps Error Glossary](https://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/) for more Information

## Changelog
### 0.0.3
_Note: You can update savely from 0.0.2 without any changes in your code_
* Added 'getStatus' method `$mc->getStatus($email, $list = "")`
* Added ability to resubscribe a user, if he has unsubscribed. The user will have to confirm his subscription again via double-opt-in (if not disabled)
#### Other
* Centralized the validation warning via a constant
### 0.0.2
_Note: You can update savely from 0.0.1 without any changes in your code_
#### New Features
Expand Down
73 changes: 52 additions & 21 deletions SubscribeToMailchimp.module
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ class SubscribeToMailchimp extends WireData implements Module, ConfigurableModul
return [
'title' => 'Subscribe to Mailchimp',
'summary' => 'Subscribe, update, unsubscribe or delete a user in your Mailchimp mailing list.',
'version' => '0.0.2',
'version' => '0.0.3',
'author' => 'danielstieber',
'href' => 'https://github.com/danielstieber/SubscribeToMailchimp',
'icon' => 'address-card',
'autoload'=> false,
'singular'=> false
];
}
const WARNING_VALIDATION = "A user managed to send a form without an email address. Please check your form validation! A server-sided validation (e.g. via Valitron) is strongly recommended.";

static public function getDefaults() {
return array(
"double_opt_in" => "checked",
Expand All @@ -27,7 +29,7 @@ class SubscribeToMailchimp extends WireData implements Module, ConfigurableModul
if(empty($list)) // use default list from module settings, if no list is provided
$list = $this->default_list;
if(isset($email)) {
$api = $this->getApiBase($list);
$api = $this->getApiBase($list); // get the api path
$param['email_address'] = $email;
foreach ($data as $k => $v) {
if(!empty(trim($v))) { // avoid to override data of existing leads with empty field values
Expand All @@ -39,24 +41,31 @@ class SubscribeToMailchimp extends WireData implements Module, ConfigurableModul
'Content-Type' => 'application/json',
'Authorization' => 'Basic '.base64_encode('user:'.$this->api_key),
]);
if($http->send($api . md5($email), json_encode($param), 'PATCH')) { // try to update if contact already exists
return true;
} else { // create new, if update failed (=contact does not exist)
$status = $this->getStatus($email, $list); // get the current subscription status of the user
if($status !== false) {
if($this->double_opt_in)
if ($status == 'unsubscribed') {
if($this->double_opt_in)
$param['status'] = 'pending';
else
$param['status'] = 'subscribed';
}
$response = $http->send($api . md5($email), json_encode($param), 'PATCH');
} else {
if($this->double_opt_in)
$param['status'] = 'pending';
else
$param['status'] = 'subscribed';
$response = $http->send($api, json_encode($param), 'POST');
if($response !== false) {
return true;
} else {
$this->warning("Mailchimp request not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
return false;
}
}
if($response !== false) {
return true;
} else {
$this->warning("Mailchimp request subscribe() not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
return false;
}
} else {
// Log warning, if a user is able to send a form without email.
$this->warning("A user managed to send a form without an email address. Please check your form validation! A server-sided validation (e.g. via Valitron) is strongly recommended.");
$this->warning(self::WARNING_VALIDATION); // Log warning, if a user is able to send a form without email.
return false;
}
}
Expand All @@ -65,7 +74,7 @@ class SubscribeToMailchimp extends WireData implements Module, ConfigurableModul
if(empty($list)) // use default list from module settings, if no list is provided
$list = $this->default_list;
if(isset($email)) {
$api = $this->getApiBase($list);
$api = $this->getApiBase($list); // get the api path
$http = new WireHttp();
$http->setHeaders([
'Content-Type' => 'application/json',
Expand All @@ -76,21 +85,20 @@ class SubscribeToMailchimp extends WireData implements Module, ConfigurableModul
if($response !== false) {
return true;
} else {
$this->warning("Mailchimp request not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
$this->warning("Mailchimp request unsubscribe() not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
return false;
}
return true;
} else {
// Log warning, if a user is able to send a form without email.
$this->warning("A user managed to send a form without an email address. Please check your form validation! A server-sided validation (e.g. via Valitron) is strongly recommended.");
$this->warning(self::WARNING_VALIDATION); // Log warning, if a user is able to send a form without email.
return false;
}
}
public function delete($email, $list = "") {
if(empty($list)) // use default list from module settings, if no list is provided
$list = $this->default_list;
if(isset($email)) {
$api = $this->getApiBase($list);
$api = $this->getApiBase($list); // get the api path
$http = new WireHttp();
$http->setHeaders([
'Content-Type' => 'application/json',
Expand All @@ -100,13 +108,36 @@ class SubscribeToMailchimp extends WireData implements Module, ConfigurableModul
if($response !== false) {
return true;
} else {
$this->warning("Mailchimp request not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
$this->warning("Mailchimp request delete() not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
return false;
}
return true;
} else {
$this->warning(self::WARNING_VALIDATION); // Log warning, if a user is able to send a form without email.
return false;
}
}
public function getStatus($email, $list = "") {
$param = [];
if(empty($list)) // use default list from module settings, if no list is provided
$list = $this->default_list;
if(isset($email)) {
$api = $this->getApiBase($list); // get the api path
$http = new WireHttp();
$http->setHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Basic '.base64_encode('user:'.$this->api_key),
]);
$response = $http->get($api . md5($email), json_encode($param)); // get the entry of the given mail adress on the list
if($response !== false) {
return json_decode($response)->status; // return the subscription status
} else {
$this->warning("Mailchimp request getStatus() not successful: " . $http->getError(), Notice::log); // Log warning in Processwire backend
return false;
}
return true;
} else {
// Log warning, if a user is able to send a form without email.
$this->warning("A user managed to send a form without an email address. Please check your form validation! A server-sided validation (e.g. via Valitron) is strongly recommended.");
$this->warning(self::WARNING_VALIDATION); // Log warning, if a user is able to send a form without email.
return false;
}
}
Expand Down