暫無描述

class-acf-field-text.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // default_value
  76. acf_render_field_setting(
  77. $field,
  78. array(
  79. 'label' => __( 'Default Value', 'acf' ),
  80. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  81. 'type' => 'text',
  82. 'name' => 'default_value',
  83. )
  84. );
  85. // placeholder
  86. acf_render_field_setting(
  87. $field,
  88. array(
  89. 'label' => __( 'Placeholder Text', 'acf' ),
  90. 'instructions' => __( 'Appears within the input', 'acf' ),
  91. 'type' => 'text',
  92. 'name' => 'placeholder',
  93. )
  94. );
  95. // prepend
  96. acf_render_field_setting(
  97. $field,
  98. array(
  99. 'label' => __( 'Prepend', 'acf' ),
  100. 'instructions' => __( 'Appears before the input', 'acf' ),
  101. 'type' => 'text',
  102. 'name' => 'prepend',
  103. )
  104. );
  105. // append
  106. acf_render_field_setting(
  107. $field,
  108. array(
  109. 'label' => __( 'Append', 'acf' ),
  110. 'instructions' => __( 'Appears after the input', 'acf' ),
  111. 'type' => 'text',
  112. 'name' => 'append',
  113. )
  114. );
  115. // maxlength
  116. acf_render_field_setting(
  117. $field,
  118. array(
  119. 'label' => __( 'Character Limit', 'acf' ),
  120. 'instructions' => __( 'Leave blank for no limit', 'acf' ),
  121. 'type' => 'number',
  122. 'name' => 'maxlength',
  123. )
  124. );
  125. }
  126. /**
  127. * validate_value
  128. *
  129. * Validates a field's value.
  130. *
  131. * @date 29/1/19
  132. * @since 5.7.11
  133. *
  134. * @param (bool|string) Whether the value is vaid or not.
  135. * @param mixed $value The field value.
  136. * @param array $field The field array.
  137. * @param string $input The HTML input name.
  138. * @return (bool|string)
  139. */
  140. function validate_value( $valid, $value, $field, $input ) {
  141. // Check maxlength
  142. if ( $field['maxlength'] && ( acf_strlen( $value ) > $field['maxlength'] ) ) {
  143. return sprintf( __( 'Value must not exceed %d characters', 'acf' ), $field['maxlength'] );
  144. }
  145. // Return.
  146. return $valid;
  147. }
  148. /**
  149. * Return the schema array for the REST API.
  150. *
  151. * @param array $field
  152. * @return array
  153. */
  154. function get_rest_schema( array $field ) {
  155. $schema = parent::get_rest_schema( $field );
  156. if ( ! empty( $field['maxlength'] ) ) {
  157. $schema['maxLength'] = (int) $field['maxlength'];
  158. }
  159. return $schema;
  160. }
  161. }
  162. // initialize
  163. acf_register_field_type( 'acf_field_text' );
  164. endif; // class_exists check