-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wysiwyg.php
103 lines (91 loc) · 3.15 KB
/
Wysiwyg.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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license MIT
*/
namespace craft\wpimport\acfadapters;
use craft\base\FieldInterface;
use craft\ckeditor\CkeConfig;
use craft\ckeditor\Field;
use craft\ckeditor\Plugin;
use craft\helpers\ArrayHelper;
use craft\helpers\StringHelper;
use craft\wpimport\BaseAcfAdapter;
/**
* @author Pixel & Tonic, Inc. <[email protected]>
*/
class Wysiwyg extends BaseAcfAdapter
{
public static function type(): string
{
return 'wysiwyg';
}
public function create(array $data): FieldInterface
{
$field = new Field();
$field->ckeConfig = $this->ckeConfig($data['toolbar'])?->uid;
return $field;
}
private function ckeConfig(string $name): ?CkeConfig
{
$configsService = Plugin::getInstance()->getCkeConfigs();
/** @var CkeConfig|null $config */
$config = ArrayHelper::firstWhere(
$configsService->getAll(),
fn(CkeConfig $config) => StringHelper::toSnakeCase($config->name) === $name,
);
if ($config) {
return $config;
}
$toolbarName = ArrayHelper::firstWhere(
array_keys($this->command->wpInfo['wysiwyg_toolbars']),
fn(string $key) => StringHelper::toSnakeCase($key) === $name,
);
if (!$toolbarName) {
return null;
}
$config = new CkeConfig();
$config->uid = StringHelper::UUID();
$config->name = $toolbarName;
$config->toolbar = [];
foreach ($this->command->wpInfo['wysiwyg_toolbars'][$toolbarName] as $row) {
$rowButtons = [];
foreach ($row as $button) {
$ckeButton = match ($button) {
'formatselect' => 'heading',
'bold' => 'bold',
'italic' => 'italic',
'bullist' => 'bulletedList',
'numlist' => 'numberedList',
'blockquote' => 'blockQuote',
'alignleft', 'aligncenter', 'alignright' => 'alignment',
'link' => 'link',
'wp_more' => 'pageBreak',
'strikethrough' => 'strikethrough',
'hr' => 'horizontalLine',
'forecolor' => 'fontColor',
'removeformat' => 'removeFormat',
'outdent' => 'outdent',
'indent' => 'indent',
'undo' => 'undo',
'redo' => 'redo',
default => null,
};
if ($ckeButton) {
$rowButtons[] = $ckeButton;
}
}
if (!empty($rowButtons)) {
if (!empty($config->toolbar)) {
$config->toolbar[] = '|';
}
array_push($config->toolbar, ...array_values(array_unique($rowButtons)));
}
}
$this->command->do("Creating `$config->name` CKEditor config", function() use ($configsService, $config) {
$configsService->save($config);
});
return $config;
}
}