← All changes
|
includes/Core/Integration/ZohoSheet/ZohoSheetHandler.php
+65
-17
2.10.0
→
V3.3.2
View file →
| @@ -6,11 +6,16 @@ | ||
| 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 |
| @@ -45,11 +50,17 @@ | ||
| 45 | 50 | * @return JSON zoho crm api response and status |
| 46 | 51 | */ |
| 47 | 52 | public static function generateTokens() |
| 48 | 53 | { |
| 49 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 50 | - $inputJSON = file_get_contents('php://input'); | |
| 51 | - $requestsParams = json_decode($inputJSON); | |
| 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 | + | |
| 52 | 63 | if ( |
| 53 | 64 | empty($requestsParams->{'accounts-server'}) |
| 54 | 65 | || empty($requestsParams->dataCenter) |
| 55 | 66 | || empty($requestsParams->clientId) |
| @@ -101,12 +112,19 @@ | ||
| 101 | 112 | * @return JSON crm module data |
| 102 | 113 | */ |
| 103 | 114 | public static function refreshWorkbooksAjaxHelper() |
| 104 | 115 | { |
| 105 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 116 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 106 | 117 | $authorizationHeader = null; |
| 107 | - $inputJSON = file_get_contents('php://input'); | |
| 108 | - $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 | + | |
| 109 | 127 | if ( |
| 110 | 128 | empty($queryParams->tokenDetails) |
| 111 | 129 | || empty($queryParams->dataCenter) |
| 112 | 130 | || empty($queryParams->clientId) |
| @@ -168,12 +186,18 @@ | ||
| 168 | 186 | * @return JSON crm layout data |
| 169 | 187 | */ |
| 170 | 188 | public static function refreshWorksheetsAjaxHelper() |
| 171 | 189 | { |
| 172 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 190 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 173 | 191 | $authorizationHeader = null; |
| 174 | - $inputJSON = file_get_contents('php://input'); | |
| 175 | - $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 | + } | |
| 176 | 200 | if ( |
| 177 | 201 | empty($queryParams->workbook) |
| 178 | 202 | || empty($queryParams->tokenDetails) |
| 179 | 203 | || empty($queryParams->dataCenter) |
| @@ -234,12 +258,19 @@ | ||
| 234 | 258 | * @return JSON crm layout data |
| 235 | 259 | */ |
| 236 | 260 | public static function refreshWorksheetHeadersAjaxHelper() |
| 237 | 261 | { |
| 238 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 262 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 239 | 263 | $authorizationHeader = null; |
| 240 | - $inputJSON = file_get_contents('php://input'); | |
| 241 | - $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 | + | |
| 242 | 273 | if ( |
| 243 | 274 | empty($queryParams->workbook) |
| 244 | 275 | || empty($queryParams->worksheet) |
| 245 | 276 | || empty($queryParams->tokenDetails) |
| @@ -264,17 +295,34 @@ | ||
| 264 | 295 | if ((int) $queryParams->headerRow < 1) { |
| 265 | 296 | $queryParams->headerRow = 1; |
| 266 | 297 | } |
| 267 | 298 | |
| 268 | - $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}"; | |
| 269 | 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 | + | |
| 270 | 312 | $authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 271 | 313 | $worksheetHeadersMetaResponse = HttpHelper::get($worksheetHeadersMetaApiEndpoint, null, $authorizationHeader); |
| 272 | 314 | |
| 273 | - // wp_send_json_success($worksheetHeadersMetaResponse, 200); | |
| 274 | - | |
| 275 | 315 | if (!is_wp_error($worksheetHeadersMetaResponse)) { |
| 276 | - $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 | + : []; | |
| 277 | 325 | |
| 278 | 326 | usort($allHeaders, 'strnatcasecmp'); |
| 279 | 327 | $response['worksheet_headers'] = $allHeaders; |
| 280 | 328 | } else { |
| @@ -406,9 +454,9 @@ | ||
| 406 | 454 | } |
| 407 | 455 | } |
| 408 | 456 | |
| 409 | 457 | // $actions = $integrationDetails->actions; |
| 410 | - $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID); | |
| 458 | + $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID, $this->_formID, $entryID); | |
| 411 | 459 | |
| 412 | 460 | $zsheetApiResponse = $recordApiHelper->executeRecordApi( |
| 413 | 461 | $workbook, |
| 414 | 462 | $worksheet, |