PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 / Rapidmail / RapidmailHandler.php

RapidmailHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/Core/Integration/Rapidmail/RapidmailHandler.php

222 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Rapidmail Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\Rapidmail;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use BitCode\BitForm\Core\Integration\IntegrationHandler;
15 use BitCode\BitForm\Core\Util\HttpHelper;
16 use BitCode\BitForm\GlobalHelper;
17 use WP_Error;
18
19 class RapidmailHandler
20 {
21 private $_integrationID;
22 public static $apiBaseUri = 'https://apiv3.emailsys.net/v1';
23 protected $_defaultHeader;
24
25 private $formID;
26
27 public function __construct($integrationID, $formID)
28 {
29 $this->_integrationID = $integrationID;
30
31 $this->formID = $formID;
32 }
33
34 /**
35 * Helps to register ajax function's with wp
36 *
37 * @return null
38 */
39 public static function registerAjax()
40 {
41 add_action('wp_ajax_bitforms_rapidmail_authorization', [__CLASS__, 'checkAuthorization']);
42 add_action('wp_ajax_bitforms_rapidmail_get_all_recipients', [__CLASS__, 'getAllRecipients']);
43 add_action('wp_ajax_bitforms_rapidmail_get_all_fields', [__CLASS__, 'getAllFields']);
44 }
45
46 public static function checkAuthorization()
47 {
48 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
49
50
51 GlobalHelper::requirePostMethod();
52
53 try {
54 $tokenRequestParams = GlobalHelper::formatRequestData();
55 } catch (\InvalidArgumentException $e) {
56 wp_send_json_error($e->getMessage(), 400);
57 }
58
59 if (
60 empty($tokenRequestParams->username)
61 || empty($tokenRequestParams->password)
62 ) {
63 wp_send_json_error(
64 __(
65 'Requested parameter is empty',
66 'bit-form'
67 ),
68 400
69 );
70 }
71 $header = [
72 'Authorization' => 'Basic ' . base64_encode("$tokenRequestParams->username:$tokenRequestParams->password"),
73 'Accept' => '*/*',
74 'verify' => false
75 ];
76 $apiEndpoint = self::$apiBaseUri . '/apiusers';
77
78 $apiResponse = HttpHelper::get($apiEndpoint, null, $header);
79 if (!(property_exists($apiResponse, '_embedded') && property_exists($apiResponse->_embedded, 'apiusers'))) {
80 wp_send_json_error(
81 // empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
82 'Unauthorize',
83 400
84 );
85 } else {
86 $apiResponse->generates_on = \time();
87 wp_send_json_success($apiResponse, 200);
88 }
89 }
90 }
91
92 /**
93 * Process request for getting recipientlists from rapidmail
94 *
95 * @param $queryParams Mandatory params to get recipients
96 *
97 * @return JSON rapidmailmail recipientlists data
98 */
99 public static function getAllRecipients()
100 {
101 $response = null;
102 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
103
104
105 GlobalHelper::requirePostMethod();
106
107 try {
108 $queryParams = GlobalHelper::formatRequestData();
109 } catch (\InvalidArgumentException $e) {
110 wp_send_json_error($e->getMessage(), 400);
111 }
112
113 if (
114 empty($queryParams->username)
115 || empty($queryParams->password)
116 ) {
117 wp_send_json_error(
118 __(
119 'Requested parameter is empty',
120 'bit-form'
121 ),
122 400
123 );
124 }
125 $header = [
126 'Authorization' => 'Basic ' . base64_encode("$queryParams->username:$queryParams->password"),
127 'Accept' => '*/*',
128 'verify' => false
129 ];
130 $recipientApiEndpoint = self::$apiBaseUri . '/recipientlists';
131 $apiResponse = HttpHelper::get($recipientApiEndpoint, null, $header);
132 $tempRecipient = $apiResponse->_embedded->recipientlists;
133 $data = [];
134
135 foreach ($tempRecipient as $list) {
136 $data[] = (object) [
137 'id' => $list->id,
138 'name' => $list->name
139 ];
140 }
141 $response['recipientlists'] = $data;
142 wp_send_json_success($response, 200);
143 }
144 }
145
146 public static function getAllFields($queryParams)
147 {
148 if (
149 empty($queryParams->username)
150 || empty($queryParams->password)
151 ) {
152 wp_send_json_error(
153 __(
154 'Requested parameter is empty',
155 'bit-form'
156 ),
157 400
158 );
159 }
160 $header = [
161 'Authorization' => 'Basic ' . base64_encode("$queryParams->username:$queryParams->password"),
162 'Accept' => '*/*',
163 'verify' => false
164 ];
165 $recipientApiEndpoint = self::$apiBaseUri . '/recipientlists';
166 $apiResponse = HttpHelper::get($recipientApiEndpoint, null, $header);
167 $tempRecipient = $apiResponse->_embedded->recipientlists;
168 $data = [];
169
170 foreach ($tempRecipient as $list) {
171 $data[] = (object) [
172 'id' => $list->id,
173 'name' => $list->name
174 ];
175 }
176 $response['recipientlists'] = $data;
177 wp_send_json_success($response, 200);
178 }
179
180 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
181 {
182 $integrationDetails = $integrationData->integration_details;
183 if (is_string($integrationDetails)) {
184 $integrationDetails = json_decode($integrationDetails);
185 }
186 $fieldMap = $integrationDetails->field_map;
187 $defaultDataConf = $integrationDetails->default;
188 $username = $integrationDetails->username;
189 $password = $integrationDetails->password;
190 $recipientLists = $defaultDataConf->recipientlists;
191
192 if (
193 empty($username)
194 || empty($password)
195 || empty($fieldMap)
196 ) {
197 $error = new WP_Error('REQ_FIELD_EMPTY', __('username, password, fields are required for rapidmail api', 'bit-form'));
198 return $error;
199 }
200 if (empty($recipientLists)) {
201 $error = new WP_Error('REQ_FIELD_EMPTY', __('Recipient List are required for rapidmail api', 'bit-form'));
202 return $error;
203 }
204 $actions = $integrationDetails->actions;
205
206 $recordApiHelper = new RecordApiHelper($integrationDetails, $username, $password, $logID, $entryID);
207 $rapidmailResponse = $recordApiHelper->executeRecordApi(
208 $this->_integrationID,
209 $defaultDataConf,
210 $recipientLists,
211 $fieldValues,
212 $fieldMap,
213 $actions,
214 $this->formID,
215 );
216 if (is_wp_error($rapidmailResponse)) {
217 return $rapidmailResponse;
218 }
219 return $rapidmailResponse;
220 }
221 }
222