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

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