-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathCheckbox_Field.php
More file actions
107 lines (93 loc) · 2.09 KB
/
Copy pathCheckbox_Field.php
File metadata and controls
107 lines (93 loc) · 2.09 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
<?php
namespace Carbon_Fields\Field;
/**
* Single checkbox field class.
*/
class Checkbox_Field extends Field {
/**
* @{inheritDoc}
*/
protected $default_value = false;
/**
* The value that is saved in the database when
* this checkbox field is enabled.
*
* @var string
*/
protected $option_value = 'yes';
/**
* Allowed HTML attributes for the field.
*
* @var string[]
*/
protected $allowed_attributes = [ 'disabled' ];
/**
* Get the option value.
*
* @return string
*/
public function get_option_value() {
return $this->option_value;
}
/**
* Set the option value.
*
* @param string $value New value
* @return self $this
*/
public function set_option_value( $value ) {
$this->option_value = $value;
return $this;
}
/**
* {@inheritDoc}
*/
public function set_value_from_input( $input ) {
parent::set_value_from_input( $input );
if ( $this->get_value() !== $this->get_option_value() ) {
$this->set_value( '' );
}
return $this;
}
/**
* {@inheritDoc}
*/
public function set_value( $value ) {
if ( is_bool( $value ) ) {
$value = $value ? $this->get_option_value() : '';
}
return parent::set_value( $value );
}
/**
* Return a differently formatted value for end-users
*
* @return mixed
*/
public function get_formatted_value() {
return ( $this->get_value() === $this->get_option_value() );
}
/**
* Returns an array that holds the field data, suitable for JSON representation.
* In addition to default data, option value and label are added.
*
* @param bool $load Should the value be loaded from the database or use the value from the current instance.
* @return array
*/
public function to_json( $load ) {
$field_data = parent::to_json( $load );
$field_data = array_merge( $field_data, array(
'option_value' => $this->get_option_value(),
'option_label' => parent::get_label(),
) );
return $field_data;
}
/**
* Get the field label.
* Label here is empty because it is displayed in the front-end.
*
* @return string Label of the field.
*/
public function get_label() {
return '';
}
}