From 7da491d2109f2fe1bdb00f56489618a604d4270b Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Thu, 7 Aug 2025 11:56:41 +0200 Subject: [PATCH 1/2] (feat): add commentdata --- src/CommentData.php | 49 +++++++++++++++++++++++++++++++++++++++++++++ src/PostData.php | 23 +++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/CommentData.php diff --git a/src/CommentData.php b/src/CommentData.php new file mode 100644 index 0000000..5740d4e --- /dev/null +++ b/src/CommentData.php @@ -0,0 +1,49 @@ +comment_ID, + post: null !== get_post((int)$comment->comment_post_ID) ? PostData::fromPost(get_post((int)$comment->comment_post_ID)) : null, + author: $comment->comment_author, + authorEmail: $comment->comment_author_email, + authorUrl: $comment->comment_author_url, + authorIp: $comment->comment_author_IP, + date: CarbonImmutable::createFromFormat('Y-m-d H:i:s', $comment->comment_date)?: null, + dateGmt: CarbonImmutable::createFromFormat('Y-m-d H:i:s', $comment->comment_date_gmt)?: null, + content: $comment->comment_content, + approved: (bool) $comment->comment_approved, + agent: $comment->comment_agent, + type: $comment->comment_type, + parent: null !== get_comment((int) $comment->comment_parent) ? CommentData::fromComment(get_comment((int) $comment->comment_parent)) : null, + user: false !== get_userdata((int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : null, + ); + } +} diff --git a/src/PostData.php b/src/PostData.php index 0726552..abab18b 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -310,4 +310,27 @@ public function parent(): ?PostData return static::fromPost($parent); } + + public function commentCount(): ?int + { + if (null === $this->id || ! post_type_supports($this->postType, 'comments')) { + return null; + } + + return (int) get_comments_number($this->id); + } + + /** + * @return Collection + */ + public function comments(): Collection + { + if (null === $this->id || ! post_type_supports($this->postType, 'comments')) { + return collect(); + } + + $comments = get_comments(['post_id' => $this->id]); + + return CommentData::collect($comments, Collection::class); + } } From f969f2ae2b09cf9b8a2fb55e8937e2b62a7f480b Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Wed, 10 Sep 2025 09:23:28 +0200 Subject: [PATCH 2/2] (chore): phpstan --- src/PostData.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PostData.php b/src/PostData.php index abab18b..3381681 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -112,6 +112,7 @@ private static function dataClass(string $postType): string throw new RuntimeException(sprintf('The class "%s" must extend %s.', $classFQN, PostData::class)); } + /** @var class-string $classFQN */ return $classFQN; } @@ -329,6 +330,7 @@ public function comments(): Collection return collect(); } + /** @var array $comments */ $comments = get_comments(['post_id' => $this->id]); return CommentData::collect($comments, Collection::class);