PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.0
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 / Acumbamail / AcumbamailHandler.php

AcumbamailHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.10.0, at includes/Core/Integration/Acumbamail/AcumbamailHandler.php

184 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\Acumbamail;
4
5 use BitCode\BitForm\Core\Integration\IntegrationHandler;
6 use BitCode\BitForm\Core\Util\HttpHelper;
7 use WP_Error;
8
9 /**
10 * Provide functionality for Acumbamail integration
11 */
12 class AcumbamailHandler
13 {
14 private $integrationID;
15 public static $baseUrl = 'https://acumbamail.com/api/1/';
16
17 public function __construct($integrationID, $fromID)
18 {
19 $this->integrationID = $integrationID;
20 }
21
22 public static function registerAjax()
23 {
24 add_action('wp_ajax_bitforms_acumbamail_authorization_and_fetch_subscriber_list', [__CLASS__, 'acumbamailAuthAndFetchSubscriberList']);
25 add_action('wp_ajax_bitforms_acumbamail_fetch_all_list', [__CLASS__, 'fetchAllLists']);
26 add_action('wp_ajax_bitforms_acumbamail_refresh_fields', [__CLASS__, 'acumbamailRefreshFields']);
27 }
28
29 public static function fetchAllLists()
30 {
31 if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
32 wp_send_json_error(__('Token expired', 'bit-form'), 401);
33 }
34
35 $inputJSON = file_get_contents('php://input');
36 $requestParams = json_decode($inputJSON);
37
38 if (empty($requestParams->auth_token)) {
39 wp_send_json_error(
40 __(
41 'Requested parameter is empty',
42 'bit-form'
43 ),
44 400
45 );
46 }
47
48 $apiEndpoint = self::$baseUrl . 'getLists/';
49 $requestParams = [
50 'auth_token' => $requestParams->auth_token,
51 ];
52
53 $response = HttpHelper::post($apiEndpoint, $requestParams);
54
55 if ('Unauthorized' !== $response) {
56 wp_send_json_success($response, 200);
57 } else {
58 wp_send_json_error(
59 'The token is invalid',
60 400
61 );
62 }
63 }
64
65 public static function acumbamailAuthAndFetchSubscriberList()
66 {
67 if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
68 wp_send_json_error(__('Token expired', 'bit-form'), 401);
69 }
70
71 $inputJSON = file_get_contents('php://input');
72 $requestParams = json_decode($inputJSON);
73 if (empty($requestParams->auth_token)) {
74 wp_send_json_error(
75 __(
76 'Requested parameter is empty',
77 'bit-form'
78 ),
79 400
80 );
81 }
82 $apiEndpoint = self::$baseUrl . 'getLists/';
83
84 $queryParams = [
85 'auth_token' => $requestParams->auth_token,
86 ];
87
88 $response = HttpHelper::post($apiEndpoint, $queryParams, null);
89
90 if ('Unauthorized' !== $response) {
91 wp_send_json_success($response, 200);
92 } else {
93 wp_send_json_error(
94 'The token is invalid',
95 400
96 );
97 }
98 }
99
100 public static function acumbamailRefreshFields()
101 {
102 if (!isset($_REQUEST['_ajax_nonce']) && !wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
103 wp_send_json_error(__('Token expired', 'bit-form'), 401);
104 }
105
106 $inputJSON = file_get_contents('php://input');
107 $refreshFieldsRequestParams = json_decode($inputJSON);
108
109 if (empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->list_id)) {
110 wp_send_json_error(
111 __(
112 'Requested parameter is empty',
113 'bit-form'
114 ),
115 400
116 );
117 }
118 $apiEndpoint = self::$baseUrl . 'getFields/';
119
120 $requestParams = [
121 'auth_token' => $refreshFieldsRequestParams->auth_token,
122 'list_id' => $refreshFieldsRequestParams->list_id,
123 ];
124
125 $response = HttpHelper::post($apiEndpoint, $requestParams);
126
127 $formattedResponse = [];
128 foreach ($response as $key => $value) {
129 if ('email' === $key) {
130 $formattedResponse[$key] = [
131 $key => $value,
132 'required' => true,
133 ];
134 } else {
135 $formattedResponse[$key] = [
136 $key => $value,
137 'required' => false,
138 ];
139 }
140 }
141
142 if ('Unauthorized' !== $response) {
143 wp_send_json_success($formattedResponse, 200);
144 } else {
145 wp_send_json_error(
146 'The token is invalid',
147 400
148 );
149 }
150 }
151
152 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
153 {
154 $integrationDetails = json_decode($integrationData->integration_details);
155 $auth_token = $integrationDetails->auth_token;
156 $listId = $integrationDetails->listId;
157 $mainAction = $integrationDetails->mainAction;
158 $fieldMap = $integrationDetails->field_map;
159 $defaultDataConf = $integrationDetails->default;
160
161 if (
162 empty($listId)
163 || empty($fieldMap)
164 || empty($auth_token)
165 ) {
166 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Acumbamail api', 'bit-form'));
167 }
168 $recordApiHelper = new RecordApiHelper($auth_token, $this->integrationID, $logID, $entryID);
169 $acumbamailApiResponse = $recordApiHelper->execute(
170 $listId,
171 $mainAction,
172 $defaultDataConf,
173 $fieldValues,
174 $fieldMap,
175 $auth_token
176 );
177
178 if (is_wp_error($acumbamailApiResponse)) {
179 return $acumbamailApiResponse;
180 }
181 return $acumbamailApiResponse;
182 }
183 }
184