PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.0.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.0.2
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 V3.0.2, at includes/Core/Integration/WebHooks/WebHooksHandler.php

225 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\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 GlobalHelper::requirePostMethod();
50
51 try {
52 $webhookDetails = GlobalHelper::formatRequestData();
53 } catch (\InvalidArgumentException $e) {
54 wp_send_json_error($e->getMessage(), 400);
55 }
56
57 $details = is_string($webhookDetails) ? json_decode($webhookDetails)->hookDetails : $webhookDetails->hookDetails;
58 $method = isset($details->method) ? $details->method : 'get';
59 $data = isset($details->url) ? WebHooksHandler::urlParserWrapper($details->url) : false;
60 $response = null;
61 if ($data) {
62 $url = $data['url'];
63 // $url = $details->url;
64 $params = $data['params'];
65 $params = IntegrationHandler::replaceFieldWithValue($params, []);
66 $params['entry_id'] = 'test';
67 switch (strtoupper($method)) {
68 case 'GET':
69 $response = HttpHelper::get($url, $params);
70 break;
71
72 case 'POST':
73 $response = HttpHelper::post($url, $params);
74 break;
75
76 default:
77 $response = HttpHelper::request($url, $method, $params);
78 break;
79 }
80 }
81 if (is_wp_error($response)) {
82 wp_send_json_error(
83 empty($response) ? 'Unknown Error Occured' : $response->get_error_message(),
84 400
85 );
86 }
87 if (empty($data['url'])) {
88 wp_send_json_error(__('webhook url is empty', 'bit-form'), 400);
89 }
90 wp_send_json_success(['msg' => 'webhook executed succcessfully', 'response' => $response], 200);
91 } else {
92 wp_send_json_error(
93 __(
94 'Token expired',
95 'bit-form'
96 ),
97 401
98 );
99 }
100 }
101
102 public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID)
103 {
104 $details = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details;
105 $method = isset($details->method) ? $details->method : 'get';
106 $data = isset($details->url) ? $this->urlParserWrapper($details->url) : false;
107
108 $entryDetails = [
109 'formId' => $this->formID,
110 'entryId' => $entryID,
111 'fieldValues' => $fieldValues
112 ];
113
114 if ($data) {
115 $url = $data['url']; // fix for integromat
116 // $url = $details->url;
117 $params = $data['params'];
118 $params = IntegrationHandler::replaceFieldWithValue($params, $fieldValues);
119 $params['entry_id'] = $entryID;
120 switch (strtoupper($method)) {
121 case 'GET':
122 $response = HttpHelper::get($url, $params);
123 break;
124
125 case 'POST':
126 $response = HttpHelper::post($url, $params);
127 break;
128
129 default:
130 $response = HttpHelper::request($url, $method, $params);
131 break;
132 }
133
134 if (is_wp_error($response)) {
135 $this->_logResponse->apiResponse(
136 $logID,
137 $this->webhookID,
138 ['type' => 'record', 'type_name' => 'web hooks'],
139 'errors',
140 $response,
141 $entryDetails
142 );
143 } else {
144 $this->_logResponse->apiResponse(
145 $logID,
146 $this->webhookID,
147 ['type' => 'record', 'type_name' => 'web hooks'],
148 'success',
149 $response,
150 $entryDetails
151 );
152 }
153 return $response;
154 } else {
155 // if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
156 // return false;
157 // }
158
159 $this->_logResponse->apiResponse(
160 $logID,
161 $this->webhookID,
162 ['type' => 'record', 'type_name' => 'web hooks'],
163 'errors',
164 'There is something wrong in the webhook url',
165 $entryDetails
166 );
167 }
168 }
169
170 private static function urlParserWrapper($url)
171 {
172 if (empty($url)) {
173 return false;
174 }
175 $parsedURL = wp_parse_url($url);
176
177 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
178 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
179 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
180 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
181 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
182 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
183 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
184 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
185
186 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
187 $params = [];
188 foreach (explode('&', $Query) as $keyValue) {
189 if (empty($keyValue)) {
190 continue;
191 }
192 list($field, $value) = explode('=', $keyValue);
193 if ('' === trim($value)) {
194 continue;
195 }
196 if (isset($params[$field])) {
197 if (\is_array($params[$field])) {
198 $params[$field][] = sanitize_text_field(urldecode($value));
199 } else {
200 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
201 }
202 } else {
203 $params[$field] = sanitize_text_field(urldecode($value));
204 }
205 }
206
207 if (!wp_http_validate_url($cleanURL)) {
208 return false;
209 }
210
211 return ['url' => $cleanURL, 'params' => $params];
212 }
213
214 private function iterate($array)
215 {
216 $ar = [];
217 if (is_array($array)) {
218 foreach ($array as $k => $v) {
219 $ar[$k] = str_replace("\'", "'", $v);
220 }
221 }
222 return $ar;
223 }
224 }
225