class-acf-field-url.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. if ( ! class_exists( 'acf_field_url' ) ) :
  3. class acf_field_url 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 = 'url';
  19. $this->label = __( 'Url', 'acf' );
  20. $this->defaults = array(
  21. 'default_value' => '',
  22. 'placeholder' => '',
  23. );
  24. }
  25. /*
  26. * render_field()
  27. *
  28. * Create the HTML interface for your field
  29. *
  30. * @param $field - an array holding all the field's data
  31. *
  32. * @type action
  33. * @since 3.6
  34. * @date 23/01/13
  35. */
  36. function render_field( $field ) {
  37. // vars
  38. $atts = array();
  39. $keys = array( 'type', 'id', 'class', 'name', 'value', 'placeholder', 'pattern' );
  40. $keys2 = array( 'readonly', 'disabled', 'required' );
  41. $html = '';
  42. // atts (value="123")
  43. foreach ( $keys as $k ) {
  44. if ( isset( $field[ $k ] ) ) {
  45. $atts[ $k ] = $field[ $k ];
  46. }
  47. }
  48. // atts2 (disabled="disabled")
  49. foreach ( $keys2 as $k ) {
  50. if ( ! empty( $field[ $k ] ) ) {
  51. $atts[ $k ] = $k;
  52. }
  53. }
  54. // remove empty atts
  55. $atts = acf_clean_atts( $atts );
  56. // render
  57. $html .= '<div class="acf-input-wrap acf-url">';
  58. $html .= '<i class="acf-icon -globe -small"></i>' . acf_get_text_input( $atts );
  59. $html .= '</div>';
  60. // return
  61. echo $html;
  62. }
  63. /*
  64. * render_field_settings()
  65. *
  66. * Create extra options for your field. This is rendered when editing a field.
  67. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  68. *
  69. * @type action
  70. * @since 3.6
  71. * @date 23/01/13
  72. *
  73. * @param $field - an array holding all the field's data
  74. */
  75. function render_field_settings( $field ) {
  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. }
  86. /**
  87. * Renders the field settings used in the "Presentation" tab.
  88. *
  89. * @since 6.0
  90. *
  91. * @param array $field The field settings array.
  92. * @return void
  93. */
  94. function render_field_presentation_settings( $field ) {
  95. acf_render_field_setting(
  96. $field,
  97. array(
  98. 'label' => __( 'Placeholder Text', 'acf' ),
  99. 'instructions' => __( 'Appears within the input', 'acf' ),
  100. 'type' => 'text',
  101. 'name' => 'placeholder',
  102. )
  103. );
  104. }
  105. /*
  106. * validate_value
  107. *
  108. * description
  109. *
  110. * @type function
  111. * @date 11/02/2014
  112. * @since 5.0.0
  113. *
  114. * @param $post_id (int)
  115. * @return $post_id (int)
  116. */
  117. function validate_value( $valid, $value, $field, $input ) {
  118. // bail early if empty
  119. if ( empty( $value ) ) {
  120. return $valid;
  121. }
  122. if ( strpos( $value, '://' ) !== false ) {
  123. // url
  124. } elseif ( strpos( $value, '//' ) === 0 ) {
  125. // protocol relative url
  126. } else {
  127. $valid = __( 'Value must be a valid URL', 'acf' );
  128. }
  129. // return
  130. return $valid;
  131. }
  132. /**
  133. * Return the schema array for the REST API.
  134. *
  135. * @param array $field
  136. * @return array
  137. */
  138. public function get_rest_schema( array $field ) {
  139. $schema = parent::get_rest_schema( $field );
  140. $schema['format'] = 'uri';
  141. return $schema;
  142. }
  143. }
  144. // initialize
  145. acf_register_field_type( 'acf_field_url' );
  146. endif; // class_exists check