-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comment.php
110 lines (96 loc) · 3.43 KB
/
Comment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/
namespace craft\wpimport\importers;
use Craft;
use craft\base\ElementInterface;
use craft\elements\User as UserElement;
use craft\enums\CmsEdition;
use craft\fieldlayoutelements\CustomField;
use craft\helpers\DateTimeHelper;
use craft\wpimport\BaseImporter;
use craft\wpimport\generators\fields\WpId;
use Throwable;
use verbb\comments\elements\Comment as CommentElement;
use verbb\comments\services\Comments as CommentsService;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class Comment extends BaseImporter
{
public const SLUG = 'comment';
public function slug(): string
{
return self::SLUG;
}
public function apiUri(): string
{
return 'wp/v2/comments';
}
public function label(): string
{
return 'Comments';
}
public function elementType(): string
{
return CommentElement::class;
}
public function supported(?string &$reason): bool
{
if (!$this->command->importComments) {
$reason = 'The Comments plugin isn’t enabled.';
return false;
}
return true;
}
public function prep(): void
{
$fieldLayout = Craft::$app->fields->getLayoutByType(CommentElement::class);
$field = WpId::get();
if (!$fieldLayout->getFieldById($field->id)) {
$this->command->do('Updating the comment field layout', function() use ($fieldLayout, $field) {
$this->command->addElementsToLayout($fieldLayout, 'Meta', [
new CustomField($field),
]);
$configData = [$fieldLayout->uid => $fieldLayout->getConfig()];
Craft::$app->projectConfig->set(CommentsService::CONFIG_FIELDLAYOUT_KEY, $configData);
});
}
}
public function populate(ElementInterface $element, array $data): void
{
/** @var CommentElement $element */
$element->ownerId = $this->command->importPost($data['post']);
$element->ownerSiteId = Craft::$app->sites->primarySite->id;
$element->siteId = Craft::$app->sites->primarySite->id;
if ($data['author'] && Craft::$app->edition->value >= CmsEdition::Pro->value) {
try {
$element->userId = $this->command->import(User::SLUG, $data['author'], [
'roles' => 'administrator,editor,author,contributor,viewer,subscriber',
]);
} catch (Throwable) {
}
}
if (!$element->userId) {
// just see if we have an existing user with this email
$element->userId = UserElement::find()->email($data['author_email'])->ids()[0] ?? null;
}
$element->name = $data['author_name'];
$element->email = $data['author_email'];
$element->url = $data['author_url'];
$element->ipAddress = $data['author_ip'];
$element->userAgent = $data['author_user_agent'];
$element->commentDate = DateTimeHelper::toDateTime($data['date_gmt']);
$element->comment = $data['content']['raw'];
$element->status = match ($data['status']) {
'approved' => CommentElement::STATUS_APPROVED,
default => CommentElement::STATUS_PENDING
};
if ($data['parent']) {
$element->setParentId($this->command->import(self::SLUG, $data['parent']));
}
}
}