$data * * @return NotificationEntity * @throws InvalidArgumentException */ public static function create(array $data): NotificationEntity { /** * Normalize/augment notification factory input (e.g. Pro attachment fields from DB). * * @since 1.2.0 * * @param array $data */ $data = apply_filters('ivyforms/notification/factory_data', $data); $smartLogic = self::normalizeSmartLogic($data['smartLogic'] ?? false); $contact = new NotificationContact( $data['sender'] ?? '', $data['receiver'] ?? '', $data['replyTo'] ?? '', $data['cc'] ?? '', $data['bcc'] ?? '' ); $notificationValueObject = new Notification( $data['id'] ?? 0, $data['name'] ?? '', $contact, $data['enabled'] ?? true, $data['subject'] ?? '', $data['message'] ?? '', (bool) ($data['showEmptyFields'] ?? false), $smartLogic, $data['formId'] ?? 0 ); $notificationEntity = new NotificationEntity($notificationValueObject); if (isset($data['id'])) { $notificationEntity->setId($data['id']); } if (isset($data['name'])) { $notificationEntity->setName($data['name']); } if (isset($data['sender'])) { $notificationEntity->getNotificationContact()->setSender($data['sender']); } if (isset($data['replyTo'])) { $notificationEntity->getNotificationContact()->setReplyTo($data['replyTo']); } if (isset($data['receiver'])) { $notificationEntity->getNotificationContact()->setReceiver($data['receiver']); } if (isset($data['cc'])) { $notificationEntity->getNotificationContact()->setCc($data['cc']); } if (isset($data['bcc'])) { $notificationEntity->getNotificationContact()->setBcc($data['bcc']); } if (isset($data['enabled'])) { $notificationEntity->setEnabled($data['enabled']); } if (isset($data['subject'])) { $notificationEntity->setSubject($data['subject']); } if (isset($data['message'])) { $notificationEntity->setMessage(wp_unslash($data['message'])); } if (isset($data['showEmptyFields'])) { $notificationEntity->setShowEmptyFields((bool) $data['showEmptyFields']); } if (isset($data['smartLogic'])) { $notificationEntity->setSmartLogic($smartLogic); } if (isset($data['formId'])) { $notificationEntity->setFormId($data['formId']); } $notificationEntity->setExtensionData(self::extractExtensionData($data)); return $notificationEntity; } /** * Keys in $data that are not owned by Lite. * * @param array $data * @return array */ private static function extractExtensionData(array $data): array { return array_diff_key($data, array_flip(self::KNOWN_NOTIFICATION_DATA_KEYS)); } /** * Normalize smartLogic from DB/API payload. * * @param mixed $smartLogic * @return array */ private static function normalizeSmartLogic($smartLogic): array { if (is_array($smartLogic)) { $match = $smartLogic['match'] ?? 'any'; return [ 'enabled' => (bool)($smartLogic['enabled'] ?? false), 'match' => in_array($match, ['any', 'all'], true) ? $match : 'any', 'rules' => is_array($smartLogic['rules'] ?? null) ? $smartLogic['rules'] : [], ]; } if (is_string($smartLogic)) { $decoded = json_decode($smartLogic, true); if (is_array($decoded)) { return self::normalizeSmartLogic($decoded); } return [ 'enabled' => in_array(strtolower($smartLogic), ['1', 'true'], true), 'match' => 'any', 'rules' => [], ]; } return [ 'enabled' => (bool)$smartLogic, 'match' => 'any', 'rules' => [], ]; } }