Skip to content

Commit 120af4e

Browse files
committed
chore: add ability to exclude emails
1 parent 1006179 commit 120af4e

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

includes/Contacts/Create.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ private function mapWebHookData(array $user_data) : array
102102
return $hold;
103103
}
104104

105+
/**
106+
* Stop adding a contact if they're in our exlcuded list.
107+
*
108+
* @param mixed $user_data
109+
* @return array
110+
* @since 1.0.1
111+
*/
112+
private function dropExcludedEmails($user_data, $excluded_emails): array
113+
{
114+
$user_email = $user_data['email'] ?? '';
115+
if(in_array($user_email, $excluded_emails) || empty($user_email)) {
116+
return array();
117+
}
118+
119+
return $user_data;
120+
}
121+
105122
/**
106123
* Adds the contact to Mautic.
107124
*
@@ -204,15 +221,19 @@ public function setCustomMappings(array $mapping): object
204221
* @return void
205222
* @since 1.0.0
206223
*/
207-
public function add(array $user_data, array $custom_mappings = array()): ?int
224+
public function add(array $user_data, array $excluded_emails = array()): ?int
208225
{
209226

210227
if(empty($user_data['is_marketing_allowed']) ) {
211228
return null; // Only opted in contacts please...
212229
}
213230

214-
$id = $this->addContactToMautic($user_data);
231+
$user_data = $this->dropExcludedEmails($user_data, $excluded_emails);
232+
if(empty($user_data)) {
233+
return null;
234+
}
215235

236+
$id = $this->addContactToMautic($user_data);
216237
if(empty($id)) {
217238
return null;
218239
}

index.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Created on: 29/03/2023 (d/m/y)
1111
*
1212
* @link https://uriahsvictor.com
13-
* @since 1.0.0
13+
* @since 1.0.1
1414
* @license GPLv2
1515
*/
1616

@@ -53,12 +53,13 @@ function init(ServerRequestInterface $request): string
5353
$custom_mappings = customContactDataMappings();
5454
$segments = contactSegments();
5555
$tags = contactTags();
56+
$excluded_emails = excludedEmails();
5657

5758
$contactCreate = new CreateContact($plugin_id);
5859
$id = $contactCreate->setCustomMappings($custom_mappings)
5960
->setSegments($segments)
6061
->setTags($tags)
61-
->add($user_data);
62+
->add($user_data, $excluded_emails);
6263

6364
break;
6465
case 'install.deactivated':
@@ -68,8 +69,14 @@ function init(ServerRequestInterface $request): string
6869
break;
6970
}
7071

71-
// In production you might probably just want to send back an empty string...
72-
return ($id) ?: "The username and password set for the Client is probably wrong. Or the URL you set for the Mautic API is wrong.";
72+
/**
73+
* This will return null if:
74+
*
75+
* The username and password set for the Client is wrong.
76+
* The URL you set for the Mautic API is wrong.
77+
* The contact email is in the excluded list.
78+
*/
79+
return json_encode($id);
7380
}
7481

7582
// ------
@@ -100,7 +107,7 @@ function customContactDataMappings(): array
100107
'11538' => array( // Add other plugins you own.
101108
'id' => 'freemius_id',
102109
'gross' => 'dps_gross',
103-
)
110+
),
104111
// You can add further plugin IDs and their mappings.
105112
);
106113

@@ -125,7 +132,8 @@ function contactSegments(): array
125132
),
126133
'11538' => array( // Add other plugins you own.
127134
3
128-
)
135+
),
136+
// You can add further plugin IDs and their mappings.
129137
);
130138

131139
}
@@ -156,7 +164,20 @@ function contactTags(): array
156164
'dps-pro-user'
157165
),
158166
'dps-user'
159-
)
167+
),
168+
// You can add further plugin IDs and their mappings.
169+
);
170+
}
171+
172+
/**
173+
* Email addresses that should be excluded from being added to mautic.
174+
*
175+
* @return array
176+
* @since 1.0.1
177+
*/
178+
function excludedEmails(): array
179+
{
180+
return array(
160181
);
161182
}
162183

0 commit comments

Comments
 (0)