| @@ -1,13 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace IvyForms\ValueObjects\Entry; |
| 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\ValidationException; |
| 11 | 6 | use IvyForms\Common\Helpers\EntryHelper; |
| 12 | 7 | use IvyForms\Services\Translations\BackendStrings; |
| 13 | 8 | |
| @@ -81,9 +76,9 @@ | ||
| 81 | 76 | ?string $sourceURL |
| 82 | 77 | ) { |
| 83 | 78 | $this->id = $this->validateId($id); |
| 84 | 79 | $this->formId = $this->validateId($formId); |
| 85 | - $this->userId = $this->validateId($userId); | |
| 80 | + $this->userId = $this->validateUserId($userId); | |
| 86 | 81 | $this->dateCreated = gmdate('Y-m-d H:i:s'); |
| 87 | 82 | $this->dateEdited = gmdate('Y-m-d H:i:s'); |
| 88 | 83 | $this->status = $this->validateString($status, 45, 'status'); |
| 89 | 84 | $this->ipAddress = $this->validateString($ipAddress, 45, 'ipAddress'); |
| @@ -187,9 +182,25 @@ | ||
| 187 | 182 | public function isStarred(): bool |
| 188 | 183 | { |
| 189 | 184 | return $this->starred; |
| 190 | 185 | } |
| 186 | + | |
| 191 | 187 | /** |
| 188 | + * Get the author name for the entry. | |
| 189 | + * | |
| 190 | + * @return string The author name or 'Guest' if no user is associated. | |
| 191 | + */ | |
| 192 | + public function getAuthor(): string | |
| 193 | + { | |
| 194 | + if ($this->getUserId() === null) { | |
| 195 | + return esc_html__('Guest', 'ivyforms'); | |
| 196 | + } | |
| 197 | + | |
| 198 | + $userData = get_userdata($this->getUserId()); | |
| 199 | + return $userData ? $userData->user_nicename : esc_html__('Guest', 'ivyforms'); | |
| 200 | + } | |
| 201 | + | |
| 202 | + /** | |
| 192 | 203 | * Validates the ID. |
| 193 | 204 | * |
| 194 | 205 | * @param int $id |
| 195 | 206 | * |
| @@ -202,21 +213,43 @@ | ||
| 202 | 213 | throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); |
| 203 | 214 | } |
| 204 | 215 | return $id; |
| 205 | 216 | } |
| 217 | + | |
| 206 | 218 | /** |
| 207 | - * Validates a string value. | |
| 219 | + * Validates the user ID (nullable). | |
| 208 | 220 | * |
| 209 | - * @param string $value | |
| 221 | + * @param int|null $userId | |
| 222 | + * | |
| 223 | + * @return int|null | |
| 224 | + * @throws ValidationException | |
| 225 | + */ | |
| 226 | + private function validateUserId(?int $userId): ?int | |
| 227 | + { | |
| 228 | + if ($userId === null) { | |
| 229 | + return null; | |
| 230 | + } | |
| 231 | + if ($userId < 0) { | |
| 232 | + throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']); | |
| 233 | + } | |
| 234 | + return $userId; | |
| 235 | + } | |
| 236 | + /** | |
| 237 | + * Validates a string value (nullable). | |
| 238 | + * | |
| 239 | + * @param string|null $value | |
| 210 | 240 | * @param int $maxLength |
| 211 | 241 | * @param string $fieldName |
| 212 | 242 | * |
| 213 | - * @return string | |
| 243 | + * @return string|null | |
| 214 | 244 | * |
| 215 | 245 | * @throws ValidationException |
| 216 | 246 | */ |
| 217 | - private function validateString(string $value, int $maxLength, string $fieldName): string | |
| 247 | + private function validateString(?string $value, int $maxLength, string $fieldName): ?string | |
| 218 | 248 | { |
| 249 | + if ($value === null) { | |
| 250 | + return null; | |
| 251 | + } | |
| 219 | 252 | if (strlen($value) > $maxLength) { |
| 220 | 253 | throw new ValidationException( |
| 221 | 254 | sprintf( |
| 222 | 255 | /* translators: 1: String value, 2: String max length. */ |
| @@ -239,9 +272,9 @@ | ||
| 239 | 272 | return [ |
| 240 | 273 | 'id' => $this->getId(), |
| 241 | 274 | 'formId' => $this->getFormId(), |
| 242 | 275 | 'userId' => $this->getUserId(), |
| 243 | - 'author' => get_userdata($this->getUserId())->user_nicename ?? '', | |
| 276 | + 'author' => $this->getAuthor(), | |
| 244 | 277 | 'dateCreated' => $this->getDateCreated(), |
| 245 | 278 | 'dateEdited' => $this->getDateEdited(), |
| 246 | 279 | 'status' => $this->getStatus(), |
| 247 | 280 | 'ipAddress' => $this->getIpAddress(), |