-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
94 lines (81 loc) · 2.42 KB
/
User.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
<?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\StringHelper;
use craft\wpimport\BaseImporter;
use craft\wpimport\generators\fields\WpId;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class User extends BaseImporter
{
public const SLUG = 'user';
public function slug(): string
{
return self::SLUG;
}
public function apiUri(): string
{
return 'wp/v2/users';
}
public function label(): string
{
return 'Users';
}
public function queryParams(): array
{
return [
'roles' => 'administrator,editor,author,contributor',
];
}
public function elementType(): string
{
return UserElement::class;
}
public function supported(?string &$reason): bool
{
if (Craft::$app->edition->value < CmsEdition::Pro->value) {
$reason = 'Craft Pro is required.';
return false;
}
return true;
}
public function prep(): void
{
$fieldLayout = Craft::$app->fields->getLayoutByType(UserElement::class);
$field = WpId::get();
if (!$fieldLayout->getFieldById($field->id)) {
$this->command->do('Updating the user field layout', function() use ($fieldLayout, $field) {
$this->command->addElementsToLayout($fieldLayout, 'Meta', [
new CustomField($field),
]);
Craft::$app->users->saveLayout($fieldLayout);
});
}
}
public function find(array $data): ?ElementInterface
{
return UserElement::find()->email($data['email'])->one();
}
public function populate(ElementInterface $element, array $data): void
{
if (UserElement::find()->username($data['username'])->exists()) {
$data['username'] .= '_' . StringHelper::randomString(5);
}
/** @var UserElement $element */
$element->username = $data['username'];
$element->firstName = $data['first_name'];
$element->lastName = $data['last_name'];
$element->email = $data['email'];
$element->admin = in_array('administrator', $data['roles']);
}
}