class-acf-field-text.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. if ( ! class_exists( 'acf_field_text' ) ) :
  3. class acf_field_text extends acf_field {
  4. /*
  5. * initialize
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'text';
  19. $this->label = __( 'Text', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'maxlength' => '',
  23. 'placeholder' => '',
  24. 'prepend' => '',
  25. 'append' => '',
  26. );
  27. }
  28. /*
  29. * render_field()
  30. *
  31. * Create the HTML interface for your field
  32. *
  33. * @param $field - an array holding all the field's data
  34. *
  35. * @type action
  36. * @since 3.6
  37. * @date 23/01/13
  38. */
  39. function render_field( $field ) {
  40. $html = '';
  41. // Prepend text.
  42. if ( $field['prepend'] !== '' ) {
  43. $field['class'] .= ' acf-is-prepended';
  44. $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
  45. }
  46. // Append text.
  47. if ( $field['append'] !== '' ) {
  48. $field['class'] .= ' acf-is-appended';
  49. $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
  50. }
  51. // Input.
  52. $input_attrs = array();
  53. foreach ( array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'maxlength', 'pattern', 'readonly', 'disabled', 'required' ) as $k ) {
  54. if ( isset( $field[ $k ] ) ) {
  55. $input_attrs[ $k ] = $field[ $k ];
  56. }
  57. }
  58. $html .= '<div class="acf-input-wrap">' . acf_get_text_input( acf_filter_attrs( $input_attrs ) ) . '</div>';
  59. // Display.
  60. echo $html;
  61. }
  62. /*
  63. * render_field_settings()
  64. *
  65. * Create extra options for your field. This is rendered when editing a field.
  66. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  67. *
  68. * @param $field - an array holding all the field's data
  69. *
  70. * @type action
  71. * @since 3.6
  72. * @date 23/01/13
  73. */
  74. function render_field_settings( $field ) {
  75. acf_render_field_setting(
  76. $field,
  77. array(
  78. 'label' => __( 'Default Value', 'acf' ),
  79. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  80. 'type' => 'text',
  81. 'name' => 'default_value',
  82. )
  83. );
  84. }
  85. /**
  86. * Renders the field settings used in the "Validation" tab.
  87. *
  88. * @since 6.0
  89. *
  90. * @param array $field The field settings array.
  91. * @return void
  92. */
  93. function render_field_validation_settings( $field ) {
  94. acf_render_field_setting(
  95. $field,
  96. array(
  97. 'label' => __( 'Character Limit', 'acf' ),
  98. 'instructions' => __( 'Leave blank for no limit', 'acf' ),
  99. 'type' => 'number',
  100. 'name' => 'maxlength',
  101. )
  102. );
  103. }
  104. /**
  105. * Renders the field settings used in the "Presentation" tab.
  106. *
  107. * @since 6.0
  108. *
  109. * @param array $field The field settings array.
  110. * @return void
  111. */
  112. function render_field_presentation_settings( $field ) {
  113. acf_render_field_setting(
  114. $field,
  115. array(
  116. 'label' => __( 'Placeholder Text', 'acf' ),
  117. 'instructions' => __( 'Appears within the input', 'acf' ),
  118. 'type' => 'text',
  119. 'name' => 'placeholder',
  120. )
  121. );
  122. acf_render_field_setting(
  123. $field,
  124. array(
  125. 'label' => __( 'Prepend', 'acf' ),
  126. 'instructions' => __( 'Appears before the input', 'acf' ),
  127. 'type' => 'text',
  128. 'name' => 'prepend',
  129. )
  130. );
  131. acf_render_field_setting(
  132. $field,
  133. array(
  134. 'label' => __( 'Append', 'acf' ),
  135. 'instructions' => __( 'Appears after the input', 'acf' ),
  136. 'type' => 'text',
  137. 'name' => 'append',
  138. )
  139. );
  140. }
  141. /**
  142. * validate_value
  143. *
  144. * Validates a field's value.
  145. *
  146. * @date 29/1/19
  147. * @since 5.7.11
  148. *
  149. * @param (bool|string) Whether the value is vaid or not.
  150. * @param mixed $value The field value.
  151. * @param array $field The field array.
  152. * @param string $input The HTML input name.
  153. * @return (bool|string)
  154. */
  155. function validate_value( $valid, $value, $field, $input ) {
  156. // Check maxlength
  157. if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) {
  158. return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] );
  159. }
  160. // Return.
  161. return $valid;
  162. }
  163. /**
  164. * Return the schema array for the REST API.
  165. *
  166. * @param array $field
  167. * @return array
  168. */
  169. function get_rest_schema( array $field ) {
  170. $schema = parent::get_rest_schema( $field );
  171. if ( ! empty( $field['maxlength'] ) ) {
  172. $schema['maxLength'] = (int) $field['maxlength'];
  173. }
  174. return $schema;
  175. }
  176. }
  177. // initialize
  178. acf_register_field_type( 'acf_field_text' );
  179. endif; // class_exists check