| @@ -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[^ ${}]*}/'; |
| @@ -207,17 +218,33 @@ | ||
| 207 | 218 | preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 208 | 219 | $uniqueFieldsInStr = array_unique($matchedField[0]); |
| 209 | 220 | foreach ($uniqueFieldsInStr as $key => $value) { |
| 210 | 221 | $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]); | |
| 222 | + | |
| 223 | + $repeaterArr = explode('.', $fieldName); | |
| 224 | + | |
| 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 | + | |
| 233 | + foreach ($repeaterFieldValues as $value) { | |
| 234 | + if (isset($value[$repeaterArr[1]])) { | |
| 235 | + $repeaterValues[] = $value[$repeaterArr[1]]; | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + $stringToReplaceField = $repeaterValues; | |
| 214 | 240 | } |
| 215 | 241 | } |
| 216 | 242 | return $stringToReplaceField; |
| 217 | 243 | } |
| 218 | 244 | |
| 219 | - public static function updatedHiddenFieldValue($formID) { | |
| 245 | + public static function updatedHiddenFieldValue($formID) | |
| 246 | + { | |
| 220 | 247 | $hiddenFields = []; |
| 221 | 248 | $frontendFormManger = new FrontendFormManager($formID); |
| 222 | 249 | |
| 223 | 250 | if ($frontendFormManger->isHoneypotActive()) { |
| @@ -229,10 +256,10 @@ | ||
| 229 | 256 | ]; |
| 230 | 257 | } |
| 231 | 258 | $csrfTokens = Helpers::csrfEecrypted(); |
| 232 | 259 | $hiddenFields[] = [ |
| 233 | - 'name' => 't_identy', | |
| 234 | - 'value' => $csrfTokens['t_identy'], | |
| 260 | + 'name' => 't_identity', | |
| 261 | + 'value' => $csrfTokens['t_identity'], | |
| 235 | 262 | ]; |
| 236 | 263 | $hiddenFields[] = [ |
| 237 | 264 | 'name' => 'csrf', |
| 238 | 265 | 'value' => $csrfTokens['csrf'] |
| @@ -239,9 +266,10 @@ | ||
| 239 | 266 | ]; |
| 240 | 267 | return $hiddenFields; |
| 241 | 268 | } |
| 242 | 269 | |
| 243 | - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) { | |
| 270 | + public static function maybeSetCronForIntegration($workFlowReturnedData, $opType, $isFormRequest = true) | |
| 271 | + { | |
| 244 | 272 | $responseData = []; |
| 245 | 273 | $entryId = $workFlowReturnedData['triggerData']['entryID']; |
| 246 | 274 | $responseData['entry_id'] = $entryId; |
| 247 | 275 | if (isset($workFlowReturnedData['message'])) { |
| @@ -264,8 +292,14 @@ | ||
| 264 | 292 | } |
| 265 | 293 | |
| 266 | 294 | $triggerData = $workFlowReturnedData['triggerData']; |
| 267 | 295 | |
| 296 | + $trnasientData = get_transient("bitform_trigger_transient_{$entryId}"); | |
| 297 | + $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; | |
| 298 | + if (!empty($trnasientData['fields'])) { | |
| 299 | + $workFlowReturnedData['fields'] = array_merge($workFlowReturnedData['fields'], $trnasientData['fields']); | |
| 300 | + } | |
| 301 | + | |
| 268 | 302 | if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) { |
| 269 | 303 | if (!headers_sent()) { |
| 270 | 304 | header('Connection: close'); |
| 271 | 305 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| @@ -270,12 +304,21 @@ | ||
| 270 | 304 | header('Connection: close'); |
| 271 | 305 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| 272 | 306 | header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset')); |
| 273 | 307 | status_header(200); |
| 274 | - $response = [ | |
| 275 | - 'success' => true, | |
| 276 | - 'data' => $responseData, | |
| 277 | - ]; | |
| 308 | + if (!$isFormRequest) { | |
| 309 | + $response = [ | |
| 310 | + 'status' => 200, | |
| 311 | + 'code' => 4000, | |
| 312 | + 'message' => 'Data Added Successfully!!', | |
| 313 | + 'success' => true | |
| 314 | + ]; | |
| 315 | + } else { | |
| 316 | + $response = [ | |
| 317 | + 'success' => true, | |
| 318 | + 'data' => $responseData, | |
| 319 | + ]; | |
| 320 | + } | |
| 278 | 321 | } |
| 279 | 322 | ob_start(); |
| 280 | 323 | echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message']; |
| 281 | 324 | ob_end_flush(); |
| @@ -354,9 +397,9 @@ | ||
| 354 | 397 | 'log_id' => $triggerData['logID'], |
| 355 | 398 | 'integration_id' => 0, |
| 356 | 399 | 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]), |
| 357 | 400 | 'response_type' => 'queued', |
| 358 | - 'response_obj' => json_encode(['status' => 'queued']), | |
| 401 | + 'response_obj' => wp_json_encode(['status' => 'queued']), | |
| 359 | 402 | 'created_at' => current_time('mysql'), |
| 360 | 403 | ] |
| 361 | 404 | ); |
| 362 | 405 | $responseData['cronNotOk'] = [ |
| @@ -365,6 +408,79 @@ | ||
| 365 | 408 | $queueuEntry, |
| 366 | 409 | ]; |
| 367 | 410 | } |
| 368 | 411 | return $responseData; |
| 412 | + } | |
| 413 | + | |
| 414 | + public static function repeaterFieldValueFormatter($fieldValues) | |
| 415 | + { | |
| 416 | + $formattedArray = []; | |
| 417 | + | |
| 418 | + foreach ($fieldValues as $key => $value) { | |
| 419 | + if (is_array($value)) { | |
| 420 | + foreach ($value as $index => $v) { | |
| 421 | + if (is_array($v)) { | |
| 422 | + foreach ($v as $k1 => $v1) { | |
| 423 | + $formattedArray[$k1][] = $v1; | |
| 424 | + } | |
| 425 | + } else { | |
| 426 | + $formattedArray[$key][] = $v; | |
| 427 | + } | |
| 428 | + } | |
| 429 | + } else { | |
| 430 | + $formattedArray[$key] = $value; | |
| 431 | + } | |
| 432 | + } | |
| 433 | + | |
| 434 | + return $formattedArray; | |
| 435 | + } | |
| 436 | + | |
| 437 | + /** | |
| 438 | + * | |
| 439 | + * @param mixed $fieldValues | |
| 440 | + * @param mixed $returnRepeaterValue default is array | |
| 441 | + * @return array and repeater field value as array or string | |
| 442 | + */ | |
| 443 | + public static function formattedRepeaterValue($fieldValues, $returnRepeaterValue = '') | |
| 444 | + { | |
| 445 | + // get the repeater field and store it in an array | |
| 446 | + $repeaterField = []; | |
| 447 | + foreach ($fieldValues as $key => $value) { | |
| 448 | + if (!preg_match('/\./', $key)) { | |
| 449 | + continue; | |
| 450 | + } | |
| 451 | + $repeaterField[] = $key; | |
| 452 | + } | |
| 453 | + | |
| 454 | + // put the value of the child key to the parent repeater field\ | |
| 455 | + foreach ($repeaterField as $key) { | |
| 456 | + list($parentKey, $childKey) = explode('.', $key); | |
| 457 | + $repeatValues = isset($fieldValues[$parentKey]) ? $fieldValues[$parentKey] : $key; | |
| 458 | + | |
| 459 | + if (!is_array($repeatValues)) { | |
| 460 | + $fieldValues[$key] = $fieldValues[$childKey]; | |
| 461 | + continue; | |
| 462 | + } | |
| 463 | + | |
| 464 | + foreach ($repeatValues as $value) { | |
| 465 | + if (isset($value[$childKey])) { | |
| 466 | + if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) { | |
| 467 | + $fieldValues[$key] = []; | |
| 468 | + } | |
| 469 | + if (!empty($value[$childKey])) { | |
| 470 | + $fieldValues[$key][] = $value[$childKey]; | |
| 471 | + } | |
| 472 | + } | |
| 473 | + } | |
| 474 | + } | |
| 475 | + | |
| 476 | + // convert the array to string repeater field value | |
| 477 | + if ('string' === $returnRepeaterValue) { | |
| 478 | + foreach ($repeaterField as $key) { | |
| 479 | + if (isset($fieldValues[$key]) && is_array($fieldValues[$key])) { | |
| 480 | + $fieldValues[$key] = implode(', ', $fieldValues[$key]); | |
| 481 | + } | |
| 482 | + } | |
| 483 | + } | |
| 484 | + return $fieldValues; | |
| 369 | 485 | } |
| 370 | 486 | } |