Нет описания

class-acf-field-textarea.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. if ( ! class_exists( 'acf_field_textarea' ) ) :
  3. class acf_field_textarea 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 = 'textarea';
  19. $this->label = __( 'Text Area', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'new_lines' => '',
  23. 'maxlength' => '',
  24. 'placeholder' => '',
  25. 'rows' => '',
  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. // vars
  41. $atts = array();
  42. $keys = array( 'id', 'class', 'name', 'value', 'placeholder', 'rows', 'maxlength' );
  43. $keys2 = array( 'readonly', 'disabled', 'required' );
  44. // rows
  45. if ( ! $field['rows'] ) {
  46. $field['rows'] = 8;
  47. }
  48. // atts (value="123")
  49. foreach ( $keys as $k ) {
  50. if ( isset( $field[ $k ] ) ) {
  51. $atts[ $k ] = $field[ $k ];
  52. }
  53. }
  54. // atts2 (disabled="disabled")
  55. foreach ( $keys2 as $k ) {
  56. if ( ! empty( $field[ $k ] ) ) {
  57. $atts[ $k ] = $k;
  58. }
  59. }
  60. // remove empty atts
  61. $atts = acf_clean_atts( $atts );
  62. // return
  63. acf_textarea_input( $atts );
  64. }
  65. /*
  66. * render_field_settings()
  67. *
  68. * Create extra options for your field. This is rendered when editing a field.
  69. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  70. *
  71. * @param $field - an array holding all the field's data
  72. *
  73. * @type action
  74. * @since 3.6
  75. * @date 23/01/13
  76. */
  77. function render_field_settings( $field ) {
  78. // default_value
  79. acf_render_field_setting(
  80. $field,
  81. array(
  82. 'label' => __( 'Default Value', 'acf' ),
  83. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  84. 'type' => 'textarea',
  85. 'name' => 'default_value',
  86. )
  87. );
  88. // placeholder
  89. acf_render_field_setting(
  90. $field,
  91. array(
  92. 'label' => __( 'Placeholder Text', 'acf' ),
  93. 'instructions' => __( 'Appears within the input', 'acf' ),
  94. 'type' => 'text',
  95. 'name' => 'placeholder',
  96. )
  97. );
  98. // maxlength
  99. acf_render_field_setting(
  100. $field,
  101. array(
  102. 'label' => __( 'Character Limit', 'acf' ),
  103. 'instructions' => __( 'Leave blank for no limit', 'acf' ),
  104. 'type' => 'number',
  105. 'name' => 'maxlength',
  106. )
  107. );
  108. // rows
  109. acf_render_field_setting(
  110. $field,
  111. array(
  112. 'label' => __( 'Rows', 'acf' ),
  113. 'instructions' => __( 'Sets the textarea height', 'acf' ),
  114. 'type' => 'number',
  115. 'name' => 'rows',
  116. 'placeholder' => 8,
  117. )
  118. );
  119. // formatting
  120. acf_render_field_setting(
  121. $field,
  122. array(
  123. 'label' => __( 'New Lines', 'acf' ),
  124. 'instructions' => __( 'Controls how new lines are rendered', 'acf' ),
  125. 'type' => 'select',
  126. 'name' => 'new_lines',
  127. 'choices' => array(
  128. 'wpautop' => __( 'Automatically add paragraphs', 'acf' ),
  129. 'br' => __( 'Automatically add &lt;br&gt;', 'acf' ),
  130. '' => __( 'No Formatting', 'acf' ),
  131. ),
  132. )
  133. );
  134. }
  135. /*
  136. * format_value()
  137. *
  138. * This filter is applied to the $value after it is loaded from the db and before it is returned to the template
  139. *
  140. * @type filter
  141. * @since 3.6
  142. * @date 23/01/13
  143. *
  144. * @param $value (mixed) the value which was loaded from the database
  145. * @param $post_id (mixed) the $post_id from which the value was loaded
  146. * @param $field (array) the field array holding all the field options
  147. *
  148. * @return $value (mixed) the modified value
  149. */
  150. function format_value( $value, $post_id, $field ) {
  151. // bail early if no value or not for template
  152. if ( empty( $value ) || ! is_string( $value ) ) {
  153. return $value;
  154. }
  155. // new lines
  156. if ( $field['new_lines'] == 'wpautop' ) {
  157. $value = wpautop( $value );
  158. } elseif ( $field['new_lines'] == 'br' ) {
  159. $value = nl2br( $value );
  160. }
  161. // return
  162. return $value;
  163. }
  164. /**
  165. * validate_value
  166. *
  167. * Validates a field's value.
  168. *
  169. * @date 29/1/19
  170. * @since 5.7.11
  171. *
  172. * @param (bool|string) Whether the value is vaid or not.
  173. * @param mixed $value The field value.
  174. * @param array $field The field array.
  175. * @param string $input The HTML input name.
  176. * @return (bool|string)
  177. */
  178. function validate_value( $valid, $value, $field, $input ) {
  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. // Return.
  184. return $valid;
  185. }
  186. /**
  187. * Return the schema array for the REST API.
  188. *
  189. * @param array $field
  190. * @return array
  191. */
  192. function get_rest_schema( array $field ) {
  193. $schema = parent::get_rest_schema( $field );
  194. if ( ! empty( $field['maxlength'] ) ) {
  195. $schema['maxLength'] = (int) $field['maxlength'];
  196. }
  197. return $schema;
  198. }
  199. }
  200. // initialize
  201. acf_register_field_type( 'acf_field_textarea' );
  202. endif; // class_exists check