| @@ -6,8 +6,16 @@ | ||
| 6 | 6 | use BitCode\BitForm\Core\Form\FormManager; |
| 7 | 7 | |
| 8 | 8 | final class FieldValueHandler |
| 9 | 9 | { |
| 10 | + /** | |
| 11 | + * @param mixed $stringToReplaceField | |
| 12 | + * @param mixed $fieldValues | |
| 13 | + * @param mixed $formID | |
| 14 | + * @param bool $stripShortcodesFromValues | |
| 15 | + * | |
| 16 | + * @return string | |
| 17 | + */ | |
| 10 | 18 | public static function replaceFieldWithValue($stringToReplaceField, $fieldValues, $formID = null, $stripShortcodesFromValues = false) |
| 11 | 19 | { |
| 12 | 20 | if (empty($stringToReplaceField)) { |
| 13 | 21 | return $stringToReplaceField; |
| @@ -16,8 +24,12 @@ | ||
| 16 | 24 | $stringToReplaceField = wp_json_encode($stringToReplaceField); |
| 17 | 25 | } |
| 18 | 26 | $fieldValues = $formID ? self::sortValueBasedOnLayout($formID, $fieldValues) : $fieldValues; |
| 19 | 27 | |
| 28 | + // Must run on the raw template: after substitution an empty field and an empty template | |
| 29 | + // are the same empty string. | |
| 30 | + $stringToReplaceField = self::resolveConditionalBlocks($stringToReplaceField, $fieldValues, $formID); | |
| 31 | + | |
| 20 | 32 | if ($formID) { |
| 21 | 33 | $stringToReplaceField = self::replaceValueOfBf_all_data($stringToReplaceField, $fieldValues, $formID); |
| 22 | 34 | $stringToReplaceField = self::replaceRepeaterFieldValue($stringToReplaceField, $fieldValues, $formID); |
| 23 | 35 | } |
| @@ -202,8 +214,318 @@ | ||
| 202 | 214 | } |
| 203 | 215 | return false; |
| 204 | 216 | } |
| 205 | 217 | |
| 218 | + /** | |
| 219 | + * Whether a resolved value renders as nothing. | |
| 220 | + * | |
| 221 | + * Stricter than isEmpty(): whitespace-only is blank (some smart-tag resolvers return a | |
| 222 | + * single space), so is an all-blank array. `0` / `'0'` never are. | |
| 223 | + * | |
| 224 | + * @param mixed $val | |
| 225 | + * | |
| 226 | + * @return bool | |
| 227 | + */ | |
| 228 | + public static function isBlank($val) | |
| 229 | + { | |
| 230 | + if (null === $val || false === $val) { | |
| 231 | + return true; | |
| 232 | + } | |
| 233 | + if (is_object($val)) { | |
| 234 | + $val = (array) $val; | |
| 235 | + } | |
| 236 | + if (is_array($val)) { | |
| 237 | + foreach ($val as $key => $item) { | |
| 238 | + // Composite meta sub-values (_latitude, …) never render on their own. | |
| 239 | + if (is_string($key) && 0 === strpos($key, '_')) { | |
| 240 | + continue; | |
| 241 | + } | |
| 242 | + if (!self::isBlank($item)) { | |
| 243 | + return false; | |
| 244 | + } | |
| 245 | + } | |
| 246 | + return true; | |
| 247 | + } | |
| 248 | + if (!is_scalar($val)) { | |
| 249 | + return true; | |
| 250 | + } | |
| 251 | + | |
| 252 | + return '' === trim(str_replace("\xc2\xa0", '', (string) $val)); | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Resolve `${bf_if:…}` … `${bf_endif}` template blocks. | |
| 257 | + * | |
| 258 | + * Syntax, operators and traps: docs/template-conditional-blocks.md. | |
| 259 | + * | |
| 260 | + * @param string $content | |
| 261 | + * @param array $fieldValues | |
| 262 | + * @param mixed $formID needed to reach repeater rows; without it a child key reads blank | |
| 263 | + * | |
| 264 | + * @return string | |
| 265 | + */ | |
| 266 | + private static function resolveConditionalBlocks($content, $fieldValues, $formID = null) | |
| 267 | + { | |
| 268 | + if (false === strpos($content, '${bf_if') && false === strpos($content, '${bf_unless')) { | |
| 269 | + return self::stripConditionalBlockTags($content); | |
| 270 | + } | |
| 271 | + | |
| 272 | + $conditionValues = $fieldValues; | |
| 273 | + if ($formID) { | |
| 274 | + $formManager = FormManager::getInstance($formID); | |
| 275 | + // Repeater children have no top-level key; flatten the rows in, real values still win. | |
| 276 | + $conditionValues = array_merge(self::restructureRepeaterData($fieldValues, $formManager), $fieldValues); | |
| 277 | + } | |
| 278 | + | |
| 279 | + // Matches a block whose body holds no further opener, i.e. the innermost one. | |
| 280 | + $innerMost = '/\$\{bf_(if|if_any|if_all|unless):([^{}$]*)\}((?:(?!\$\{bf_(?:if|if_any|if_all|unless):)[\s\S])*?)\$\{bf_endif\}/'; | |
| 281 | + | |
| 282 | + // Bounded so a malformed template can never spin here. | |
| 283 | + for ($pass = 0; $pass < 200; $pass++) { | |
| 284 | + $resolved = preg_replace_callback($innerMost, function ($matches) use ($conditionValues) { | |
| 285 | + $branches = preg_split('/\$\{bf_else\}/', $matches[3], 2); | |
| 286 | + $truthy = isset($branches[0]) ? $branches[0] : ''; | |
| 287 | + $falsy = isset($branches[1]) ? $branches[1] : ''; | |
| 288 | + | |
| 289 | + return self::evaluateBlockCondition($matches[1], $matches[2], $conditionValues) ? $truthy : $falsy; | |
| 290 | + }, $content, -1, $replacedCount); | |
| 291 | + | |
| 292 | + if (null === $resolved) { | |
| 293 | + break; // preg failure (e.g. backtrack limit): leave the content untouched | |
| 294 | + } | |
| 295 | + $content = $resolved; | |
| 296 | + if (!$replacedCount) { | |
| 297 | + break; | |
| 298 | + } | |
| 299 | + } | |
| 300 | + | |
| 301 | + return self::stripConditionalBlockTags($content); | |
| 302 | + } | |
| 303 | + | |
| 304 | + /** | |
| 305 | + * @param string $type if|if_any|if_all|unless | |
| 306 | + * @param string $rawKeys comma separated conditions | |
| 307 | + * @param array $fieldValues | |
| 308 | + * | |
| 309 | + * @return bool | |
| 310 | + */ | |
| 311 | + private static function evaluateBlockCondition($type, $rawKeys, $fieldValues) | |
| 312 | + { | |
| 313 | + $conditions = array_filter(array_map('trim', explode(',', (string) $rawKeys)), function ($condition) { | |
| 314 | + return '' !== $condition; | |
| 315 | + }); | |
| 316 | + if (empty($conditions)) { | |
| 317 | + return false; | |
| 318 | + } | |
| 319 | + | |
| 320 | + $results = []; | |
| 321 | + foreach ($conditions as $condition) { | |
| 322 | + $results[] = self::conditionHolds($condition, $fieldValues); | |
| 323 | + } | |
| 324 | + | |
| 325 | + if ('if_all' === $type) { | |
| 326 | + return !in_array(false, $results, true); | |
| 327 | + } | |
| 328 | + if ('unless' === $type) { | |
| 329 | + return !in_array(true, $results, true); | |
| 330 | + } | |
| 331 | + | |
| 332 | + return in_array(true, $results, true); | |
| 333 | + } | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * `key`, or `key operator value`. | |
| 337 | + * | |
| 338 | + * @param string $condition | |
| 339 | + * @param array $fieldValues | |
| 340 | + * | |
| 341 | + * @return bool | |
| 342 | + */ | |
| 343 | + private static function conditionHolds($condition, $fieldValues) | |
| 344 | + { | |
| 345 | + $operators = self::blockOperators(); | |
| 346 | + // Longest name first, or `not_equal` reads as `equal`. Field keys never contain a space. | |
| 347 | + $pattern = '/^(\S+)\s+(' . implode('|', $operators) . ')(?:\s+([\s\S]*))?$/'; | |
| 348 | + | |
| 349 | + if (!preg_match($pattern, trim($condition), $parts)) { | |
| 350 | + return !self::isBlank(self::conditionValue(trim($condition), $fieldValues)); | |
| 351 | + } | |
| 352 | + | |
| 353 | + $value = self::conditionValue($parts[1], $fieldValues); | |
| 354 | + $operator = $parts[2]; | |
| 355 | + $expected = isset($parts[3]) ? trim($parts[3]) : ''; | |
| 356 | + | |
| 357 | + if ('null' === $operator) { | |
| 358 | + return self::isBlank($value); | |
| 359 | + } | |
| 360 | + if ('not_null' === $operator) { | |
| 361 | + return !self::isBlank($value); | |
| 362 | + } | |
| 363 | + | |
| 364 | + // Multi-value fields and repeater children arrive as a list. | |
| 365 | + $candidates = is_array($value) || is_object($value) ? self::stripMetaSubfields((array) $value) : [$value]; | |
| 366 | + $negated = in_array($operator, ['not_equal', 'not_contain'], true); | |
| 367 | + foreach ($candidates as $candidate) { | |
| 368 | + if (is_array($candidate) || is_object($candidate)) { | |
| 369 | + continue; | |
| 370 | + } | |
| 371 | + // compareValue answers the positive form, so one match settles either case: it satisfies | |
| 372 | + // `contain` and rules out `not_contain`. | |
| 373 | + if (self::compareValue($operator, (string) $candidate, $expected)) { | |
| 374 | + return !$negated; | |
| 375 | + } | |
| 376 | + } | |
| 377 | + | |
| 378 | + return $negated; | |
| 379 | + } | |
| 380 | + | |
| 381 | + /** | |
| 382 | + * @return string[] operator names, longest first | |
| 383 | + */ | |
| 384 | + private static function blockOperators() | |
| 385 | + { | |
| 386 | + return [ | |
| 387 | + 'greater_or_equal', | |
| 388 | + 'less_or_equal', | |
| 389 | + 'not_contain', | |
| 390 | + 'start_with', | |
| 391 | + 'not_equal', | |
| 392 | + 'not_null', | |
| 393 | + 'end_with', | |
| 394 | + 'contain', | |
| 395 | + 'greater', | |
| 396 | + 'equal', | |
| 397 | + 'less', | |
| 398 | + 'null', | |
| 399 | + ]; | |
| 400 | + } | |
| 401 | + | |
| 402 | + /** | |
| 403 | + * @param string $key field key, or a `_bf_*` smart tag | |
| 404 | + * @param array $fieldValues | |
| 405 | + * | |
| 406 | + * @return mixed | |
| 407 | + */ | |
| 408 | + private static function conditionValue($key, $fieldValues) | |
| 409 | + { | |
| 410 | + if (0 === strpos($key, '_')) { | |
| 411 | + return SmartTags::getSmartTagValue($key, false, ''); | |
| 412 | + } | |
| 413 | + $value = isset($fieldValues[$key]) ? $fieldValues[$key] : null; | |
| 414 | + if (is_array($value) && isset($value['value'])) { | |
| 415 | + $value = $value['value']; | |
| 416 | + } | |
| 417 | + | |
| 418 | + return $value; | |
| 419 | + } | |
| 420 | + | |
| 421 | + /** | |
| 422 | + * @param string $operator | |
| 423 | + * @param string $value the submitted value | |
| 424 | + * @param string $expected the value written in the template | |
| 425 | + * | |
| 426 | + * @return bool | |
| 427 | + */ | |
| 428 | + private static function compareValue($operator, $value, $expected) | |
| 429 | + { | |
| 430 | + switch ($operator) { | |
| 431 | + case 'equal': | |
| 432 | + case 'not_equal': | |
| 433 | + return 0 === strcasecmp(trim($value), $expected); | |
| 434 | + case 'contain': | |
| 435 | + case 'not_contain': | |
| 436 | + return '' !== $expected && false !== stripos($value, $expected); | |
| 437 | + case 'start_with': | |
| 438 | + return '' !== $expected && 0 === stripos($value, $expected); | |
| 439 | + case 'end_with': | |
| 440 | + return '' !== $expected && 0 === strcasecmp($expected, (string) substr($value, -strlen($expected))); | |
| 441 | + case 'greater': | |
| 442 | + return self::isNumericPair($value, $expected) && (float) $value > (float) $expected; | |
| 443 | + case 'less': | |
| 444 | + return self::isNumericPair($value, $expected) && (float) $value < (float) $expected; | |
| 445 | + case 'greater_or_equal': | |
| 446 | + return self::isNumericPair($value, $expected) && (float) $value >= (float) $expected; | |
| 447 | + case 'less_or_equal': | |
| 448 | + return self::isNumericPair($value, $expected) && (float) $value <= (float) $expected; | |
| 449 | + default: | |
| 450 | + return false; | |
| 451 | + } | |
| 452 | + } | |
| 453 | + | |
| 454 | + /** | |
| 455 | + * @param string $value | |
| 456 | + * @param string $expected | |
| 457 | + * | |
| 458 | + * @return bool both sides compare as numbers | |
| 459 | + */ | |
| 460 | + private static function isNumericPair($value, $expected) | |
| 461 | + { | |
| 462 | + return is_numeric(trim($value)) && is_numeric($expected); | |
| 463 | + } | |
| 464 | + | |
| 465 | + /** | |
| 466 | + * Drop leftover block tags so an unbalanced template never leaks them into the output. | |
| 467 | + * | |
| 468 | + * @param string $content | |
| 469 | + * | |
| 470 | + * @return string | |
| 471 | + */ | |
| 472 | + private static function stripConditionalBlockTags($content) | |
| 473 | + { | |
| 474 | + return preg_replace('/\$\{bf_(?:if|if_any|if_all|unless):[^{}$]*\}|\$\{bf_(?:else|endif)\}/', '', $content); | |
| 475 | + } | |
| 476 | + | |
| 477 | + /** | |
| 478 | + * Values a field kept on an entry edit, posted as `<fieldKey>_old` instead of resubmitted. | |
| 479 | + * | |
| 480 | + * @param mixed $postData submitted data, keyed by field key | |
| 481 | + * @param string $fieldKey | |
| 482 | + * | |
| 483 | + * @return array retained values, empty when the field kept nothing | |
| 484 | + */ | |
| 485 | + public static function retainedOldValues($postData, $fieldKey) | |
| 486 | + { | |
| 487 | + if (!is_array($postData) || !isset($postData[$fieldKey . '_old'])) { | |
| 488 | + return []; | |
| 489 | + } | |
| 490 | + return self::flattenOldValues($postData[$fieldKey . '_old']); | |
| 491 | + } | |
| 492 | + | |
| 493 | + private static function flattenOldValues($value) | |
| 494 | + { | |
| 495 | + if (is_object($value)) { | |
| 496 | + $value = (array) $value; | |
| 497 | + } | |
| 498 | + if (!is_array($value)) { | |
| 499 | + if (!is_string($value) && !is_numeric($value)) { | |
| 500 | + return []; | |
| 501 | + } | |
| 502 | + $value = trim((string) $value); | |
| 503 | + if ('' === $value) { | |
| 504 | + return []; | |
| 505 | + } | |
| 506 | + // A repeater posts one JSON list per row, so a list can arrive nested. | |
| 507 | + $decoded = json_decode($value, true); | |
| 508 | + if (!is_array($decoded)) { | |
| 509 | + $retained = []; | |
| 510 | + foreach (explode(',', $value) as $item) { | |
| 511 | + $item = trim($item); | |
| 512 | + if ('' !== $item) { | |
| 513 | + $retained[] = $item; | |
| 514 | + } | |
| 515 | + } | |
| 516 | + return $retained; | |
| 517 | + } | |
| 518 | + $value = $decoded; | |
| 519 | + } | |
| 520 | + | |
| 521 | + $retained = []; | |
| 522 | + foreach ($value as $item) { | |
| 523 | + $retained = array_merge($retained, self::flattenOldValues($item)); | |
| 524 | + } | |
| 525 | + return $retained; | |
| 526 | + } | |
| 527 | + | |
| 206 | 528 | public static function formatFieldValueForMail($fields, $fieldValues = []) |
| 207 | 529 | { |
| 208 | 530 | $formattedFldValues = $fieldValues; |
| 209 | 531 | $file_upload_types = Helpers::$file_upload_types; |
| @@ -373,13 +695,16 @@ | ||
| 373 | 695 | |
| 374 | 696 | public static function sortValueBasedOnLayout($formId, $fieldValues) |
| 375 | 697 | { |
| 376 | 698 | $formManager = FormManager::getInstance($formId); |
| 377 | - $layout = $formManager->getFormLayout(); | |
| 378 | 699 | $formLayout = $formManager->getFlatenFormLayout(); // returns all layouts (lg, md, sm) |
| 379 | - $fieldKeyOrderbasedOnLayout = array_map(function ($fld) { | |
| 380 | - return $fld->i; | |
| 381 | - }, $formLayout->lg); | |
| 700 | + // A form saved without a layout (or a minimal/legacy form_content) has no ->lg | |
| 701 | + $lgLayout = isset($formLayout->lg) ? (array) $formLayout->lg : []; | |
| 702 | + $fieldKeyOrderbasedOnLayout = array_filter(array_map(function ($fld) { | |
| 703 | + return isset($fld->i) ? $fld->i : null; | |
| 704 | + }, $lgLayout), function ($key) { | |
| 705 | + return !is_null($key); | |
| 706 | + }); | |
| 382 | 707 | $ordered = []; |
| 383 | 708 | |
| 384 | 709 | foreach ($fieldKeyOrderbasedOnLayout as $key) { |
| 385 | 710 | if (array_key_exists($key, $fieldValues)) { |
| @@ -406,18 +731,20 @@ | ||
| 406 | 731 | $formManager = FormManager::getInstance($formId); |
| 407 | 732 | $formFields = $formManager->getFields(); |
| 408 | 733 | $orderedFormFields = $formManager->getFieldsBasedOnLayout(); // ordered form fields based on layout(lg) order |
| 409 | 734 | foreach ($matchesArray as $match) { |
| 735 | + // Each tag binds from the untouched submitted values: reusing a filtered result would | |
| 736 | + // let the first tag in a template starve the second. | |
| 410 | 737 | switch ($match) { |
| 411 | 738 | case '${bf_all_data}': |
| 412 | - $fieldValues = self::bindFormData($orderedFormFields, $fieldValues, $formId); | |
| 413 | - $table = self::generateTable($fieldValues, $orderedFormFields); | |
| 739 | + $boundValues = self::bindFormData($orderedFormFields, $fieldValues, $formId); | |
| 740 | + $table = self::generateTable($boundValues, $orderedFormFields, $formId); | |
| 414 | 741 | $stringToReplaceField = str_replace('${bf_all_data}', $table, $stringToReplaceField); |
| 415 | 742 | break; |
| 416 | 743 | |
| 417 | 744 | case '${bf_all_data.onlyValues}': |
| 418 | - $fieldValues = self::bindFormData($orderedFormFields, $fieldValues, $formId, true); | |
| 419 | - $table = self::generateTable($fieldValues, $orderedFormFields); | |
| 745 | + $boundValues = self::bindFormData($orderedFormFields, $fieldValues, $formId, true); | |
| 746 | + $table = self::generateTable($boundValues, $orderedFormFields, $formId); | |
| 420 | 747 | $stringToReplaceField = str_replace('${bf_all_data.onlyValues}', $table, $stringToReplaceField); |
| 421 | 748 | break; |
| 422 | 749 | default: |
| 423 | 750 | Log::debug_log([ |
| @@ -520,10 +847,11 @@ | ||
| 520 | 847 | } |
| 521 | 848 | |
| 522 | 849 | // Skip processing for hidden or empty fields only when $isOnlyValues is true |
| 523 | 850 | if ($isOnlyValues) { |
| 524 | - // Check if the value is strictly an empty string or null, but allow 0 | |
| 525 | - if (!isset($formData[$key]) || '' === $formData[$key] || null === $formData[$key]) { | |
| 851 | + // Blank means empty string, null, or an array with nothing in it (unchecked | |
| 852 | + // checkbox group, file field with no upload). 0 is a real value. | |
| 853 | + if (!isset($formData[$key]) || self::isBlank($formData[$key])) { | |
| 526 | 854 | return $fieldNewData; |
| 527 | 855 | } |
| 528 | 856 | |
| 529 | 857 | if (isset($field['valid']['hide']) && $field['valid']['hide']) { |
| @@ -559,9 +887,9 @@ | ||
| 559 | 887 | return $fieldNewData; |
| 560 | 888 | }, []); |
| 561 | 889 | } |
| 562 | 890 | |
| 563 | - private static function generateTable($fieldValues, $formFields) | |
| 891 | + private static function generateTable($fieldValues, $formFields, $formId = null) | |
| 564 | 892 | { |
| 565 | 893 | if (empty($fieldValues)) { |
| 566 | 894 | Log::debug_log([ |
| 567 | 895 | 'status' => 'error', |
| @@ -587,10 +915,12 @@ | ||
| 587 | 915 | if (is_array($value)) { |
| 588 | 916 | if ('repeater' === $fieldType) { |
| 589 | 917 | $table .= "<table style='width: 100%; border-collapse: collapse;'>"; |
| 590 | 918 | |
| 919 | + $subKeys = self::repeaterColumnKeys($value, $fk, $formId); | |
| 920 | + | |
| 591 | 921 | $table .= '<tr>'; |
| 592 | - foreach (array_keys($value[0]) as $subKey) { | |
| 922 | + foreach ($subKeys as $subKey) { | |
| 593 | 923 | $subLabel = self::getLabel($formFields, $subKey) ?? $subKey; |
| 594 | 924 | $table .= "<th style='border: 1px solid #dddddd; padding: 8px; background-color: #f2f2f2;'>" . $subLabel . '</th>'; |
| 595 | 925 | } |
| 596 | 926 | $table .= '</tr>'; |
| @@ -595,10 +925,16 @@ | ||
| 595 | 925 | } |
| 596 | 926 | $table .= '</tr>'; |
| 597 | 927 | |
| 598 | 928 | foreach ($value as $row) { |
| 929 | + if (!is_array($row)) { | |
| 930 | + continue; | |
| 931 | + } | |
| 599 | 932 | $table .= '<tr>'; |
| 600 | - foreach ($row as $subKey => $subValue) { | |
| 933 | + // Walk the shared column list so a row missing a conditionally hidden | |
| 934 | + // sub-field still lines up with the header. | |
| 935 | + foreach ($subKeys as $subKey) { | |
| 936 | + $subValue = array_key_exists($subKey, $row) ? $row[$subKey] : ''; | |
| 601 | 937 | $subFieldType = self::getFldType($subKey, $formFields); |
| 602 | 938 | if (is_array($subValue)) { |
| 603 | 939 | if (self::isCompositeFieldType($subFieldType)) { |
| 604 | 940 | $subValue = self::joinCompositeFieldValue($subValue, $subFieldType); |
| @@ -643,12 +979,13 @@ | ||
| 643 | 979 | } |
| 644 | 980 | } elseif (self::isCompositeFieldType($fieldType)) { |
| 645 | 981 | $table .= self::joinCompositeFieldValue($value, $fieldType); |
| 646 | 982 | } elseif ('signature' === $fieldType) { |
| 647 | - if ('signature-failed.png' === $subValue) { | |
| 648 | - $table .= ''; | |
| 649 | - } else { | |
| 650 | - $table .= self::imgMarkup($value); | |
| 983 | + // A signature arrives here wrapped in a one-item list; the failed-capture | |
| 984 | + // placeholder renders nothing. | |
| 985 | + $signature = reset($value); | |
| 986 | + if (false !== $signature && 'signature-failed.png' !== $signature) { | |
| 987 | + $table .= self::imgMarkup($signature); | |
| 651 | 988 | } |
| 652 | 989 | } |
| 653 | 990 | } else { |
| 654 | 991 | $table .= $value; |
| @@ -673,16 +1010,32 @@ | ||
| 673 | 1010 | } |
| 674 | 1011 | |
| 675 | 1012 | private static function imgMarkup($filename) |
| 676 | 1013 | { |
| 677 | - return "<img src='{$filename}' alt='{$filename}' width='250'/>"; | |
| 1014 | + if (!is_scalar($filename)) { | |
| 1015 | + return ''; | |
| 1016 | + } | |
| 1017 | + $filename = (string) $filename; | |
| 1018 | + | |
| 1019 | + return "<img src='" . self::escFileHref($filename) . "' alt='" . esc_attr($filename) . "' width='250'/>"; | |
| 678 | 1020 | } |
| 679 | 1021 | |
| 680 | 1022 | private static function anchorMarkup($filename) |
| 681 | 1023 | { |
| 682 | - return "<a href='{$filename}' rel='noopener noreferrer' target='_blank' style='color:blue'>{$filename}</a>"; | |
| 1024 | + if (!is_scalar($filename)) { | |
| 1025 | + return ''; | |
| 1026 | + } | |
| 1027 | + $filename = (string) $filename; | |
| 1028 | + | |
| 1029 | + return "<a href='" . self::escFileHref($filename) . "' rel='noopener noreferrer' target='_blank' style='color:blue'>" . esc_html($filename) . '</a>'; | |
| 683 | 1030 | } |
| 684 | 1031 | |
| 1032 | + /** Escape a file reference for an href/src. Not esc_url(): it rewrites a bare file name to `http://<name>`. */ | |
| 1033 | + private static function escFileHref($value) | |
| 1034 | + { | |
| 1035 | + return esc_attr(wp_kses_bad_protocol($value, wp_allowed_protocols())); | |
| 1036 | + } | |
| 1037 | + | |
| 685 | 1038 | public static function replaceRepeaterFieldValue($stringToReplaceField, $fieldValues, $formID) |
| 686 | 1039 | { |
| 687 | 1040 | if (!is_string($stringToReplaceField) || empty($stringToReplaceField)) { |
| 688 | 1041 | return $stringToReplaceField; // Return as-is if nothing to replace |
| @@ -704,9 +1057,9 @@ | ||
| 704 | 1057 | foreach ($matches[1] as $fk) { |
| 705 | 1058 | $repeaterFieldKey = $fk; |
| 706 | 1059 | $fieldType = isset($formFields[$repeaterFieldKey]['type']) && !empty($formFields[$repeaterFieldKey]['type']) ? $formFields[$repeaterFieldKey]['type'] : null; |
| 707 | 1060 | if ('repeater' === $fieldType) { |
| 708 | - $repeaterMarkup = self::repeaterFieldTable($fieldValues[$repeaterFieldKey] ?? [], $formFields, $repeaterFieldKey); | |
| 1061 | + $repeaterMarkup = self::repeaterFieldTable($fieldValues[$repeaterFieldKey] ?? [], $formFields, $repeaterFieldKey, $formID); | |
| 709 | 1062 | $stringToReplaceField = str_replace('${' . $fk . '}', $repeaterMarkup, $stringToReplaceField); |
| 710 | 1063 | } else { |
| 711 | 1064 | if ('signature' === $fieldType) { |
| 712 | 1065 | $stringToReplaceField = self::replaceImgTagForRepeatedSignature($stringToReplaceField, $flatFieldData[$repeaterFieldKey], $repeaterFieldKey); |
| @@ -1008,10 +1361,54 @@ | ||
| 1008 | 1361 | return true; |
| 1009 | 1362 | } |
| 1010 | 1363 | } |
| 1011 | 1364 | |
| 1012 | - private static function repeaterFieldTable($repeaterFieldData, $formFields, $repeaterFieldKey) | |
| 1365 | + /** | |
| 1366 | + * Collect the column keys of a repeater table as the union of every row's keys, | |
| 1367 | + * not just the first row's. Conditional logic can hide a sub-field in one row and | |
| 1368 | + * show it in the next; keying off row 0 alone drops that column's header and | |
| 1369 | + * shifts every later row's cells. Ordering follows the repeater's own nested | |
| 1370 | + * layout when the form id is known, with any leftover keys appended. | |
| 1371 | + * | |
| 1372 | + * @param array $rows | |
| 1373 | + * @param string $repeaterFieldKey | |
| 1374 | + * @param int|string|null $formId | |
| 1375 | + * @return array | |
| 1376 | + */ | |
| 1377 | + private static function repeaterColumnKeys($rows, $repeaterFieldKey, $formId = null) | |
| 1013 | 1378 | { |
| 1379 | + $present = []; | |
| 1380 | + foreach ($rows as $row) { | |
| 1381 | + if (!is_array($row)) { | |
| 1382 | + continue; | |
| 1383 | + } | |
| 1384 | + foreach (array_keys($row) as $subKey) { | |
| 1385 | + $present[$subKey] = true; | |
| 1386 | + } | |
| 1387 | + } | |
| 1388 | + | |
| 1389 | + if (empty($present)) { | |
| 1390 | + return []; | |
| 1391 | + } | |
| 1392 | + | |
| 1393 | + $ordered = []; | |
| 1394 | + if ($formId) { | |
| 1395 | + $nestedLayout = FormManager::getInstance($formId)->getFormNestedLayout(); | |
| 1396 | + $repeaterLayout = isset($nestedLayout->{$repeaterFieldKey}->lg) ? $nestedLayout->{$repeaterFieldKey}->lg : []; | |
| 1397 | + foreach ((array) $repeaterLayout as $fld) { | |
| 1398 | + $subKey = isset($fld->i) ? $fld->i : null; | |
| 1399 | + if ($subKey && isset($present[$subKey])) { | |
| 1400 | + $ordered[] = $subKey; | |
| 1401 | + unset($present[$subKey]); | |
| 1402 | + } | |
| 1403 | + } | |
| 1404 | + } | |
| 1405 | + | |
| 1406 | + return array_merge($ordered, array_keys($present)); | |
| 1407 | + } | |
| 1408 | + | |
| 1409 | + private static function repeaterFieldTable($repeaterFieldData, $formFields, $repeaterFieldKey, $formId = null) | |
| 1410 | + { | |
| 1014 | 1411 | $repeaterFieldData = self::decodeIfJson($repeaterFieldData); |
| 1015 | 1412 | |
| 1016 | 1413 | if (!is_array($repeaterFieldData) || !isset($repeaterFieldData[0]) || !is_array($repeaterFieldData[0])) { |
| 1017 | 1414 | return ''; // Safely return empty if not a valid repeater structure |
| @@ -1022,9 +1419,9 @@ | ||
| 1022 | 1419 | // $table .= '</tr>'; |
| 1023 | 1420 | // $table .= '<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">'; |
| 1024 | 1421 | // $table .= '<table style="width: 100%; border-collapse: collapse;">'; |
| 1025 | 1422 | |
| 1026 | - $headers = array_keys($repeaterFieldData[0]); | |
| 1423 | + $headers = self::repeaterColumnKeys($repeaterFieldData, $repeaterFieldKey, $formId); | |
| 1027 | 1424 | $table .= '<tr>'; // open tr (for column header) |
| 1028 | 1425 | foreach ($headers as $fk) { |
| 1029 | 1426 | $table .= '<th style="border: 1px solid #dddddd; padding: 8px; ">' . self::getLabel($formFields, $fk) . '</th>'; |
| 1030 | 1427 | } |
| @@ -1030,10 +1427,17 @@ | ||
| 1030 | 1427 | } |
| 1031 | 1428 | $table .= '</tr>'; // close tr (for column header) |
| 1032 | 1429 | |
| 1033 | 1430 | foreach ($repeaterFieldData as $row) { |
| 1431 | + if (!is_array($row)) { | |
| 1432 | + continue; | |
| 1433 | + } | |
| 1034 | 1434 | $table .= '<tr>'; // open tr (for table data row) |
| 1035 | - foreach ($row as $k=>$value) { | |
| 1435 | + // Walk the column list, not the row's own keys, so a sub-field hidden by | |
| 1436 | + // conditional logic in this row renders an empty cell instead of shifting | |
| 1437 | + // every following cell one column to the left. | |
| 1438 | + foreach ($headers as $k) { | |
| 1439 | + $value = array_key_exists($k, $row) ? $row[$k] : ''; | |
| 1036 | 1440 | $fldTyp = self::getFldType($k, $formFields); |
| 1037 | 1441 | if (is_array($value)) { |
| 1038 | 1442 | if (in_array($fldTyp, ['advanced-file-up', 'file-up'])) { |
| 1039 | 1443 | $newValue = self::unorderedAnchorListMarkup($value); |