← All changes
|
includes/Core/Integration/Hubspot/HubspotHandler.php
+84
-52
2.10.0
→
3.3.1
View file →
| @@ -1,10 +1,15 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Integration\Hubspot; |
| 4 | 4 | |
| 5 | +if (!defined('ABSPATH')) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 5 | 9 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 6 | 10 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 | +use BitCode\BitForm\GlobalHelper; | |
| 7 | 12 | use WP_Error; |
| 8 | 13 | |
| 9 | 14 | final class HubspotHandler |
| 10 | 15 | { |
| @@ -11,11 +16,14 @@ | ||
| 11 | 16 | private $_integrationID; |
| 12 | 17 | public static $apiBaseUri = 'https://apiv3.emailsys.net/v1'; |
| 13 | 18 | protected $_defaultHeader; |
| 14 | 19 | |
| 15 | - public function __construct($integrationID) | |
| 20 | + private $formId; | |
| 21 | + | |
| 22 | + public function __construct($integrationID, $fromID) | |
| 16 | 23 | { |
| 17 | 24 | $this->_integrationID = $integrationID; |
| 25 | + $this->formId = $fromID; | |
| 18 | 26 | } |
| 19 | 27 | |
| 20 | 28 | public static function registerAjax() |
| 21 | 29 | { |
| @@ -20,9 +28,8 @@ | ||
| 20 | 28 | public static function registerAjax() |
| 21 | 29 | { |
| 22 | 30 | add_action('wp_ajax_bitforms_hubspot_authorize', [__CLASS__, 'hubspotAuthorize']); |
| 23 | 31 | add_action('wp_ajax_bitforms_hubspot_pipeline', [__CLASS__, 'getAllPipelines']); |
| 24 | - add_action('wp_ajax_bitforms_hubspot_pipeline_tickets', [__CLASS__, 'getAllPipelinesTickets']); | |
| 25 | 32 | add_action('wp_ajax_bitforms_hubspot_owners', [__CLASS__, 'getAllOwners']); |
| 26 | 33 | add_action('wp_ajax_bitforms_hubspot_contacts', [__CLASS__, 'getAllContacts']); |
| 27 | 34 | add_action('wp_ajax_bitforms_hubspot_company', [__CLASS__, 'getAllCompany']); |
| 28 | 35 | } |
| @@ -28,13 +35,18 @@ | ||
| 28 | 35 | } |
| 29 | 36 | |
| 30 | 37 | public static function hubspotAuthorize() |
| 31 | 38 | { |
| 32 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 39 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 33 | 40 | $authorizationHeader = null; |
| 34 | - $inputJSON = file_get_contents('php://input'); | |
| 35 | - $requestsParams = json_decode($inputJSON); | |
| 36 | 41 | |
| 42 | + GlobalHelper::requirePostMethod(); | |
| 43 | + try { | |
| 44 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 45 | + } catch (\InvalidArgumentException $e) { | |
| 46 | + wp_send_json_error($e->getMessage(), 400); | |
| 47 | + } | |
| 48 | + | |
| 37 | 49 | if (empty($requestsParams->api_key)) { |
| 38 | 50 | wp_send_json_error( |
| 39 | 51 | __( |
| 40 | 52 | 'Requested parameter is empty', |
| @@ -61,13 +73,19 @@ | ||
| 61 | 73 | } |
| 62 | 74 | |
| 63 | 75 | public static function getAllPipelines() |
| 64 | 76 | { |
| 65 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 77 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 66 | 78 | $authorizationHeader = null; |
| 67 | - $inputJSON = file_get_contents('php://input'); | |
| 68 | - $requestsParams = json_decode($inputJSON); | |
| 69 | 79 | |
| 80 | + GlobalHelper::requirePostMethod(); | |
| 81 | + | |
| 82 | + try { | |
| 83 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 84 | + } catch (\InvalidArgumentException $e) { | |
| 85 | + wp_send_json_error($e->getMessage(), 400); | |
| 86 | + } | |
| 87 | + | |
| 70 | 88 | if (empty($requestsParams->apiKey)) { |
| 71 | 89 | wp_send_json_error( |
| 72 | 90 | __( |
| 73 | 91 | 'Requested parameter is empty', |
| @@ -112,13 +130,19 @@ | ||
| 112 | 130 | } |
| 113 | 131 | |
| 114 | 132 | public static function getAllOwners() |
| 115 | 133 | { |
| 116 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 134 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 117 | 135 | $authorizationHeader = null; |
| 118 | - $inputJSON = file_get_contents('php://input'); | |
| 119 | - $requestsParams = json_decode($inputJSON); | |
| 120 | 136 | |
| 137 | + GlobalHelper::requirePostMethod(); | |
| 138 | + | |
| 139 | + try { | |
| 140 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 141 | + } catch (\InvalidArgumentException $e) { | |
| 142 | + wp_send_json_error($e->getMessage(), 400); | |
| 143 | + } | |
| 144 | + | |
| 121 | 145 | if (empty($requestsParams->apiKey)) { |
| 122 | 146 | wp_send_json_error( |
| 123 | 147 | __( |
| 124 | 148 | 'Requested parameter is empty', |
| @@ -153,59 +177,66 @@ | ||
| 153 | 177 | } |
| 154 | 178 | |
| 155 | 179 | public static function getAllContacts() |
| 156 | 180 | { |
| 157 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 181 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 158 | 182 | $authorizationHeader = null; |
| 159 | - $inputJSON = file_get_contents('php://input'); | |
| 160 | - $requestsParams = json_decode($inputJSON); | |
| 161 | 183 | |
| 162 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 163 | - $inputJSON = file_get_contents('php://input'); | |
| 164 | - $requestsParams = json_decode($inputJSON); | |
| 184 | + GlobalHelper::requirePostMethod(); | |
| 165 | 185 | |
| 166 | - if (empty($requestsParams->apiKey)) { | |
| 167 | - wp_send_json_error( | |
| 168 | - __( | |
| 169 | - 'Requested parameter is empty', | |
| 170 | - 'bit-form' | |
| 171 | - ), | |
| 172 | - 400 | |
| 173 | - ); | |
| 174 | - } | |
| 175 | - $apiKey = $requestsParams->apiKey; | |
| 176 | - $apiEndpoint = 'https://api.hubapi.com/crm/v3/objects/contacts'; | |
| 177 | - $authorizationHeader['Accept'] = 'application/json'; | |
| 178 | - $authorizationHeader['authorization'] = "Bearer {$apiKey}"; | |
| 186 | + try { | |
| 187 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 188 | + } catch (\InvalidArgumentException $e) { | |
| 189 | + wp_send_json_error($e->getMessage(), 400); | |
| 190 | + } | |
| 179 | 191 | |
| 180 | - $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); | |
| 181 | - if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) { | |
| 182 | - wp_send_json_error( | |
| 183 | - empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, | |
| 184 | - 400 | |
| 185 | - ); | |
| 186 | - } | |
| 187 | - $response = []; | |
| 188 | - $contacts = $apiResponse->results; | |
| 192 | + if (empty($requestsParams->apiKey)) { | |
| 193 | + wp_send_json_error( | |
| 194 | + __( | |
| 195 | + 'Requested parameter is empty', | |
| 196 | + 'bit-form' | |
| 197 | + ), | |
| 198 | + 400 | |
| 199 | + ); | |
| 200 | + } | |
| 201 | + $apiKey = $requestsParams->apiKey; | |
| 202 | + $apiEndpoint = 'https://api.hubapi.com/crm/v3/objects/contacts'; | |
| 203 | + $authorizationHeader['Accept'] = 'application/json'; | |
| 204 | + $authorizationHeader['authorization'] = "Bearer {$apiKey}"; | |
| 189 | 205 | |
| 190 | - foreach ($contacts as $contact) { | |
| 191 | - $name = $contact->properties->firstname; | |
| 192 | - $response[] = (object) [ | |
| 193 | - 'contactId' => $contact->id, | |
| 194 | - 'contactName' => $name, | |
| 195 | - ]; | |
| 196 | - } | |
| 197 | - wp_send_json_success($response, 200); | |
| 206 | + $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); | |
| 207 | + if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) { | |
| 208 | + wp_send_json_error( | |
| 209 | + empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, | |
| 210 | + 400 | |
| 211 | + ); | |
| 198 | 212 | } |
| 213 | + $response = []; | |
| 214 | + $contacts = $apiResponse->results; | |
| 215 | + | |
| 216 | + foreach ($contacts as $contact) { | |
| 217 | + $name = $contact->properties->firstname; | |
| 218 | + $response[] = (object) [ | |
| 219 | + 'contactId' => $contact->id, | |
| 220 | + 'contactName' => $name, | |
| 221 | + ]; | |
| 222 | + } | |
| 223 | + wp_send_json_success($response, 200); | |
| 199 | 224 | } |
| 200 | 225 | } |
| 201 | 226 | |
| 202 | 227 | public static function getAllCompany() |
| 203 | 228 | { |
| 204 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 229 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 205 | 230 | $authorizationHeader = null; |
| 206 | - $inputJSON = file_get_contents('php://input'); | |
| 207 | - $requestsParams = json_decode($inputJSON); | |
| 231 | + | |
| 232 | + GlobalHelper::requirePostMethod(); | |
| 233 | + | |
| 234 | + try { | |
| 235 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 236 | + } catch (\InvalidArgumentException $e) { | |
| 237 | + wp_send_json_error($e->getMessage(), 400); | |
| 238 | + } | |
| 208 | 239 | if (empty($requestsParams->apiKey)) { |
| 209 | 240 | wp_send_json_error( |
| 210 | 241 | __( |
| 211 | 242 | 'Requested parameter is empty', |
| @@ -262,14 +293,15 @@ | ||
| 262 | 293 | return $error; |
| 263 | 294 | } |
| 264 | 295 | |
| 265 | 296 | // $actions = $integrationDetails->actions; |
| 266 | - $recordApiHelper = new HubspotRecordApiHelper($logID, $apiKey); | |
| 297 | + $recordApiHelper = new HubspotRecordApiHelper($logID, $apiKey, $entryID); | |
| 267 | 298 | $hubspotResponse = $recordApiHelper->executeRecordApi( |
| 268 | 299 | $integId, |
| 269 | 300 | $integrationDetails, |
| 270 | 301 | $fieldValues, |
| 271 | - $fieldMap | |
| 302 | + $fieldMap, | |
| 303 | + $this->formId | |
| 272 | 304 | ); |
| 273 | 305 | if (is_wp_error($hubspotResponse)) { |
| 274 | 306 | return $hubspotResponse; |
| 275 | 307 | } |