| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | use BitCode\BitForm\Admin\Form\Helpers; |
| 7 | 7 | use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 8 | 8 | use BitCode\BitForm\Core\Database\FormEntryModel; |
| 9 | 9 | use BitCode\BitForm\Core\Database\IntegrationModel; |
| 10 | -use BitCode\BitForm\Core\Form\FormManager; | |
| 11 | 10 | use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 12 | 11 | use BitCode\BitForm\Core\Util\Log; |
| 13 | 12 | use BitCode\BitForm\Core\Util\MailNotifier; |
| 14 | 13 | use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| @@ -14,9 +13,9 @@ | ||
| 14 | 13 | use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 15 | 14 | |
| 16 | 15 | final class IntegrationHandler |
| 17 | 16 | { |
| 18 | - private $_formID; | |
| 17 | + private static $_formID; | |
| 19 | 18 | private static $_integrationModel; |
| 20 | 19 | private $_user_details; |
| 21 | 20 | |
| 22 | 21 | /** |
| @@ -28,9 +27,9 @@ | ||
| 28 | 27 | * @return void |
| 29 | 28 | */ |
| 30 | 29 | public function __construct($formID, $user_details = null) |
| 31 | 30 | { |
| 32 | - $this->_formID = $formID; | |
| 31 | + static::$_formID = $formID; | |
| 33 | 32 | static::$_integrationModel = new IntegrationModel(); |
| 34 | 33 | $this->_user_details = $user_details; |
| 35 | 34 | } |
| 36 | 35 | |
| @@ -36,9 +35,9 @@ | ||
| 36 | 35 | |
| 37 | 36 | public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) |
| 38 | 37 | { |
| 39 | 38 | $conditions = [ |
| 40 | - 'form_id' => $this->_formID, | |
| 39 | + 'form_id' => static::$_formID, | |
| 41 | 40 | 'id' => $integrationID, |
| 42 | 41 | ]; |
| 43 | 42 | if (!is_null($integrationType)) { |
| 44 | 43 | $conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| @@ -51,9 +50,8 @@ | ||
| 51 | 50 | 'id', |
| 52 | 51 | 'integration_name', |
| 53 | 52 | 'integration_type', |
| 54 | 53 | 'integration_details', |
| 55 | - 'status', | |
| 56 | 54 | 'form_id', |
| 57 | 55 | ], |
| 58 | 56 | $conditions |
| 59 | 57 | ); |
| @@ -61,9 +59,9 @@ | ||
| 61 | 59 | |
| 62 | 60 | public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) |
| 63 | 61 | { |
| 64 | 62 | $conditions = [ |
| 65 | - 'form_id' => $this->_formID, | |
| 63 | + 'form_id' => static::$_formID, | |
| 66 | 64 | ]; |
| 67 | 65 | if (!is_null($integrationType)) { |
| 68 | 66 | $conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 69 | 67 | } |
| @@ -129,9 +127,9 @@ | ||
| 129 | 127 | 'integration_name' => $integrationName, |
| 130 | 128 | 'integration_type' => $integrationType, |
| 131 | 129 | 'integration_details' => $integrationDetails, |
| 132 | 130 | 'category' => $integrationCategory, |
| 133 | - 'form_id' => $this->_formID, | |
| 131 | + 'form_id' => static::$_formID, | |
| 134 | 132 | 'user_id' => $this->_user_details['id'], |
| 135 | 133 | 'user_ip' => $this->_user_details['ip'], |
| 136 | 134 | 'status' => $status, |
| 137 | 135 | 'created_at' => $this->_user_details['time'], |
| @@ -150,9 +148,9 @@ | ||
| 150 | 148 | 'integration_name' => $integrationName, |
| 151 | 149 | 'integration_type' => $integrationType, |
| 152 | 150 | 'integration_details' => $integrationDetails, |
| 153 | 151 | 'category' => $integrationCategory, |
| 154 | - 'form_id' => $this->_formID, | |
| 152 | + 'form_id' => static::$_formID, | |
| 155 | 153 | 'user_id' => $this->_user_details['id'], |
| 156 | 154 | 'user_ip' => $this->_user_details['ip'], |
| 157 | 155 | 'status' => $status, |
| 158 | 156 | 'updated_at' => $this->_user_details['time'], |
| @@ -162,23 +160,8 @@ | ||
| 162 | 160 | ] |
| 163 | 161 | ); |
| 164 | 162 | } |
| 165 | 163 | |
| 166 | - public function updateIntegrationStatus($integrationID, $status) | |
| 167 | - { | |
| 168 | - $data = ['status' => (int) $status]; | |
| 169 | - if (!empty($this->_user_details['time'])) { | |
| 170 | - $data['updated_at'] = $this->_user_details['time']; | |
| 171 | - } | |
| 172 | - return static::$_integrationModel->update( | |
| 173 | - $data, | |
| 174 | - [ | |
| 175 | - 'id' => $integrationID, | |
| 176 | - 'form_id' => $this->_formID, | |
| 177 | - ] | |
| 178 | - ); | |
| 179 | - } | |
| 180 | - | |
| 181 | 164 | public function duplicateAllInAForm($oldFormId) |
| 182 | 165 | { |
| 183 | 166 | $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at']; |
| 184 | 167 | $integDupData = [ |
| @@ -185,9 +168,9 @@ | ||
| 185 | 168 | 'integration_name', |
| 186 | 169 | 'integration_type', |
| 187 | 170 | 'integration_details', |
| 188 | 171 | 'category', |
| 189 | - $this->_formID, | |
| 172 | + static::$_formID, | |
| 190 | 173 | $this->_user_details['id'], |
| 191 | 174 | $this->_user_details['ip'], |
| 192 | 175 | 'status', |
| 193 | 176 | $this->_user_details['time'], |
| @@ -200,9 +183,9 @@ | ||
| 200 | 183 | { |
| 201 | 184 | return static::$_integrationModel->delete( |
| 202 | 185 | [ |
| 203 | 186 | 'id' => $integrationID, |
| 204 | - 'form_id' => $this->_formID, | |
| 187 | + 'form_id' => static::$_formID, | |
| 205 | 188 | ] |
| 206 | 189 | ); |
| 207 | 190 | } |
| 208 | 191 | |
| @@ -234,29 +217,13 @@ | ||
| 234 | 217 | } |
| 235 | 218 | $fieldPattern = '/\${\w[^ ${}]*}/'; |
| 236 | 219 | preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 237 | 220 | $uniqueFieldsInStr = array_unique($matchedField[0]); |
| 238 | - | |
| 239 | - foreach ($uniqueFieldsInStr as $value) { | |
| 221 | + foreach ($uniqueFieldsInStr as $key => $value) { | |
| 240 | 222 | $fieldName = substr($value, 2, strlen($value) - 3); |
| 241 | - | |
| 242 | - $repeaterArr = explode('.', $fieldName); | |
| 243 | 223 | if (isset($fieldValues[$fieldName])) { |
| 244 | - $stringToReplaceField = is_string($fieldValues[$fieldName]) | |
| 245 | - ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) | |
| 246 | - : wp_json_encode($fieldValues[$fieldName]); | |
| 247 | - } elseif (2 === count($repeaterArr) && isset($fieldValues[$repeaterArr[0]])) { | |
| 248 | - $repeaterValues = []; | |
| 249 | - $repeaterFieldValues = $fieldValues[$repeaterArr[0]]; | |
| 250 | - foreach ($repeaterFieldValues as $value) { | |
| 251 | - if (isset($value[$repeaterArr[1]])) { | |
| 252 | - $repeaterValues[] = $value[$repeaterArr[1]]; | |
| 253 | - } | |
| 254 | - } | |
| 255 | - | |
| 256 | - $stringToReplaceField = $repeaterValues; | |
| 257 | - } else { | |
| 258 | - $stringToReplaceField = FieldValueHandler::replaceSmartTagWithValue($value); | |
| 224 | + $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) : | |
| 225 | + wp_json_encode($fieldValues[$fieldName]); | |
| 259 | 226 | } |
| 260 | 227 | } |
| 261 | 228 | return $stringToReplaceField; |
| 262 | 229 | } |
| @@ -263,9 +230,9 @@ | ||
| 263 | 230 | |
| 264 | 231 | public static function updatedHiddenFieldValue($formID) |
| 265 | 232 | { |
| 266 | 233 | $hiddenFields = []; |
| 267 | - $frontendFormManger = FrontendFormManager::getInstance($formID); | |
| 234 | + $frontendFormManger = new FrontendFormManager($formID); | |
| 268 | 235 | |
| 269 | 236 | if ($frontendFormManger->isHoneypotActive()) { |
| 270 | 237 | $str = '_bitform' . '_' . $formID . '_' . time() . '_'; |
| 271 | 238 | $honpotToken = Helpers::honeypotEncryptedToken($str); |
| @@ -285,9 +252,9 @@ | ||
| 285 | 252 | ]; |
| 286 | 253 | return $hiddenFields; |
| 287 | 254 | } |
| 288 | 255 | |
| 289 | - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType, $isFormRequest = true) | |
| 256 | + public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) | |
| 290 | 257 | { |
| 291 | 258 | $responseData = []; |
| 292 | 259 | $entryId = $workFlowReturnedData['triggerData']['entryID']; |
| 293 | 260 | $responseData['entry_id'] = $entryId; |
| @@ -298,11 +265,8 @@ | ||
| 298 | 265 | $responseData['msg_id'] = isset($workFlowReturnedData['msg_id']) ? $workFlowReturnedData['msg_id'] : 0; |
| 299 | 266 | if (isset($workFlowReturnedData['msg_duration'])) { |
| 300 | 267 | $responseData['msg_duration'] = $workFlowReturnedData['msg_duration']; |
| 301 | 268 | } |
| 302 | - if (isset($workFlowReturnedData['afterSubmit'])) { | |
| 303 | - $responseData['afterSubmit'] = $workFlowReturnedData['afterSubmit']; | |
| 304 | - } | |
| 305 | 269 | } |
| 306 | 270 | if (isset($workFlowReturnedData['redirectPage'])) { |
| 307 | 271 | $responseData['redirectPage'] = $workFlowReturnedData['redirectPage']; |
| 308 | 272 | } |
| @@ -308,11 +272,8 @@ | ||
| 308 | 272 | } |
| 309 | 273 | if ('update' === $opType && isset($workFlowReturnedData['updatedData'])) { |
| 310 | 274 | $responseData['updatedData'] = $workFlowReturnedData['updatedData']; |
| 311 | 275 | } |
| 312 | - if (isset($workFlowReturnedData['new_nonce'])) { | |
| 313 | - $responseData['new_nonce'] = $workFlowReturnedData['new_nonce']; | |
| 314 | - } | |
| 315 | 276 | if (!isset($workFlowReturnedData['triggerData'])) { |
| 316 | 277 | return $responseData; |
| 317 | 278 | } |
| 318 | 279 | |
| @@ -319,12 +280,10 @@ | ||
| 319 | 280 | $triggerData = $workFlowReturnedData['triggerData']; |
| 320 | 281 | |
| 321 | 282 | $trnasientData = get_transient("bitform_trigger_transient_{$entryId}"); |
| 322 | 283 | $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; |
| 323 | - | |
| 324 | - if (!empty($trnasientData['fields'])) { | |
| 325 | - $fieldData = isset($workFlowReturnedData['fields']) ? $workFlowReturnedData['fields'] : $workFlowReturnedData['updatedData']; | |
| 326 | - $workFlowReturnedData['fields'] = array_merge($fieldData, $trnasientData['fields']); | |
| 284 | + if(!empty($trnasientData['fields'])){ | |
| 285 | + $workFlowReturnedData['fields'] = array_merge($workFlowReturnedData['fields'], $trnasientData['fields']); | |
| 327 | 286 | } |
| 328 | 287 | |
| 329 | 288 | if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) { |
| 330 | 289 | if (!headers_sent()) { |
| @@ -331,24 +290,15 @@ | ||
| 331 | 290 | header('Connection: close'); |
| 332 | 291 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| 333 | 292 | header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset')); |
| 334 | 293 | status_header(200); |
| 335 | - if (!$isFormRequest) { | |
| 336 | - $response = [ | |
| 337 | - 'status' => 200, | |
| 338 | - 'code' => 4000, | |
| 339 | - 'message' => 'Data Added Successfully!!', | |
| 340 | - 'success' => true | |
| 341 | - ]; | |
| 342 | - } else { | |
| 343 | - $response = [ | |
| 344 | - 'success' => true, | |
| 345 | - 'data' => $responseData, | |
| 346 | - ]; | |
| 347 | - } | |
| 294 | + $response = [ | |
| 295 | + 'success' => true, | |
| 296 | + 'data' => $responseData, | |
| 297 | + ]; | |
| 348 | 298 | } |
| 349 | 299 | ob_start(); |
| 350 | - echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : esc_html($workFlowReturnedData['message']); | |
| 300 | + echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message']; | |
| 351 | 301 | ob_end_flush(); |
| 352 | 302 | flush(); |
| 353 | 303 | session_write_close(); |
| 354 | 304 | |
| @@ -363,21 +313,15 @@ | ||
| 363 | 313 | 'id' => $triggerData['entryID'], |
| 364 | 314 | ] |
| 365 | 315 | ); |
| 366 | 316 | |
| 367 | - $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 368 | 317 | if (!is_wp_error($updatedStatus)) { |
| 369 | 318 | if ($workFlowReturnedData['dflt_template']) { |
| 370 | - // $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 371 | - do_action('bitform_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData); | |
| 319 | + $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 320 | + do_action('bf_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData); | |
| 372 | 321 | } elseif (isset($triggerData['dblOptin'])) { |
| 373 | 322 | foreach ($triggerData['dblOptin'] as $value) { |
| 374 | - try { | |
| 375 | - MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']); | |
| 376 | - } catch (\Throwable $mailError) { | |
| 377 | - // response already flushed — a mail failure here must not kill the rest of the run | |
| 378 | - Log::debug_log('[+] Double opt-in mail failed: ' . $mailError->getMessage()); | |
| 379 | - } | |
| 323 | + MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']); | |
| 380 | 324 | } |
| 381 | 325 | } |
| 382 | 326 | } |
| 383 | 327 | if (function_exists('fastcgi_finish_request')) { |
| @@ -388,20 +332,16 @@ | ||
| 388 | 332 | die; |
| 389 | 333 | } |
| 390 | 334 | return $workFlowReturnedData; |
| 391 | 335 | } |
| 392 | - if (isset($triggerData['mail']) && !empty($triggerData['mail'])) { | |
| 336 | + | |
| 337 | + if (isset($triggerData['mail'])) { | |
| 393 | 338 | $formManager = new AdminFormManager($triggerData['formID']); |
| 394 | 339 | $formContent = $formManager->getFormContent(); |
| 395 | 340 | $submitted_fields = $formContent->fields; |
| 396 | 341 | $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']); |
| 397 | 342 | foreach ($triggerData['mail'] as $value) { |
| 398 | - try { | |
| 399 | - MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID'], false, $triggerData['logID']); | |
| 400 | - } catch (\Throwable $mailError) { | |
| 401 | - // response already flushed — a mail failure here must not skip the integrations below | |
| 402 | - Log::debug_log('[+] Mail notification failed: ' . $mailError->getMessage()); | |
| 403 | - } | |
| 343 | + MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']); | |
| 404 | 344 | } |
| 405 | 345 | } |
| 406 | 346 | do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 407 | 347 | |
| @@ -415,9 +355,9 @@ | ||
| 415 | 355 | return $workFlowReturnedData; |
| 416 | 356 | } |
| 417 | 357 | |
| 418 | 358 | $entryID = $triggerData['entryID']; |
| 419 | - if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && empty($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) { | |
| 359 | + if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) { | |
| 420 | 360 | $eventScheuled = wp_schedule_single_event(time(), 'bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]); |
| 421 | 361 | $scheduleTime = wp_next_scheduled('bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]); |
| 422 | 362 | $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}"); |
| 423 | 363 | } elseif (!empty($triggerData['integrations']) || !empty($triggerData['mail']) || isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) { |
| @@ -426,15 +366,9 @@ | ||
| 426 | 366 | $triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails']; |
| 427 | 367 | $triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template']; |
| 428 | 368 | $triggerData['integrationRun'] = $workFlowReturnedData['integrationRun']; |
| 429 | 369 | } |
| 430 | - | |
| 431 | - // Generate one-time trigger token for security | |
| 432 | - $triggerToken = wp_hash($triggerData['entryID'] . $triggerData['logID'] . time() . wp_rand(), 'nonce'); | |
| 433 | - $triggerData['trigger_token'] = $triggerToken; | |
| 434 | - | |
| 435 | 370 | set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS); |
| 436 | - $queuedAt = current_time('mysql'); | |
| 437 | 371 | $entryLog = new FormEntryLogModel(); |
| 438 | 372 | $queueuEntry = $entryLog->log_history_insert( |
| 439 | 373 | [ |
| 440 | 374 | 'log_id' => $triggerData['logID'], |
| @@ -440,257 +374,17 @@ | ||
| 440 | 374 | 'log_id' => $triggerData['logID'], |
| 441 | 375 | 'integration_id' => 0, |
| 442 | 376 | 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]), |
| 443 | 377 | 'response_type' => 'queued', |
| 444 | - // 'trigger' is a durable copy of the transient payload: object-cache hosts can | |
| 445 | - // evict the transient (and it expires after 1h), which would otherwise lose the run | |
| 446 | - 'response_obj' => wp_json_encode(['status' => 'queued', 'queued_at' => $queuedAt, 'trigger' => $triggerData]), | |
| 447 | - 'created_at' => $queuedAt, | |
| 378 | + 'response_obj' => json_encode(['status' => 'queued']), | |
| 379 | + 'created_at' => current_time('mysql'), | |
| 448 | 380 | ] |
| 449 | 381 | ); |
| 450 | - if (\is_int($queueuEntry)) { | |
| 451 | - // Safety net: the browser-fired bitforms_trigger_workflow request can be lost | |
| 452 | - // (navigation abort, WAF, network drop). This event re-runs the queued workflow; | |
| 453 | - // it no-ops if the browser trigger already claimed the run. | |
| 454 | - wp_schedule_single_event( | |
| 455 | - time() + 2 * MINUTE_IN_SECONDS, | |
| 456 | - 'bitforms_reclaim_workflow', | |
| 457 | - [(int) $entryID, (int) $triggerData['formID'], (int) $triggerData['logID'], $queueuEntry] | |
| 458 | - ); | |
| 459 | - if (!wp_next_scheduled('bitforms_reclaim_sweep')) { | |
| 460 | - wp_schedule_event(time() + HOUR_IN_SECONDS, 'hourly', 'bitforms_reclaim_sweep'); | |
| 461 | - } | |
| 462 | - } | |
| 463 | 382 | $responseData['cronNotOk'] = [ |
| 464 | 383 | $triggerData['entryID'], |
| 465 | 384 | $triggerData['logID'], |
| 466 | 385 | $queueuEntry, |
| 467 | - $triggerToken, // Add one-time token as 4th element | |
| 468 | 386 | ]; |
| 469 | 387 | } |
| 470 | 388 | return $responseData; |
| 471 | 389 | } |
| 472 | - | |
| 473 | - public static function repeaterFieldValueFormatter($fieldValues) | |
| 474 | - { | |
| 475 | - $formattedArray = []; | |
| 476 | - | |
| 477 | - foreach ($fieldValues as $key => $value) { | |
| 478 | - if (is_array($value)) { | |
| 479 | - foreach ($value as $index => $v) { | |
| 480 | - if (is_array($v)) { | |
| 481 | - foreach ($v as $k1 => $v1) { | |
| 482 | - $formattedArray[$k1][] = $v1; | |
| 483 | - } | |
| 484 | - } else { | |
| 485 | - $formattedArray[$key][] = $v; | |
| 486 | - } | |
| 487 | - } | |
| 488 | - } else { | |
| 489 | - $formattedArray[$key] = $value; | |
| 490 | - } | |
| 491 | - } | |
| 492 | - | |
| 493 | - return $formattedArray; | |
| 494 | - } | |
| 495 | - | |
| 496 | - /** | |
| 497 | - * | |
| 498 | - * @param mixed $fieldValues | |
| 499 | - * @param mixed $returnRepeaterValue default is array | |
| 500 | - * @param mixed $formID form ID required for string formatting | |
| 501 | - * @return array and repeater field value as array or string | |
| 502 | - */ | |
| 503 | - public static function formattedRepeaterValue($fieldValues, $returnRepeaterValue = '', $formID = null) | |
| 504 | - { | |
| 505 | - // get the repeater field and store it in an array | |
| 506 | - $repeaterField = []; | |
| 507 | - foreach ($fieldValues as $key => $value) { | |
| 508 | - if (!preg_match('/\./', $key)) { | |
| 509 | - continue; | |
| 510 | - } | |
| 511 | - $repeaterField[] = $key; | |
| 512 | - } | |
| 513 | - | |
| 514 | - // put the value of the child key to the parent repeater field\ | |
| 515 | - foreach ($repeaterField as $key) { | |
| 516 | - list($parentKey, $childKey) = explode('.', $key); | |
| 517 | - $repeatValues = isset($fieldValues[$parentKey]) ? $fieldValues[$parentKey] : $key; | |
| 518 | - | |
| 519 | - if (!is_array($repeatValues)) { | |
| 520 | - $fieldValues[$key] = $fieldValues[$childKey]; | |
| 521 | - continue; | |
| 522 | - } | |
| 523 | - | |
| 524 | - foreach ($repeatValues as $value) { | |
| 525 | - if (isset($value[$childKey])) { | |
| 526 | - if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) { | |
| 527 | - $fieldValues[$key] = []; | |
| 528 | - } | |
| 529 | - if (!empty($value[$childKey])) { | |
| 530 | - $fieldValues[$key][] = $value[$childKey]; | |
| 531 | - } | |
| 532 | - } | |
| 533 | - } | |
| 534 | - } | |
| 535 | - | |
| 536 | - // convert the array to string repeater field value | |
| 537 | - if ('string' === $returnRepeaterValue) { | |
| 538 | - $form = new FormManager($formID); | |
| 539 | - foreach ($repeaterField as $key) { | |
| 540 | - if (isset($fieldValues[$key]) && is_array($fieldValues[$key])) { | |
| 541 | - $fieldValues[$key] = implode(', ', self::flattenArray($fieldValues[$key])); | |
| 542 | - } | |
| 543 | - } | |
| 544 | - | |
| 545 | - foreach ($fieldValues as $key=>$value) { | |
| 546 | - if (!$form->isRepeaterField($key)) { | |
| 547 | - continue; | |
| 548 | - } | |
| 549 | - $repeaterValues = $fieldValues[$key]; | |
| 550 | - | |
| 551 | - $newFieldValues = self::constructRepeaterValue($repeaterValues, $formID); | |
| 552 | - | |
| 553 | - $fieldValues[$key] = is_array($newFieldValues) ? json_encode(array_values($newFieldValues), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $fieldValues[$key]; | |
| 554 | - } | |
| 555 | - } | |
| 556 | - | |
| 557 | - return $fieldValues; | |
| 558 | - } | |
| 559 | - | |
| 560 | - private static function flattenArray($array) | |
| 561 | - { | |
| 562 | - return array_map(function ($val) { | |
| 563 | - if (is_array($val)) { | |
| 564 | - return count($val) > 1 ? ('[' . implode(', ') . ']') : implode(', ', $val); | |
| 565 | - } else { | |
| 566 | - return $val; | |
| 567 | - } | |
| 568 | - }, $array); | |
| 569 | - } | |
| 570 | - | |
| 571 | - /** | |
| 572 | - * Replace file field value with web url | |
| 573 | - * @param mixed $fieldValues | |
| 574 | - * @param number $formId | |
| 575 | - * @param number $entryId | |
| 576 | - * @return mixed $fieldValues | |
| 577 | - */ | |
| 578 | - public static function handleFileUrl($fieldValues, $formId, $entryId) | |
| 579 | - { | |
| 580 | - if (!$formId || !$entryId) { | |
| 581 | - return $fieldValues; | |
| 582 | - } | |
| 583 | - $form = FormManager::getInstance($formId); | |
| 584 | - $formFields = $form->getFields(); | |
| 585 | - | |
| 586 | - $fldsIncludeFile = ['file-up', 'advanced-file-up', 'repeater', 'signature']; | |
| 587 | - foreach ($fieldValues as $key =>$value) { | |
| 588 | - $fldTyp = $formFields[$key]['type'] ?? null; | |
| 589 | - if (!in_array($fldTyp, $fldsIncludeFile)) { | |
| 590 | - continue; | |
| 591 | - } | |
| 592 | - | |
| 593 | - if ('repeater' === $fldTyp && is_array($value)) { | |
| 594 | - foreach ($value as $rIndex => $rValue) { | |
| 595 | - foreach ($rValue as $subKey=>$subValue) { | |
| 596 | - if (!in_array($formFields[$subKey]['type'], $fldsIncludeFile)) { | |
| 597 | - continue; | |
| 598 | - } | |
| 599 | - // for file fields inside repeater | |
| 600 | - if (is_array($subValue)) { | |
| 601 | - foreach ($subValue as $fileIndex=>$fileValue) { | |
| 602 | - $fieldValues[$key][$rIndex][$subKey][$fileIndex] = self::constructFileUrl($formId, $entryId, $fileValue); | |
| 603 | - } | |
| 604 | - } else { | |
| 605 | - // for signature field inside repeater; | |
| 606 | - $fieldValues[$key][$rIndex][$subKey] = self::constructFileUrl($formId, $entryId, $subValue); | |
| 607 | - } | |
| 608 | - } | |
| 609 | - } | |
| 610 | - } | |
| 611 | - | |
| 612 | - if (in_array($fldTyp, ['file-up', 'advanced-file-up', 'signature'], true)) { | |
| 613 | - if (is_array($value)) { | |
| 614 | - foreach ($value as $fldIndex=>$fldValue) { | |
| 615 | - $fieldValues[$key][$fldIndex] = self::constructFileUrl($formId, $entryId, $fldValue); | |
| 616 | - } | |
| 617 | - } else { | |
| 618 | - $fieldValues[$key] = self::constructFileUrl($formId, $entryId, $value); | |
| 619 | - } | |
| 620 | - } | |
| 621 | - } | |
| 622 | - | |
| 623 | - return $fieldValues; | |
| 624 | - } | |
| 625 | - | |
| 626 | - public static function assignRepeaterFieldValue($fieldValues, $formId) | |
| 627 | - { | |
| 628 | - if (!$formId) { | |
| 629 | - return $fieldValues; | |
| 630 | - } | |
| 631 | - $form = new FormManager($formId); | |
| 632 | - $formFields = $form->getFields(); | |
| 633 | - | |
| 634 | - foreach ($fieldValues as $key=>$value) { | |
| 635 | - if (!$form->isRepeaterField($key)) { | |
| 636 | - continue; | |
| 637 | - } | |
| 638 | - | |
| 639 | - if ('repeater' === $formFields[$key]['type']) { | |
| 640 | - if (is_array($value)) { | |
| 641 | - foreach ($value as $rIndex=>$rValue) { | |
| 642 | - foreach ($rValue as $subKey =>$subValue) { | |
| 643 | - $fieldValues[$subKey] = $subValue; | |
| 644 | - } | |
| 645 | - } | |
| 646 | - } | |
| 647 | - } | |
| 648 | - } | |
| 649 | - | |
| 650 | - return $fieldValues; | |
| 651 | - } | |
| 652 | - | |
| 653 | - public static function constructRepeaterValue($repeaterValues, $formId) | |
| 654 | - { | |
| 655 | - if (!$formId || !is_array($repeaterValues)) { | |
| 656 | - return $repeaterValues; | |
| 657 | - } | |
| 658 | - $form = FormManager::getInstance($formId); | |
| 659 | - $formFields = $form->getFields(); | |
| 660 | - $newRptrValue = []; | |
| 661 | - foreach ($repeaterValues as $key=>$value) { | |
| 662 | - $newRepeatedValue = []; | |
| 663 | - foreach ($value as $rKey =>$rValue) { | |
| 664 | - $fldLbl = isset($formFields[$rKey]) ? $formFields[$rKey]['label'] : $rKey; | |
| 665 | - $newRepeatedValue[$fldLbl] = $rValue; | |
| 666 | - } | |
| 667 | - $newRptrValue[$key] = $newRepeatedValue; | |
| 668 | - } | |
| 669 | - | |
| 670 | - return $newRptrValue; | |
| 671 | - } | |
| 672 | - | |
| 673 | - private static function constructFileUrl($formId, $entryId, $fileName) | |
| 674 | - { | |
| 675 | - $webPath = Helpers::getWebPathWithEncryptedEntryId($formId, $entryId); | |
| 676 | - | |
| 677 | - return rtrim($webPath, '/') . '/' . ltrim($fileName, '/'); | |
| 678 | - } | |
| 679 | - | |
| 680 | - // private static function constructFileUrl($formId, $entryId, $fileName) | |
| 681 | - // { | |
| 682 | - // $restBaseUrl = get_rest_url(); | |
| 683 | - // if (!$restBaseUrl) { | |
| 684 | - // return $fileName; | |
| 685 | - // } | |
| 686 | - // //http://bitlocal.local/wp-json/bitform/v1/bitform-file-download/?formID=1&entryID=16&fileID=bit-form-1005.pdf | |
| 687 | - // $queryParam = build_query([ | |
| 688 | - // 'formID' => $formId, | |
| 689 | - // 'entryID' => $entryId, | |
| 690 | - // 'fileID' => $fileName | |
| 691 | - // ]); | |
| 692 | - | |
| 693 | - // $path = $restBaseUrl . 'bitform' . '/' . 'v1' . '/' . 'bitform-file-download' . '/' . '?' . $queryParam; | |
| 694 | - // return rtrim($path, '/'); | |
| 695 | - // } | |
| 696 | 390 | } |