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