| @@ -1,100 +1,41 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Common\Helpers; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -use IvyForms\Common\Exceptions\ValidationException; | |
| 7 | -use IvyForms\Entity\FieldOptions\FieldOptions; | |
| 8 | -use IvyForms\Factory\FieldOptions\FieldOptionsFactory; | |
| 9 | - | |
| 10 | -if (!defined('ABSPATH')) { | |
| 11 | - exit; // Exit if accessed directly | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * General helper utilities related to Field processing. | |
| 16 | - */ | |
| 17 | 5 | class Helper |
| 18 | 6 | { |
| 19 | 7 | /** |
| 20 | - * Resolve parentId using fieldIndex -> parentId map if needed. | |
| 8 | + * Parse JSON string or array data to array | |
| 21 | 9 | * |
| 22 | - * @param array<string,mixed> $data Passed by reference; parentId modified in place. | |
| 23 | - * @param array<int,int> $parentMap Map of fieldIndex => newly created parent field id. | |
| 10 | + * @param mixed $data | |
| 11 | + * | |
| 12 | + * @return array<string, mixed> | |
| 24 | 13 | */ |
| 25 | - public static function resolveParentId(array &$data, array $parentMap): void | |
| 14 | + public static function parseToArray($data): array | |
| 26 | 15 | { |
| 27 | - if (!array_key_exists('parentId', $data)) { | |
| 28 | - return; | |
| 16 | + $parsedData = []; | |
| 17 | + if (!empty($data)) { | |
| 18 | + $parsedData = is_string($data) | |
| 19 | + ? json_decode($data, true) | |
| 20 | + : $data; | |
| 29 | 21 | } |
| 30 | - $pid = $data['parentId']; | |
| 31 | - $needsResolve = ($pid === 0 || $pid === '0' || $pid === null); | |
| 32 | - if ($needsResolve && isset($data['fieldIndex']) && isset($parentMap[$data['fieldIndex']])) { | |
| 33 | - $data['parentId'] = $parentMap[$data['fieldIndex']]; | |
| 34 | - } | |
| 22 | + return $parsedData; | |
| 35 | 23 | } |
| 36 | 24 | |
| 37 | 25 | /** |
| 38 | - * Partition field options into new, update, and submitted IDs. | |
| 26 | + * Convert page ID to page URL using WordPress get_permalink | |
| 39 | 27 | * |
| 40 | - * @param array<string, mixed> $fieldData | |
| 41 | - * @param array<int> $existingOptionIds | |
| 42 | - * @return array{ | |
| 43 | - * newOptions: array<int, FieldOptions>, | |
| 44 | - * updateOptions: array<int, FieldOptions>, | |
| 45 | - * submittedOptionIds: array<int, int> | |
| 46 | - * } | |
| 47 | - * @throws ValidationException | |
| 28 | + * @param string|int $pageId The page ID to convert | |
| 29 | + * | |
| 30 | + * @return string The page permalink URL, or empty string if not found | |
| 48 | 31 | */ |
| 49 | - public static function partitionFieldOptions(array $fieldData, array $existingOptionIds): array | |
| 32 | + public static function getPageUrlFromId($pageId): string | |
| 50 | 33 | { |
| 51 | - $newOptions = []; | |
| 52 | - $updateOptions = []; | |
| 53 | - $submittedOptionIds = []; | |
| 54 | - foreach ($fieldData['fieldOptions'] as $optionData) { | |
| 55 | - $option = FieldOptionsFactory::create($optionData); | |
| 56 | - if ( | |
| 57 | - isset($optionData['id']) && $optionData['id'] > 0 | |
| 58 | - && in_array($optionData['id'], $existingOptionIds, true) | |
| 59 | - ) { | |
| 60 | - $updateOptions[] = $option; | |
| 61 | - $submittedOptionIds[] = $optionData['id']; | |
| 62 | - continue; | |
| 63 | - } | |
| 64 | - $newOptions[] = $option; | |
| 34 | + if (empty($pageId)) { | |
| 35 | + return ''; | |
| 65 | 36 | } |
| 66 | - return [ | |
| 67 | - 'newOptions' => $newOptions, | |
| 68 | - 'updateOptions' => $updateOptions, | |
| 69 | - 'submittedOptionIds' => $submittedOptionIds, | |
| 70 | - ]; | |
| 71 | - } | |
| 72 | 37 | |
| 73 | - /** | |
| 74 | - * Batch process field options: add new, update existing, delete orphaned. | |
| 75 | - * | |
| 76 | - * @param int $fieldId | |
| 77 | - * @param array<string, mixed> $fieldData | |
| 78 | - * @param object $fieldOptionsService | |
| 79 | - * @param object $fieldOptionsRepository | |
| 80 | - * @return void | |
| 81 | - * @throws ValidationException | |
| 82 | - */ | |
| 83 | - public static function batchProcessFieldOptions( | |
| 84 | - int $fieldId, | |
| 85 | - array $fieldData, | |
| 86 | - object $fieldOptionsService, | |
| 87 | - object $fieldOptionsRepository | |
| 88 | - ): void { | |
| 89 | - $existingOptions = $fieldOptionsService->getByFieldId($fieldId); | |
| 90 | - $existingOptionIds = array_map(fn($opt) => $opt->getId(), $existingOptions); | |
| 91 | - $partition = self::partitionFieldOptions($fieldData, $existingOptionIds); | |
| 92 | - $newOptionIds = $fieldOptionsService->addOptions($partition['newOptions']); | |
| 93 | - $fieldOptionsService->updateOptions($partition['updateOptions']); | |
| 94 | - $submittedOptionIds = array_merge($partition['submittedOptionIds'], $newOptionIds); | |
| 95 | - $orphanedOptionIds = array_diff($existingOptionIds, $submittedOptionIds); | |
| 96 | - if (!empty($orphanedOptionIds)) { | |
| 97 | - $fieldOptionsRepository->deleteByOptionIds($orphanedOptionIds); | |
| 98 | - } | |
| 38 | + $pagePermalink = get_permalink((int) $pageId); | |
| 39 | + return $pagePermalink ? $pagePermalink : ''; | |
| 99 | 40 | } |
| 100 | 41 | } |