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

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

218 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 GlobalHelper::requirePostMethod();
50
51 try {
52 $tokenRequestParams = GlobalHelper::formatRequestData();
53 } catch (\InvalidArgumentException $e) {
54 wp_send_json_error($e->getMessage(), 400);
55 }
56
57 if (
58 empty($tokenRequestParams->username)
59 || empty($tokenRequestParams->password)
60 ) {
61 wp_send_json_error(
62 __(
63 'Requested parameter is empty',
64 'bit-form'
65 ),
66 400
67 );
68 }
69 $header = [
70 'Authorization' => 'Basic ' . base64_encode("$tokenRequestParams->username:$tokenRequestParams->password"),
71 'Accept' => '*/*',
72 'verify' => false
73 ];
74 $apiEndpoint = self::$apiBaseUri . '/apiusers';
75
76 $apiResponse = HttpHelper::get($apiEndpoint, null, $header);
77 if (!(property_exists($apiResponse, '_embedded') && property_exists($apiResponse->_embedded, 'apiusers'))) {
78 wp_send_json_error(
79 // empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
80 'Unauthorize',
81 400
82 );
83 } else {
84 $apiResponse->generates_on = \time();
85 wp_send_json_success($apiResponse, 200);
86 }
87 }
88 }
89
90 /**
91 * Process request for getting recipientlists from rapidmail
92 *
93 * @param $queryParams Mandatory params to get recipients
94 *
95 * @return JSON rapidmailmail recipientlists data
96 */
97 public static function getAllRecipients()
98 {
99 $response = null;
100 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
101 GlobalHelper::requirePostMethod();
102
103 try {
104 $queryParams = GlobalHelper::formatRequestData();
105 } catch (\InvalidArgumentException $e) {
106 wp_send_json_error($e->getMessage(), 400);
107 }
108
109 if (
110 empty($queryParams->username)
111 || empty($queryParams->password)
112 ) {
113 wp_send_json_error(
114 __(
115 'Requested parameter is empty',
116 'bit-form'
117 ),
118 400
119 );
120 }
121 $header = [
122 'Authorization' => 'Basic ' . base64_encode("$queryParams->username:$queryParams->password"),
123 'Accept' => '*/*',
124 'verify' => false
125 ];
126 $recipientApiEndpoint = self::$apiBaseUri . '/recipientlists';
127 $apiResponse = HttpHelper::get($recipientApiEndpoint, null, $header);
128 $tempRecipient = $apiResponse->_embedded->recipientlists;
129 $data = [];
130
131 foreach ($tempRecipient as $list) {
132 $data[] = (object) [
133 'id' => $list->id,
134 'name' => $list->name
135 ];
136 }
137 $response['recipientlists'] = $data;
138 wp_send_json_success($response, 200);
139 }
140 }
141
142 public static function getAllFields($queryParams)
143 {
144 if (
145 empty($queryParams->username)
146 || empty($queryParams->password)
147 ) {
148 wp_send_json_error(
149 __(
150 'Requested parameter is empty',
151 'bit-form'
152 ),
153 400
154 );
155 }
156 $header = [
157 'Authorization' => 'Basic ' . base64_encode("$queryParams->username:$queryParams->password"),
158 'Accept' => '*/*',
159 'verify' => false
160 ];
161 $recipientApiEndpoint = self::$apiBaseUri . '/recipientlists';
162 $apiResponse = HttpHelper::get($recipientApiEndpoint, null, $header);
163 $tempRecipient = $apiResponse->_embedded->recipientlists;
164 $data = [];
165
166 foreach ($tempRecipient as $list) {
167 $data[] = (object) [
168 'id' => $list->id,
169 'name' => $list->name
170 ];
171 }
172 $response['recipientlists'] = $data;
173 wp_send_json_success($response, 200);
174 }
175
176 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
177 {
178 $integrationDetails = $integrationData->integration_details;
179 if (is_string($integrationDetails)) {
180 $integrationDetails = json_decode($integrationDetails);
181 }
182 $fieldMap = $integrationDetails->field_map;
183 $defaultDataConf = $integrationDetails->default;
184 $username = $integrationDetails->username;
185 $password = $integrationDetails->password;
186 $recipientLists = $defaultDataConf->recipientlists;
187
188 if (
189 empty($username)
190 || empty($password)
191 || empty($fieldMap)
192 ) {
193 $error = new WP_Error('REQ_FIELD_EMPTY', __('username, password, fields are required for rapidmail api', 'bit-form'));
194 return $error;
195 }
196 if (empty($recipientLists)) {
197 $error = new WP_Error('REQ_FIELD_EMPTY', __('Recipient List are required for rapidmail api', 'bit-form'));
198 return $error;
199 }
200 $actions = $integrationDetails->actions;
201
202 $recordApiHelper = new RecordApiHelper($integrationDetails, $username, $password, $logID, $entryID);
203 $rapidmailResponse = $recordApiHelper->executeRecordApi(
204 $this->_integrationID,
205 $defaultDataConf,
206 $recipientLists,
207 $fieldValues,
208 $fieldMap,
209 $actions,
210 $this->formID,
211 );
212 if (is_wp_error($rapidmailResponse)) {
213 return $rapidmailResponse;
214 }
215 return $rapidmailResponse;
216 }
217 }
218