From ebd389acdbcaaaf94afa7f257cf0895c3a1affb8 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/3] (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 0fdd57e..44e2b5b 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -311,4 +311,27 @@ public function parent(): ?static 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 bdc722a4ac5e671c0532a8d7a921acab367d132d 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/3] (chore): phpstan --- src/PostData.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PostData.php b/src/PostData.php index 44e2b5b..7717e36 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -330,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); From 8fbc983dc3b8289e104f7476530881e300a90f96 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 3 Feb 2026 15:51:19 +0100 Subject: [PATCH 3/3] fix: constructor --- src/CommentData.php | 5 +++-- src/PostData.php | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/CommentData.php b/src/CommentData.php index 5740d4e..32a2f6c 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -7,6 +7,7 @@ use Carbon\CarbonImmutable; use Spatie\LaravelData\Data; +/** @phpstan-consistent-constructor */ class CommentData extends Data { public function __construct( @@ -27,9 +28,9 @@ public function __construct( ) { } - public static function fromComment(\WP_Comment $comment): CommentData + public static function fromComment(\WP_Comment $comment): static { - return new self( + return new static( id: (int) $comment->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, diff --git a/src/PostData.php b/src/PostData.php index 7717e36..0205fa1 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -322,16 +322,24 @@ public function commentCount(): ?int } /** + * @param array $args + * * @return Collection */ - public function comments(): Collection + public function comments(array $args = []): Collection { if (null === $this->id || ! post_type_supports($this->postType, 'comments')) { return collect(); } + $args = wp_parse_args($args, [ + 'post_id' => $this->id, + 'status' => 'approve', + 'type' => 'comment', + ]); + /** @var array $comments */ - $comments = get_comments(['post_id' => $this->id]); + $comments = get_comments($args); return CommentData::collect($comments, Collection::class); }