Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,30 @@ 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;
```

#### 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:
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ parameters:
-
identifier: function.impossibleType
path: src/Traits/HasMeta.php
count: 1
count: 3

54 changes: 54 additions & 0 deletions src/CommentData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Yard\Data;

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,
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,
) {
$this->loadMeta();
}

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,
Comment thread
ictbeheer marked this conversation as resolved.
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,
);
Comment thread
ictbeheer marked this conversation as resolved.
}
}
1 change: 1 addition & 0 deletions src/Contracts/PostDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(
string $postType,
string $slug,
?ImageData $thumbnail,
?int $commentCount,
);

public function id(): ?int;
Expand Down
26 changes: 26 additions & 0 deletions src/PostData.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function __construct(
#[MapInputName('post_name')]
public string $slug,
public ?ImageData $thumbnail,
public ?int $commentCount,
) {
if (null !== $id) {
$this->loadMeta();
Expand All @@ -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: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null,
);
}

Expand All @@ -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: post_type_supports($post->post_type, 'comments') ? (int) $post->comment_count : null,
);
}

Expand Down Expand Up @@ -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, \WP_Comment> $comments */
$comments = get_comments($args);

return CommentData::collect($comments, Collection::class);
}
}
4 changes: 4 additions & 0 deletions src/Traits/HasMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/PostDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
postType: 'post',
slug: 'hello-world',
thumbnail: null,
commentCount: 0,
);
});

Expand Down
Loading