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..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; } @@ -310,4 +311,28 @@ 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(); + } + + /** @var array $comments */ + $comments = get_comments(['post_id' => $this->id]); + + return CommentData::collect($comments, Collection::class); + } }