PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Infrastructure / WP / REST / SubmissionMultipartReader.php

SubmissionMultipartReader.php in The Innovative Form Builder – IvyForms 1.5, at backend/src/Infrastructure/WP/REST/SubmissionMultipartReader.php

96 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 * @licence See COPYING.md for license details.
6 */
7
8 declare(strict_types=1);
9
10 namespace IvyForms\Infrastructure\WP\REST;
11
12 use IvyForms\Common\Exceptions\ValidationException;
13 use IvyForms\Services\Translations\BackendStrings;
14 use WP_REST_Request;
15
16 /**
17 * Reads multipart file params and decodes the ivyforms_fm upload map.
18 */
19 final class SubmissionMultipartReader
20 {
21 /**
22 * @param WP_REST_Request<array<string, mixed>> $request
23 *
24 * @return array<string, mixed>
25 */
26 public static function resolveUploadedFiles(WP_REST_Request $request): array
27 {
28 return $request->get_file_params();
29 }
30
31 /**
32 * @return array<string, string>
33 */
34 public static function decodeFileParamMap(string $mapRaw): array
35 {
36 /** @var array<string, mixed> $decodedMap */
37 $decodedMap = json_decode(wp_unslash($mapRaw), true);
38 if (!is_array($decodedMap)) {
39 throw new ValidationException(
40 BackendStrings::getExceptionStrings()['invalid_request_data']
41 );
42 }
43
44 $normalized = [];
45 foreach ($decodedMap as $paramName => $logicalKey) {
46 if (is_string($paramName) && is_string($logicalKey)) {
47 $normalized[$paramName] = $logicalKey;
48 }
49 }
50
51 return $normalized;
52 }
53
54 /**
55 * @param array<string, string> $decodedMap
56 */
57 public static function resolveLogicalUploadKey(string $paramName, array $decodedMap): ?string
58 {
59 if (strpos($paramName, 'ivyforms_fu_') !== 0 || !isset($decodedMap[$paramName])) {
60 return null;
61 }
62 $logicalKey = $decodedMap[$paramName];
63
64 return strpos($logicalKey, 'file-upload_') === 0 ? $logicalKey : null;
65 }
66
67 /**
68 * @param array<string, mixed> $filesLookup
69 * @param array<string, string> $decodedMap
70 *
71 * @return array<string, int>
72 */
73 public static function groupUploadCountsByLogicalKey(array $filesLookup, array $decodedMap): array
74 {
75 /** @var array<string, int> $counts */
76 $counts = [];
77
78 foreach ($filesLookup as $paramName => $fileStruct) {
79 $logicalKey = self::resolveLogicalUploadKey((string) $paramName, $decodedMap);
80 if ($logicalKey === null || !is_array($fileStruct)) {
81 continue;
82 }
83 if (($fileStruct['error'] ?? \UPLOAD_ERR_NO_FILE) === \UPLOAD_ERR_NO_FILE) {
84 continue;
85 }
86
87 if (!isset($counts[$logicalKey])) {
88 $counts[$logicalKey] = 0;
89 }
90 $counts[$logicalKey]++;
91 }
92
93 return $counts;
94 }
95 }
96