CodeEditor.php
4.11 KB
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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Controls;
use YahnisElsts\AdminMenuEditor\Customizable\Rendering\Renderer;
class CodeEditor extends ClassicControl {
protected $type = 'codeEditor';
protected $koComponentName = 'ame-code-editor';
/**
* @var string MIME type for CodeMirror.
*/
protected $mimeType = 'text/html';
const SCRIPT_ACTION = 'admin_print_footer_scripts';
protected $editorSettings = [];
protected $editorId = null;
protected $triedToEnqueueEditor = false;
public function __construct($settings = [], $params = []) {
parent::__construct($settings, $params);
if ( isset($params['mimeType']) ) {
$this->mimeType = $params['mimeType'];
}
}
public function renderContent(Renderer $renderer) {
$id = '_acm_' . $this->id;
$this->editorId = $id;
$stringValue = $this->getMainSettingValue('');
if ( $stringValue === null ) {
$stringValue = '';
}
echo '<div class="ame-code-editor-control-wrap">';
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- buildInputElement() is safe.
echo $this->buildInputElement(
[
'id' => $id,
'class' => 'large-text',
'cols' => 100,
'rows' => 5,
],
'textarea',
esc_textarea($stringValue)
);
$this->outputSiblingDescription();
echo '</div>';
if ( $this->enqueueCodeEditor() ) {
static::enqueueDependencies();
if ( did_action(self::SCRIPT_ACTION) ) {
$this->outputInitScript();
} else {
add_action(self::SCRIPT_ACTION, [$this, 'outputInitScript']);
}
}
}
protected function enqueueCodeEditor() {
//Don't enqueue the editor more than once.
if ( $this->triedToEnqueueEditor ) {
return !empty($this->editorSettings);
}
$this->triedToEnqueueEditor = true;
$additionalCodeMirrorOptions = [];
//Strangely, WordPress disables linting for CSS by default even though
//it is actually supported. Let's enable it explicitly. This needs to be
//done *before* calling wp_enqueue_code_editor() because that function
//uses the "lint" option to decide whether to enqueue the linter(s).
if ($this->mimeType === 'text/css') {
$additionalCodeMirrorOptions['lint'] = true;
}
//This can return false if, for example, the user has disabled syntax highlighting.
$this->editorSettings = wp_enqueue_code_editor([
'type' => $this->mimeType,
'codemirror' => $additionalCodeMirrorOptions,
]);
if ( empty($this->editorSettings) ) {
return false;
}
//Enable linting and a few other things for CSS and JavaScript.
if (
isset($this->editorSettings['codemirror'])
&& in_array(
$this->mimeType,
['text/css', 'text/javascript', 'application/javascript']
)
) {
$this->editorSettings['codemirror'] = array_merge(
$this->editorSettings['codemirror'],
[
'lint' => true,
'autoCloseBrackets' => true,
'matchBrackets' => true,
]
);
if ( empty($this->editorSettings['codemirror']['gutters']) ) {
$this->editorSettings['codemirror']['gutters'][] = 'CodeMirror-lint-markers';
}
//For CSS in particular, setting mode to "css" appears to enable
//some additional linting, like warnings for unknown CSS properties.
if ( $this->mimeType === 'text/css' ) {
$this->editorSettings['codemirror']['mode'] = 'css';
}
}
return true;
}
public function outputInitScript() {
if ( !$this->editorId || empty($this->editorSettings) ) {
//Code editor was not enqueued for some reason.
return;
}
?>
<script type="text/javascript">
jQuery(function () {
if (typeof wp['codeEditor'] !== 'undefined') {
wp.codeEditor.initialize(
<?php echo wp_json_encode($this->editorId); ?>,
<?php echo wp_json_encode($this->editorSettings); ?>
);
}
});
</script>
<?php
}
public function enqueueKoComponentDependencies() {
parent::enqueueKoComponentDependencies();
$this->enqueueCodeEditor();
}
protected function getKoComponentParams() {
if ( !$this->triedToEnqueueEditor ) {
$this->enqueueCodeEditor();
}
$params = parent::getKoComponentParams();
$params['mimeType'] = $this->mimeType;
$params['editorSettings'] = $this->editorSettings;
return $params;
}
}