class-acf-field-link.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. if ( ! class_exists( 'acf_field_link' ) ) :
  3. class acf_field_link extends acf_field {
  4. /*
  5. * __construct
  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 = 'link';
  19. $this->label = __( 'Link', 'acf' );
  20. $this->category = 'relational';
  21. $this->defaults = array(
  22. 'return_format' => 'array',
  23. );
  24. }
  25. /*
  26. * get_link
  27. *
  28. * description
  29. *
  30. * @type function
  31. * @date 16/5/17
  32. * @since 5.5.13
  33. *
  34. * @param $post_id (int)
  35. * @return $post_id (int)
  36. */
  37. function get_link( $value = '' ) {
  38. // vars
  39. $link = array(
  40. 'title' => '',
  41. 'url' => '',
  42. 'target' => '',
  43. );
  44. // array (ACF 5.6.0)
  45. if ( is_array( $value ) ) {
  46. $link = array_merge( $link, $value );
  47. // post id (ACF < 5.6.0)
  48. } elseif ( is_numeric( $value ) ) {
  49. $link['title'] = get_the_title( $value );
  50. $link['url'] = get_permalink( $value );
  51. // string (ACF < 5.6.0)
  52. } elseif ( is_string( $value ) ) {
  53. $link['url'] = $value;
  54. }
  55. // return
  56. return $link;
  57. }
  58. /*
  59. * render_field()
  60. *
  61. * Create the HTML interface for your field
  62. *
  63. * @param $field - an array holding all the field's data
  64. *
  65. * @type action
  66. * @since 3.6
  67. * @date 23/01/13
  68. */
  69. function render_field( $field ) {
  70. // vars
  71. $div = array(
  72. 'id' => $field['id'],
  73. 'class' => $field['class'] . ' acf-link',
  74. );
  75. // render scripts/styles
  76. acf_enqueue_uploader();
  77. // get link
  78. $link = $this->get_link( $field['value'] );
  79. // classes
  80. if ( $link['url'] ) {
  81. $div['class'] .= ' -value';
  82. }
  83. if ( $link['target'] === '_blank' ) {
  84. $div['class'] .= ' -external';
  85. }
  86. /*
  87. <textarea id="<?php echo esc_attr($field['id']); ?>-textarea"><?php
  88. echo esc_textarea('<a href="'.$link['url'].'" target="'.$link['target'].'">'.$link['title'].'</a>');
  89. ?></textarea>*/
  90. ?>
  91. <div <?php echo acf_esc_attrs( $div ); ?>>
  92. <div class="acf-hidden">
  93. <a class="link-node" href="<?php echo esc_url( $link['url'] ); ?>" target="<?php echo esc_attr( $link['target'] ); ?>"><?php echo esc_html( $link['title'] ); ?></a>
  94. <?php foreach ( $link as $k => $v ) : ?>
  95. <?php
  96. acf_hidden_input(
  97. array(
  98. 'class' => "input-$k",
  99. 'name' => $field['name'] . "[$k]",
  100. 'value' => $v,
  101. )
  102. );
  103. ?>
  104. <?php endforeach; ?>
  105. </div>
  106. <a href="#" class="button" data-name="add" target=""><?php _e( 'Select Link', 'acf' ); ?></a>
  107. <div class="link-wrap">
  108. <span class="link-title"><?php echo esc_html( $link['title'] ); ?></span>
  109. <a class="link-url" href="<?php echo esc_url( $link['url'] ); ?>" target="_blank"><?php echo esc_html( $link['url'] ); ?></a>
  110. <i class="acf-icon -link-ext acf-js-tooltip" title="<?php _e( 'Opens in a new window/tab', 'acf' ); ?>"></i><a class="acf-icon -pencil -clear acf-js-tooltip" data-name="edit" href="#" title="<?php _e( 'Edit', 'acf' ); ?>"></a><a class="acf-icon -cancel -clear acf-js-tooltip" data-name="remove" href="#" title="<?php _e( 'Remove', 'acf' ); ?>"></a>
  111. </div>
  112. </div>
  113. <?php
  114. }
  115. /*
  116. * render_field_settings()
  117. *
  118. * Create extra options for your field. This is rendered when editing a field.
  119. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  120. *
  121. * @type action
  122. * @since 3.6
  123. * @date 23/01/13
  124. *
  125. * @param $field - an array holding all the field's data
  126. */
  127. function render_field_settings( $field ) {
  128. acf_render_field_setting(
  129. $field,
  130. array(
  131. 'label' => __( 'Return Value', 'acf' ),
  132. 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
  133. 'type' => 'radio',
  134. 'name' => 'return_format',
  135. 'layout' => 'horizontal',
  136. 'choices' => array(
  137. 'array' => __( 'Link Array', 'acf' ),
  138. 'url' => __( 'Link URL', 'acf' ),
  139. ),
  140. )
  141. );
  142. }
  143. /*
  144. * format_value()
  145. *
  146. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  147. *
  148. * @type filter
  149. * @since 3.6
  150. * @date 23/01/13
  151. *
  152. * @param $value (mixed) the value which was loaded from the database
  153. * @param $post_id (mixed) the $post_id from which the value was loaded
  154. * @param $field (array) the field array holding all the field options
  155. *
  156. * @return $value (mixed) the modified value
  157. */
  158. function format_value( $value, $post_id, $field ) {
  159. // bail early if no value
  160. if ( empty( $value ) ) {
  161. return $value;
  162. }
  163. // get link
  164. $link = $this->get_link( $value );
  165. // format value
  166. if ( $field['return_format'] == 'url' ) {
  167. return $link['url'];
  168. }
  169. // return link
  170. return $link;
  171. }
  172. /*
  173. * validate_value
  174. *
  175. * description
  176. *
  177. * @type function
  178. * @date 11/02/2014
  179. * @since 5.0.0
  180. *
  181. * @param $post_id (int)
  182. * @return $post_id (int)
  183. */
  184. function validate_value( $valid, $value, $field, $input ) {
  185. // bail early if not required
  186. if ( ! $field['required'] ) {
  187. return $valid;
  188. }
  189. // URL is required
  190. if ( empty( $value ) || empty( $value['url'] ) ) {
  191. return false;
  192. }
  193. // return
  194. return $valid;
  195. }
  196. /*
  197. * update_value()
  198. *
  199. * This filter is appied to the $value before it is updated in the db
  200. *
  201. * @type filter
  202. * @since 3.6
  203. * @date 23/01/13
  204. *
  205. * @param $value - the value which will be saved in the database
  206. * @param $post_id - the $post_id of which the value will be saved
  207. * @param $field - the field array holding all the field options
  208. *
  209. * @return $value - the modified value
  210. */
  211. function update_value( $value, $post_id, $field ) {
  212. // Check if value is an empty array and convert to empty string.
  213. if ( empty( $value ) || empty( $value['url'] ) ) {
  214. $value = '';
  215. }
  216. // return
  217. return $value;
  218. }
  219. /**
  220. * Return the schema array for the REST API.
  221. *
  222. * @param array $field
  223. * @return array
  224. */
  225. public function get_rest_schema( array $field ) {
  226. return array(
  227. 'type' => array( 'object', 'null' ),
  228. 'required' => ! empty( $field['required'] ),
  229. 'properties' => array(
  230. 'title' => array(
  231. 'type' => 'string',
  232. ),
  233. 'url' => array(
  234. 'type' => 'string',
  235. 'required' => true,
  236. 'format' => 'uri',
  237. ),
  238. 'target' => array(
  239. 'type' => 'string',
  240. ),
  241. ),
  242. );
  243. }
  244. }
  245. // initialize
  246. acf_register_field_type( 'acf_field_link' );
  247. endif; // class_exists check
  248. ?>