← All changes
|
backend/src/Factory/Notification/NotificationFactory.php
+115
-15
0.1.2
→
1.5
View file →
| @@ -1,16 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\Factory\Notification; |
| 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\Entity\Notification\Notification as NotificationEntity; |
| 12 | 7 | use IvyForms\ValueObjects\Notification\Notification; |
| 8 | +use IvyForms\ValueObjects\Notification\NotificationContact; | |
| 13 | 9 | |
| 14 | 10 | /** |
| 15 | 11 | * Class NotificationFactory |
| 16 | 12 | * |
| @@ -18,8 +14,27 @@ | ||
| 18 | 14 | */ |
| 19 | 15 | class NotificationFactory |
| 20 | 16 | { |
| 21 | 17 | /** |
| 18 | + * Lite-owned keys; everything else is treated as extension data (Pro / add-ons). | |
| 19 | + * | |
| 20 | + * @var string[] | |
| 21 | + */ | |
| 22 | + private const KNOWN_NOTIFICATION_DATA_KEYS = [ | |
| 23 | + 'id', | |
| 24 | + 'name', | |
| 25 | + 'sender', | |
| 26 | + 'replyTo', | |
| 27 | + 'receiver', | |
| 28 | + 'enabled', | |
| 29 | + 'subject', | |
| 30 | + 'message', | |
| 31 | + 'showEmptyFields', | |
| 32 | + 'smartLogic', | |
| 33 | + 'formId', | |
| 34 | + ]; | |
| 35 | + | |
| 36 | + /** | |
| 22 | 37 | * Create a NotificationEntity from an array of data. |
| 23 | 38 | * |
| 24 | 39 | * @param array<string, mixed> $data |
| 25 | 40 | * |
| @@ -27,17 +42,36 @@ | ||
| 27 | 42 | * @throws InvalidArgumentException |
| 28 | 43 | */ |
| 29 | 44 | public static function create(array $data): NotificationEntity |
| 30 | 45 | { |
| 46 | + /** | |
| 47 | + * Normalize/augment notification factory input (e.g. Pro attachment fields from DB). | |
| 48 | + * | |
| 49 | + * @since 1.2.0 | |
| 50 | + * | |
| 51 | + * @param array<string, mixed> $data | |
| 52 | + */ | |
| 53 | + $data = apply_filters('ivyforms/notification/factory_data', $data); | |
| 54 | + | |
| 55 | + $smartLogic = self::normalizeSmartLogic($data['smartLogic'] ?? false); | |
| 56 | + | |
| 57 | + $contact = new NotificationContact( | |
| 58 | + $data['sender'] ?? '', | |
| 59 | + $data['receiver'] ?? '', | |
| 60 | + $data['replyTo'] ?? '', | |
| 61 | + $data['cc'] ?? '', | |
| 62 | + $data['bcc'] ?? '' | |
| 63 | + ); | |
| 64 | + | |
| 31 | 65 | $notificationValueObject = new Notification( |
| 32 | 66 | $data['id'] ?? 0, |
| 33 | 67 | $data['name'] ?? '', |
| 34 | - $data['sender'] ?? '', | |
| 35 | - $data['receiver'] ?? '', | |
| 36 | - $data['status'] ?? false, | |
| 68 | + $contact, | |
| 69 | + $data['enabled'] ?? true, | |
| 37 | 70 | $data['subject'] ?? '', |
| 38 | 71 | $data['message'] ?? '', |
| 39 | - $data['smartLogic'] ?? false, | |
| 72 | + (bool) ($data['showEmptyFields'] ?? false), | |
| 73 | + $smartLogic, | |
| 40 | 74 | $data['formId'] ?? 0 |
| 41 | 75 | ); |
| 42 | 76 | |
| 43 | 77 | $notificationEntity = new NotificationEntity($notificationValueObject); |
| @@ -50,29 +84,45 @@ | ||
| 50 | 84 | $notificationEntity->setName($data['name']); |
| 51 | 85 | } |
| 52 | 86 | |
| 53 | 87 | if (isset($data['sender'])) { |
| 54 | - $notificationEntity->setSender($data['sender']); | |
| 88 | + $notificationEntity->getNotificationContact()->setSender($data['sender']); | |
| 55 | 89 | } |
| 56 | 90 | |
| 91 | + if (isset($data['replyTo'])) { | |
| 92 | + $notificationEntity->getNotificationContact()->setReplyTo($data['replyTo']); | |
| 93 | + } | |
| 94 | + | |
| 57 | 95 | if (isset($data['receiver'])) { |
| 58 | - $notificationEntity->setReceiver($data['receiver']); | |
| 96 | + $notificationEntity->getNotificationContact()->setReceiver($data['receiver']); | |
| 59 | 97 | } |
| 60 | 98 | |
| 61 | - if (isset($data['status'])) { | |
| 62 | - $notificationEntity->setStatus($data['status']); | |
| 99 | + if (isset($data['cc'])) { | |
| 100 | + $notificationEntity->getNotificationContact()->setCc($data['cc']); | |
| 63 | 101 | } |
| 64 | 102 | |
| 103 | + if (isset($data['bcc'])) { | |
| 104 | + $notificationEntity->getNotificationContact()->setBcc($data['bcc']); | |
| 105 | + } | |
| 106 | + | |
| 107 | + if (isset($data['enabled'])) { | |
| 108 | + $notificationEntity->setEnabled($data['enabled']); | |
| 109 | + } | |
| 110 | + | |
| 65 | 111 | if (isset($data['subject'])) { |
| 66 | 112 | $notificationEntity->setSubject($data['subject']); |
| 67 | 113 | } |
| 68 | 114 | |
| 69 | 115 | if (isset($data['message'])) { |
| 70 | - $notificationEntity->setMessage($data['message']); | |
| 116 | + $notificationEntity->setMessage(wp_unslash($data['message'])); | |
| 71 | 117 | } |
| 72 | 118 | |
| 119 | + if (isset($data['showEmptyFields'])) { | |
| 120 | + $notificationEntity->setShowEmptyFields((bool) $data['showEmptyFields']); | |
| 121 | + } | |
| 122 | + | |
| 73 | 123 | if (isset($data['smartLogic'])) { |
| 74 | - $notificationEntity->setSmartLogic($data['smartLogic']); | |
| 124 | + $notificationEntity->setSmartLogic($smartLogic); | |
| 75 | 125 | } |
| 76 | 126 | |
| 77 | 127 | if (isset($data['formId'])) { |
| 78 | 128 | $notificationEntity->setFormId($data['formId']); |
| @@ -77,7 +127,57 @@ | ||
| 77 | 127 | if (isset($data['formId'])) { |
| 78 | 128 | $notificationEntity->setFormId($data['formId']); |
| 79 | 129 | } |
| 80 | 130 | |
| 131 | + $notificationEntity->setExtensionData(self::extractExtensionData($data)); | |
| 132 | + | |
| 81 | 133 | return $notificationEntity; |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Keys in $data that are not owned by Lite. | |
| 138 | + * | |
| 139 | + * @param array<string, mixed> $data | |
| 140 | + * @return array<string, mixed> | |
| 141 | + */ | |
| 142 | + private static function extractExtensionData(array $data): array | |
| 143 | + { | |
| 144 | + return array_diff_key($data, array_flip(self::KNOWN_NOTIFICATION_DATA_KEYS)); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Normalize smartLogic from DB/API payload. | |
| 149 | + * | |
| 150 | + * @param mixed $smartLogic | |
| 151 | + * @return array<string, mixed> | |
| 152 | + */ | |
| 153 | + private static function normalizeSmartLogic($smartLogic): array | |
| 154 | + { | |
| 155 | + if (is_array($smartLogic)) { | |
| 156 | + $match = $smartLogic['match'] ?? 'any'; | |
| 157 | + | |
| 158 | + return [ | |
| 159 | + 'enabled' => (bool)($smartLogic['enabled'] ?? false), | |
| 160 | + 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any', | |
| 161 | + 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [], | |
| 162 | + ]; | |
| 163 | + } | |
| 164 | + | |
| 165 | + if (is_string($smartLogic)) { | |
| 166 | + $decoded = json_decode($smartLogic, true); | |
| 167 | + if (is_array($decoded)) { | |
| 168 | + return self::normalizeSmartLogic($decoded); | |
| 169 | + } | |
| 170 | + return [ | |
| 171 | + 'enabled' => in_array(strtolower($smartLogic), ['1', 'true'], true), | |
| 172 | + 'match' => 'any', | |
| 173 | + 'rules' => [], | |
| 174 | + ]; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return [ | |
| 178 | + 'enabled' => (bool)$smartLogic, | |
| 179 | + 'match' => 'any', | |
| 180 | + 'rules' => [], | |
| 181 | + ]; | |
| 82 | 182 | } |
| 83 | 183 | } |