From 93d0af28d041176aeaa1b9ebb1bdc1f233f9044b 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/5] (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 f073edb3149b3d79f732b52ee22a71e90f54b740 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/5] (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 c14094a6f217eba87b3ef9327a92ec0cfd79e5b8 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/5] 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); } From 98e8d793673185e84126510a21d9c9f80d814003 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Thu, 2 Apr 2026 12:32:26 +0200 Subject: [PATCH 4/5] fix: use native commentCount --- src/CommentData.php | 2 +- src/Contracts/PostDataInterface.php | 1 + src/PostData.php | 12 +++--------- tests/src/PostDataTest.php | 1 + 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/CommentData.php b/src/CommentData.php index 32a2f6c..ad70394 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -40,7 +40,7 @@ public static function fromComment(\WP_Comment $comment): static 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, + approved: $comment->comment_approved === '1', 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, diff --git a/src/Contracts/PostDataInterface.php b/src/Contracts/PostDataInterface.php index f80c9f5..05502a0 100644 --- a/src/Contracts/PostDataInterface.php +++ b/src/Contracts/PostDataInterface.php @@ -23,6 +23,7 @@ public function __construct( string $postType, string $slug, ?ImageData $thumbnail, + int $commentCount, ); public function id(): ?int; diff --git a/src/PostData.php b/src/PostData.php index 0205fa1..0c155a4 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -46,6 +46,7 @@ public function __construct( #[MapInputName('post_name')] public string $slug, public ?ImageData $thumbnail, + public int $commentCount, ) { if (null !== $id) { $this->loadMeta(); @@ -67,6 +68,7 @@ public static function fromPost(\WP_Post $post): static postType: $post->post_type, slug: $post->post_name, thumbnail: get_post_thumbnail_id($post->ID) ? new ImageData(get_post_thumbnail_id($post->ID)) : null, + commentCount: (int) $post->comment_count, ); } @@ -84,6 +86,7 @@ public static function fromCorcel(Post $post): static postType: $post->post_type, slug: $post->post_name, thumbnail: get_post_thumbnail_id($post->ID) ? new ImageData(get_post_thumbnail_id($post->ID)) : null, + commentCount: (int) $post->comment_count, ); } @@ -312,15 +315,6 @@ 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); - } - /** * @param array $args * diff --git a/tests/src/PostDataTest.php b/tests/src/PostDataTest.php index 76c4be1..a641b3f 100644 --- a/tests/src/PostDataTest.php +++ b/tests/src/PostDataTest.php @@ -15,6 +15,7 @@ postType: 'post', slug: 'hello-world', thumbnail: null, + commentCount: 0, ); }); From e2efc5d6fa34c721c72e74af28b65c372912ae41 Mon Sep 17 00:00:00 2001 From: Yard workflows Date: Thu, 2 Apr 2026 10:32:49 +0000 Subject: [PATCH 5/5] style: apply php-cs-fixer changes --- src/CommentData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CommentData.php b/src/CommentData.php index ad70394..cf1f54c 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -40,7 +40,7 @@ public static function fromComment(\WP_Comment $comment): static 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: $comment->comment_approved === '1', + approved: '1' === $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,