FormTableRenderer.php
3.54 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Rendering;
class FormTableRenderer extends ClassicRenderer {
protected $isInsideRow = false;
protected $sectionNestingLevel = 0;
public function renderSection($section) {
if ( !$section->shouldRender() ) {
//Should this even be allowed?
echo sprintf(
'<!-- Notice: Rendering a section that does not think it should be rendered: %s (%s) -->',
esc_html($section->getTitle()),
esc_html($section->getId())
);
}
echo '<div>';
$this->renderSectionHeading($section);
$description = $section->getDescription();
if ( !empty($description) ) {
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML intentionally allowed.
echo "\n", '<p>', $description, '</p>';
}
//Render each section as a table.
echo '<table class="form-table ame-rendered-form-table">';
$this->renderSectionChildren($section);
echo '</table>';
echo '</div>';
}
protected function renderSectionHeading($section, $headingLevel = 2) {
$title = $section->getTitle();
if ( !empty($title) ) {
echo sprintf('<h%d>', (int)$headingLevel);
$this->renderSectionTitleContent($section);
echo sprintf('</h%d>', (int)$headingLevel);
}
}
protected function renderChildSection($section) {
$this->sectionNestingLevel++;
if ( $section->hasTitle() ) {
$headingLevel = min(6, 2 + $this->sectionNestingLevel);
echo '<tr class="ame-nested-section-header"><td colspan="2">';
$this->renderSectionHeading($section, $headingLevel);
echo '</td></tr>';
}
$this->renderSectionChildren($section);
$this->sectionNestingLevel--;
}
protected function renderControlGroup($group) {
$isStacked = $group->isStacked();
/*
* - Render top-level groups as table rows. Optionally, their contents
* can be wrapped in a fieldset element.
* - Render nested groups as either fieldset elements or a flat sequence
* of controls.
*/
$isFieldset = $group->wantsFieldset();
if ( $isFieldset === null ) {
$isFieldset = $this->isInsideRow;
}
$renderAsTableRow = !$this->isInsideRow;
$wasInsideRow = $this->isInsideRow;
if ( $renderAsTableRow ) {
$this->isInsideRow = true;
printf('<tr id="%s">', esc_attr($group->getId()));
printf(
'<th scope="row" class="%s">',
esc_attr($group->hasTooltip() ? 'ame-customizable-cg-has-tooltip' : '')
);
$this->renderGroupTitleContent($group);
echo '</th>';
echo '<td>';
}
if ( $isFieldset ) {
/** @noinspection HtmlUnknownAttribute */
printf('<fieldset %s>', $group->isEnabled() ? '' : 'disabled');
}
$this->renderGroupChildren($group, $isStacked);
if ( $isFieldset ) {
echo '</fieldset>';
}
if ( $renderAsTableRow ) {
echo '</td>';
echo '</tr>';
$this->isInsideRow = $wasInsideRow;
}
}
public function renderControl($control, $parentContext = null) {
if ( !$control->shouldRender() ) {
//This should really never happen.
echo '<!-- Skipped control: ' . esc_html($control->getId()) . ' -->';
return;
}
$addLineBreaks = ($parentContext && !$control->declinesExternalLineBreaks());
if ( $addLineBreaks ) {
echo '<p>';
}
parent::renderControl($control, $parentContext);
if ( $addLineBreaks ) {
echo '</p>';
}
}
public function enqueueDependencies($containerSelector = '') {
static $done = false;
if ( $done ) {
return;
}
$done = true;
parent::enqueueDependencies($containerSelector);
wp_enqueue_style(
'ame-form-table-renderer',
plugins_url('assets/form-table.css', AME_CUSTOMIZABLE_BASE_FILE),
array(),
'20221129'
);
}
}