-
Notifications
You must be signed in to change notification settings - Fork 0
/
Taxonomy.php
184 lines (154 loc) · 5.22 KB
/
Taxonomy.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?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\Category;
use craft\fieldlayoutelements\CustomField;
use craft\fieldlayoutelements\TitleField;
use craft\fields\Categories;
use craft\helpers\StringHelper;
use craft\models\CategoryGroup;
use craft\models\CategoryGroup_SiteSettings;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use craft\models\Site;
use craft\wpimport\BaseConfigurableImporter;
use craft\wpimport\Command;
use craft\wpimport\generators\fields\Description;
use craft\wpimport\generators\fields\WpId;
use Throwable;
use yii\console\Exception;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class Taxonomy extends BaseConfigurableImporter
{
private CategoryGroup $categoryGroup;
private Categories $field;
public function __construct(private array $data, Command $command, array $config = [])
{
parent::__construct($command, $config);
}
public function slug(): string
{
return $this->data['slug'];
}
public function apiUri(): string
{
return sprintf('%s/%s', $this->data['rest_namespace'], $this->data['rest_base'] ?: $this->data['slug']);
}
public function label(): string
{
return $this->data['name'];
}
public function typeLabel(): string
{
return 'Taxonomy';
}
public function elementType(): string
{
return Category::class;
}
public function populate(ElementInterface $element, array $data): void
{
/** @var Category $element */
$element->groupId = $this->categoryGroup()->id;
if (!$this->categoryGroup()->maxLevels && $data['parent']) {
$element->setParentId($this->command->import($this->slug(), $data['parent']));
}
$element->title = $data['name'];
$element->slug = $data['slug'];
$fieldValues = [
Description::get()->handle => $data['description'],
];
if (!empty($data['acf'])) {
$fieldValues = array_merge($fieldValues, $this->command->prepareAcfFieldValues(
$this->command->fieldsForEntity('taxonomy', $this->slug()),
$data['acf'],
));
}
foreach ($fieldValues as $handle => $value) {
try {
$element->setFieldValue($handle, $value);
} catch (Throwable) {
}
}
}
public function categoryGroup(): CategoryGroup
{
if (isset($this->categoryGroup)) {
return $this->categoryGroup;
}
$groupHandle = StringHelper::toHandle($this->label());
$group = Craft::$app->categories->getGroupByHandle($groupHandle);
if ($group) {
return $this->categoryGroup = $group;
}
$group = new CategoryGroup();
$group->name = $this->label();
$group->handle = $groupHandle;
if (!$this->hierarchical()) {
$group->maxLevels = 1;
}
$fieldLayout = new FieldLayout();
$fieldLayout->setTabs([
new FieldLayoutTab([
'layout' => $fieldLayout,
'name' => 'Content',
'elements' => [
new TitleField(),
new CustomField(Description::get()),
],
]),
...$this->command->acfLayoutTabsForEntity('taxonomy', $this->slug(), $fieldLayout),
new FieldLayoutTab([
'layout' => $fieldLayout,
'name' => 'Meta',
'elements' => [
new CustomField(WpId::get()),
],
]),
]);
$group->setFieldLayout($fieldLayout);
$group->setSiteSettings(array_map(fn(Site $site) => new CategoryGroup_SiteSettings([
'siteId' => $site->id,
]), Craft::$app->sites->getAllSites(true)));
$this->command->do("Creating `$group->name` category group", function() use ($group) {
if (!Craft::$app->categories->saveGroup($group)) {
throw new Exception(implode(', ', $group->getFirstErrors()));
}
});
return $this->categoryGroup = $group;
}
public function field(): Categories
{
if (isset($this->field)) {
return $this->field;
}
$fieldHandle = StringHelper::toHandle($this->label());
/** @var Categories|null $field */
$field = Craft::$app->fields->getFieldByHandle($fieldHandle);
if ($field) {
return $this->field = $field;
}
$field = new Categories();
$field->name = $this->label();
$field->handle = $fieldHandle;
$field->source = sprintf('group:%s', $this->categoryGroup()->uid);
$this->command->do("Creating `$field->name` Categories field", function() use ($field) {
if (!Craft::$app->fields->saveField($field)) {
throw new Exception(implode(', ', $field->getFirstErrors()));
}
});
return $this->field = $field;
}
private function hierarchical(): bool
{
return $this->data['hierarchical'] ?? false;
}
}