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/8] (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/8] (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/8] 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/8] 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/8] 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, From 231bb232bfbc4acb676b586d921d07472dcd37c2 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Mon, 13 Apr 2026 17:28:54 +0200 Subject: [PATCH 6/8] chore: review comments --- README.md | 8 ++++++++ src/Contracts/PostDataInterface.php | 2 +- src/PostData.php | 6 +++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b9e8970..2abe6fa 100644 --- a/README.md +++ b/README.md @@ -283,6 +283,14 @@ class VacancyData extends PostData $vacancyTypeIcon = $vacancyData->type->first()?->icon; ``` +#### Reading comments for your Data Objects + +If the post type of the data object supports comments, you can retrieve them as a collection of `CommentData` + +```php +$postData->comments()->first()?->authorEmail; +``` + ### UserData Create UserData from current user: diff --git a/src/Contracts/PostDataInterface.php b/src/Contracts/PostDataInterface.php index 05502a0..fd30512 100644 --- a/src/Contracts/PostDataInterface.php +++ b/src/Contracts/PostDataInterface.php @@ -23,7 +23,7 @@ public function __construct( string $postType, string $slug, ?ImageData $thumbnail, - int $commentCount, + ?int $commentCount, ); public function id(): ?int; diff --git a/src/PostData.php b/src/PostData.php index 0c155a4..a015665 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -46,7 +46,7 @@ public function __construct( #[MapInputName('post_name')] public string $slug, public ?ImageData $thumbnail, - public int $commentCount, + public ?int $commentCount, ) { if (null !== $id) { $this->loadMeta(); @@ -68,7 +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, + commentCount: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null, ); } @@ -86,7 +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, + commentCount: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null, ); } From d52ae29d243e7eda25e97895c273357429223504 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Thu, 23 Apr 2026 14:09:06 +0200 Subject: [PATCH 7/8] feat: allow meta on commentdata Co-authored-by: Copilot --- README.md | 16 ++++++++++++++++ phpstan.neon.dist | 2 +- src/CommentData.php | 4 ++++ src/Traits/HasMeta.php | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2abe6fa..8b8e0b4 100644 --- a/README.md +++ b/README.md @@ -291,6 +291,22 @@ If the post type of the data object supports comments, you can retrieve them as $postData->comments()->first()?->authorEmail; ``` +#### Extending CommentData + +You can add extra meta fields to comments by extending the default CommentData object + +```php +namespace App\Data; + +use Yard\Data\CommentData; + +class LinkedInCommmentData extends CommentDataData { + + #[Meta()] + public string $linkedInUserName; +} +``` + ### UserData Create UserData from current user: diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 13cbc9f..a933ae0 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -15,5 +15,5 @@ parameters: - identifier: function.impossibleType path: src/Traits/HasMeta.php - count: 1 + count: 3 diff --git a/src/CommentData.php b/src/CommentData.php index cf1f54c..f895761 100644 --- a/src/CommentData.php +++ b/src/CommentData.php @@ -6,10 +6,13 @@ use Carbon\CarbonImmutable; use Spatie\LaravelData\Data; +use Yard\Data\Traits\HasMeta; /** @phpstan-consistent-constructor */ class CommentData extends Data { + use HasMeta; + public function __construct( public int $id, public ?PostData $post, @@ -26,6 +29,7 @@ public function __construct( public ?CommentData $parent, public ?UserData $user, ) { + $this->loadMeta(); } public static function fromComment(\WP_Comment $comment): static diff --git a/src/Traits/HasMeta.php b/src/Traits/HasMeta.php index 15e161f..adcb24c 100644 --- a/src/Traits/HasMeta.php +++ b/src/Traits/HasMeta.php @@ -8,6 +8,7 @@ use Spatie\LaravelData\Data; use Yard\Data\Attributes\Meta; use Yard\Data\Attributes\MetaPrefix; +use Yard\Data\CommentData; use Yard\Data\PostData; use Yard\Data\TermData; @@ -76,6 +77,9 @@ private function objectID(): string|int if (is_a($this, TermData::class)) { return $this->taxonomy . '_' . $this->id; } + if (is_a($this, CommentData::class)) { + return 'comment_' . $this->id; + } return 0; } From 100b92b8eda444ef74d508c999586338c2da72e4 Mon Sep 17 00:00:00 2001 From: Maarten Bruna <14947039+ictbeheer@users.noreply.github.com> Date: Tue, 28 Apr 2026 10:36:23 +0200 Subject: [PATCH 8/8] fix: phpdoc Co-authored-by: Copilot --- src/PostData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PostData.php b/src/PostData.php index a015665..02239d6 100644 --- a/src/PostData.php +++ b/src/PostData.php @@ -332,7 +332,7 @@ public function comments(array $args = []): Collection 'type' => 'comment', ]); - /** @var array $comments */ + /** @var array $comments */ $comments = get_comments($args); return CommentData::collect($comments, Collection::class);