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-text.php
209 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'acf_field_text' ) ) : |
| 4 | |
| 5 | class acf_field_text 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 = 'text'; |
| 22 | $this->label = __( 'Text', 'acf' ); |
| 23 | $this->description = __( 'A basic text input, useful for storing single string values.', 'acf' ); |
| 24 | $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-text.png'; |
| 25 | $this->doc_url = 'https://www.advancedcustomfields.com/resources/text/'; |
| 26 | $this->defaults = array( |
| 27 | 'default_value' => '', |
| 28 | 'maxlength' => '', |
| 29 | 'placeholder' => '', |
| 30 | 'prepend' => '', |
| 31 | 'append' => '', |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Create the HTML interface for your field |
| 38 | * |
| 39 | * @param $field - an array holding all the field's data |
| 40 | * |
| 41 | * @type action |
| 42 | * @since 3.6 |
| 43 | * @date 23/01/13 |
| 44 | */ |
| 45 | function render_field( $field ) { |
| 46 | $html = ''; |
| 47 | |
| 48 | // Prepend text. |
| 49 | if ( $field['prepend'] !== '' ) { |
| 50 | $field['class'] .= ' acf-is-prepended'; |
| 51 | $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>'; |
| 52 | } |
| 53 | |
| 54 | // Append text. |
| 55 | if ( $field['append'] !== '' ) { |
| 56 | $field['class'] .= ' acf-is-appended'; |
| 57 | $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>'; |
| 58 | } |
| 59 | |
| 60 | // Input. |
| 61 | $input_attrs = array(); |
| 62 | foreach ( array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'maxlength', 'pattern', 'readonly', 'disabled', 'required' ) as $k ) { |
| 63 | if ( isset( $field[ $k ] ) ) { |
| 64 | $input_attrs[ $k ] = $field[ $k ]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ( isset( $field['input-data'] ) && is_array( $field['input-data'] ) ) { |
| 69 | foreach ( $field['input-data'] as $name => $attr ) { |
| 70 | $input_attrs[ 'data-' . $name ] = $attr; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | $html .= '<div class="acf-input-wrap">' . acf_get_text_input( acf_filter_attrs( $input_attrs ) ) . '</div>'; |
| 75 | |
| 76 | // Display. |
| 77 | echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- only safe HTML output generated and escaped by functions above. |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Create extra options for your field. This is rendered when editing a field. |
| 83 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 84 | * |
| 85 | * @param $field - an array holding all the field's data |
| 86 | * |
| 87 | * @type action |
| 88 | * @since 3.6 |
| 89 | * @date 23/01/13 |
| 90 | */ |
| 91 | function render_field_settings( $field ) { |
| 92 | acf_render_field_setting( |
| 93 | $field, |
| 94 | array( |
| 95 | 'label' => __( 'Default Value', 'acf' ), |
| 96 | 'instructions' => __( 'Appears when creating a new post', 'acf' ), |
| 97 | 'type' => 'text', |
| 98 | 'name' => 'default_value', |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Renders the field settings used in the "Validation" tab. |
| 105 | * |
| 106 | * @since 6.0 |
| 107 | * |
| 108 | * @param array $field The field settings array. |
| 109 | * @return void |
| 110 | */ |
| 111 | function render_field_validation_settings( $field ) { |
| 112 | acf_render_field_setting( |
| 113 | $field, |
| 114 | array( |
| 115 | 'label' => __( 'Character Limit', 'acf' ), |
| 116 | 'instructions' => __( 'Leave blank for no limit', 'acf' ), |
| 117 | 'type' => 'number', |
| 118 | 'name' => 'maxlength', |
| 119 | ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Renders the field settings used in the "Presentation" tab. |
| 125 | * |
| 126 | * @since 6.0 |
| 127 | * |
| 128 | * @param array $field The field settings array. |
| 129 | * @return void |
| 130 | */ |
| 131 | function render_field_presentation_settings( $field ) { |
| 132 | acf_render_field_setting( |
| 133 | $field, |
| 134 | array( |
| 135 | 'label' => __( 'Placeholder Text', 'acf' ), |
| 136 | 'instructions' => __( 'Appears within the input', 'acf' ), |
| 137 | 'type' => 'text', |
| 138 | 'name' => 'placeholder', |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | acf_render_field_setting( |
| 143 | $field, |
| 144 | array( |
| 145 | 'label' => __( 'Prepend', 'acf' ), |
| 146 | 'instructions' => __( 'Appears before the input', 'acf' ), |
| 147 | 'type' => 'text', |
| 148 | 'name' => 'prepend', |
| 149 | ) |
| 150 | ); |
| 151 | |
| 152 | acf_render_field_setting( |
| 153 | $field, |
| 154 | array( |
| 155 | 'label' => __( 'Append', 'acf' ), |
| 156 | 'instructions' => __( 'Appears after the input', 'acf' ), |
| 157 | 'type' => 'text', |
| 158 | 'name' => 'append', |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * validate_value |
| 165 | * |
| 166 | * Validates a field's value. |
| 167 | * |
| 168 | * @date 29/1/19 |
| 169 | * @since 5.7.11 |
| 170 | * |
| 171 | * @param (bool|string) Whether the value is vaid or not. |
| 172 | * @param mixed $value The field value. |
| 173 | * @param array $field The field array. |
| 174 | * @param string $input The HTML input name. |
| 175 | * @return (bool|string) |
| 176 | */ |
| 177 | function validate_value( $valid, $value, $field, $input ) { |
| 178 | |
| 179 | // Check maxlength |
| 180 | if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) { |
| 181 | return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] ); |
| 182 | } |
| 183 | |
| 184 | // Return. |
| 185 | return $valid; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Return the schema array for the REST API. |
| 190 | * |
| 191 | * @param array $field |
| 192 | * @return array |
| 193 | */ |
| 194 | function get_rest_schema( array $field ) { |
| 195 | $schema = parent::get_rest_schema( $field ); |
| 196 | |
| 197 | if ( ! empty( $field['maxlength'] ) ) { |
| 198 | $schema['maxLength'] = (int) $field['maxlength']; |
| 199 | } |
| 200 | |
| 201 | return $schema; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | // initialize |
| 207 | acf_register_field_type( 'acf_field_text' ); |
| 208 | endif; // class_exists check |
| 209 |