← All changes
|
backend/src/Factory/Confirmation/ConfirmationFactory.php
+63
-7
0.2
→
1.5
View file →
| @@ -1,16 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Factory\Confirmation; |
| 4 | 4 | |
| 5 | -// phpcs:disable PSR1.Files.SideEffects | |
| 6 | -if (!defined('ABSPATH')) { | |
| 7 | - exit; // Exit if accessed directly | |
| 8 | -} | |
| 9 | - | |
| 10 | 5 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 11 | 6 | use IvyForms\Common\Exceptions\ValidationException; |
| 12 | 7 | use IvyForms\Entity\Confirmation\Confirmation as ConfirmationEntity; |
| 8 | +use IvyForms\Services\Translations\BackendStrings; | |
| 13 | 9 | use IvyForms\ValueObjects\Confirmation\Confirmation; |
| 14 | 10 | |
| 15 | 11 | /** |
| 16 | 12 | * Class ConfirmationFactory |
| @@ -29,17 +25,23 @@ | ||
| 29 | 25 | * @throws ValidationException |
| 30 | 26 | */ |
| 31 | 27 | public static function create(array $data): ConfirmationEntity |
| 32 | 28 | { |
| 29 | + $smartLogic = self::normalizeSmartLogic($data['smartLogic'] ?? false); | |
| 30 | + | |
| 33 | 31 | $confirmationValueObject = new Confirmation( |
| 34 | 32 | $data['id'] ?? 0, |
| 35 | 33 | $data['formId'] ?? 0, |
| 34 | + $data['name'] ?? BackendStrings::getSettingsFormBuilderStrings()['default_confirmation_name'], | |
| 36 | 35 | $data['type'] ?? '', |
| 37 | 36 | $data['enabled'] ?? true, |
| 38 | 37 | $data['showForm'] ?? false, |
| 39 | 38 | $data['message'] ?? '', |
| 40 | 39 | $data['url'] ?? '', |
| 41 | - $data['page'] ?? '' | |
| 40 | + $data['page'] ?? '', | |
| 41 | + (int) ($data['position'] ?? 0), | |
| 42 | + (bool) ($data['isDefault'] ?? false), | |
| 43 | + $smartLogic | |
| 42 | 44 | ); |
| 43 | 45 | |
| 44 | 46 | $confirmationEntity = new ConfirmationEntity($confirmationValueObject); |
| 45 | 47 | |
| @@ -63,9 +65,9 @@ | ||
| 63 | 65 | $confirmationEntity->setShowForm($data['showForm']); |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | if (isset($data['message'])) { |
| 67 | - $confirmationEntity->setMessage($data['message']); | |
| 69 | + $confirmationEntity->setMessage(wp_unslash($data['message'])); | |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | if (isset($data['url'])) { |
| 71 | 73 | $confirmationEntity->setUrl($data['url']); |
| @@ -74,7 +76,61 @@ | ||
| 74 | 76 | if (isset($data['page'])) { |
| 75 | 77 | $confirmationEntity->setPage($data['page']); |
| 76 | 78 | } |
| 77 | 79 | |
| 80 | + if (isset($data['name'])) { | |
| 81 | + $confirmationEntity->setName($data['name']); | |
| 82 | + } | |
| 83 | + | |
| 84 | + if (isset($data['position'])) { | |
| 85 | + $confirmationEntity->setPosition((int) $data['position']); | |
| 86 | + } | |
| 87 | + | |
| 88 | + if (isset($data['isDefault'])) { | |
| 89 | + $confirmationEntity->setIsDefault((bool) $data['isDefault']); | |
| 90 | + } | |
| 91 | + | |
| 92 | + if (isset($data['smartLogic'])) { | |
| 93 | + $confirmationEntity->setSmartLogic($smartLogic); | |
| 94 | + } | |
| 95 | + | |
| 78 | 96 | return $confirmationEntity; |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Normalize smartLogic from DB/API payload. | |
| 101 | + * | |
| 102 | + * @param mixed $smartLogic | |
| 103 | + * @return array<string, mixed> | |
| 104 | + */ | |
| 105 | + private static function normalizeSmartLogic($smartLogic): array | |
| 106 | + { | |
| 107 | + if (is_array($smartLogic)) { | |
| 108 | + $match = $smartLogic['match'] ?? 'any'; | |
| 109 | + | |
| 110 | + return [ | |
| 111 | + 'enabled' => (bool)($smartLogic['enabled'] ?? false), | |
| 112 | + 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any', | |
| 113 | + 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [], | |
| 114 | + ]; | |
| 115 | + } | |
| 116 | + | |
| 117 | + if (is_string($smartLogic)) { | |
| 118 | + $decoded = json_decode($smartLogic, true); | |
| 119 | + if (is_array($decoded)) { | |
| 120 | + return self::normalizeSmartLogic($decoded); | |
| 121 | + } | |
| 122 | + | |
| 123 | + return [ | |
| 124 | + 'enabled' => in_array(strtolower($smartLogic), ['1', 'true'], true), | |
| 125 | + 'match' => 'any', | |
| 126 | + 'rules' => [], | |
| 127 | + ]; | |
| 128 | + } | |
| 129 | + | |
| 130 | + return [ | |
| 131 | + 'enabled' => (bool)$smartLogic, | |
| 132 | + 'match' => 'any', | |
| 133 | + 'rules' => [], | |
| 134 | + ]; | |
| 79 | 135 | } |
| 80 | 136 | } |