| @@ -218,11 +218,26 @@ | ||
| 218 | 218 | preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 219 | 219 | $uniqueFieldsInStr = array_unique($matchedField[0]); |
| 220 | 220 | foreach ($uniqueFieldsInStr as $key => $value) { |
| 221 | 221 | $fieldName = substr($value, 2, strlen($value) - 3); |
| 222 | + | |
| 223 | + $repeaterArr = explode('.', $fieldName); | |
| 224 | + | |
| 222 | 225 | if (isset($fieldValues[$fieldName])) { |
| 223 | - $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) : | |
| 224 | - wp_json_encode($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; | |
| 225 | 240 | } |
| 226 | 241 | } |
| 227 | 242 | return $stringToReplaceField; |
| 228 | 243 | } |
| @@ -393,6 +408,79 @@ | ||
| 393 | 408 | $queueuEntry, |
| 394 | 409 | ]; |
| 395 | 410 | } |
| 396 | 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; | |
| 397 | 485 | } |
| 398 | 486 | } |