Keine Beschreibung

class-acf-field-url.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. // default_value
  77. acf_render_field_setting(
  78. $field,
  79. array(
  80. 'label' => __( 'Default Value', 'acf' ),
  81. 'instructions' => __( 'Appears when creating a new post', 'acf' ),
  82. 'type' => 'text',
  83. 'name' => 'default_value',
  84. )
  85. );
  86. // placeholder
  87. acf_render_field_setting(
  88. $field,
  89. array(
  90. 'label' => __( 'Placeholder Text', 'acf' ),
  91. 'instructions' => __( 'Appears within the input', 'acf' ),
  92. 'type' => 'text',
  93. 'name' => 'placeholder',
  94. )
  95. );
  96. }
  97. /*
  98. * validate_value
  99. *
  100. * description
  101. *
  102. * @type function
  103. * @date 11/02/2014
  104. * @since 5.0.0
  105. *
  106. * @param $post_id (int)
  107. * @return $post_id (int)
  108. */
  109. function validate_value( $valid, $value, $field, $input ) {
  110. // bail early if empty
  111. if ( empty( $value ) ) {
  112. return $valid;
  113. }
  114. if ( strpos( $value, '://' ) !== false ) {
  115. // url
  116. } elseif ( strpos( $value, '//' ) === 0 ) {
  117. // protocol relative url
  118. } else {
  119. $valid = __( 'Value must be a valid URL', 'acf' );
  120. }
  121. // return
  122. return $valid;
  123. }
  124. /**
  125. * Return the schema array for the REST API.
  126. *
  127. * @param array $field
  128. * @return array
  129. */
  130. public function get_rest_schema( array $field ) {
  131. $schema = parent::get_rest_schema( $field );
  132. $schema['format'] = 'uri';
  133. return $schema;
  134. }
  135. }
  136. // initialize
  137. acf_register_field_type( 'acf_field_url' );
  138. endif; // class_exists check