PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.2
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 / WebHooks / WebHooksHandler.php

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

226 lines 6.3 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\Core\Util\Utilities;
18 use BitCode\BitForm\GlobalHelper;
19
20 /**
21 * Provide functionality for webhooks
22 */
23 class WebHooksHandler
24 {
25 private $formID;
26 private $webhookID;
27
28 private $_logResponse;
29
30 public function __construct($webhookID, $formID)
31 {
32 $this->formID = $formID;
33 $this->webhookID = $webhookID;
34 $this->_logResponse = new UtilApiResponse();
35 }
36
37 /**
38 * Helps to register ajax function's with wp
39 *
40 * @return null
41 */
42 public static function registerAjax()
43 {
44 add_action('wp_ajax_bitforms_test_webhook', [__CLASS__, 'testWebhook']);
45 }
46
47 public static function testWebhook()
48 {
49 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
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) ? (Utilities::jsonObj($webhookDetails)->hookDetails ?? null) : ($webhookDetails->hookDetails ?? null);
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 (is_wp_error($response)) {
136 $this->_logResponse->apiResponse(
137 $logID,
138 $this->webhookID,
139 ['type' => 'record', 'type_name' => 'web hooks'],
140 'errors',
141 $response,
142 $entryDetails
143 );
144 } else {
145 $this->_logResponse->apiResponse(
146 $logID,
147 $this->webhookID,
148 ['type' => 'record', 'type_name' => 'web hooks'],
149 'success',
150 $response,
151 $entryDetails
152 );
153 }
154 return $response;
155 } else {
156 // if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
157 // return false;
158 // }
159
160 $this->_logResponse->apiResponse(
161 $logID,
162 $this->webhookID,
163 ['type' => 'record', 'type_name' => 'web hooks'],
164 'errors',
165 'There is something wrong in the webhook url',
166 $entryDetails
167 );
168 }
169 }
170
171 private static function urlParserWrapper($url)
172 {
173 if (empty($url)) {
174 return false;
175 }
176 $parsedURL = wp_parse_url($url);
177
178 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
179 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
180 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
181 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
182 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
183 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
184 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
185 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
186
187 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
188 $params = [];
189 foreach (explode('&', $Query) as $keyValue) {
190 if (empty($keyValue)) {
191 continue;
192 }
193 list($field, $value) = explode('=', $keyValue);
194 if ('' === trim($value)) {
195 continue;
196 }
197 if (isset($params[$field])) {
198 if (\is_array($params[$field])) {
199 $params[$field][] = sanitize_text_field(urldecode($value));
200 } else {
201 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
202 }
203 } else {
204 $params[$field] = sanitize_text_field(urldecode($value));
205 }
206 }
207
208 if (!wp_http_validate_url($cleanURL)) {
209 return false;
210 }
211
212 return ['url' => $cleanURL, 'params' => $params];
213 }
214
215 private function iterate($array)
216 {
217 $ar = [];
218 if (is_array($array)) {
219 foreach ($array as $k => $v) {
220 $ar[$k] = str_replace("\'", "'", $v);
221 }
222 }
223 return $ar;
224 }
225 }
226