diff --git a/src/CommentData.php b/src/CommentData.php new file mode 100644 index 0000000..cf1f54c --- /dev/null +++ b/src/CommentData.php @@ -0,0 +1,50 @@ +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: '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, + user: false !== get_userdata((int) $comment->user_id) ? UserData::fromUser(get_userdata((int) $comment->user_id)) : 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 0fdd57e..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, ); } @@ -311,4 +314,27 @@ public function parent(): ?static return static::fromPost($parent); } + + /** + * @param array $args + * + * @return 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($args); + + return CommentData::collect($comments, Collection::class); + } } 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, ); });