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