class-acf-field-accordion.php
1 year ago
class-acf-field-button-group.php
1 year ago
class-acf-field-checkbox.php
1 year ago
class-acf-field-color_picker.php
1 year ago
class-acf-field-date_picker.php
1 year ago
class-acf-field-date_time_picker.php
1 year ago
class-acf-field-email.php
1 year ago
class-acf-field-file.php
1 year ago
class-acf-field-google-map.php
1 year ago
class-acf-field-group.php
1 year ago
class-acf-field-icon_picker.php
1 year ago
class-acf-field-image.php
1 year ago
class-acf-field-link.php
1 year ago
class-acf-field-message.php
1 year ago
class-acf-field-number.php
1 year ago
class-acf-field-oembed.php
1 year ago
class-acf-field-output.php
1 year ago
class-acf-field-page_link.php
1 year ago
class-acf-field-password.php
1 year ago
class-acf-field-post_object.php
1 year ago
class-acf-field-radio.php
1 year ago
class-acf-field-range.php
1 year ago
class-acf-field-relationship.php
1 year ago
class-acf-field-select.php
1 year ago
class-acf-field-separator.php
1 year ago
class-acf-field-tab.php
1 year ago
class-acf-field-taxonomy.php
1 year ago
class-acf-field-text.php
1 year ago
class-acf-field-textarea.php
1 year ago
class-acf-field-time_picker.php
1 year ago
class-acf-field-true_false.php
1 year ago
class-acf-field-url.php
1 year ago
class-acf-field-user.php
1 year ago
class-acf-field-wysiwyg.php
1 year ago
class-acf-field.php
1 year ago
index.php
1 year ago
class-acf-field-true_false.php
302 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'acf_field_true_false' ) ) : |
| 4 | |
| 5 | class acf_field_true_false extends acf_field { |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * This function will setup the field type data |
| 10 | * |
| 11 | * @type function |
| 12 | * @date 5/03/2014 |
| 13 | * @since 5.0.0 |
| 14 | * |
| 15 | * @param n/a |
| 16 | * @return n/a |
| 17 | */ |
| 18 | function initialize() { |
| 19 | |
| 20 | // vars |
| 21 | $this->name = 'true_false'; |
| 22 | $this->label = __( 'True / False', 'acf' ); |
| 23 | $this->category = 'choice'; |
| 24 | $this->description = __( 'A toggle that allows you to pick a value of 1 or 0 (on or off, true or false, etc). Can be presented as a stylized switch or checkbox.', 'acf' ); |
| 25 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-true-false.png'; |
| 26 | $this->doc_url = 'https://www.advancedcustomfields.com/resources/true-false/'; |
| 27 | $this->defaults = array( |
| 28 | 'default_value' => 0, |
| 29 | 'message' => '', |
| 30 | 'ui' => 0, |
| 31 | 'ui_on_text' => '', |
| 32 | 'ui_off_text' => '', |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * Create the HTML interface for your field |
| 39 | * |
| 40 | * @param $field - an array holding all the field's data |
| 41 | * |
| 42 | * @type action |
| 43 | * @since 3.6 |
| 44 | * @date 23/01/13 |
| 45 | */ |
| 46 | function render_field( $field ) { |
| 47 | |
| 48 | // vars |
| 49 | $input = array( |
| 50 | 'type' => 'checkbox', |
| 51 | 'id' => $field['id'], |
| 52 | 'name' => $field['name'], |
| 53 | 'value' => '1', |
| 54 | 'class' => $field['class'], |
| 55 | 'autocomplete' => 'off', |
| 56 | ); |
| 57 | |
| 58 | $hidden = array( |
| 59 | 'name' => $field['name'], |
| 60 | 'value' => 0, |
| 61 | ); |
| 62 | |
| 63 | $active = $field['value'] ? true : false; |
| 64 | $switch = ''; |
| 65 | |
| 66 | // checked |
| 67 | if ( $active ) { |
| 68 | $input['checked'] = 'checked'; |
| 69 | } |
| 70 | |
| 71 | // ui |
| 72 | if ( $field['ui'] ) { |
| 73 | |
| 74 | // vars |
| 75 | if ( $field['ui_on_text'] === '' ) { |
| 76 | $field['ui_on_text'] = __( 'Yes', 'acf' ); |
| 77 | } |
| 78 | if ( $field['ui_off_text'] === '' ) { |
| 79 | $field['ui_off_text'] = __( 'No', 'acf' ); |
| 80 | } |
| 81 | |
| 82 | // update input |
| 83 | $input['class'] .= ' acf-switch-input'; |
| 84 | // $input['style'] = 'display:none;'; |
| 85 | $switch .= '<div class="acf-switch' . ( $active ? ' -on' : '' ) . '">'; |
| 86 | $switch .= '<span class="acf-switch-on">' . $field['ui_on_text'] . '</span>'; |
| 87 | $switch .= '<span class="acf-switch-off">' . $field['ui_off_text'] . '</span>'; |
| 88 | $switch .= '<div class="acf-switch-slider"></div>'; |
| 89 | $switch .= '</div>'; |
| 90 | } |
| 91 | |
| 92 | ?> |
| 93 | <div class="acf-true-false"> |
| 94 | <?php acf_hidden_input( $hidden ); ?> |
| 95 | <label> |
| 96 | <input <?php echo acf_esc_attrs( $input ); ?>/> |
| 97 | <?php |
| 98 | if ( $switch ) { |
| 99 | echo acf_esc_html( $switch );} |
| 100 | ?> |
| 101 | <?php |
| 102 | if ( $field['message'] ) : |
| 103 | ?> |
| 104 | <span class="message"><?php echo acf_esc_html( $field['message'] ); ?></span><?php endif; ?> |
| 105 | </label> |
| 106 | </div> |
| 107 | <?php |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Create extra options for your field. This is rendered when editing a field. |
| 113 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 114 | * |
| 115 | * @type action |
| 116 | * @since 3.6 |
| 117 | * @date 23/01/13 |
| 118 | * |
| 119 | * @param $field - an array holding all the field's data |
| 120 | */ |
| 121 | function render_field_settings( $field ) { |
| 122 | acf_render_field_setting( |
| 123 | $field, |
| 124 | array( |
| 125 | 'label' => __( 'Message', 'acf' ), |
| 126 | 'instructions' => __( 'Displays text alongside the checkbox', 'acf' ), |
| 127 | 'type' => 'text', |
| 128 | 'name' => 'message', |
| 129 | ) |
| 130 | ); |
| 131 | |
| 132 | acf_render_field_setting( |
| 133 | $field, |
| 134 | array( |
| 135 | 'label' => __( 'Default Value', 'acf' ), |
| 136 | 'instructions' => '', |
| 137 | 'type' => 'true_false', |
| 138 | 'name' => 'default_value', |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Renders the field settings used in the "Presentation" tab. |
| 145 | * |
| 146 | * @since 6.0 |
| 147 | * |
| 148 | * @param array $field The field settings array. |
| 149 | * @return void |
| 150 | */ |
| 151 | function render_field_presentation_settings( $field ) { |
| 152 | acf_render_field_setting( |
| 153 | $field, |
| 154 | array( |
| 155 | 'label' => __( 'On Text', 'acf' ), |
| 156 | 'instructions' => __( 'Text shown when active', 'acf' ), |
| 157 | 'type' => 'text', |
| 158 | 'name' => 'ui_on_text', |
| 159 | 'placeholder' => __( 'Yes', 'acf' ), |
| 160 | 'conditions' => array( |
| 161 | 'field' => 'ui', |
| 162 | 'operator' => '==', |
| 163 | 'value' => 1, |
| 164 | ), |
| 165 | ) |
| 166 | ); |
| 167 | |
| 168 | acf_render_field_setting( |
| 169 | $field, |
| 170 | array( |
| 171 | 'label' => __( 'Off Text', 'acf' ), |
| 172 | 'instructions' => __( 'Text shown when inactive', 'acf' ), |
| 173 | 'type' => 'text', |
| 174 | 'name' => 'ui_off_text', |
| 175 | 'placeholder' => __( 'No', 'acf' ), |
| 176 | 'conditions' => array( |
| 177 | 'field' => 'ui', |
| 178 | 'operator' => '==', |
| 179 | 'value' => 1, |
| 180 | ), |
| 181 | ) |
| 182 | ); |
| 183 | |
| 184 | acf_render_field_setting( |
| 185 | $field, |
| 186 | array( |
| 187 | 'label' => __( 'Stylized UI', 'acf' ), |
| 188 | 'instructions' => __( 'Use a stylized checkbox using select2', 'acf' ), |
| 189 | 'type' => 'true_false', |
| 190 | 'name' => 'ui', |
| 191 | 'ui' => 1, |
| 192 | 'class' => 'acf-field-object-true-false-ui', |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
| 199 | * |
| 200 | * @type filter |
| 201 | * @since 3.6 |
| 202 | * @date 23/01/13 |
| 203 | * |
| 204 | * @param $value (mixed) the value which was loaded from the database |
| 205 | * @param $post_id (mixed) the post_id from which the value was loaded |
| 206 | * @param $field (array) the field array holding all the field options |
| 207 | * |
| 208 | * @return $value (mixed) the modified value |
| 209 | */ |
| 210 | function format_value( $value, $post_id, $field ) { |
| 211 | |
| 212 | return empty( $value ) ? false : true; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * description |
| 218 | * |
| 219 | * @type function |
| 220 | * @date 11/02/2014 |
| 221 | * @since 5.0.0 |
| 222 | * |
| 223 | * @param $post_id (int) |
| 224 | * @return $post_id (int) |
| 225 | */ |
| 226 | function validate_value( $valid, $value, $field, $input ) { |
| 227 | |
| 228 | // bail early if not required |
| 229 | if ( ! $field['required'] ) { |
| 230 | return $valid; |
| 231 | } |
| 232 | |
| 233 | // value may be '0' |
| 234 | if ( ! $value ) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // return |
| 239 | return $valid; |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /** |
| 244 | * This function will translate field settings |
| 245 | * |
| 246 | * @type function |
| 247 | * @date 8/03/2016 |
| 248 | * @since 5.3.2 |
| 249 | * |
| 250 | * @param $field (array) |
| 251 | * @return $field |
| 252 | */ |
| 253 | function translate_field( $field ) { |
| 254 | |
| 255 | // translate |
| 256 | $field['message'] = acf_translate( $field['message'] ); |
| 257 | $field['ui_on_text'] = acf_translate( $field['ui_on_text'] ); |
| 258 | $field['ui_off_text'] = acf_translate( $field['ui_off_text'] ); |
| 259 | |
| 260 | // return |
| 261 | return $field; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Return the schema array for the REST API. |
| 266 | * |
| 267 | * @param array $field |
| 268 | * @return array |
| 269 | */ |
| 270 | public function get_rest_schema( array $field ) { |
| 271 | $schema = array( |
| 272 | 'type' => array( 'boolean', 'null' ), |
| 273 | 'required' => ! empty( $field['required'] ), |
| 274 | ); |
| 275 | |
| 276 | if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) { |
| 277 | $schema['default'] = (bool) $field['default_value']; |
| 278 | } |
| 279 | |
| 280 | return $schema; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Apply basic formatting to prepare the value for default REST output. |
| 285 | * |
| 286 | * @param mixed $value |
| 287 | * @param string|integer $post_id |
| 288 | * @param array $field |
| 289 | * @return mixed |
| 290 | */ |
| 291 | public function format_value_for_rest( $value, $post_id, array $field ) { |
| 292 | return (bool) $value; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | // initialize |
| 298 | acf_register_field_type( 'acf_field_true_false' ); |
| 299 | endif; // class_exists check |
| 300 | |
| 301 | ?> |
| 302 |