class-acf-field-email.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. if ( ! class_exists( 'acf_field_email' ) ) :
  3. class acf_field_email 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 = 'email';
  19. $this->label = __( 'Email', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'placeholder' => '',
  23. 'prepend' => '',
  24. 'append' => '',
  25. );
  26. }
  27. /*
  28. * render_field()
  29. *
  30. * Create the HTML interface for your field
  31. *
  32. * @param $field - an array holding all the field's data
  33. *
  34. * @type action
  35. * @since 3.6
  36. * @date 23/01/13
  37. */
  38. function render_field( $field ) {
  39. // vars
  40. $atts = array();
  41. $keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'pattern' );
  42. $keys2 = array( 'readonly', 'disabled', 'required', 'multiple' );
  43. $html = '';
  44. // prepend
  45. if ( $field['prepend'] !== '' ) {
  46. $field['class'] .= ' acf-is-prepended';
  47. $html .= '<div class="acf-input-prepend">' . acf_esc_html( $field['prepend'] ) . '</div>';
  48. }
  49. // append
  50. if ( $field['append'] !== '' ) {
  51. $field['class'] .= ' acf-is-appended';
  52. $html .= '<div class="acf-input-append">' . acf_esc_html( $field['append'] ) . '</div>';
  53. }
  54. // atts (value="123")
  55. foreach ( $keys as $k ) {
  56. if ( isset( $field[ $k ] ) ) {
  57. $atts[ $k ] = $field[ $k ];
  58. }
  59. }
  60. // atts2 (disabled="disabled")
  61. foreach ( $keys2 as $k ) {
  62. if ( ! empty( $field[ $k ] ) ) {
  63. $atts[ $k ] = $k;
  64. }
  65. }
  66. // remove empty atts
  67. $atts = acf_clean_atts( $atts );
  68. // render
  69. $html .= '<div class="acf-input-wrap">' . acf_get_text_input( $atts ) . '</div>';
  70. // return
  71. echo $html;
  72. }
  73. /*
  74. * render_field_settings()
  75. *
  76. * Create extra options for your field. This is rendered when editing a field.
  77. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  78. *
  79. * @type action
  80. * @since 3.6
  81. * @date 23/01/13
  82. *
  83. * @param $field - an array holding all the field's data
  84. */
  85. function render_field_settings( $field ) {
  86. acf_render_field_setting(
  87. $field,
  88. array(
  89. 'label' => __( 'Default Value', 'acf' ),
  90. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  91. 'type' => 'text',
  92. 'name' => 'default_value',
  93. )
  94. );
  95. }
  96. /**
  97. * Renders the field settings used in the "Presentation" tab.
  98. *
  99. * @since 6.0
  100. *
  101. * @param array $field The field settings array.
  102. * @return void
  103. */
  104. function render_field_presentation_settings( $field ) {
  105. acf_render_field_setting(
  106. $field,
  107. array(
  108. 'label' => __( 'Placeholder Text', 'acf' ),
  109. 'instructions' => __( 'Appears within the input', 'acf' ),
  110. 'type' => 'text',
  111. 'name' => 'placeholder',
  112. )
  113. );
  114. acf_render_field_setting(
  115. $field,
  116. array(
  117. 'label' => __( 'Prepend', 'acf' ),
  118. 'instructions' => __( 'Appears before the input', 'acf' ),
  119. 'type' => 'text',
  120. 'name' => 'prepend',
  121. )
  122. );
  123. acf_render_field_setting(
  124. $field,
  125. array(
  126. 'label' => __( 'Append', 'acf' ),
  127. 'instructions' => __( 'Appears after the input', 'acf' ),
  128. 'type' => 'text',
  129. 'name' => 'append',
  130. )
  131. );
  132. }
  133. /**
  134. * Validate the email value. If this method returns TRUE, the input value is valid. If
  135. * FALSE or a string is returned, the input value is invalid and the user is shown a
  136. * notice. If a string is returned, the string is show as the message text.
  137. *
  138. * @param bool $valid Whether the value is valid.
  139. * @param mixed $value The field value.
  140. * @param array $field The field array.
  141. * @param string $input The request variable name for the inbound field.
  142. *
  143. * @return bool|string
  144. */
  145. public function validate_value( $valid, $value, $field, $input ) {
  146. $flags = defined( 'FILTER_FLAG_EMAIL_UNICODE' ) ? FILTER_FLAG_EMAIL_UNICODE : 0;
  147. if ( $value && filter_var( wp_unslash( $value ), FILTER_VALIDATE_EMAIL, $flags ) === false ) {
  148. return sprintf( __( "'%s' is not a valid email address", 'acf' ), esc_html( $value ) );
  149. }
  150. return $valid;
  151. }
  152. /**
  153. * Return the schema array for the REST API.
  154. *
  155. * @param array $field
  156. * @return array
  157. */
  158. public function get_rest_schema( array $field ) {
  159. $schema = parent::get_rest_schema( $field );
  160. $schema['format'] = 'email';
  161. return $schema;
  162. }
  163. }
  164. // initialize
  165. acf_register_field_type( 'acf_field_email' );
  166. endif; // class_exists check