← All changes
|
includes/Core/Integration/Hubspot/HubspotHandler.php
+101
-60
2.0
→
3.3.1
View file →
| @@ -1,36 +1,52 @@ | ||
| 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 | -final class HubspotHandler { | |
| 14 | +final class HubspotHandler | |
| 15 | +{ | |
| 10 | 16 | private $_integrationID; |
| 11 | 17 | public static $apiBaseUri = 'https://apiv3.emailsys.net/v1'; |
| 12 | 18 | protected $_defaultHeader; |
| 13 | 19 | |
| 14 | - public function __construct($integrationID) { | |
| 20 | + private $formId; | |
| 21 | + | |
| 22 | + public function __construct($integrationID, $fromID) | |
| 23 | + { | |
| 15 | 24 | $this->_integrationID = $integrationID; |
| 25 | + $this->formId = $fromID; | |
| 16 | 26 | } |
| 17 | 27 | |
| 18 | - public static function registerAjax() { | |
| 28 | + public static function registerAjax() | |
| 29 | + { | |
| 19 | 30 | add_action('wp_ajax_bitforms_hubspot_authorize', [__CLASS__, 'hubspotAuthorize']); |
| 20 | 31 | add_action('wp_ajax_bitforms_hubspot_pipeline', [__CLASS__, 'getAllPipelines']); |
| 21 | - add_action('wp_ajax_bitforms_hubspot_pipeline_tickets', [__CLASS__, 'getAllPipelinesTickets']); | |
| 22 | 32 | add_action('wp_ajax_bitforms_hubspot_owners', [__CLASS__, 'getAllOwners']); |
| 23 | 33 | add_action('wp_ajax_bitforms_hubspot_contacts', [__CLASS__, 'getAllContacts']); |
| 24 | 34 | add_action('wp_ajax_bitforms_hubspot_company', [__CLASS__, 'getAllCompany']); |
| 25 | 35 | } |
| 26 | 36 | |
| 27 | - public static function hubspotAuthorize() { | |
| 28 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 37 | + public static function hubspotAuthorize() | |
| 38 | + { | |
| 39 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 29 | 40 | $authorizationHeader = null; |
| 30 | - $inputJSON = file_get_contents('php://input'); | |
| 31 | - $requestsParams = json_decode($inputJSON); | |
| 32 | 41 | |
| 42 | + GlobalHelper::requirePostMethod(); | |
| 43 | + try { | |
| 44 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 45 | + } catch (\InvalidArgumentException $e) { | |
| 46 | + wp_send_json_error($e->getMessage(), 400); | |
| 47 | + } | |
| 48 | + | |
| 33 | 49 | if (empty($requestsParams->api_key)) { |
| 34 | 50 | wp_send_json_error( |
| 35 | 51 | __( |
| 36 | 52 | 'Requested parameter is empty', |
| @@ -55,14 +71,21 @@ | ||
| 55 | 71 | wp_send_json_success(true); |
| 56 | 72 | } |
| 57 | 73 | } |
| 58 | 74 | |
| 59 | - public static function getAllPipelines() { | |
| 60 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 75 | + public static function getAllPipelines() | |
| 76 | + { | |
| 77 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 61 | 78 | $authorizationHeader = null; |
| 62 | - $inputJSON = file_get_contents('php://input'); | |
| 63 | - $requestsParams = json_decode($inputJSON); | |
| 64 | 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 | + | |
| 65 | 88 | if (empty($requestsParams->apiKey)) { |
| 66 | 89 | wp_send_json_error( |
| 67 | 90 | __( |
| 68 | 91 | 'Requested parameter is empty', |
| @@ -105,14 +128,21 @@ | ||
| 105 | 128 | wp_send_json_success($response, 200); |
| 106 | 129 | } |
| 107 | 130 | } |
| 108 | 131 | |
| 109 | - public static function getAllOwners() { | |
| 110 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 132 | + public static function getAllOwners() | |
| 133 | + { | |
| 134 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 111 | 135 | $authorizationHeader = null; |
| 112 | - $inputJSON = file_get_contents('php://input'); | |
| 113 | - $requestsParams = json_decode($inputJSON); | |
| 114 | 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 | + | |
| 115 | 145 | if (empty($requestsParams->apiKey)) { |
| 116 | 146 | wp_send_json_error( |
| 117 | 147 | __( |
| 118 | 148 | 'Requested parameter is empty', |
| @@ -145,59 +175,68 @@ | ||
| 145 | 175 | wp_send_json_success($response, 200); |
| 146 | 176 | } |
| 147 | 177 | } |
| 148 | 178 | |
| 149 | - public static function getAllContacts() { | |
| 150 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 179 | + public static function getAllContacts() | |
| 180 | + { | |
| 181 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 151 | 182 | $authorizationHeader = null; |
| 152 | - $inputJSON = file_get_contents('php://input'); | |
| 153 | - $requestsParams = json_decode($inputJSON); | |
| 154 | 183 | |
| 155 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 156 | - $inputJSON = file_get_contents('php://input'); | |
| 157 | - $requestsParams = json_decode($inputJSON); | |
| 184 | + GlobalHelper::requirePostMethod(); | |
| 158 | 185 | |
| 159 | - if (empty($requestsParams->apiKey)) { | |
| 160 | - wp_send_json_error( | |
| 161 | - __( | |
| 162 | - 'Requested parameter is empty', | |
| 163 | - 'bit-form' | |
| 164 | - ), | |
| 165 | - 400 | |
| 166 | - ); | |
| 167 | - } | |
| 168 | - $apiKey = $requestsParams->apiKey; | |
| 169 | - $apiEndpoint = 'https://api.hubapi.com/crm/v3/objects/contacts'; | |
| 170 | - $authorizationHeader['Accept'] = 'application/json'; | |
| 171 | - $authorizationHeader['authorization'] = "Bearer {$apiKey}"; | |
| 186 | + try { | |
| 187 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 188 | + } catch (\InvalidArgumentException $e) { | |
| 189 | + wp_send_json_error($e->getMessage(), 400); | |
| 190 | + } | |
| 172 | 191 | |
| 173 | - $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); | |
| 174 | - if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) { | |
| 175 | - wp_send_json_error( | |
| 176 | - empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, | |
| 177 | - 400 | |
| 178 | - ); | |
| 179 | - } | |
| 180 | - $response = []; | |
| 181 | - $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}"; | |
| 182 | 205 | |
| 183 | - foreach ($contacts as $contact) { | |
| 184 | - $name = $contact->properties->firstname; | |
| 185 | - $response[] = (object) [ | |
| 186 | - 'contactId' => $contact->id, | |
| 187 | - 'contactName' => $name, | |
| 188 | - ]; | |
| 189 | - } | |
| 190 | - 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 | + ); | |
| 191 | 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); | |
| 192 | 224 | } |
| 193 | 225 | } |
| 194 | 226 | |
| 195 | - public static function getAllCompany() { | |
| 196 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 227 | + public static function getAllCompany() | |
| 228 | + { | |
| 229 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 197 | 230 | $authorizationHeader = null; |
| 198 | - $inputJSON = file_get_contents('php://input'); | |
| 199 | - $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 | + } | |
| 200 | 239 | if (empty($requestsParams->apiKey)) { |
| 201 | 240 | wp_send_json_error( |
| 202 | 241 | __( |
| 203 | 242 | 'Requested parameter is empty', |
| @@ -234,9 +273,10 @@ | ||
| 234 | 273 | wp_send_json_success($response, 200); |
| 235 | 274 | } |
| 236 | 275 | } |
| 237 | 276 | |
| 238 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 277 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 278 | + { | |
| 239 | 279 | $integrationDetails = $integrationData->integration_details; |
| 240 | 280 | if (is_string($integrationDetails)) { |
| 241 | 281 | $integrationDetails = json_decode($integrationDetails); |
| 242 | 282 | } |
| @@ -253,14 +293,15 @@ | ||
| 253 | 293 | return $error; |
| 254 | 294 | } |
| 255 | 295 | |
| 256 | 296 | // $actions = $integrationDetails->actions; |
| 257 | - $recordApiHelper = new HubspotRecordApiHelper($logID, $apiKey); | |
| 297 | + $recordApiHelper = new HubspotRecordApiHelper($logID, $apiKey, $entryID); | |
| 258 | 298 | $hubspotResponse = $recordApiHelper->executeRecordApi( |
| 259 | 299 | $integId, |
| 260 | 300 | $integrationDetails, |
| 261 | 301 | $fieldValues, |
| 262 | - $fieldMap | |
| 302 | + $fieldMap, | |
| 303 | + $this->formId | |
| 263 | 304 | ); |
| 264 | 305 | if (is_wp_error($hubspotResponse)) { |
| 265 | 306 | return $hubspotResponse; |
| 266 | 307 | } |