PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / classes / fields / boolean.php
pods / classes / fields Last commit date
avatar.php 2 weeks ago boolean.php 2 weeks ago code.php 2 weeks ago color.php 2 weeks ago comment.php 2 weeks ago currency.php 2 weeks ago date.php 2 weeks ago datetime.php 2 weeks ago email.php 2 weeks ago file.php 2 weeks ago heading.php 2 weeks ago html.php 2 weeks ago link.php 2 weeks ago number.php 2 weeks ago oembed.php 2 weeks ago paragraph.php 2 weeks ago password.php 2 weeks ago phone.php 2 weeks ago pick.php 2 weeks ago slug.php 2 weeks ago taxonomy.php 2 weeks ago text.php 2 weeks ago time.php 2 weeks ago website.php 2 weeks ago wysiwyg.php 2 weeks ago
boolean.php
290 lines
1 <?php
2
3 /**
4 * Handles boolean field type data and operations.
5 *
6 * @package Pods\Fields
7 */
8 class PodsField_Boolean extends PodsField {
9
10 /**
11 * {@inheritdoc}
12 */
13 public static $type = 'boolean';
14
15 /**
16 * {@inheritdoc}
17 */
18 public static $label = 'Yes / No';
19
20 /**
21 * {@inheritdoc}
22 */
23 public static $prepare = '%d';
24
25 /**
26 * {@inheritdoc}
27 */
28 public function setup() {
29
30 static::$label = __( 'Yes / No', 'pods' );
31 }
32
33 /**
34 * {@inheritdoc}
35 */
36 public function options() {
37
38 $options = array(
39 static::$type . '_format_type' => array(
40 'label' => __( 'Input Type', 'pods' ),
41 'default' => 'checkbox',
42 'type' => 'pick',
43 'data' => array(
44 'checkbox' => __( 'Checkbox', 'pods' ),
45 'radio' => __( 'Radio Buttons', 'pods' ),
46 'dropdown' => __( 'Drop Down', 'pods' ),
47 ),
48 'pick_show_select_text' => 0,
49 'dependency' => true,
50 ),
51 static::$type . '_yes_label' => array(
52 'label' => __( 'Yes Label', 'pods' ),
53 'default' => __( 'Yes', 'pods' ),
54 'type' => 'text',
55 ),
56 static::$type . '_no_label' => array(
57 'label' => __( 'No Label', 'pods' ),
58 'default' => __( 'No', 'pods' ),
59 'type' => 'text',
60 ),
61 );
62
63 return $options;
64 }
65
66 /**
67 * {@inheritdoc}
68 */
69 public function schema( $options = null ) {
70
71 $schema = 'BOOL DEFAULT 0';
72
73 return $schema;
74 }
75
76 /**
77 * {@inheritdoc}
78 */
79 public function is_empty( $value = null ) {
80
81 $is_empty = false;
82
83 // is_empty() is used for if/else statements. Value should be true to pass.
84 $value = $this->pre_save( $value );
85
86 if ( ! $value ) {
87 $is_empty = true;
88 }
89
90 return $is_empty;
91
92 }
93
94 /**
95 * {@inheritdoc}
96 */
97 public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
98
99 $yesno = array(
100 1 => pods_v( static::$type . '_yes_label', $options ),
101 0 => pods_v( static::$type . '_no_label', $options ),
102 );
103
104 // Deprecated handling for 1.x
105 if ( ! parent::$deprecated && isset( $yesno[ (int) $value ] ) ) {
106 $value = $yesno[ (int) $value ];
107 }
108
109 return $value;
110 }
111
112 /**
113 * {@inheritdoc}
114 */
115 public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
116
117 $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options;
118 $form_field_type = PodsForm::$field_type;
119
120 if ( is_array( $value ) ) {
121 if ( ! empty( $value ) ) {
122 $value = true;
123 } else {
124 $value = false;
125 }
126 }
127
128 $field_type = 'checkbox';
129
130 if ( 'radio' === pods_v( static::$type . '_format_type', $options ) ) {
131 $field_type = 'radio';
132 } elseif ( 'dropdown' === pods_v( static::$type . '_format_type', $options ) ) {
133 $field_type = 'select';
134 }
135
136 if ( isset( $options['name'] ) && ! pods_permission( $options ) ) {
137 if ( pods_v( 'read_only', $options, false ) ) {
138 $options['readonly'] = true;
139 } else {
140 return;
141 }
142 } elseif ( ! pods_has_permissions( $options ) && pods_v( 'read_only', $options, false ) ) {
143 $options['readonly'] = true;
144 }
145
146 if ( 1 === $value || '1' === $value || true === $value ) {
147 $value = 1;
148 } else {
149 $value = 0;
150 }
151
152 if ( ! empty( $options['disable_dfv'] ) ) {
153 return pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) );
154 }
155
156 $type = pods_v( 'type', $options, static::$type );
157
158 $args = compact( array_keys( get_defined_vars() ) );
159 $args = (object) $args;
160
161 $this->render_input_script( $args );
162 }
163
164 /**
165 * {@inheritdoc}
166 */
167 public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
168
169 if ( 'checkbox' !== pods_v( static::$type . '_format_type', $options ) ) {
170 $data = array(
171 1 => pods_v( static::$type . '_yes_label', $options ),
172 0 => pods_v( static::$type . '_no_label', $options ),
173 );
174 } else {
175 $data = array(
176 1 => pods_v( static::$type . '_yes_label', $options ),
177 );
178 }
179
180 return $data;
181 }
182
183 /**
184 * {@inheritdoc}
185 */
186 public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
187 $validate = parent::validate( $value, $name, $options, $fields, $pod, $id, $params );
188
189 if ( ! $this->is_required( $options ) ) {
190 // Any value can be parsed to boolean.
191 return $validate;
192 }
193
194 $errors = array();
195
196 if ( is_array( $validate ) ) {
197 $errors = $validate;
198 }
199
200 $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
201
202 $yes_required = ( 'checkbox' === pods_v( static::$type . '_format_type', $options ) );
203
204 if ( $yes_required && ! $check ) {
205 $errors[] = __( 'This field is required.', 'pods' );
206 }
207
208 if ( ! empty( $errors ) ) {
209 return $errors;
210 }
211
212 return $validate;
213 }
214
215 /**
216 * Replicates filter_var() with `FILTER_VALIDATE_BOOLEAN` and adds custom input for yes/no values.
217 *
218 * {@inheritdoc}
219 */
220 public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
221
222 $yes = strtolower( pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ) );
223 $no = strtolower( pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ) );
224
225 if ( is_string( $value ) ) {
226 $value = strtolower( $value );
227 }
228
229 if ( $yes === $value ) {
230 $value = 1;
231 } else {
232 // Validate: 1, "1", true, "true", "on", and "yes" as 1, all others are 0.
233 $value = (int) filter_var( $value, FILTER_VALIDATE_BOOLEAN );
234 }
235
236 return $value;
237 }
238
239 /**
240 * {@inheritdoc}
241 */
242 public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
243
244 $yesno = array(
245 1 => pods_v( static::$type . '_yes_label', $options, __( 'Yes', 'pods' ), true ),
246 0 => pods_v( static::$type . '_no_label', $options, __( 'No', 'pods' ), true ),
247 );
248
249 if ( isset( $yesno[ (int) $value ] ) ) {
250 $value = strip_tags( $yesno[ (int) $value ], '<strong><a><em><span><img>' );
251 }
252
253 return $value;
254 }
255
256 /**
257 * {@inheritdoc}
258 */
259 public function build_dfv_field_item_data( $args ) {
260 if ( empty( $args->options['data'] ) || ! is_array( $args->options['data'] ) ) {
261 return [];
262 }
263
264 $boolean_data = $args->options['data'];
265
266 $value = 0;
267
268 // If we have values, let's cast them.
269 if ( isset( $args->value ) ) {
270 $value = (int) $args->value;
271 }
272
273 $data = [];
274
275 foreach ( $boolean_data as $key => $label ) {
276 $data[] = [
277 'id' => esc_html( $key ),
278 'icon' => '',
279 'name' => wp_strip_all_tags( html_entity_decode( $label ) ),
280 'edit_link' => '',
281 'link' => '',
282 'download' => '',
283 'selected' => (int) $key === $value,
284 ];
285 }
286
287 return $data;
288 }
289 }
290