RadioButtonBar.php
2.39 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
<?php
namespace YahnisElsts\AdminMenuEditor\Customizable\Controls;
use YahnisElsts\AdminMenuEditor\Customizable\Rendering\Renderer;
class RadioButtonBar extends ChoiceControl {
protected $type = 'radio-bar';
protected $koComponentName = 'ame-radio-button-bar';
protected $declinesExternalLineBreaks = true;
protected $controlClass = 'ame-radio-button-bar-control';
public function renderContent(Renderer $renderer) {
$fieldName = $this->getFieldName();
$currentValue = $this->mainSetting->getValue();
//phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $this->buildTag(
'fieldset',
[
'class' => array_merge([$this->controlClass], $this->classes),
'style' => $this->styles,
'disabled' => !$this->isEnabled(),
'data-bind' => $this->makeKoDataBind($this->getKoEnableBinding()),
]
);
foreach ($this->options as $option) {
$isChecked = ($currentValue === $option->value);
echo $this->buildTag('label', array(
'class' => 'ame-radio-bar-item',
'title' => $option->description,
));
echo $this->buildTag(
'input',
array_merge(array(
'type' => 'radio',
'name' => $fieldName,
'value' => $this->mainSetting->encodeForForm($option->value),
'class' => $this->inputClasses,
'checked' => $isChecked,
'disabled' => !$option->enabled,
'data-bind' => $this->makeKoDataBind([
'checked' => $this->getKoObservableExpression($option->value),
'checkedValue' => wp_json_encode($option->value),
'ameObservableChangeEvents' => 'true',
]),
), $this->inputAttributes)
);
$buttonContent = esc_html($option->label);
if ( is_string($option->icon) && (strpos($option->icon, 'dashicons-') !== false) ) {
$buttonContent = sprintf(
'<span class="dashicons %s"></span> %s',
esc_attr($option->icon),
$buttonContent
);
}
$buttonClasses = ['button', 'ame-radio-bar-button'];
if ( !empty($option->label) ) {
$buttonClasses[] = 'ame-rb-has-label';
}
//Note that we can't use a "button" element because then the label
//won't correctly select the radio input when clicked. It's probably
//because a label can't be associated with two elements.
echo $this->buildTag(
'span',
['class' => $buttonClasses],
$buttonContent
);
echo '</label>';
}
echo '</fieldset>';
//phpcs:enable
}
}