PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Form / Element / Checkbox.php
codepress-admin-columns / classes / Form / Element Last commit date
Checkbox.php 2 days ago Input.php 2 days ago MultiSelect.php 2 days ago Radio.php 2 days ago Select.php 2 days ago
Checkbox.php
137 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\Form\Element;
6
7 use AC\Form\Element;
8
9 class Checkbox extends Element
10 {
11 protected ?bool $vertical = null;
12
13 protected ?bool $multiple = null;
14
15 protected ?bool $disabled = null;
16
17 protected function get_type(): string
18 {
19 return 'checkbox';
20 }
21
22 protected function get_classes(): array
23 {
24 $classes = [
25 $this->get_type() . '-labels',
26 ];
27
28 if ($this->is_vertical()) {
29 $classes[] = 'vertical';
30 }
31
32 return $classes;
33 }
34
35 public function render(): string
36 {
37 $elements = $this->get_elements();
38
39 if (! $elements) {
40 return '';
41 }
42
43 $template = '<div class="%s">%s</div>';
44
45 return sprintf($template, implode(' ', $this->get_classes()), implode("\n", $elements));
46 }
47
48 private function get_elements(): ?array
49 {
50 if ($this->is_multiple()) {
51 $this->set_name($this->get_name() . '[]');
52 }
53
54 $options = $this->get_options();
55
56 if (empty($options)) {
57 return null;
58 }
59
60 $elements = [];
61
62 $value = (array)$this->get_value();
63
64 foreach ($options as $key => $label) {
65 $input = new Input((string)$this->get_name());
66
67 $input
68 ->set_value($key)
69 ->set_type($this->get_type())
70 ->set_id($this->get_id() . '-' . $key);
71
72 if (in_array($key, $value, true)) {
73 $input->set_attribute('checked', 'checked');
74 }
75
76 if ($this->disabled) {
77 $input->set_attribute('disabled', 'disabled');
78 }
79
80 $attributes = $this->get_attributes();
81
82 $elements[] = sprintf(
83 '<label %s>%s%s</label>',
84 $this->get_attributes_as_string($attributes),
85 $input->render(),
86 esc_html($label)
87 );
88 }
89
90 if ($description = $this->render_description()) {
91 $elements[] = $description;
92 }
93
94 return $elements;
95 }
96
97 public function set_multiple(bool $multiple): self
98 {
99 $this->multiple = $multiple;
100
101 return $this;
102 }
103
104 public function is_multiple(): bool
105 {
106 if (empty($this->multiple)) {
107 return false;
108 }
109
110 return $this->multiple;
111 }
112
113 public function set_vertical(bool $vertical): self
114 {
115 $this->vertical = $vertical;
116
117 return $this;
118 }
119
120 public function is_vertical(): bool
121 {
122 if (empty($this->vertical)) {
123 return false;
124 }
125
126 return $this->vertical;
127 }
128
129 public function set_disabled(bool $disabled): self
130 {
131 $this->disabled = $disabled;
132
133 return $this;
134 }
135
136 }
137