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