- Import
This commit is contained in:
@@ -87,6 +87,42 @@ final class CommentService
|
||||
];
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $data */
|
||||
public function import(string $slug, array $data): int
|
||||
{
|
||||
$createdAt = $this->normalizeDate((string) ($data['created_at'] ?? $data['date'] ?? ''));
|
||||
$updatedAt = $this->normalizeDate((string) ($data['updated_at'] ?? $data['modified'] ?? $createdAt));
|
||||
$status = (string) ($data['status'] ?? 'approved');
|
||||
if (!in_array($status, ['pending', 'approved', 'spam'], true)) {
|
||||
$status = match ($status) {
|
||||
'published', 'published-comment', 'visible', '1', 'true' => 'approved',
|
||||
'trash', 'deleted', 'spam-comment' => 'spam',
|
||||
default => 'pending',
|
||||
};
|
||||
}
|
||||
|
||||
$statement = $this->pdo->prepare(
|
||||
'INSERT INTO comments
|
||||
(slug, author, email, website, body, status, ip_hash, user_agent, created_at, updated_at)
|
||||
VALUES
|
||||
(:slug, :author, :email, :website, :body, :status, :ip_hash, :user_agent, :created_at, :updated_at)'
|
||||
);
|
||||
$statement->execute([
|
||||
'slug' => $slug,
|
||||
'author' => trim((string) ($data['author'] ?? $data['name'] ?? 'Imported')),
|
||||
'email' => trim((string) ($data['email'] ?? '')),
|
||||
'website' => trim((string) ($data['website'] ?? $data['site_url'] ?? $data['url'] ?? '')),
|
||||
'body' => trim((string) ($data['body'] ?? $data['content'] ?? $data['comment'] ?? '')),
|
||||
'status' => $status,
|
||||
'ip_hash' => trim((string) ($data['ip_hash'] ?? '')),
|
||||
'user_agent' => substr((string) ($data['user_agent'] ?? $data['agent'] ?? 'import'), 0, 300),
|
||||
'created_at' => $createdAt,
|
||||
'updated_at' => $updatedAt,
|
||||
]);
|
||||
|
||||
return (int) $this->pdo->lastInsertId();
|
||||
}
|
||||
|
||||
/** @return array<int, array<string, mixed>> */
|
||||
public function list(?string $status = null, ?string $slug = null): array
|
||||
{
|
||||
@@ -144,4 +180,10 @@ final class CommentService
|
||||
|
||||
return hash_hmac('sha256', $ip, $salt);
|
||||
}
|
||||
|
||||
private function normalizeDate(string $value): string
|
||||
{
|
||||
$timestamp = strtotime($value);
|
||||
return date('c', $timestamp ?: time());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user