@@ -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