← All changes
|
includes/Core/Integration/WebHooks/WebHooksHandler.php
+48
-20
2.0
→
2.15.3
View file →
| @@ -13,14 +13,18 @@ | ||
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * Provide functionality for webhooks |
| 16 | 16 | */ |
| 17 | -class WebHooksHandler { | |
| 18 | - private $fromID; | |
| 17 | +class WebHooksHandler | |
| 18 | +{ | |
| 19 | + private $formID; | |
| 19 | 20 | private $webhookID; |
| 20 | 21 | |
| 21 | - public function __construct($webhookID, $fromID) { | |
| 22 | - $this->formID = $fromID; | |
| 22 | + private $_logResponse; | |
| 23 | + | |
| 24 | + public function __construct($webhookID, $formID) | |
| 25 | + { | |
| 26 | + $this->formID = $formID; | |
| 23 | 27 | $this->webhookID = $webhookID; |
| 24 | 28 | $this->_logResponse = new UtilApiResponse(); |
| 25 | 29 | } |
| 26 | 30 | |
| @@ -28,13 +32,15 @@ | ||
| 28 | 32 | * Helps to register ajax function's with wp |
| 29 | 33 | * |
| 30 | 34 | * @return null |
| 31 | 35 | */ |
| 32 | - public static function registerAjax() { | |
| 36 | + public static function registerAjax() | |
| 37 | + { | |
| 33 | 38 | add_action('wp_ajax_bitforms_test_webhook', [__CLASS__, 'testWebhook']); |
| 34 | 39 | } |
| 35 | 40 | |
| 36 | - public static function testWebhook() { | |
| 41 | + public static function testWebhook() | |
| 42 | + { | |
| 37 | 43 | if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field($_REQUEST['_ajax_nonce']), 'bitforms_save')) { |
| 38 | 44 | $inputJSON = file_get_contents('php://input'); |
| 39 | 45 | $webhookDetails = json_decode($inputJSON); |
| 40 | 46 | $details = is_string($webhookDetails) ? json_decode($webhookDetails)->hookDetails : $webhookDetails->hookDetails; |
| @@ -79,16 +85,18 @@ | ||
| 79 | 85 | ); |
| 80 | 86 | } |
| 81 | 87 | } |
| 82 | 88 | |
| 83 | - public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID) { | |
| 89 | + public function execute(IntegrationHandler $integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID) | |
| 90 | + { | |
| 84 | 91 | $details = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details; |
| 85 | 92 | $method = isset($details->method) ? $details->method : 'get'; |
| 86 | 93 | $data = isset($details->url) ? $this->urlParserWrapper($details->url) : false; |
| 94 | + | |
| 87 | 95 | if ($data) { |
| 88 | 96 | $url = $data['url']; |
| 89 | 97 | $params = $data['params']; |
| 90 | - $params = IntegrationHandler::replaceFieldWithValue($params, self::iterate($fieldValues)); | |
| 98 | + $params = IntegrationHandler::replaceFieldWithValue($params, $fieldValues); | |
| 91 | 99 | $params['entry_id'] = $entryID; |
| 92 | 100 | switch (strtoupper($method)) { |
| 93 | 101 | case 'GET': |
| 94 | 102 | $response = HttpHelper::get($url, $params); |
| @@ -103,29 +111,48 @@ | ||
| 103 | 111 | break; |
| 104 | 112 | } |
| 105 | 113 | |
| 106 | 114 | // if bitform pro is not active then return the response without response log |
| 107 | - if (!class_exists('BitCode\\BitFormPro\\Plugin')) { | |
| 108 | - return $response; | |
| 109 | - } | |
| 115 | + // if (!class_exists('BitCode\\BitFormPro\\Plugin')) { | |
| 116 | + // return $response; | |
| 117 | + // } | |
| 110 | 118 | |
| 111 | 119 | // if bitform pro is active then return the response with response log |
| 112 | 120 | if (is_wp_error($response)) { |
| 113 | - $this->_logResponse->apiResponse($logID, $this->webhookID, ['type' => 'record', 'type_name' => 'web hooks'], 'errors', $response); | |
| 121 | + $this->_logResponse->apiResponse( | |
| 122 | + $logID, | |
| 123 | + $this->webhookID, | |
| 124 | + ['type' => 'record', 'type_name' => 'web hooks'], | |
| 125 | + 'errors', | |
| 126 | + $response | |
| 127 | + ); | |
| 114 | 128 | } else { |
| 115 | - $this->_logResponse->apiResponse($logID, $this->webhookID, ['type' => 'record', 'type_name' => 'web hooks'], 'success', $response); | |
| 129 | + $this->_logResponse->apiResponse( | |
| 130 | + $logID, | |
| 131 | + $this->webhookID, | |
| 132 | + ['type' => 'record', 'type_name' => 'web hooks'], | |
| 133 | + 'success', | |
| 134 | + $response | |
| 135 | + ); | |
| 116 | 136 | } |
| 117 | 137 | return $response; |
| 118 | 138 | } else { |
| 119 | - if (!class_exists('BitCode\\BitFormPro\\Plugin')) { | |
| 120 | - return false; | |
| 121 | - } | |
| 139 | + // if (!class_exists('BitCode\\BitFormPro\\Plugin')) { | |
| 140 | + // return false; | |
| 141 | + // } | |
| 122 | 142 | |
| 123 | - $this->_logResponse->apiResponse($logID, $this->webhookID, ['type' => 'record', 'type_name' => 'web hooks'], 'errors', 'There is something wrong in the webhook url'); | |
| 143 | + $this->_logResponse->apiResponse( | |
| 144 | + $logID, | |
| 145 | + $this->webhookID, | |
| 146 | + ['type' => 'record', 'type_name' => 'web hooks'], | |
| 147 | + 'errors', | |
| 148 | + 'There is something wrong in the webhook url' | |
| 149 | + ); | |
| 124 | 150 | } |
| 125 | 151 | } |
| 126 | 152 | |
| 127 | - private static function urlParserWrapper($url) { | |
| 153 | + private static function urlParserWrapper($url) | |
| 154 | + { | |
| 128 | 155 | if (empty($url)) { |
| 129 | 156 | return false; |
| 130 | 157 | } |
| 131 | 158 | $parsedURL = wp_parse_url($url); |
| @@ -162,9 +189,10 @@ | ||
| 162 | 189 | |
| 163 | 190 | return ['url' => $cleanURL, 'params' => $params]; |
| 164 | 191 | } |
| 165 | 192 | |
| 166 | - private function iterate($array) { | |
| 193 | + private function iterate($array) | |
| 194 | + { | |
| 167 | 195 | $ar = []; |
| 168 | 196 | if (is_array($array)) { |
| 169 | 197 | foreach ($array as $k => $v) { |
| 170 | 198 | $ar[$k] = str_replace("\'", "'", $v); |
| @@ -171,5 +199,5 @@ | ||
| 171 | 199 | } |
| 172 | 200 | } |
| 173 | 201 | return $ar; |
| 174 | 202 | } |
| 175 | -} | |
| 203 | +} | |