| @@ -10,9 +10,10 @@ | ||
| 10 | 10 | use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 11 | 11 | use BitCode\BitForm\Core\Util\MailNotifier; |
| 12 | 12 | use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 13 | 13 | |
| 14 | -final class IntegrationHandler { | |
| 14 | +final class IntegrationHandler | |
| 15 | +{ | |
| 15 | 16 | private static $_formID; |
| 16 | 17 | private static $_integrationModel; |
| 17 | 18 | private $_user_details; |
| 18 | 19 | |
| @@ -23,15 +24,17 @@ | ||
| 23 | 24 | * $formID will be 0 and category app |
| 24 | 25 | * @param Array $user_details Details of user accessing data |
| 25 | 26 | * @return void |
| 26 | 27 | */ |
| 27 | - public function __construct($formID, $user_details = null) { | |
| 28 | + public function __construct($formID, $user_details = null) | |
| 29 | + { | |
| 28 | 30 | static::$_formID = $formID; |
| 29 | 31 | static::$_integrationModel = new IntegrationModel(); |
| 30 | 32 | $this->_user_details = $user_details; |
| 31 | 33 | } |
| 32 | 34 | |
| 33 | - public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) { | |
| 35 | + public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) | |
| 36 | + { | |
| 34 | 37 | $conditions = [ |
| 35 | 38 | 'form_id' => static::$_formID, |
| 36 | 39 | 'id' => $integrationID, |
| 37 | 40 | ]; |
| @@ -52,9 +55,10 @@ | ||
| 52 | 55 | $conditions |
| 53 | 56 | ); |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | - public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) { | |
| 59 | + public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) | |
| 60 | + { | |
| 57 | 61 | $conditions = [ |
| 58 | 62 | 'form_id' => static::$_formID, |
| 59 | 63 | ]; |
| 60 | 64 | if (!is_null($integrationType)) { |
| @@ -82,9 +86,10 @@ | ||
| 82 | 86 | $conditions |
| 83 | 87 | ); |
| 84 | 88 | } |
| 85 | 89 | |
| 86 | - public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null) { | |
| 90 | + public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null) | |
| 91 | + { | |
| 87 | 92 | $conditions = []; |
| 88 | 93 | if (!is_null($integrationType)) { |
| 89 | 94 | $conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 90 | 95 | } |
| @@ -110,9 +115,10 @@ | ||
| 110 | 115 | $conditions |
| 111 | 116 | ); |
| 112 | 117 | } |
| 113 | 118 | |
| 114 | - public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) { | |
| 119 | + public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) | |
| 120 | + { | |
| 115 | 121 | if (null === $status) { |
| 116 | 122 | $status = 1; |
| 117 | 123 | } |
| 118 | 124 | return static::$_integrationModel->insert( |
| @@ -130,9 +136,10 @@ | ||
| 130 | 136 | ] |
| 131 | 137 | ); |
| 132 | 138 | } |
| 133 | 139 | |
| 134 | - public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) { | |
| 140 | + public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) | |
| 141 | + { | |
| 135 | 142 | if (null === $status) { |
| 136 | 143 | $status = 1; |
| 137 | 144 | } |
| 138 | 145 | return static::$_integrationModel->update( |
| @@ -152,9 +159,10 @@ | ||
| 152 | 159 | ] |
| 153 | 160 | ); |
| 154 | 161 | } |
| 155 | 162 | |
| 156 | - public function duplicateAllInAForm($oldFormId) { | |
| 163 | + public function duplicateAllInAForm($oldFormId) | |
| 164 | + { | |
| 157 | 165 | $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at']; |
| 158 | 166 | $integDupData = [ |
| 159 | 167 | 'integration_name', |
| 160 | 168 | 'integration_type', |
| @@ -169,9 +177,10 @@ | ||
| 169 | 177 | ]; |
| 170 | 178 | return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]); |
| 171 | 179 | } |
| 172 | 180 | |
| 173 | - public function deleteIntegration($integrationID) { | |
| 181 | + public function deleteIntegration($integrationID) | |
| 182 | + { | |
| 174 | 183 | return static::$_integrationModel->delete( |
| 175 | 184 | [ |
| 176 | 185 | 'id' => $integrationID, |
| 177 | 186 | 'form_id' => static::$_formID, |
| @@ -178,9 +187,10 @@ | ||
| 178 | 187 | ] |
| 179 | 188 | ); |
| 180 | 189 | } |
| 181 | 190 | |
| 182 | - public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) { | |
| 191 | + public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) | |
| 192 | + { | |
| 183 | 193 | if (empty($dataToReplaceField)) { |
| 184 | 194 | return false; |
| 185 | 195 | } |
| 186 | 196 | if (is_string($dataToReplaceField)) { |
| @@ -198,9 +208,10 @@ | ||
| 198 | 208 | } |
| 199 | 209 | return $dataToReplaceField; |
| 200 | 210 | } |
| 201 | 211 | |
| 202 | - private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) { | |
| 212 | + private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) | |
| 213 | + { | |
| 203 | 214 | if (empty($stringToReplaceField)) { |
| 204 | 215 | return $stringToReplaceField; |
| 205 | 216 | } |
| 206 | 217 | $fieldPattern = '/\${\w[^ ${}]*}/'; |
| @@ -205,21 +216,38 @@ | ||
| 205 | 216 | } |
| 206 | 217 | $fieldPattern = '/\${\w[^ ${}]*}/'; |
| 207 | 218 | preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 208 | 219 | $uniqueFieldsInStr = array_unique($matchedField[0]); |
| 209 | - foreach ($uniqueFieldsInStr as $key => $value) { | |
| 220 | + | |
| 221 | + foreach ($uniqueFieldsInStr as $value) { | |
| 210 | 222 | $fieldName = substr($value, 2, strlen($value) - 3); |
| 211 | - if (!empty($fieldValues[$fieldName])) { | |
| 212 | - $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) : | |
| 213 | - wp_json_encode($fieldValues[$fieldName]); | |
| 223 | + | |
| 224 | + $repeaterArr = explode('.', $fieldName); | |
| 225 | + if (isset($fieldValues[$fieldName])) { | |
| 226 | + $stringToReplaceField = is_string($fieldValues[$fieldName]) | |
| 227 | + ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) | |
| 228 | + : wp_json_encode($fieldValues[$fieldName]); | |
| 229 | + } elseif (2 === count($repeaterArr) && isset($fieldValues[$repeaterArr[0]])) { | |
| 230 | + $repeaterValues = []; | |
| 231 | + $repeaterFieldValues = $fieldValues[$repeaterArr[0]]; | |
| 232 | + foreach ($repeaterFieldValues as $value) { | |
| 233 | + if (isset($value[$repeaterArr[1]])) { | |
| 234 | + $repeaterValues[] = $value[$repeaterArr[1]]; | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + $stringToReplaceField = $repeaterValues; | |
| 239 | + } else { | |
| 240 | + $stringToReplaceField = FieldValueHandler::replaceSmartTagWithValue($value); | |
| 214 | 241 | } |
| 215 | 242 | } |
| 216 | 243 | return $stringToReplaceField; |
| 217 | 244 | } |
| 218 | 245 | |
| 219 | - public static function updatedHiddenFieldValue($formID) { | |
| 246 | + public static function updatedHiddenFieldValue($formID) | |
| 247 | + { | |
| 220 | 248 | $hiddenFields = []; |
| 221 | - $frontendFormManger = new FrontendFormManager($formID); | |
| 249 | + $frontendFormManger = FrontendFormManager::getInstance($formID); | |
| 222 | 250 | |
| 223 | 251 | if ($frontendFormManger->isHoneypotActive()) { |
| 224 | 252 | $str = '_bitform' . '_' . $formID . '_' . time() . '_'; |
| 225 | 253 | $honpotToken = Helpers::honeypotEncryptedToken($str); |
| @@ -229,10 +257,10 @@ | ||
| 229 | 257 | ]; |
| 230 | 258 | } |
| 231 | 259 | $csrfTokens = Helpers::csrfEecrypted(); |
| 232 | 260 | $hiddenFields[] = [ |
| 233 | - 'name' => 't_identy', | |
| 234 | - 'value' => $csrfTokens['t_identy'], | |
| 261 | + 'name' => 't_identity', | |
| 262 | + 'value' => $csrfTokens['t_identity'], | |
| 235 | 263 | ]; |
| 236 | 264 | $hiddenFields[] = [ |
| 237 | 265 | 'name' => 'csrf', |
| 238 | 266 | 'value' => $csrfTokens['csrf'] |
| @@ -239,9 +267,10 @@ | ||
| 239 | 267 | ]; |
| 240 | 268 | return $hiddenFields; |
| 241 | 269 | } |
| 242 | 270 | |
| 243 | - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) { | |
| 271 | + public static function maybeSetCronForIntegration($workFlowReturnedData, $opType, $isFormRequest = true) | |
| 272 | + { | |
| 244 | 273 | $responseData = []; |
| 245 | 274 | $entryId = $workFlowReturnedData['triggerData']['entryID']; |
| 246 | 275 | $responseData['entry_id'] = $entryId; |
| 247 | 276 | if (isset($workFlowReturnedData['message'])) { |
| @@ -258,8 +287,11 @@ | ||
| 258 | 287 | } |
| 259 | 288 | if ('update' === $opType && isset($workFlowReturnedData['updatedData'])) { |
| 260 | 289 | $responseData['updatedData'] = $workFlowReturnedData['updatedData']; |
| 261 | 290 | } |
| 291 | + if (isset($workFlowReturnedData['new_nonce'])) { | |
| 292 | + $responseData['new_nonce'] = $workFlowReturnedData['new_nonce']; | |
| 293 | + } | |
| 262 | 294 | if (!isset($workFlowReturnedData['triggerData'])) { |
| 263 | 295 | return $responseData; |
| 264 | 296 | } |
| 265 | 297 | |
| @@ -264,8 +296,16 @@ | ||
| 264 | 296 | } |
| 265 | 297 | |
| 266 | 298 | $triggerData = $workFlowReturnedData['triggerData']; |
| 267 | 299 | |
| 300 | + $trnasientData = get_transient("bitform_trigger_transient_{$entryId}"); | |
| 301 | + $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; | |
| 302 | + | |
| 303 | + if (!empty($trnasientData['fields'])) { | |
| 304 | + $fieldData = isset($workFlowReturnedData['fields']) ? $workFlowReturnedData['fields'] : $workFlowReturnedData['updatedData']; | |
| 305 | + $workFlowReturnedData['fields'] = array_merge($fieldData, $trnasientData['fields']); | |
| 306 | + } | |
| 307 | + | |
| 268 | 308 | if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) { |
| 269 | 309 | if (!headers_sent()) { |
| 270 | 310 | header('Connection: close'); |
| 271 | 311 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| @@ -270,12 +310,21 @@ | ||
| 270 | 310 | header('Connection: close'); |
| 271 | 311 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| 272 | 312 | header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset')); |
| 273 | 313 | status_header(200); |
| 274 | - $response = [ | |
| 275 | - 'success' => true, | |
| 276 | - 'data' => $responseData, | |
| 277 | - ]; | |
| 314 | + if (!$isFormRequest) { | |
| 315 | + $response = [ | |
| 316 | + 'status' => 200, | |
| 317 | + 'code' => 4000, | |
| 318 | + 'message' => 'Data Added Successfully!!', | |
| 319 | + 'success' => true | |
| 320 | + ]; | |
| 321 | + } else { | |
| 322 | + $response = [ | |
| 323 | + 'success' => true, | |
| 324 | + 'data' => $responseData, | |
| 325 | + ]; | |
| 326 | + } | |
| 278 | 327 | } |
| 279 | 328 | ob_start(); |
| 280 | 329 | echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message']; |
| 281 | 330 | ob_end_flush(); |
| @@ -293,11 +342,12 @@ | ||
| 293 | 342 | 'id' => $triggerData['entryID'], |
| 294 | 343 | ] |
| 295 | 344 | ); |
| 296 | 345 | |
| 346 | + $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 297 | 347 | if (!is_wp_error($updatedStatus)) { |
| 298 | 348 | if ($workFlowReturnedData['dflt_template']) { |
| 299 | - $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 349 | + // $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 300 | 350 | do_action('bf_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData); |
| 301 | 351 | } elseif (isset($triggerData['dblOptin'])) { |
| 302 | 352 | foreach ($triggerData['dblOptin'] as $value) { |
| 303 | 353 | MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']); |
| @@ -313,15 +363,15 @@ | ||
| 313 | 363 | } |
| 314 | 364 | return $workFlowReturnedData; |
| 315 | 365 | } |
| 316 | 366 | |
| 317 | - if (isset($triggerData['mail'])) { | |
| 367 | + if (isset($triggerData['mail']) && !empty($triggerData['mail'])) { | |
| 318 | 368 | $formManager = new AdminFormManager($triggerData['formID']); |
| 319 | 369 | $formContent = $formManager->getFormContent(); |
| 320 | 370 | $submitted_fields = $formContent->fields; |
| 321 | 371 | $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']); |
| 322 | 372 | foreach ($triggerData['mail'] as $value) { |
| 323 | - MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']); | |
| 373 | + MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID'], false, $triggerData['logID']); | |
| 324 | 374 | } |
| 325 | 375 | } |
| 326 | 376 | do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 327 | 377 | |
| @@ -346,8 +396,9 @@ | ||
| 346 | 396 | $triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails']; |
| 347 | 397 | $triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template']; |
| 348 | 398 | $triggerData['integrationRun'] = $workFlowReturnedData['integrationRun']; |
| 349 | 399 | } |
| 400 | + | |
| 350 | 401 | set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS); |
| 351 | 402 | $entryLog = new FormEntryLogModel(); |
| 352 | 403 | $queueuEntry = $entryLog->log_history_insert( |
| 353 | 404 | [ |
| @@ -354,9 +405,9 @@ | ||
| 354 | 405 | 'log_id' => $triggerData['logID'], |
| 355 | 406 | 'integration_id' => 0, |
| 356 | 407 | 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]), |
| 357 | 408 | 'response_type' => 'queued', |
| 358 | - 'response_obj' => json_encode(['status' => 'queued']), | |
| 409 | + 'response_obj' => wp_json_encode(['status' => 'queued']), | |
| 359 | 410 | 'created_at' => current_time('mysql'), |
| 360 | 411 | ] |
| 361 | 412 | ); |
| 362 | 413 | $responseData['cronNotOk'] = [ |
| @@ -365,6 +416,79 @@ | ||
| 365 | 416 | $queueuEntry, |
| 366 | 417 | ]; |
| 367 | 418 | } |
| 368 | 419 | return $responseData; |
| 420 | + } | |
| 421 | + | |
| 422 | + public static function repeaterFieldValueFormatter($fieldValues) | |
| 423 | + { | |
| 424 | + $formattedArray = []; | |
| 425 | + | |
| 426 | + foreach ($fieldValues as $key => $value) { | |
| 427 | + if (is_array($value)) { | |
| 428 | + foreach ($value as $index => $v) { | |
| 429 | + if (is_array($v)) { | |
| 430 | + foreach ($v as $k1 => $v1) { | |
| 431 | + $formattedArray[$k1][] = $v1; | |
| 432 | + } | |
| 433 | + } else { | |
| 434 | + $formattedArray[$key][] = $v; | |
| 435 | + } | |
| 436 | + } | |
| 437 | + } else { | |
| 438 | + $formattedArray[$key] = $value; | |
| 439 | + } | |
| 440 | + } | |
| 441 | + | |
| 442 | + return $formattedArray; | |
| 443 | + } | |
| 444 | + | |
| 445 | + /** | |
| 446 | + * | |
| 447 | + * @param mixed $fieldValues | |
| 448 | + * @param mixed $returnRepeaterValue default is array | |
| 449 | + * @return array and repeater field value as array or string | |
| 450 | + */ | |
| 451 | + public static function formattedRepeaterValue($fieldValues, $returnRepeaterValue = '') | |
| 452 | + { | |
| 453 | + // get the repeater field and store it in an array | |
| 454 | + $repeaterField = []; | |
| 455 | + foreach ($fieldValues as $key => $value) { | |
| 456 | + if (!preg_match('/\./', $key)) { | |
| 457 | + continue; | |
| 458 | + } | |
| 459 | + $repeaterField[] = $key; | |
| 460 | + } | |
| 461 | + | |
| 462 | + // put the value of the child key to the parent repeater field\ | |
| 463 | + foreach ($repeaterField as $key) { | |
| 464 | + list($parentKey, $childKey) = explode('.', $key); | |
| 465 | + $repeatValues = isset($fieldValues[$parentKey]) ? $fieldValues[$parentKey] : $key; | |
| 466 | + | |
| 467 | + if (!is_array($repeatValues)) { | |
| 468 | + $fieldValues[$key] = $fieldValues[$childKey]; | |
| 469 | + continue; | |
| 470 | + } | |
| 471 | + | |
| 472 | + foreach ($repeatValues as $value) { | |
| 473 | + if (isset($value[$childKey])) { | |
| 474 | + if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) { | |
| 475 | + $fieldValues[$key] = []; | |
| 476 | + } | |
| 477 | + if (!empty($value[$childKey])) { | |
| 478 | + $fieldValues[$key][] = $value[$childKey]; | |
| 479 | + } | |
| 480 | + } | |
| 481 | + } | |
| 482 | + } | |
| 483 | + | |
| 484 | + // convert the array to string repeater field value | |
| 485 | + if ('string' === $returnRepeaterValue) { | |
| 486 | + foreach ($repeaterField as $key) { | |
| 487 | + if (isset($fieldValues[$key]) && is_array($fieldValues[$key])) { | |
| 488 | + $fieldValues[$key] = implode(', ', $fieldValues[$key]); | |
| 489 | + } | |
| 490 | + } | |
| 491 | + } | |
| 492 | + return $fieldValues; | |
| 369 | 493 | } |
| 370 | 494 | } |