Skip to content

Commit d05e948

Browse files
committed
fix: mailer lite existing subscriber issue fixed
1 parent 3197934 commit d05e948

File tree

2 files changed

+192
-107
lines changed

2 files changed

+192
-107
lines changed

frontend-dev/src/components/AllIntegrations/MailerLite/MailerLiteActions.jsx

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,23 @@ import TableCheckBox from '../../Utilities/TableCheckBox'
1010
import 'react-multiple-select-dropdown-lite/dist/index.css'
1111
import { getAllGroups } from './MailerLiteCommonFunc'
1212

13-
export default function MailerLiteActions({
14-
mailerLiteConf,
15-
setMailerLiteConf,
16-
loading,
17-
setLoading
18-
}) {
13+
export default function MailerLiteActions({ mailerLiteConf, setMailerLiteConf, loading, setLoading }) {
1914
const [actionMdl, setActionMdl] = useState({ show: false, action: () => {} })
15+
2016
const actionHandler = (e, type) => {
2117
const newConf = { ...mailerLiteConf }
18+
2219
if (type === 'group') {
2320
getAllGroups(mailerLiteConf, setMailerLiteConf, loading, setLoading)
24-
if (e.target.checked) {
25-
newConf.actions.group = true
26-
} else {
27-
delete newConf.actions.group
28-
}
29-
setActionMdl({ show: type })
30-
}
31-
if (type === 'mailer_lite_type') {
32-
if (e.target.checked) {
33-
newConf.actions.mailer_lite_type = true
34-
} else {
35-
delete newConf.actions.mailer_lite_type
36-
}
37-
setActionMdl({ show: type })
3821
}
39-
if (type === 'update') {
40-
if (e.target.checked) {
41-
newConf.actions.update = true
42-
} else {
43-
delete newConf.actions.update
44-
}
45-
}
46-
if (type === 'double_opt_in') {
47-
if (e.target.checked) {
48-
newConf.actions.double_opt_in = true
49-
} else {
50-
delete newConf.actions.double_opt_in
51-
}
22+
23+
if (e.target.checked) {
24+
newConf.actions[type] = true
25+
} else {
26+
delete newConf.actions[type]
5227
}
5328

29+
setActionMdl({ show: type })
5430
setMailerLiteConf({ ...newConf })
5531
}
5632
const clsActionMdl = () => {
@@ -61,7 +37,6 @@ export default function MailerLiteActions({
6137
const newConf = { ...mailerLiteConf }
6238
if (type === 'group_ids' && val.length) {
6339
newConf.actions.group = true
64-
newConf.actions.update = true
6540
} else if (type === 'group_ids' && val.length < 1) {
6641
delete newConf.actions.group
6742
}
@@ -103,23 +78,23 @@ export default function MailerLiteActions({
10378
<div className="pos-rel d-flx w-8">
10479
<TableCheckBox
10580
checked={mailerLiteConf?.group_ids.length || false}
106-
onChange={(e) => actionHandler(e, 'group')}
81+
onChange={e => actionHandler(e, 'group')}
10782
className="wdt-200 mt-4 mr-2"
10883
value="group"
10984
title={__('Groups', 'bit-integrations')}
11085
subTitle={__('Add Groups', 'bit-integrations')}
11186
/>
11287
<TableCheckBox
11388
checked={mailerLiteConf?.mailer_lite_type || false}
114-
onChange={(e) => actionHandler(e, 'mailer_lite_type')}
89+
onChange={e => actionHandler(e, 'mailer_lite_type')}
11590
className="wdt-200 mt-4 mr-2"
11691
value="type"
11792
title={__('Type', 'bit-integrations')}
11893
subTitle={__('Add Type', 'bit-integrations')}
11994
/>
12095
<TableCheckBox
12196
checked={mailerLiteConf.actions?.double_opt_in || false}
122-
onChange={(e) => actionHandler(e, 'double_opt_in')}
97+
onChange={e => actionHandler(e, 'double_opt_in')}
12398
className="wdt-200 mt-4 mr-2"
12499
value="double_opt_in"
125100
title={__('Double Opt-in', 'bit-integrations')}
@@ -128,7 +103,7 @@ export default function MailerLiteActions({
128103
<TableCheckBox
129104
checked={mailerLiteConf.actions?.update || false}
130105
isInfo={mailerLiteConf?.group_ids.length}
131-
onChange={(e) => actionHandler(e, 'update')}
106+
onChange={e => actionHandler(e, 'update')}
132107
className="wdt-200 mt-4 mr-2"
133108
value="user_share"
134109
title={__('Update subscriber', 'bit-integrations')}
@@ -161,11 +136,11 @@ export default function MailerLiteActions({
161136
<MultiSelect
162137
className="msl-wrp-options"
163138
defaultValue={mailerLiteConf?.group_ids}
164-
options={mailerLiteConf?.groups?.map((group) => ({
139+
options={mailerLiteConf?.groups?.map(group => ({
165140
label: group.name,
166141
value: group.group_id
167142
}))}
168-
onChange={(val) => setChanges(val, 'group_ids')}
143+
onChange={val => setChanges(val, 'group_ids')}
169144
customValue
170145
/>
171146
<button
@@ -194,8 +169,8 @@ export default function MailerLiteActions({
194169
<MultiSelect
195170
className="msl-wrp-options"
196171
defaultValue={mailerLiteConf?.mailer_lite_type}
197-
options={mailerLiteTypes.map((type) => ({ label: type.label, value: type.value }))}
198-
onChange={(val) => setChanges(val, 'mailer_lite_type')}
172+
options={mailerLiteTypes.map(type => ({ label: type.label, value: type.value }))}
173+
onChange={val => setChanges(val, 'mailer_lite_type')}
199174
customValue
200175
singleSelect
201176
/>

includes/Actions/MailerLite/RecordApiHelper.php

Lines changed: 175 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public function existSubscriber($auth_token, $email)
5959

6060
$response = HttpHelper::get($apiEndpoints, null, $this->_defaultHeader);
6161

62-
return ! (property_exists($response, 'error') || 'Resource not found.' === $response->message)
63-
64-
;
62+
return !empty($response->data);
6563
}
6664

6765
public function enableDoubleOptIn($auth_token)
@@ -84,81 +82,142 @@ public function enableDoubleOptIn($auth_token)
8482
HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
8583
}
8684

85+
// public function addSubscriber($auth_token, $groupIds, $type, $finalData)
86+
// {
87+
// $apiEndpoints = $this->_baseUrl . 'subscribers';
88+
// $splitGroupIds = null;
89+
90+
// if (!empty($groupIds)) {
91+
// $splitGroupIds = explode(',', $groupIds);
92+
// }
93+
94+
// if (empty($finalData['email'])) {
95+
// return ['success' => false, 'message' => __('Required field Email is empty', 'bit-integrations'), 'code' => 400];
96+
// }
97+
98+
// if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) {
99+
// $requestParams = [
100+
// 'email' => $finalData['email'],
101+
// 'status' => $type ? $type : 'active',
102+
// ];
103+
// } else {
104+
// $requestParams = [
105+
// 'email' => $finalData['email'],
106+
// 'type' => $type ? $type : 'active',
107+
// ];
108+
// }
109+
110+
// foreach ($finalData as $key => $value) {
111+
// if ('email' !== $key) {
112+
// $requestParams['fields'][$key] = $value;
113+
// }
114+
// }
115+
// $requestParams['fields'] = !empty($requestParams['fields']) ? (object) $requestParams['fields'] : [];
116+
// $email = $finalData['email'];
117+
// $isExist = $this->existSubscriber($auth_token, $email);
118+
// $response = null;
119+
120+
// if ($isExist && empty($this->_actions->update)) {
121+
// return ['success' => false, 'message' => __('Subscriber already exist', 'bit-integrations'), 'code' => 400];
122+
// }
123+
124+
// if ($isExist && !empty($this->_actions->update)) {
125+
// if (!empty($groupIds)) {
126+
// if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) {
127+
// if (!empty($this->_actions->double_opt_in)) {
128+
// $requestParams['opted_in_at'] = date('Y-m-d H:i:s');
129+
// $requestParams['optin_ip'] = $_SERVER['REMOTE_ADDR'];
130+
// }
131+
132+
// $requestParams['groups'] = $splitGroupIds;
133+
// $response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
134+
// } else {
135+
// if (!empty($this->_actions->double_opt_in)) {
136+
// $this->enableDoubleOptIn($auth_token);
137+
// }
138+
139+
// for ($i = 0; $i < \count($splitGroupIds); $i++) {
140+
// $apiEndpoints = $this->_baseUrl . 'groups/' . $splitGroupIds[$i] . '/subscribers';
141+
// $response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
142+
// }
143+
// }
144+
145+
// return $response;
146+
// }
147+
// $response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
148+
// $response->update = true;
149+
// } else {
150+
// if (!empty($groupIds)) {
151+
// if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) {
152+
// $requestParams['groups'] = $splitGroupIds;
153+
154+
// if (!empty($this->_actions->double_opt_in)) {
155+
// $requestParams['opted_in_at'] = date('Y-m-d H:i:s');
156+
// $requestParams['optin_ip'] = $_SERVER['REMOTE_ADDR'];
157+
// }
158+
159+
// $response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
160+
// } else {
161+
// if (!empty($this->_actions->double_opt_in)) {
162+
// $this->enableDoubleOptIn($auth_token);
163+
// }
164+
165+
// for ($i = 0; $i < \count($splitGroupIds); $i++) {
166+
// $apiEndpoints = $this->_baseUrl . 'groups/' . $splitGroupIds[$i] . '/subscribers';
167+
// $response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
168+
// }
169+
// }
170+
171+
// return $response;
172+
// }
173+
// $response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
174+
// }
175+
176+
// return $response;
177+
// }
178+
87179
public function addSubscriber($auth_token, $groupIds, $type, $finalData)
88180
{
89-
$apiEndpoints = $this->_baseUrl . 'subscribers';
90-
$splitGroupIds = null;
91-
if (!empty($groupIds)) {
92-
$splitGroupIds = explode(',', $groupIds);
93-
}
94-
95181
if (empty($finalData['email'])) {
96-
return ['success' => false, 'message' => __('Required field Email is empty', 'bit-integrations'), 'code' => 400];
97-
}
98-
if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) {
99-
$requestParams = [
100-
'email' => $finalData['email'],
101-
'status' => $type ? $type : 'active',
102-
];
103-
} else {
104-
$requestParams = [
105-
'email' => $finalData['email'],
106-
'type' => $type ? $type : 'active',
182+
return [
183+
'success' => false,
184+
'message' => __('Required field Email is empty', 'bit-integrations'),
185+
'code' => 400
107186
];
108187
}
109188

110-
foreach ($finalData as $key => $value) {
111-
if ('email' !== $key) {
112-
$requestParams['fields'][$key] = $value;
113-
}
114-
}
115-
$requestParams['fields'] = !empty($requestParams['fields']) ? (object) $requestParams['fields'] : [];
116189
$email = $finalData['email'];
190+
$splitGroupIds = !empty($groupIds) ? explode(',', $groupIds) : [];
191+
$isMailerLiteV2 = $this->_baseUrl === 'https://connect.mailerlite.com/api/';
192+
$apiEndpoint = $this->_baseUrl . 'subscribers';
193+
194+
$requestParams = self::prepareRequestParams($finalData, $type, $isMailerLiteV2);
195+
117196
$isExist = $this->existSubscriber($auth_token, $email);
118197
$response = null;
119198

120-
if ($isExist && !empty($this->_actions->update)) {
121-
if (!empty($this->_actions->double_opt_in)) {
122-
$this->enableDoubleOptIn($auth_token);
123-
}
124-
if (!empty($groupIds)) {
125-
if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) {
126-
$requestParams['groups'] = $splitGroupIds;
127-
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
128-
} else {
129-
for ($i = 0; $i < \count($splitGroupIds); $i++) {
130-
$apiEndpoints = $this->_baseUrl . 'groups/' . $splitGroupIds[$i] . '/subscribers';
131-
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
132-
}
133-
}
134-
135-
return $response;
136-
}
137-
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
199+
if ($isExist && empty($this->_actions->update)) {
200+
return [
201+
'success' => false,
202+
'message' => __('Subscriber already exist', 'bit-integrations'),
203+
'code' => 400
204+
];
205+
}
206+
207+
self::handleDoubleOptIn($this, $auth_token, $requestParams, $isMailerLiteV2);
208+
209+
if (!empty($splitGroupIds)) {
210+
return self::sendToGroups($this, $splitGroupIds, $requestParams, $isMailerLiteV2);
211+
}
212+
213+
if ($isExist) {
214+
$response = HttpHelper::post($apiEndpoint, $requestParams, $this->_defaultHeader);
138215
$response->update = true;
139-
} elseif ($isExist && empty($this->_actions->update)) {
140-
return ['success' => false, 'message' => __('Subscriber already exist', 'bit-integrations'), 'code' => 400];
141-
} else {
142-
if (!empty($this->_actions->double_opt_in)) {
143-
$this->enableDoubleOptIn($auth_token);
144-
}
145-
if (!empty($groupIds)) {
146-
if ('https://connect.mailerlite.com/api/' === $this->_baseUrl) {
147-
$requestParams['groups'] = $splitGroupIds;
148-
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
149-
} else {
150-
for ($i = 0; $i < \count($splitGroupIds); $i++) {
151-
$apiEndpoints = $this->_baseUrl . 'groups/' . $splitGroupIds[$i] . '/subscribers';
152-
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
153-
}
154-
}
155-
156-
return $response;
157-
}
158-
$response = HttpHelper::post($apiEndpoints, $requestParams, $this->_defaultHeader);
216+
217+
return $response;
159218
}
160219

161-
return $response;
220+
return HttpHelper::post($apiEndpoint, $requestParams, $this->_defaultHeader);
162221
}
163222

164223
public function generateReqDataFromFieldMap($data, $fieldMap)
@@ -197,4 +256,55 @@ public function execute(
197256

198257
return $apiResponse;
199258
}
259+
260+
private static function prepareRequestParams($finalData, $type, $isMailerLiteV2)
261+
{
262+
$email = $finalData['email'];
263+
$params = [
264+
'email' => $email,
265+
$isMailerLiteV2 ? 'status' : 'type' => $type ? $type : 'active',
266+
];
267+
268+
foreach ($finalData as $key => $value) {
269+
if ($key !== 'email') {
270+
$params['fields'][$key] = $value;
271+
}
272+
}
273+
274+
$params['fields'] = !empty($params['fields']) ? (object) $params['fields'] : [];
275+
276+
return $params;
277+
}
278+
279+
private static function handleDoubleOptIn($context, $auth_token, &$requestParams, $isMailerLiteV2)
280+
{
281+
if (empty($context->_actions->double_opt_in)) {
282+
return;
283+
}
284+
285+
if ($isMailerLiteV2) {
286+
$requestParams['opted_in_at'] = date('Y-m-d H:i:s');
287+
$requestParams['optin_ip'] = $_SERVER['REMOTE_ADDR'];
288+
} else {
289+
$context->enableDoubleOptIn($auth_token);
290+
}
291+
}
292+
293+
private static function sendToGroups($context, $groupIds, $requestParams, $isMailerLiteV2)
294+
{
295+
$response = null;
296+
297+
if ($isMailerLiteV2) {
298+
$requestParams['groups'] = $groupIds;
299+
$endpoint = $context->_baseUrl . 'subscribers';
300+
$response = HttpHelper::post($endpoint, $requestParams, $context->_defaultHeader);
301+
} else {
302+
foreach ($groupIds as $groupId) {
303+
$endpoint = $context->_baseUrl . 'groups/' . $groupId . '/subscribers';
304+
$response = HttpHelper::post($endpoint, $requestParams, $context->_defaultHeader);
305+
}
306+
}
307+
308+
return $response;
309+
}
200310
}

0 commit comments

Comments
 (0)