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 / WebHooks / WebHooksHandler.php

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

232 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * WebHooks Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\WebHooks;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use BitCode\BitForm\Core\Integration\IntegrationHandler;
15 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
16 use BitCode\BitForm\Core\Util\HttpHelper;
17 use BitCode\BitForm\GlobalHelper;
18
19 /**
20 * Provide functionality for webhooks
21 */
22 class WebHooksHandler
23 {
24 private $formID;
25 private $webhookID;
26
27 private $_logResponse;
28
29 public function __construct($webhookID, $formID)
30 {
31 $this->formID = $formID;
32 $this->webhookID = $webhookID;
33 $this->_logResponse = new UtilApiResponse();
34 }
35
36 /**
37 * Helps to register ajax function's with wp
38 *
39 * @return null
40 */
41 public static function registerAjax()
42 {
43 add_action('wp_ajax_bitforms_test_webhook', [__CLASS__, 'testWebhook']);
44 }
45
46 public static function testWebhook()
47 {
48 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
49
50 GlobalHelper::requirePostMethod();
51
52 try {
53 $webhookDetails = GlobalHelper::formatRequestData();
54 } catch (\InvalidArgumentException $e) {
55 wp_send_json_error($e->getMessage(), 400);
56 }
57
58 $details = is_string($webhookDetails) ? json_decode($webhookDetails)->hookDetails : $webhookDetails->hookDetails;
59 $method = isset($details->method) ? $details->method : 'get';
60 $data = isset($details->url) ? WebHooksHandler::urlParserWrapper($details->url) : false;
61 $response = null;
62 if ($data) {
63 $url = $data['url'];
64 // $url = $details->url;
65 $params = $data['params'];
66 $params = IntegrationHandler::replaceFieldWithValue($params, []);
67 $params['entry_id'] = 'test';
68 switch (strtoupper($method)) {
69 case 'GET':
70 $response = HttpHelper::get($url, $params);
71 break;
72
73 case 'POST':
74 $response = HttpHelper::post($url, $params);
75 break;
76
77 default:
78 $response = HttpHelper::request($url, $method, $params);
79 break;
80 }
81 }
82 if (is_wp_error($response)) {
83 wp_send_json_error(
84 empty($response) ? 'Unknown Error Occured' : $response->get_error_message(),
85 400
86 );
87 }
88 if (empty($data['url'])) {
89 wp_send_json_error(__('webhook url is empty', 'bit-form'), 400);
90 }
91 wp_send_json_success(['msg' => 'webhook executed succcessfully', 'response' => $response], 200);
92 } else {
93 wp_send_json_error(
94 __(
95 'Token expired',
96 'bit-form'
97 ),
98 401
99 );
100 }
101 }
102
103 public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID)
104 {
105 $details = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details;
106 $method = isset($details->method) ? $details->method : 'get';
107 $data = isset($details->url) ? $this->urlParserWrapper($details->url) : false;
108
109 $entryDetails = [
110 'formId' => $this->formID,
111 'entryId' => $entryID,
112 'fieldValues' => $fieldValues
113 ];
114
115 if ($data) {
116 $url = $data['url']; // fix for integromat
117 // $url = $details->url;
118 $params = $data['params'];
119 $params = IntegrationHandler::replaceFieldWithValue($params, $fieldValues);
120 $params['entry_id'] = $entryID;
121 switch (strtoupper($method)) {
122 case 'GET':
123 $response = HttpHelper::get($url, $params);
124 break;
125
126 case 'POST':
127 $response = HttpHelper::post($url, $params);
128 break;
129
130 default:
131 $response = HttpHelper::request($url, $method, $params);
132 break;
133 }
134
135 // if bitform pro is not active then return the response without response log
136 // if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
137 // return $response;
138 // }
139
140 // if bitform pro is active then return the response with response log
141 if (is_wp_error($response)) {
142 $this->_logResponse->apiResponse(
143 $logID,
144 $this->webhookID,
145 ['type' => 'record', 'type_name' => 'web hooks'],
146 'errors',
147 $response,
148 $entryDetails
149 );
150 } else {
151 $this->_logResponse->apiResponse(
152 $logID,
153 $this->webhookID,
154 ['type' => 'record', 'type_name' => 'web hooks'],
155 'success',
156 $response,
157 $entryDetails
158 );
159 }
160 return $response;
161 } else {
162 // if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
163 // return false;
164 // }
165
166 $this->_logResponse->apiResponse(
167 $logID,
168 $this->webhookID,
169 ['type' => 'record', 'type_name' => 'web hooks'],
170 'errors',
171 'There is something wrong in the webhook url',
172 $entryDetails
173 );
174 }
175 }
176
177 private static function urlParserWrapper($url)
178 {
179 if (empty($url)) {
180 return false;
181 }
182 $parsedURL = wp_parse_url($url);
183
184 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
185 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
186 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
187 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
188 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
189 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
190 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
191 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
192
193 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
194 $params = [];
195 foreach (explode('&', $Query) as $keyValue) {
196 if (empty($keyValue)) {
197 continue;
198 }
199 list($field, $value) = explode('=', $keyValue);
200 if ('' === trim($value)) {
201 continue;
202 }
203 if (isset($params[$field])) {
204 if (\is_array($params[$field])) {
205 $params[$field][] = sanitize_text_field(urldecode($value));
206 } else {
207 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
208 }
209 } else {
210 $params[$field] = sanitize_text_field(urldecode($value));
211 }
212 }
213
214 if (!wp_http_validate_url($cleanURL)) {
215 return false;
216 }
217
218 return ['url' => $cleanURL, 'params' => $params];
219 }
220
221 private function iterate($array)
222 {
223 $ar = [];
224 if (is_array($array)) {
225 foreach ($array as $k => $v) {
226 $ar[$k] = str_replace("\'", "'", $v);
227 }
228 }
229 return $ar;
230 }
231 }
232