| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* ZohoRecruit Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\ZohoRecruit; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 15 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 |
use BitCode\BitForm\Core\Util\IpTool; |
| 17 |
use BitCode\BitForm\GlobalHelper; |
| 18 |
use WP_Error; |
| 19 |
|
| 20 |
/** |
| 21 |
* Provide functionality for ZohoCrm integration |
| 22 |
*/ |
| 23 |
class ZohoRecruitHandler |
| 24 |
{ |
| 25 |
private $_formID; |
| 26 |
private $_integrationID; |
| 27 |
|
| 28 |
public function __construct($integrationID, $fromID) |
| 29 |
{ |
| 30 |
$this->_formID = $fromID; |
| 31 |
$this->_integrationID = $integrationID; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Helps to register ajax function's with wp |
| 36 |
* |
| 37 |
* @return null |
| 38 |
*/ |
| 39 |
public static function registerAjax() |
| 40 |
{ |
| 41 |
add_action('wp_ajax_bitforms_zrecruit_generate_token', [__CLASS__, 'generateTokens']); |
| 42 |
add_action('wp_ajax_bitforms_zrecruit_refresh_modules', [__CLASS__, 'refreshModulesAjaxHelper']); |
| 43 |
add_action('wp_ajax_bitforms_zrecruit_refresh_notetypes', [__CLASS__, 'refreshNoteTypesAjaxHelper']); |
| 44 |
add_action('wp_ajax_bitforms_zrecruit_refresh_related_lists', [__CLASS__, 'refreshRelatedModulesAjaxHelper']); |
| 45 |
add_action('wp_ajax_bitforms_zrecruit_get_fields', [__CLASS__, 'getFields']); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Process ajax request for generate_token |
| 50 |
* |
| 51 |
* @return string zoho recruit api response and status |
| 52 |
*/ |
| 53 |
public static function generateTokens() |
| 54 |
{ |
| 55 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 56 |
GlobalHelper::requirePostMethod(); |
| 57 |
|
| 58 |
try { |
| 59 |
$requestsParams = GlobalHelper::formatRequestData(); |
| 60 |
} catch (\InvalidArgumentException $e) { |
| 61 |
wp_send_json_error($e->getMessage(), 400); |
| 62 |
} |
| 63 |
|
| 64 |
if ( |
| 65 |
empty($requestsParams->{'accounts-server'}) |
| 66 |
|| empty($requestsParams->dataCenter) |
| 67 |
|| empty($requestsParams->clientId) |
| 68 |
|| empty($requestsParams->clientSecret) |
| 69 |
|| empty($requestsParams->redirectURI) |
| 70 |
|| empty($requestsParams->code) |
| 71 |
) { |
| 72 |
wp_send_json_error( |
| 73 |
__( |
| 74 |
'Requested parameter is empty', |
| 75 |
'bit-form' |
| 76 |
), |
| 77 |
400 |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
$apiEndpoint = \urldecode($requestsParams->{'accounts-server'}) . '/oauth/v2/token'; |
| 82 |
$requestParams = [ |
| 83 |
'grant_type' => 'authorization_code', |
| 84 |
'client_id' => $requestsParams->clientId, |
| 85 |
'client_secret' => $requestsParams->clientSecret, |
| 86 |
'redirect_uri' => \urldecode($requestsParams->redirectURI), |
| 87 |
'code' => $requestsParams->code |
| 88 |
]; |
| 89 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 90 |
|
| 91 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 92 |
wp_send_json_error( |
| 93 |
empty($apiResponse->error) ? 'Unknown' : $apiResponse->error, |
| 94 |
400 |
| 95 |
); |
| 96 |
} |
| 97 |
$apiResponse->generates_on = \time(); |
| 98 |
wp_send_json_success($apiResponse, 200); |
| 99 |
} else { |
| 100 |
wp_send_json_error( |
| 101 |
__( |
| 102 |
'Token expired', |
| 103 |
'bit-form' |
| 104 |
), |
| 105 |
401 |
| 106 |
); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Process ajax request for refresh recruit modules |
| 112 |
* |
| 113 |
* @return JSON recruit module data |
| 114 |
*/ |
| 115 |
public static function refreshModulesAjaxHelper() |
| 116 |
{ |
| 117 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 118 |
$authorizationHeader = null; |
| 119 |
|
| 120 |
GlobalHelper::requirePostMethod(); |
| 121 |
|
| 122 |
try { |
| 123 |
$queryParams = GlobalHelper::formatRequestData(); |
| 124 |
} catch (\InvalidArgumentException $e) { |
| 125 |
wp_send_json_error($e->getMessage(), 400); |
| 126 |
} |
| 127 |
if ( |
| 128 |
empty($queryParams->tokenDetails) |
| 129 |
|| empty($queryParams->dataCenter) |
| 130 |
|| empty($queryParams->clientId) |
| 131 |
|| empty($queryParams->clientSecret) |
| 132 |
) { |
| 133 |
wp_send_json_error( |
| 134 |
__( |
| 135 |
'Requested parameter is empty', |
| 136 |
'bit-form' |
| 137 |
), |
| 138 |
400 |
| 139 |
); |
| 140 |
} |
| 141 |
$response = []; |
| 142 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 143 |
$response['tokenDetails'] = ZohoRecruitHandler::_refreshAccessToken($queryParams); |
| 144 |
} |
| 145 |
$zohosIntegratedModules = [ |
| 146 |
'zohosign__ZohoSign_Document_Events', |
| 147 |
'zohoshowtime__ShowTime_Sessions', |
| 148 |
'zohoshowtime__Zoho_ShowTime', |
| 149 |
'zohosign__ZohoSign_Documents', |
| 150 |
'zohosign__ZohoSign_Recipients' |
| 151 |
]; |
| 152 |
$modulesMetaApiEndpoint = "https://recruit.zoho.{$queryParams->dataCenter}/recruit/private/json/Info/getModules?authtoken={$queryParams->tokenDetails->access_token}&scope=ZohoRecruit.modules.all&version=2"; |
| 153 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 154 |
$modulesMetaResponse = HttpHelper::get($modulesMetaApiEndpoint, null, $authorizationHeader); |
| 155 |
if (!is_wp_error($modulesMetaResponse) && (empty($modulesMetaResponse->status) || (!empty($modulesMetaResponse->status) && 'error' !== $modulesMetaResponse->status))) { |
| 156 |
$retriveModuleData = $modulesMetaResponse->response->result->row; |
| 157 |
$allModules = [ |
| 158 |
'Tasks' => (object) [ |
| 159 |
'aMod' => 'Tasks', |
| 160 |
'pl' => 'Tasks' |
| 161 |
], |
| 162 |
'Events' => (object) [ |
| 163 |
'aMod' => 'Events', |
| 164 |
'pl' => 'Events' |
| 165 |
], |
| 166 |
'Calls' => (object) [ |
| 167 |
'aMod' => 'Calls', |
| 168 |
'pl' => 'Calls' |
| 169 |
], |
| 170 |
]; |
| 171 |
foreach ($retriveModuleData as $value) { |
| 172 |
if (preg_match('/CustomModule/', $value->aMod) || preg_match('/Candidates/', $value->aMod)) { |
| 173 |
$allModules[$value->aMod] = (object) [ |
| 174 |
'aMod' => $value->aMod, |
| 175 |
'pl' => $value->pl |
| 176 |
]; |
| 177 |
} |
| 178 |
} |
| 179 |
uksort($allModules, 'strnatcasecmp'); |
| 180 |
$response['modules'] = $allModules; |
| 181 |
} else { |
| 182 |
wp_send_json_error( |
| 183 |
empty($modulesMetaResponse->error) ? 'Unknown' : $modulesMetaResponse->error, |
| 184 |
400 |
| 185 |
); |
| 186 |
} |
| 187 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 188 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['modules']); |
| 189 |
} |
| 190 |
wp_send_json_success($response, 200); |
| 191 |
} else { |
| 192 |
wp_send_json_error( |
| 193 |
__( |
| 194 |
'Token expired', |
| 195 |
'bit-form' |
| 196 |
), |
| 197 |
401 |
| 198 |
); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
public static function refreshNoteTypesAjaxHelper() |
| 203 |
{ |
| 204 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 205 |
$authorizationHeader = null; |
| 206 |
|
| 207 |
GlobalHelper::requirePostMethod(); |
| 208 |
try { |
| 209 |
$queryParams = GlobalHelper::formatRequestData(); |
| 210 |
} catch (\InvalidArgumentException $e) { |
| 211 |
wp_send_json_error($e->getMessage(), 400); |
| 212 |
} |
| 213 |
|
| 214 |
if ( |
| 215 |
empty($queryParams->tokenDetails) |
| 216 |
|| empty($queryParams->dataCenter) |
| 217 |
|| empty($queryParams->clientId) |
| 218 |
|| empty($queryParams->clientSecret) |
| 219 |
) { |
| 220 |
wp_send_json_error( |
| 221 |
__( |
| 222 |
'Requested parameter is empty', |
| 223 |
'bit-form' |
| 224 |
), |
| 225 |
400 |
| 226 |
); |
| 227 |
} |
| 228 |
$response = []; |
| 229 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 230 |
$response['tokenDetails'] = ZohoRecruitHandler::_refreshAccessToken($queryParams); |
| 231 |
} |
| 232 |
|
| 233 |
$notesMetaApiEndpoint = "https: //recruit.zoho.com/recruit/private/json/Notes/getNoteTypes?authtoken={$queryParams->tokenDetails->access_token}&scope=ZohoRecruit.modules.call.all&version=2"; |
| 234 |
|
| 235 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 236 |
$notesMetaResponse = HttpHelper::get($notesMetaApiEndpoint, null, $authorizationHeader); |
| 237 |
|
| 238 |
// wp_send_json_success($notesMetaResponse, 200); |
| 239 |
|
| 240 |
if (!is_wp_error($notesMetaResponse) && (empty($notesMetaResponse->status) || (!empty($notesMetaResponse->status) && 'error' !== $notesMetaResponse->status))) { |
| 241 |
$retriveModuleData = $notesMetaResponse->response->result->Notes->row; |
| 242 |
$allNoteTypes = []; |
| 243 |
foreach ($retriveModuleData as $value) { |
| 244 |
$allNoteTypes[$value->FL[0]->dv] = (object) [ |
| 245 |
'noteTypeId' => $value->FL[1]->content, |
| 246 |
'noteTypeName' => $value->FL[0]->dv |
| 247 |
]; |
| 248 |
} |
| 249 |
uksort($allNoteTypes, 'strnatcasecmp'); |
| 250 |
$response['noteTypes'] = $allNoteTypes; |
| 251 |
} else { |
| 252 |
wp_send_json_error( |
| 253 |
empty($notesMetaResponse->error) ? 'Unknown' : $notesMetaResponse->error, |
| 254 |
400 |
| 255 |
); |
| 256 |
} |
| 257 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 258 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['notetypes']); |
| 259 |
} |
| 260 |
wp_send_json_success($response, 200); |
| 261 |
} else { |
| 262 |
wp_send_json_error( |
| 263 |
__( |
| 264 |
'Token expired', |
| 265 |
'bit-form' |
| 266 |
), |
| 267 |
401 |
| 268 |
); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Process ajax request for refresh recruit modules |
| 274 |
* |
| 275 |
* @return JSON recruit module data |
| 276 |
*/ |
| 277 |
public static function refreshRelatedModulesAjaxHelper() |
| 278 |
{ |
| 279 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 280 |
GlobalHelper::requirePostMethod(); |
| 281 |
|
| 282 |
try { |
| 283 |
$queryParams = GlobalHelper::formatRequestData(); |
| 284 |
} catch (\InvalidArgumentException $e) { |
| 285 |
wp_send_json_error($e->getMessage(), 400); |
| 286 |
} |
| 287 |
|
| 288 |
if ( |
| 289 |
empty($queryParams->tokenDetails) |
| 290 |
|| empty($queryParams->dataCenter) |
| 291 |
|| empty($queryParams->clientId) |
| 292 |
|| empty($queryParams->clientSecret) |
| 293 |
|| empty($queryParams->module) |
| 294 |
) { |
| 295 |
wp_send_json_error( |
| 296 |
__( |
| 297 |
'Requested parameter is empty', |
| 298 |
'bit-form' |
| 299 |
), |
| 300 |
400 |
| 301 |
); |
| 302 |
} |
| 303 |
$response = []; |
| 304 |
$relatedModules = []; |
| 305 |
$allModules = [ |
| 306 |
'Tasks' => (object) [ |
| 307 |
'aMod' => 'Tasks', |
| 308 |
'pl' => 'Tasks' |
| 309 |
], |
| 310 |
'Events' => (object) [ |
| 311 |
'aMod' => 'Events', |
| 312 |
'pl' => 'Events' |
| 313 |
], |
| 314 |
'Calls' => (object) [ |
| 315 |
'aMod' => 'Calls', |
| 316 |
'pl' => 'Calls' |
| 317 |
], |
| 318 |
// 'Notes' => (object) array( |
| 319 |
// 'aMod' => 'Notes', |
| 320 |
// 'pl' => 'Notes' |
| 321 |
// ), |
| 322 |
]; |
| 323 |
foreach ($allModules as $module) { |
| 324 |
if ($module->aMod !== $queryParams->module) { |
| 325 |
$relatedModules[$module->aMod] = (object) [ |
| 326 |
'aMod' => $module->aMod, |
| 327 |
'pl' => $module->pl |
| 328 |
]; |
| 329 |
} |
| 330 |
} |
| 331 |
uksort($relatedModules, 'strnatcasecmp'); |
| 332 |
$response['related_modules'] = $relatedModules; |
| 333 |
|
| 334 |
if (!empty($response['tokenDetails']) && !empty($queryParams->id)) { |
| 335 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response['related_modules']); |
| 336 |
} |
| 337 |
wp_send_json_success($response, 200); |
| 338 |
} else { |
| 339 |
wp_send_json_error( |
| 340 |
__( |
| 341 |
'Token expired', |
| 342 |
'bit-form' |
| 343 |
), |
| 344 |
401 |
| 345 |
); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Process ajax request for refesh recruit layouts |
| 351 |
* |
| 352 |
* @return JSON recruit layout data |
| 353 |
*/ |
| 354 |
public static function getFields() |
| 355 |
{ |
| 356 |
$authorizationHeader = null; |
| 357 |
$requiredParams = null; |
| 358 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 359 |
GlobalHelper::requirePostMethod(); |
| 360 |
|
| 361 |
try { |
| 362 |
$queryParams = GlobalHelper::formatRequestData(); |
| 363 |
} catch (\InvalidArgumentException $e) { |
| 364 |
wp_send_json_error($e->getMessage(), 400); |
| 365 |
} |
| 366 |
|
| 367 |
if ( |
| 368 |
empty($queryParams->module) |
| 369 |
|| empty($queryParams->tokenDetails) |
| 370 |
|| empty($queryParams->dataCenter) |
| 371 |
|| empty($queryParams->clientId) |
| 372 |
|| empty($queryParams->clientSecret) |
| 373 |
) { |
| 374 |
wp_send_json_error( |
| 375 |
__( |
| 376 |
'Requested parameter is empty', |
| 377 |
'bit-form' |
| 378 |
), |
| 379 |
400 |
| 380 |
); |
| 381 |
} |
| 382 |
$response = []; |
| 383 |
if ((intval($queryParams->tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 384 |
$response['tokenDetails'] = ZohoRecruitHandler::_refreshAccessToken($queryParams); |
| 385 |
} |
| 386 |
$fieldsMetaApiEndpoint = "https://recruit.zoho.{$queryParams->dataCenter}/recruit/private/json/{$queryParams->module}/getFields?authtoken={$queryParams->tokenDetails->access_token}&scope=recruitapi&version=2"; |
| 387 |
$authorizationHeader['Authorization'] = "Zoho-oauthtoken {$queryParams->tokenDetails->access_token}"; |
| 388 |
$requiredParams['module'] = $queryParams->module; |
| 389 |
$fieldsMetaResponse = HttpHelper::get($fieldsMetaApiEndpoint, $requiredParams, $authorizationHeader); |
| 390 |
|
| 391 |
if (!is_wp_error($fieldsMetaResponse) && (empty($fieldsMetaResponse->status) || (!empty($fieldsMetaResponse->status) && 'error' !== $fieldsMetaResponse->status))) { |
| 392 |
$retriveFieldsData = $fieldsMetaResponse->{$queryParams->module}->section; |
| 393 |
$fields = []; |
| 394 |
$fileUploadFields = []; |
| 395 |
$requiredFields = []; |
| 396 |
$requiredFileUploadFiles = []; |
| 397 |
if ('Candidates' === $queryParams->module) { |
| 398 |
$fileUploadFields['Candidate Photo'] = (object) [ |
| 399 |
'display_label' => 'Candidate Profile Photo', |
| 400 |
'length' => 1000, |
| 401 |
'data_type' => 'UploadText', |
| 402 |
'required' => 'false' |
| 403 |
]; |
| 404 |
} |
| 405 |
if (count($retriveFieldsData) > 1) { |
| 406 |
foreach ($retriveFieldsData as $fieldValue) { |
| 407 |
foreach ($fieldValue->FL as $sectionValue) { |
| 408 |
if (null === $sectionValue->dv) { |
| 409 |
continue; |
| 410 |
} |
| 411 |
if ('UploadText' === $sectionValue->type) { |
| 412 |
$fileUploadFields[$sectionValue->dv] = (object) [ |
| 413 |
'display_label' => $sectionValue->dv, |
| 414 |
'length' => $sectionValue->maxlength, |
| 415 |
'data_type' => $sectionValue->type, |
| 416 |
'required' => $sectionValue->req |
| 417 |
]; |
| 418 |
if ('true' === $sectionValue->req) { |
| 419 |
$requiredFileUploadFiles[] = $sectionValue->dv; |
| 420 |
} |
| 421 |
} else { |
| 422 |
$fields[$sectionValue->dv] = (object) [ |
| 423 |
'display_label' => $sectionValue->dv, |
| 424 |
'length' => $sectionValue->maxlength, |
| 425 |
'data_type' => $sectionValue->type, |
| 426 |
'required' => $sectionValue->req |
| 427 |
]; |
| 428 |
if ('true' === $sectionValue->req) { |
| 429 |
$requiredFields[] = $sectionValue->dv; |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} else { |
| 435 |
foreach ($retriveFieldsData->FL as $sectionValue) { |
| 436 |
if (null === $sectionValue->dv) { |
| 437 |
continue; |
| 438 |
} |
| 439 |
if ('Candidates' === $sectionValue->aMod) { |
| 440 |
$fileUploadFields[$sectionValue->dv] = (object) [ |
| 441 |
'display_label' => 'Candidate Profile Photo', |
| 442 |
'length' => 1000, |
| 443 |
'data_type' => 'UploadText', |
| 444 |
'required' => 'false' |
| 445 |
]; |
| 446 |
} |
| 447 |
if ('UploadText' === $sectionValue->type) { |
| 448 |
$fileUploadFields[$sectionValue->dv] = (object) [ |
| 449 |
'display_label' => $sectionValue->dv, |
| 450 |
'length' => $sectionValue->maxlength, |
| 451 |
'data_type' => $sectionValue->type, |
| 452 |
'required' => $sectionValue->req |
| 453 |
]; |
| 454 |
if ('true' === $sectionValue->req) { |
| 455 |
$requiredFileUploadFiles[] = $sectionValue->dv; |
| 456 |
} |
| 457 |
} else { |
| 458 |
$fields[$sectionValue->dv] = (object) [ |
| 459 |
'display_label' => $sectionValue->dv, |
| 460 |
'length' => $sectionValue->maxlength, |
| 461 |
'data_type' => $sectionValue->type, |
| 462 |
'required' => $sectionValue->req |
| 463 |
]; |
| 464 |
if ('true' === $sectionValue->req) { |
| 465 |
$requiredFields[] = $sectionValue->dv; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
if ('Candidates' === $queryParams->module && isset($fields['Zip/Postal Code'])) { |
| 472 |
$fields['Zip Code'] = $fields['Zip/Postal Code']; |
| 473 |
unset($fields['Zip/Postal Code']); |
| 474 |
} |
| 475 |
|
| 476 |
uksort($fields, 'strnatcasecmp'); |
| 477 |
uksort($fileUploadFields, 'strnatcasecmp'); |
| 478 |
usort($requiredFields, 'strnatcasecmp'); |
| 479 |
usort($requiredFileUploadFiles, 'strnatcasecmp'); |
| 480 |
|
| 481 |
$fieldDetails = (object) [ |
| 482 |
'fields' => $fields, |
| 483 |
'fileUploadFields' => $fileUploadFields, |
| 484 |
'required' => $requiredFields, |
| 485 |
'requiredFileUploadFields' => $requiredFileUploadFiles |
| 486 |
]; |
| 487 |
$response['fieldDetails'] = $fieldDetails; |
| 488 |
} else { |
| 489 |
wp_send_json_error( |
| 490 |
'error' === $fieldsMetaResponse->status ? $fieldsMetaResponse->message : 'Unknown', |
| 491 |
400 |
| 492 |
); |
| 493 |
} |
| 494 |
if (!empty($response['tokenDetails']) && $response['tokenDetails'] && !empty($queryParams->id)) { |
| 495 |
$response['queryModule'] = $queryParams->module; |
| 496 |
ZohoRecruitHandler::_saveRefreshedToken($queryParams->formID, $queryParams->id, $response['tokenDetails'], $response); |
| 497 |
} |
| 498 |
wp_send_json_success($response, 200); |
| 499 |
} else { |
| 500 |
wp_send_json_error( |
| 501 |
__( |
| 502 |
'Token expired', |
| 503 |
'bit-form' |
| 504 |
), |
| 505 |
401 |
| 506 |
); |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Helps to refresh zoho recruit access_token |
| 512 |
* |
| 513 |
* @param object $apiData Contains required data for refresh access token |
| 514 |
* @return string | boolean $tokenDetails API token details |
| 515 |
*/ |
| 516 |
protected static function _refreshAccessToken($apiData) |
| 517 |
{ |
| 518 |
if ( |
| 519 |
empty($apiData->dataCenter) |
| 520 |
|| empty($apiData->clientId) |
| 521 |
|| empty($apiData->clientSecret) |
| 522 |
|| empty($apiData->tokenDetails) |
| 523 |
) { |
| 524 |
return false; |
| 525 |
} |
| 526 |
$tokenDetails = $apiData->tokenDetails; |
| 527 |
|
| 528 |
$dataCenter = $apiData->dataCenter; |
| 529 |
$apiEndpoint = "https://accounts.zoho.{$dataCenter}/oauth/v2/token"; |
| 530 |
$requestParams = [ |
| 531 |
'grant_type' => 'refresh_token', |
| 532 |
'client_id' => $apiData->clientId, |
| 533 |
'client_secret' => $apiData->clientSecret, |
| 534 |
'refresh_token' => $tokenDetails->refresh_token, |
| 535 |
]; |
| 536 |
|
| 537 |
$apiResponse = HttpHelper::post($apiEndpoint, $requestParams); |
| 538 |
if (is_wp_error($apiResponse) || !empty($apiResponse->error)) { |
| 539 |
return false; |
| 540 |
} |
| 541 |
$tokenDetails->generates_on = \time(); |
| 542 |
$tokenDetails->access_token = $apiResponse->access_token; |
| 543 |
return $tokenDetails; |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Save updated access_token to avoid unnecessary token generation |
| 548 |
* |
| 549 |
* @param Integer $fromID ID of Integration related form |
| 550 |
* @param Integer $integrationID ID of Zoho recruit Integration |
| 551 |
* @param Obeject $tokenDetails refreshed token info |
| 552 |
* |
| 553 |
* @return null |
| 554 |
*/ |
| 555 |
protected static function _saveRefreshedToken($formID, $integrationID, $tokenDetails, $others = null) |
| 556 |
{ |
| 557 |
if (empty($formID) || empty($integrationID)) { |
| 558 |
return; |
| 559 |
} |
| 560 |
|
| 561 |
$integrationHandler = new IntegrationHandler($formID, IpTool::getUserDetail()); |
| 562 |
$zrecruitDetails = $integrationHandler->getAIntegration($integrationID); |
| 563 |
|
| 564 |
if (is_wp_error($zrecruitDetails)) { |
| 565 |
return; |
| 566 |
} |
| 567 |
$newDetails = json_decode($zrecruitDetails[0]->integration_details); |
| 568 |
|
| 569 |
$newDetails->tokenDetails = $tokenDetails; |
| 570 |
if (!empty($others['modules'])) { |
| 571 |
$newDetails->default->modules = $others['modules']; |
| 572 |
} |
| 573 |
if (!empty($others['related_modules'])) { |
| 574 |
$newDetails->default->relatedlist['modules'] = $others['related_modules']; |
| 575 |
} |
| 576 |
|
| 577 |
$integrationHandler->updateIntegration($integrationID, $zrecruitDetails[0]->integration_name, 'Zoho Recruit', wp_json_encode($newDetails), 'form'); |
| 578 |
} |
| 579 |
|
| 580 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 581 |
{ |
| 582 |
$integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 583 |
$tokenDetails = $integrationDetails->tokenDetails; |
| 584 |
$module = $integrationDetails->module; |
| 585 |
$fieldMap = $integrationDetails->field_map; |
| 586 |
$actions = $integrationDetails->actions; |
| 587 |
$defaultDataConf = $integrationDetails->default; |
| 588 |
if ( |
| 589 |
empty($tokenDetails) |
| 590 |
|| empty($module) |
| 591 |
|| empty($fieldMap) |
| 592 |
) { |
| 593 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho recruit api', 'bit-form')); |
| 594 |
} |
| 595 |
if (empty($defaultDataConf->moduleData->{$module}->fields) || empty($defaultDataConf->modules->{$module})) { |
| 596 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for zoho recruit api', 'bit-form')); |
| 597 |
} |
| 598 |
$requiredParams = null; |
| 599 |
if ((intval($tokenDetails->generates_on) + (55 * 60)) < time()) { |
| 600 |
$requiredParams['clientId'] = $integrationDetails->clientId; |
| 601 |
$requiredParams['clientSecret'] = $integrationDetails->clientSecret; |
| 602 |
$requiredParams['dataCenter'] = $integrationDetails->dataCenter; |
| 603 |
$requiredParams['tokenDetails'] = $tokenDetails; |
| 604 |
$newTokenDetails = ZohoRecruitHandler::_refreshAccessToken((object)$requiredParams); |
| 605 |
if ($newTokenDetails) { |
| 606 |
ZohoRecruitHandler::_saveRefreshedToken($this->_formID, $this->_integrationID, $newTokenDetails); |
| 607 |
$tokenDetails = $newTokenDetails; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
$required = !empty($defaultDataConf->moduleData->{$module}->required) ? |
| 612 |
$defaultDataConf->moduleData->{$module}->required : []; |
| 613 |
|
| 614 |
$actions = $integrationDetails->actions; |
| 615 |
$fileMap = $integrationDetails->upload_field_map; |
| 616 |
$dataCenter = $integrationDetails->dataCenter; |
| 617 |
$recordApiHelper = new RecordApiHelper($dataCenter, $tokenDetails, $this->_integrationID, $logID); |
| 618 |
$zRecruitApiResponse = $recordApiHelper->executeRecordApi( |
| 619 |
$this->_formID, |
| 620 |
$entryID, |
| 621 |
$defaultDataConf, |
| 622 |
$module, |
| 623 |
$fieldValues, |
| 624 |
$fieldMap, |
| 625 |
$actions, |
| 626 |
$required, |
| 627 |
$fileMap |
| 628 |
); |
| 629 |
if (is_wp_error($zRecruitApiResponse)) { |
| 630 |
return $zRecruitApiResponse; |
| 631 |
} |
| 632 |
|
| 633 |
if ( |
| 634 |
count($integrationDetails->relatedlists) |
| 635 |
&& !empty($zRecruitApiResponse->response->result->row->success->details->FL[0]) |
| 636 |
&& 'Id' === $zRecruitApiResponse->response->result->row->success->details->FL[0]->val |
| 637 |
) { |
| 638 |
foreach ($integrationDetails->relatedlists as $relatedlist) { |
| 639 |
if (!empty($relatedlist->module)) { |
| 640 |
$recordID = $zRecruitApiResponse->response->result->row->success->details->FL[0]->content; |
| 641 |
$relatedListModule = $relatedlist->module; |
| 642 |
$defaultDataConf->moduleData->{$relatedListModule}->fields->{'SEMODULE'} = (object) [ |
| 643 |
'length' => \strlen($relatedListModule), |
| 644 |
'required' => true, |
| 645 |
'data_type' => 'string', |
| 646 |
]; |
| 647 |
$fieldValues['SEMODULE'] = $relatedListModule; |
| 648 |
$relatedlist->field_map[] = (object) |
| 649 |
[ |
| 650 |
'formField' => 'SEMODULE', |
| 651 |
'zohoFormField' => 'SEMODULE' |
| 652 |
]; |
| 653 |
|
| 654 |
$defaultDataConf->moduleData->{$relatedListModule}->fields->{'SEID'} = (object) [ |
| 655 |
'length' => \strlen($recordID), |
| 656 |
'required' => true, |
| 657 |
'data_type' => 'string', |
| 658 |
]; |
| 659 |
$fieldValues['SEID'] = $recordID; |
| 660 |
$relatedlist->field_map[] = (object) |
| 661 |
[ |
| 662 |
'formField' => 'SEID', |
| 663 |
'zohoFormField' => 'SEID' |
| 664 |
]; |
| 665 |
|
| 666 |
$zRecruitRelatedRecResp = $recordApiHelper->executeRecordApi( |
| 667 |
$this->_formID, |
| 668 |
$entryID, |
| 669 |
$defaultDataConf, |
| 670 |
$relatedListModule, |
| 671 |
$fieldValues, |
| 672 |
$relatedlist->field_map, |
| 673 |
$relatedlist->actions, |
| 674 |
!empty($defaultDataConf->moduleData->{$relatedListModule}->required) ? |
| 675 |
$defaultDataConf->moduleData->{$relatedListModule}->required : [], |
| 676 |
$relatedlist->upload_field_map |
| 677 |
); |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
return $zRecruitApiResponse; |
| 682 |
} |
| 683 |
} |
| 684 |
|