-
Notifications
You must be signed in to change notification settings - Fork 0
(feat): add commentdata #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yard\Data; | ||
|
|
||
| use Carbon\CarbonImmutable; | ||
| use Spatie\LaravelData\Data; | ||
|
|
||
| /** @phpstan-consistent-constructor */ | ||
| class CommentData extends Data | ||
| { | ||
| public function __construct( | ||
| public int $id, | ||
| public ?PostData $post, | ||
| public string $author, | ||
| public string $authorEmail, | ||
| public string $authorUrl, | ||
| public string $authorIp, | ||
| public ?CarbonImmutable $date, | ||
| public ?CarbonImmutable $dateGmt, | ||
| public string $content, | ||
| public bool $approved, | ||
| public string $agent, | ||
| public string $type, | ||
| public ?CommentData $parent, | ||
| public ?UserData $user, | ||
| ) { | ||
| } | ||
|
|
||
| public static function fromComment(\WP_Comment $comment): static | ||
| { | ||
| 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, | ||
| 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, | ||
| ); | ||
ictbeheer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is 'comment_count' er altijd? Zag dat je eerder nog een check deed of het post type comments ondersteunde?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ja, comment_count is een kolom in de wp_posts tabel. Maar misschien is het wel mooier om null te returnen voor post types die geen comments ondersteunen ipv 0 |
||
| ); | ||
| } | ||
|
|
||
|
|
@@ -311,4 +314,27 @@ public function parent(): ?static | |
|
|
||
| return static::fromPost($parent); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $args | ||
| * | ||
| * @return Collection<int, CommentData> | ||
| */ | ||
| 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<int, CommentData> $comments */ | ||
| $comments = get_comments($args); | ||
|
|
||
| return CommentData::collect($comments, Collection::class); | ||
ictbeheer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| postType: 'post', | ||
| slug: 'hello-world', | ||
| thumbnail: null, | ||
| commentCount: 0, | ||
| ); | ||
| }); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.