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

176 lines 5.7 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 use BitCode\BitForm\Core\Integration\IntegrationHandler;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for webhooks
16 */
17 class WebHooksHandler {
18 private $fromID;
19 private $webhookID;
20
21 public function __construct($webhookID, $fromID) {
22 $this->formID = $fromID;
23 $this->webhookID = $webhookID;
24 $this->_logResponse = new UtilApiResponse();
25 }
26
27 /**
28 * Helps to register ajax function's with wp
29 *
30 * @return null
31 */
32 public static function registerAjax() {
33 add_action('wp_ajax_bitforms_test_webhook', [__CLASS__, 'testWebhook']);
34 }
35
36 public static function testWebhook() {
37 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) {
38 $inputJSON = file_get_contents('php://input');
39 $webhookDetails = json_decode($inputJSON);
40 $details = is_string($webhookDetails) ? json_decode($webhookDetails)->hookDetails : $webhookDetails->hookDetails;
41 $method = isset($details->method) ? $details->method : 'get';
42 $data = isset($details->url) ? WebHooksHandler::urlParserWrapper($details->url) : false;
43 $response = null;
44 if ($data) {
45 $url = $data['url'];
46 $params = $data['params'];
47 $params['entry_id'] = 'test';
48 switch (strtoupper($method)) {
49 case 'GET':
50 $response = HttpHelper::get($url, $params);
51 break;
52
53 case 'POST':
54 $response = HttpHelper::post($url, $params);
55 break;
56
57 default:
58 $response = HttpHelper::request($url, $method, $params);
59 break;
60 }
61 }
62 if (is_wp_error($response)) {
63 wp_send_json_error(
64 empty($response) ? 'Unknown Error Occured' : $response->get_error_message(),
65 400
66 );
67 }
68 if (empty($data['url'])) {
69 wp_send_json_error(__('webhook url is empty', 'bit-form'), 400);
70 }
71 wp_send_json_success(['msg' => 'webhook executed succcessfully', 'response' => $response], 200);
72 } else {
73 wp_send_json_error(
74 __(
75 'Token expired',
76 'bit-form'
77 ),
78 401
79 );
80 }
81 }
82
83 public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID) {
84 $details = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details;
85 $method = isset($details->method) ? $details->method : 'get';
86 $data = isset($details->url) ? $this->urlParserWrapper($details->url) : false;
87 if ($data) {
88 $url = $data['url'];
89 $params = $data['params'];
90 $params = IntegrationHandler::replaceFieldWithValue($params, self::iterate($fieldValues));
91 $params['entry_id'] = $entryID;
92 switch (strtoupper($method)) {
93 case 'GET':
94 $response = HttpHelper::get($url, $params);
95 break;
96
97 case 'POST':
98 $response = HttpHelper::post($url, $params);
99 break;
100
101 default:
102 $response = HttpHelper::request($url, $method, $params);
103 break;
104 }
105
106 // if bitform pro is not active then return the response without response log
107 if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
108 return $response;
109 }
110
111 // if bitform pro is active then return the response with response log
112 if (is_wp_error($response)) {
113 $this->_logResponse->apiResponse($logID, $this->webhookID, ['type' => 'record', 'type_name' => 'web hooks'], 'errors', $response);
114 } else {
115 $this->_logResponse->apiResponse($logID, $this->webhookID, ['type' => 'record', 'type_name' => 'web hooks'], 'success', $response);
116 }
117 return $response;
118 } else {
119 if (!class_exists('BitCode\\BitFormPro\\Plugin')) {
120 return false;
121 }
122
123 $this->_logResponse->apiResponse($logID, $this->webhookID, ['type' => 'record', 'type_name' => 'web hooks'], 'errors', 'There is something wrong in the webhook url');
124 }
125 }
126
127 private static function urlParserWrapper($url) {
128 if (empty($url)) {
129 return false;
130 }
131 $parsedURL = wp_parse_url($url);
132
133 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
134 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
135 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
136 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
137 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
138 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
139 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
140 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
141
142 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
143 $params = [];
144 foreach (explode('&', $Query) as $keyValue) {
145 if (empty($keyValue)) {
146 continue;
147 }
148 list($field, $value) = explode('=', $keyValue);
149 if ('' === trim($value)) {
150 continue;
151 }
152 if (isset($params[$field])) {
153 if (\is_array($params[$field])) {
154 $params[$field][] = sanitize_text_field(urldecode($value));
155 } else {
156 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
157 }
158 } else {
159 $params[$field] = sanitize_text_field(urldecode($value));
160 }
161 }
162
163 return ['url' => $cleanURL, 'params' => $params];
164 }
165
166 private function iterate($array) {
167 $ar = [];
168 if (is_array($array)) {
169 foreach ($array as $k => $v) {
170 $ar[$k] = str_replace("\'", "'", $v);
171 }
172 }
173 return $ar;
174 }
175 }
176