PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / -3.0.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v-3.0.1
3.3.1 V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 All 138 releases
bit-form / includes / Core / Integration / MailerLite / MailerLiteHandler.php

MailerLiteHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder -3.0.1, at includes/Core/Integration/MailerLite/MailerLiteHandler.php

285 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * MailerLite Integration
5 */
6
7 namespace BitCode\BitForm\Core\Integration\MailerLite;
8
9 if (!defined('ABSPATH')) {
10 exit;
11 }
12
13 use BitCode\BitForm\Core\Integration\IntegrationHandler;
14 use BitCode\BitForm\Core\Util\HttpHelper;
15 use BitCode\BitForm\GlobalHelper;
16 use WP_Error;
17
18 /**
19 * Provide functionality for MailerLite integration
20 */
21 class MailerLiteHandler
22 {
23 private $_integrationID;
24 private static $_baseUrlV1 = 'https://api.mailerlite.com/api/v2/';
25 private static $_baseUrlV2 = 'https://connect.mailerlite.com/api/';
26 protected $_defaultHeader;
27
28 private $_formID;
29
30 public function __construct($integrationID, $fromID)
31 {
32 $this->_integrationID = $integrationID;
33 $this->_formID = $fromID;
34 }
35
36 public static function registerAjax()
37 {
38 add_action('wp_ajax_bitforms_mailerlite_fetch_all_groups', [__CLASS__, 'fetchAllGroups']);
39 add_action('wp_ajax_bitforms_mailerlite_refresh_fields', [__CLASS__, 'mailerliteRefreshFields']);
40 }
41
42 public static function fetchAllGroups()
43 {
44 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
45 GlobalHelper::requirePostMethod();
46
47 try {
48 $requestParams = GlobalHelper::formatRequestData();
49 } catch (\InvalidArgumentException $e) {
50 wp_send_json_error($e->getMessage(), 400);
51 }
52 if (
53 empty($requestParams->auth_token)
54 ) {
55 wp_send_json_error(
56 __(
57 'Requested parameter is empty',
58 'bit-form'
59 ),
60 400
61 );
62 }
63
64 if ('v2' === $requestParams->version) {
65 $apiEndpoints = self::$_baseUrlV2 . 'groups';
66 $apiKey = trim((string) $requestParams->auth_token);
67 $header = [
68 'Authorization' => "Bearer $apiKey",
69 ];
70 $formattedResponse = [];
71
72 $httpArgs = [
73 'headers' => $header,
74 'timeout' => 30,
75 'redirection' => 0,
76 'httpversion' => '1.1',
77 'user-agent' => 'Bitforms/' . BITFORMS_VERSION . '; ' . get_bloginfo('url')
78 ];
79
80 $response = wp_remote_get($apiEndpoints, $httpArgs);
81
82 if (is_wp_error($response)) {
83 wp_send_json_error($response->get_error_message(), 400);
84 }
85
86 $responseCode = wp_remote_retrieve_response_code($response);
87 $responseBody = wp_remote_retrieve_body($response);
88 $responseObj = json_decode($responseBody);
89
90 if (200 === $responseCode && isset($responseObj->data)) {
91 foreach ($responseObj->data as $value) {
92 $formattedResponse[] = [
93 'group_id' => $value->id,
94 'name' => $value->name,
95 ];
96 }
97 } elseif (isset($responseObj->message) && 'Unauthenticated.' === $responseObj->message) {
98 wp_send_json_error(
99 __(
100 'Invalid API Token',
101 'bit-form'
102 ),
103 401
104 );
105 } elseif (isset($responseObj->message)) {
106 wp_send_json_error($responseObj->message, $responseCode ?: 400);
107 } elseif (!empty($responseBody)) {
108 wp_send_json_error($responseBody, $responseCode ?: 400);
109 } else {
110 wp_send_json_error(
111 __(
112 'Unable to fetch MailerLite groups',
113 'bit-form'
114 ),
115 $responseCode ?: 400
116 );
117 }
118 } else {
119 $apiEndpoints = self::$_baseUrlV1 . 'groups/';
120
121 $header = [
122 'X-Mailerlite-Apikey' => $requestParams->auth_token,
123 ];
124
125 $response = HttpHelper::get($apiEndpoints, null, $header);
126 $formattedResponse = [];
127
128 foreach ($response as $value) {
129 $formattedResponse[] =
130 [
131 'group_id' => (string)$value->id,
132 'name' => $value->name,
133 ];
134 }
135 }
136
137 wp_send_json_success($formattedResponse, 200);
138 } else {
139 wp_send_json_error(
140 __(
141 'Token expired',
142 'bit-form'
143 ),
144 401
145 );
146 }
147 }
148
149 public static function mailerliteRefreshFields()
150 {
151 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
152 GlobalHelper::requirePostMethod();
153 try {
154 $requestParams = GlobalHelper::formatRequestData();
155 } catch (\InvalidArgumentException $e) {
156 wp_send_json_error($e->getMessage(), 400);
157 }
158
159 if (
160 empty($requestParams->auth_token)
161 ) {
162 wp_send_json_error(
163 __(
164 'Requested parameter is empty',
165 'bit-form'
166 ),
167 400
168 );
169 }
170
171 if ('v2' === $requestParams->version) {
172 $apiEndpoints = self::$_baseUrlV2 . 'fields';
173
174 $apiKey = $requestParams->auth_token;
175 $header = [
176 'Authorization' => "Bearer $apiKey",
177 ];
178
179 $response = HttpHelper::get($apiEndpoints, null, $header);
180
181 $newResponse = [];
182 foreach ($response->data as $value) {
183 if ('email' !== $value->key) {
184 $newResponse[] = [
185 'key' => $value->key,
186 'label' => $value->name,
187 'required' => 'email' === $value->key ? true : false,
188 ];
189 }
190 }
191
192 $email[] = [
193 'key' => 'email',
194 'label' => 'Email',
195 'required' => true,
196 ];
197
198 $formattedResponse = array_merge($email, $newResponse);
199
200 if (isset($response->data)) {
201 wp_send_json_success($formattedResponse, 200);
202 } elseif (isset($response->message) && 'Unauthenticated.' === $response->message) {
203 wp_send_json_error(
204 __(
205 'Invalid API Token',
206 'bit-form'
207 ),
208 401
209 );
210 }
211 } else {
212 $apiEndpoints = self::$_baseUrlV1 . 'fields';
213
214 $apiKey = $requestParams->auth_token;
215 $header = [
216 'X-Mailerlite-Apikey' => $apiKey,
217 ];
218
219 $response = HttpHelper::get($apiEndpoints, null, $header);
220
221 $formattedResponse = [];
222 foreach ($response as $value) {
223 $formattedResponse[] = [
224 'key' => $value->key,
225 'label' => $value->title,
226 'required' => 'email' === $value->key ? true : false,
227 ];
228 }
229
230 if (count($response) > 0) {
231 wp_send_json_success($formattedResponse, 200);
232 } elseif (isset($response->error->message) && 'Unauthorized' === $response->error->message) {
233 wp_send_json_error(
234 __(
235 'Invalid API Token',
236 'bit-form'
237 ),
238 401
239 );
240 }
241 }
242 } else {
243 wp_send_json_error(
244 __(
245 'Token expired',
246 'bit-form'
247 ),
248 401
249 );
250 }
251 }
252
253 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
254 {
255 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
256 $auth_token = $integrationDetails->auth_token;
257 $version = $integrationDetails->version;
258 $groupIds = $integrationDetails->group_ids;
259 $fieldMap = $integrationDetails->field_map;
260 $type = $integrationDetails->mailer_lite_type;
261 $actions = $integrationDetails->actions;
262
263 if (
264 empty($fieldMap)
265 || empty($auth_token)
266 ) {
267 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for MailerLite api', 'bit-form'));
268 }
269 $recordApiHelper = new RecordApiHelper($auth_token, $this->_integrationID, $logID, $entryID, $actions, $version);
270 $mailerliteApiResponse = $recordApiHelper->executeRecordApi(
271 $groupIds,
272 $type,
273 $fieldValues,
274 $fieldMap,
275 $auth_token,
276 $this->_formID
277 );
278
279 if (is_wp_error($mailerliteApiResponse)) {
280 return $mailerliteApiResponse;
281 }
282 return $mailerliteApiResponse;
283 }
284 }
285