PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.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 2.10.2 All 137 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 2.9.1, at includes/Core/Integration/MailerLite/MailerLiteHandler.php

245 lines 6.7 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 use BitCode\BitForm\Core\Integration\IntegrationHandler;
10 use BitCode\BitForm\Core\Util\HttpHelper;
11 use WP_Error;
12
13 /**
14 * Provide functionality for MailerLite integration
15 */
16 class MailerLiteHandler
17 {
18 private $_integrationID;
19 private static $_baseUrlV1 = 'https://api.mailerlite.com/api/v2/';
20 private static $_baseUrlV2 = 'https://connect.mailerlite.com/api/';
21 protected $_defaultHeader;
22
23 public function __construct($integrationID, $fromID)
24 {
25 $this->_integrationID = $integrationID;
26 }
27
28 public static function registerAjax()
29 {
30 add_action('wp_ajax_bitforms_mailerlite_fetch_all_groups', [__CLASS__, 'fetchAllGroups']);
31 add_action('wp_ajax_bitforms_mailerlite_refresh_fields', [__CLASS__, 'mailerliteRefreshFields']);
32 }
33
34 public static function fetchAllGroups()
35 {
36 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
37 $inputJSON = file_get_contents('php://input');
38 $requestParams = json_decode($inputJSON);
39 if (
40 empty($requestParams->auth_token)
41 ) {
42 wp_send_json_error(
43 __(
44 'Requested parameter is empty',
45 'bitform'
46 ),
47 400
48 );
49 }
50
51 if ('v2' === $requestParams->version) {
52 $apiEndpoints = self::$_baseUrlV2 . 'groups/';
53 $apiKey = $requestParams->auth_token;
54 $header = [
55 'Authorization: Bearer ' . $apiKey
56 ];
57
58 $curl = curl_init();
59 curl_setopt_array($curl, [
60 CURLOPT_URL => $apiEndpoints,
61 CURLOPT_RETURNTRANSFER => true,
62 CURLOPT_ENCODING => '',
63 CURLOPT_MAXREDIRS => 10,
64 CURLOPT_TIMEOUT => 0,
65 CURLOPT_FOLLOWLOCATION => true,
66 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
67 CURLOPT_CUSTOMREQUEST => 'GET',
68 CURLOPT_HTTPHEADER => $header,
69 ]);
70 $data = curl_exec($curl);
71 curl_close($curl);
72 $response = json_decode($data);
73
74 $formattedResponse = [];
75
76 foreach ($response->data as $value) {
77 $formattedResponse[] =
78 [
79 'group_id' => $value->id,
80 'name' => $value->name,
81 ];
82 }
83 } else {
84 $apiEndpoints = self::$_baseUrlV1 . 'groups/';
85
86 $header = [
87 'X-Mailerlite-Apikey' => $requestParams->auth_token,
88 ];
89
90 $response = HttpHelper::get($apiEndpoints, null, $header);
91 $formattedResponse = [];
92
93 foreach ($response as $value) {
94 $formattedResponse[] =
95 [
96 'group_id' => $value->id,
97 'name' => $value->name,
98 ];
99 }
100 }
101
102 wp_send_json_success($formattedResponse, 200);
103 } else {
104 wp_send_json_error(
105 __(
106 'Token expired',
107 'bitform'
108 ),
109 401
110 );
111 }
112 }
113
114 public static function mailerliteRefreshFields()
115 {
116 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
117 $inputJSON = file_get_contents('php://input');
118 $requestParams = json_decode($inputJSON);
119
120 if (
121 empty($requestParams->auth_token)
122 ) {
123 wp_send_json_error(
124 __(
125 'Requested parameter is empty',
126 'bitform'
127 ),
128 400
129 );
130 }
131
132 if ('v2' === $requestParams->version) {
133 $apiEndpoints = self::$_baseUrlV2 . 'fields';
134
135 $apiKey = $requestParams->auth_token;
136 $header = [
137 'Authorization' => "Bearer $apiKey",
138 ];
139
140 $response = HttpHelper::get($apiEndpoints, null, $header);
141
142 $newResponse = [];
143 foreach ($response->data as $value) {
144 if ('email' !== $value->key) {
145 $newResponse[] = [
146 'key' => $value->key,
147 'label' => $value->name,
148 'required' => 'email' === $value->key ? true : false,
149 ];
150 }
151 }
152
153 $email[] = [
154 'key' => 'email',
155 'label' => 'Email',
156 'required' => true,
157 ];
158
159 $formattedResponse = array_merge($email, $newResponse);
160
161 if (isset($response->data)) {
162 wp_send_json_success($formattedResponse, 200);
163 } elseif (isset($response->message) && 'Unauthenticated.' === $response->message) {
164 wp_send_json_error(
165 __(
166 'Invalid API Token',
167 'bitform'
168 ),
169 401
170 );
171 }
172 } else {
173 $apiEndpoints = self::$_baseUrlV1 . 'fields';
174
175 $apiKey = $requestParams->auth_token;
176 $header = [
177 'X-Mailerlite-Apikey' => $apiKey,
178 ];
179
180 $response = HttpHelper::get($apiEndpoints, null, $header);
181
182 $formattedResponse = [];
183 foreach ($response as $value) {
184 $formattedResponse[] = [
185 'key' => $value->key,
186 'label' => $value->title,
187 'required' => 'email' === $value->key ? true : false,
188 ];
189 }
190
191 if (count($response) > 0) {
192 wp_send_json_success($formattedResponse, 200);
193 } elseif (isset($response->error->message) && 'Unauthorized' === $response->error->message) {
194 wp_send_json_error(
195 __(
196 'Invalid API Token',
197 'bitform'
198 ),
199 401
200 );
201 }
202 }
203 } else {
204 wp_send_json_error(
205 __(
206 'Token expired',
207 'bitform'
208 ),
209 401
210 );
211 }
212 }
213
214 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
215 {
216 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
217 $auth_token = $integrationDetails->auth_token;
218 $version = $integrationDetails->version;
219 $groupIds = $integrationDetails->group_ids;
220 $fieldMap = $integrationDetails->field_map;
221 $type = $integrationDetails->mailer_lite_type;
222 $actions = $integrationDetails->actions;
223
224 if (
225 empty($fieldMap)
226 || empty($auth_token)
227 ) {
228 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for MailerLite api', 'bit-integrations'));
229 }
230 $recordApiHelper = new RecordApiHelper($auth_token, $this->_integrationID, $logID, $entryID, $actions, $version);
231 $mailerliteApiResponse = $recordApiHelper->executeRecordApi(
232 $groupIds,
233 $type,
234 $fieldValues,
235 $fieldMap,
236 $auth_token
237 );
238
239 if (is_wp_error($mailerliteApiResponse)) {
240 return $mailerliteApiResponse;
241 }
242 return $mailerliteApiResponse;
243 }
244 }
245