← All changes
|
includes/Core/Integration/ZohoSheet/ZohoSheetHandler.php
+91
-33
2.0
→
V3.3.2
View file →
| @@ -6,21 +6,28 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoSheet; |
| 9 | 9 | |
| 10 | +if (!defined('ABSPATH')) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 10 | 14 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 | 15 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 12 | 16 | use BitCode\BitForm\Core\Util\IpTool; |
| 17 | +use BitCode\BitForm\GlobalHelper; | |
| 13 | 18 | use WP_Error; |
| 14 | 19 | |
| 15 | 20 | /** |
| 16 | 21 | * Provide functionality for ZohoCrm integration |
| 17 | 22 | */ |
| 18 | -class ZohoSheetHandler { | |
| 23 | +class ZohoSheetHandler | |
| 24 | +{ | |
| 19 | 25 | private $_formID; |
| 20 | 26 | private $_integrationID; |
| 21 | 27 | |
| 22 | - public function __construct($integrationID, $fromID) { | |
| 28 | + public function __construct($integrationID, $fromID) | |
| 29 | + { | |
| 23 | 30 | $this->_formID = $fromID; |
| 24 | 31 | $this->_integrationID = $integrationID; |
| 25 | 32 | } |
| 26 | 33 | |
| @@ -28,9 +35,10 @@ | ||
| 28 | 35 | * Helps to register ajax function's with wp |
| 29 | 36 | * |
| 30 | 37 | * @return null |
| 31 | 38 | */ |
| 32 | - public static function registerAjax() { | |
| 39 | + public static function registerAjax() | |
| 40 | + { | |
| 33 | 41 | add_action('wp_ajax_bitforms_zsheet_generate_token', [__CLASS__, 'generateTokens']); |
| 34 | 42 | add_action('wp_ajax_bitforms_zsheet_refresh_workbooks', [__CLASS__, 'refreshWorkbooksAjaxHelper']); |
| 35 | 43 | add_action('wp_ajax_bitforms_zsheet_refresh_worksheets', [__CLASS__, 'refreshWorksheetsAjaxHelper']); |
| 36 | 44 | add_action('wp_ajax_bitforms_zsheet_refresh_worksheet_headers', [__CLASS__, 'refreshWorksheetHeadersAjaxHelper']); |
| @@ -40,12 +48,19 @@ | ||
| 40 | 48 | * Process ajax request for generate_token |
| 41 | 49 | * |
| 42 | 50 | * @return JSON zoho crm api response and status |
| 43 | 51 | */ |
| 44 | - public static function generateTokens() { | |
| 45 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 46 | - $inputJSON = file_get_contents('php://input'); | |
| 47 | - $requestsParams = json_decode($inputJSON); | |
| 52 | + public static function generateTokens() | |
| 53 | + { | |
| 54 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 55 | + GlobalHelper::requirePostMethod(); | |
| 56 | + | |
| 57 | + try { | |
| 58 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 59 | + } catch (\InvalidArgumentException $e) { | |
| 60 | + wp_send_json_error($e->getMessage(), 400); | |
| 61 | + } | |
| 62 | + | |
| 48 | 63 | if ( |
| 49 | 64 | empty($requestsParams->{'accounts-server'}) |
| 50 | 65 | || empty($requestsParams->dataCenter) |
| 51 | 66 | || empty($requestsParams->clientId) |
| @@ -95,13 +110,21 @@ | ||
| 95 | 110 | * Process ajax request for refresh crm modules |
| 96 | 111 | * |
| 97 | 112 | * @return JSON crm module data |
| 98 | 113 | */ |
| 99 | - public static function refreshWorkbooksAjaxHelper() { | |
| 100 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 114 | + public static function refreshWorkbooksAjaxHelper() | |
| 115 | + { | |
| 116 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 101 | 117 | $authorizationHeader = null; |
| 102 | - $inputJSON = file_get_contents('php://input'); | |
| 103 | - $queryParams = json_decode($inputJSON); | |
| 118 | + | |
| 119 | + GlobalHelper::requirePostMethod(); | |
| 120 | + | |
| 121 | + try { | |
| 122 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 123 | + } catch (\InvalidArgumentException $e) { | |
| 124 | + wp_send_json_error($e->getMessage(), 400); | |
| 125 | + } | |
| 126 | + | |
| 104 | 127 | if ( |
| 105 | 128 | empty($queryParams->tokenDetails) |
| 106 | 129 | || empty($queryParams->dataCenter) |
| 107 | 130 | || empty($queryParams->clientId) |
| @@ -161,13 +184,20 @@ | ||
| 161 | 184 | * Process ajax request for refesh crm layouts |
| 162 | 185 | * |
| 163 | 186 | * @return JSON crm layout data |
| 164 | 187 | */ |
| 165 | - public static function refreshWorksheetsAjaxHelper() { | |
| 166 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 188 | + public static function refreshWorksheetsAjaxHelper() | |
| 189 | + { | |
| 190 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 167 | 191 | $authorizationHeader = null; |
| 168 | - $inputJSON = file_get_contents('php://input'); | |
| 169 | - $queryParams = json_decode($inputJSON); | |
| 192 | + | |
| 193 | + GlobalHelper::requirePostMethod(); | |
| 194 | + | |
| 195 | + try { | |
| 196 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 197 | + } catch (\InvalidArgumentException $e) { | |
| 198 | + wp_send_json_error($e->getMessage(), 400); | |
| 199 | + } | |
| 170 | 200 | if ( |
| 171 | 201 | empty($queryParams->workbook) |
| 172 | 202 | || empty($queryParams->tokenDetails) |
| 173 | 203 | || empty($queryParams->dataCenter) |
| @@ -226,13 +256,21 @@ | ||
| 226 | 256 | * Process ajax request for refesh crm layouts |
| 227 | 257 | * |
| 228 | 258 | * @return JSON crm layout data |
| 229 | 259 | */ |
| 230 | - public static function refreshWorksheetHeadersAjaxHelper() { | |
| 231 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 260 | + public static function refreshWorksheetHeadersAjaxHelper() | |
| 261 | + { | |
| 262 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 232 | 263 | $authorizationHeader = null; |
| 233 | - $inputJSON = file_get_contents('php://input'); | |
| 234 | - $queryParams = json_decode($inputJSON); | |
| 264 | + | |
| 265 | + GlobalHelper::requirePostMethod(); | |
| 266 | + | |
| 267 | + try { | |
| 268 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 269 | + } catch (\InvalidArgumentException $e) { | |
| 270 | + wp_send_json_error($e->getMessage(), 400); | |
| 271 | + } | |
| 272 | + | |
| 235 | 273 | if ( |
| 236 | 274 | empty($queryParams->workbook) |
| 237 | 275 | || empty($queryParams->worksheet) |
| 238 | 276 | || empty($queryParams->tokenDetails) |
| @@ -257,17 +295,34 @@ | ||
| 257 | 295 | if ((int) $queryParams->headerRow < 1) { |
| 258 | 296 | $queryParams->headerRow = 1; |
| 259 | 297 | } |
| 260 | 298 | |
| 261 | - $worksheetHeadersMetaApiEndpoint = "https://sheet.zoho.{$queryParams->dataCenter}/api/v2/{$queryParams->workbook}?method=worksheet.records.fetch&worksheet_name={$queryParams->worksheet}&count=1&header_row={$queryParams->headerRow}"; | |
| 299 | + $baseUrl = "https://sheet.zoho.{$queryParams->dataCenter}/api/v2/{$queryParams->workbook}"; | |
| 262 | 300 | |
| 301 | + $params = http_build_query([ | |
| 302 | + 'method' => 'range.content.get', | |
| 303 | + 'worksheet_name' => $queryParams->worksheet, | |
| 304 | + 'start_row' => $queryParams->headerRow, | |
| 305 | + 'end_row' => $queryParams->headerRow, | |
| 306 | + 'start_column' => 1, | |
| 307 | + 'end_column' => 100 | |
| 308 | + ]); | |
| 309 | + | |
| 310 | + $worksheetHeadersMetaApiEndpoint = "{$baseUrl}?{$params}"; | |
| 311 | + | |
| 263 | 312 | $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 264 | 313 | $worksheetHeadersMetaResponse = HttpHelper::get($worksheetHeadersMetaApiEndpoint, null, $authorizationHeader); |
| 265 | 314 | |
| 266 | - // wp_send_json_success($worksheetHeadersMetaResponse, 200); | |
| 267 | - | |
| 268 | 315 | if (!is_wp_error($worksheetHeadersMetaResponse)) { |
| 269 | - $allHeaders = array_diff(array_keys((array) $worksheetHeadersMetaResponse->records[0]), ['row_index']); | |
| 316 | + $rowDetails = $worksheetHeadersMetaResponse->range_details[0]->row_details ?? []; | |
| 317 | + $allHeaders = is_array($rowDetails) | |
| 318 | + ? array_map( | |
| 319 | + function ($rowDetail) { | |
| 320 | + return isset($rowDetail->content) ? (string) $rowDetail->content : null; | |
| 321 | + }, | |
| 322 | + $rowDetails | |
| 323 | + ) | |
| 324 | + : []; | |
| 270 | 325 | |
| 271 | 326 | usort($allHeaders, 'strnatcasecmp'); |
| 272 | 327 | $response['worksheet_headers'] = $allHeaders; |
| 273 | 328 | } else { |
| @@ -294,12 +349,13 @@ | ||
| 294 | 349 | |
| 295 | 350 | /** |
| 296 | 351 | * Helps to refresh zoho crm access_token |
| 297 | 352 | * |
| 298 | - * @param Array $apiData Contains required data for refresh access token | |
| 299 | - * @return JSON $tokenDetails API token details | |
| 353 | + * @param object $apiData Contains required data for refresh access token | |
| 354 | + * @return string | boolean $tokenDetails API token details | |
| 300 | 355 | */ |
| 301 | - protected static function _refreshAccessToken($apiData) { | |
| 356 | + protected static function _refreshAccessToken($apiData) | |
| 357 | + { | |
| 302 | 358 | if ( |
| 303 | 359 | empty($apiData->dataCenter) |
| 304 | 360 | || empty($apiData->clientId) |
| 305 | 361 | || empty($apiData->clientSecret) |
| @@ -329,15 +385,16 @@ | ||
| 329 | 385 | |
| 330 | 386 | /** |
| 331 | 387 | * Save updated access_token to avoid unnecessary token generation |
| 332 | 388 | * |
| 333 | - * @param Integer $fromID ID of Integration related form | |
| 334 | - * @param Integer $integrationID ID of Zoho crm Integration | |
| 335 | - * @param Obeject $tokenDetails refreshed token info | |
| 389 | + * @param integer $fromID ID of Integration related form | |
| 390 | + * @param integer $integrationID ID of Zoho crm Integration | |
| 391 | + * @param object $tokenDetails refreshed token info | |
| 336 | 392 | * |
| 337 | 393 | * @return null |
| 338 | 394 | */ |
| 339 | - protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) { | |
| 395 | + protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) | |
| 396 | + { | |
| 340 | 397 | if (empty($formID) || empty($integrationID)) { |
| 341 | 398 | return; |
| 342 | 399 | } |
| 343 | 400 | |
| @@ -359,12 +416,13 @@ | ||
| 359 | 416 | if (!empty($others['worksheet_headers'])) { |
| 360 | 417 | $newDetails->default->worksheets->headers->{$others['worksheet']} = $others['worksheet_headers']; |
| 361 | 418 | } |
| 362 | 419 | |
| 363 | - $integrationHandler->updateIntegration($integrationID, $zsheetDetails[0]->integration_name, 'Zoho Sheet', \json_encode($newDetails), 'form'); | |
| 420 | + $integrationHandler->updateIntegration($integrationID, $zsheetDetails[0]->integration_name, 'Zoho Sheet', wp_json_encode($newDetails), 'form'); | |
| 364 | 421 | } |
| 365 | 422 | |
| 366 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 423 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 424 | + { | |
| 367 | 425 | $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 368 | 426 | |
| 369 | 427 | $tokenDetails = $integrationDetails->tokenDetails; |
| 370 | 428 | $workbook = $integrationDetails->workbook; |
| @@ -396,9 +454,9 @@ | ||
| 396 | 454 | } |
| 397 | 455 | } |
| 398 | 456 | |
| 399 | 457 | // $actions = $integrationDetails->actions; |
| 400 | - $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID); | |
| 458 | + $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID, $this->_formID, $entryID); | |
| 401 | 459 | |
| 402 | 460 | $zsheetApiResponse = $recordApiHelper->executeRecordApi( |
| 403 | 461 | $workbook, |
| 404 | 462 | $worksheet, |