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

149 lines 5.1 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\HttpHelper;
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 switch (strtoupper($method)) {
50 case 'GET':
51 $response = HttpHelper::get($url, $params);
52 break;
53
54 case 'POST':
55 $response = HttpHelper::post($url, $params);
56 break;
57
58 default:
59 $response = HttpHelper::request($url, $method, $params);
60 break;
61 }
62 }
63 if (is_wp_error($response)) {
64 wp_send_json_error(
65 empty($response) ? 'Unknown Error Occured' : $response->get_error_message(),
66 400
67 );
68 }
69 if (empty($data['url'])) {
70 wp_send_json_error(__("webhook url is empty", "bitform"), 400);
71 }
72 wp_send_json_success(__("webhook executed succcessfully", "bitform"), 200);
73 } else {
74 wp_send_json_error(
75 __(
76 'Token expired',
77 'bitform'
78 ),
79 401
80 );
81 }
82 }
83
84 public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues)
85 {
86 $details = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details;
87 $method = isset($details->method) ? $details->method : 'get';
88 $data = isset($details->url) ? $this->urlParserWrapper($details->url) : false;
89 if ($data) {
90 $url = $data['url'];
91 $params = $data['params'];
92 $params = IntegrationHandler::replaceFieldWithValue($params, $fieldValues);
93 switch (strtoupper($method)) {
94 case 'GET':
95 $response = HttpHelper::get($url, $params);
96 break;
97
98 case 'POST':
99 $response = HttpHelper::post($url, $params);
100 break;
101
102 default:
103 $response = HttpHelper::request($url, $method, $params);
104 break;
105 }
106 }
107 }
108
109 private static function urlParserWrapper($url)
110 {
111 if (empty($url)) {
112 return false;
113 }
114 $parsedURL = wp_parse_url($url);
115
116 $Scheme = isset($parsedURL['scheme']) ? $parsedURL['scheme'] . '://' : null;
117 $Usr = isset($parsedURL['usr']) ? $parsedURL['usr'] : null;
118 $Pass = isset($parsedURL['pass']) ? ':' . $parsedURL['pass'] : null;
119 $Host = isset($parsedURL['host']) ? $parsedURL['host'] : null;
120 $Port = isset($parsedURL['port']) ? ':' . $parsedURL['port'] : null;
121 $Path = isset($parsedURL['path']) ? $parsedURL['path'] : null;
122 $Query = isset($parsedURL['query']) ? $parsedURL['query'] : null;
123 $Pass = ($Pass || $Usr) ? "$Pass@" : null;
124
125 $cleanURL = "$Scheme$Usr$Pass$Host$Port$Path";
126 $params = array();
127 foreach (explode('&', $Query) as $keyValue) {
128 if (empty($keyValue)) {
129 continue;
130 }
131 list($field, $value) = explode('=', $keyValue);
132 if ('' == trim($value)) {
133 continue;
134 }
135 if (isset($params[$field])) {
136 if (\is_array($params[$field])) {
137 $params[$field][] = sanitize_text_field(urldecode($value));
138 } else {
139 $params[$field] = [$params[$field], sanitize_text_field(urldecode($value))];
140 }
141 } else {
142 $params[$field] = sanitize_text_field(urldecode($value));
143 }
144 }
145
146 return array('url' => $cleanURL, 'params' => $params);
147 }
148 }
149