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