AdminCustomizerListRenderer.php
3.83 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Rendering;
use YahnisElsts\AdminMenuEditor\Customizable\HtmlHelper;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\Section;
use YahnisElsts\AdminMenuEditor\Customizable\Controls\Tooltip;
class AdminCustomizerListRenderer extends Renderer {
protected $pendingSections = [];
public function renderStructure($structure) {
$rootSection = new Section(
$structure->getTitle() ?: 'Root',
$structure->getAsSections(),
['id' => 'structure-root']
);
$this->renderSection($rootSection);
}
protected function renderPendingSections() {
while (!empty($this->pendingSections)) {
$section = array_shift($this->pendingSections);
$this->renderSection($section);
}
}
public function renderSection($section) {
echo HtmlHelper::tag('ul', [
'class' => 'ame-ac-section',
'id' => $this->getSectionElementId($section),
]);
?>
<li class="ame-ac-section-meta">
<div class="ame-ac-section-header">
<button class="ame-ac-section-back-button">
<span class="screen-reader-text">Back</span>
</button>
<h3 class="ame-ac-section-title"><?php
echo esc_html($section->getTitle());
?></h3>
</div>
</li>
<?php
$this->renderSectionChildren($section);
echo '</ul>';
$this->renderPendingSections();
}
protected function renderChildSection($section) {
if ( $section->hasChildren() ) {
$this->pendingSections[] = $section;
}
echo HtmlHelper::tag('li', [
'class' => 'ame-ac-section-link',
'data-target-id' => $this->getSectionElementId($section),
]);
printf(
'<h3 class="ame-ac-section-title">%s</h3>',
esc_html($section->getTitle())
);
echo '</li>';
}
protected function renderControlGroup($group) {
$title = $group->getTitle();
if ( !empty($title) ) {
echo '<li class="ame-ac-control ame-ac-control-group">';
echo '<h4 class="ame-ac-group-title">';
echo esc_html($title);
echo '</h4>';
echo '</li>';
}
$this->renderGroupChildren($group);
}
protected function renderUngroupedControl($control) {
$this->renderControl($control);
}
public function renderControl($control, $parentContext = null) {
$settings = $control->getSettings();
$settingIds = array_map(function ($setting) {
return $setting->getId();
}, $settings);
//The collection of settings IDs should be an associative array. If it's not,
//assign the first setting to the "default" key.
$isAssociative = false;
foreach ($settingIds as $key => $value) {
if ( is_string($key) ) {
$isAssociative = true;
break;
}
}
if ( !$isAssociative ) {
$firstId = array_shift($settingIds);
$settingIds['default'] = $firstId;
}
echo HtmlHelper::tag('li', [
'class' => 'ame-ac-control',
'data-ac-setting-ids' => !empty($settingIds) ? wp_json_encode($settingIds) : null,
'data-ac-control-type' => $control->getType(),
]);
if ( !$control->includesOwnLabel() ) {
$this->renderControlLabel($control);
}
parent::renderControl($control, $parentContext);
echo '</li>';
}
public function renderTooltipTrigger(Tooltip $tooltip) {
// TODO: Implement renderTooltipTrigger() method.
echo '[tooltip here]';
}
protected function getSectionElementId($section) {
return 'ame-ac-section-' . $section->getId();
}
/**
* @param \YahnisElsts\AdminMenuEditor\Customizable\Controls\Control $control
* @return void
*/
protected function renderControlLabel($control) {
$label = $control->getLabel();
if ( empty($label) ) {
return;
}
$labelForId = $control->getLabelTargetId();
if ( $control->supportsLabelAssociation() && !empty($labelForId) ) {
printf(
'<label for="%s" class="ame-ac-control-label">%s</label>',
esc_attr($labelForId),
esc_html($label)
);
} else {
printf(
'<span class="ame-ac-control-label">%s</span>',
esc_html($label)
);
}
}
}