Brak opisu

class-acf-field-link.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 acf_esc_attr_e( $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. // return_format
  129. acf_render_field_setting(
  130. $field,
  131. array(
  132. 'label' => __( 'Return Value', 'acf' ),
  133. 'instructions' => __( 'Specify the returned value on front end', 'acf' ),
  134. 'type' => 'radio',
  135. 'name' => 'return_format',
  136. 'layout' => 'horizontal',
  137. 'choices' => array(
  138. 'array' => __( 'Link Array', 'acf' ),
  139. 'url' => __( 'Link URL', 'acf' ),
  140. ),
  141. )
  142. );
  143. }
  144. /*
  145. * format_value()
  146. *
  147. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  148. *
  149. * @type filter
  150. * @since 3.6
  151. * @date 23/01/13
  152. *
  153. * @param $value (mixed) the value which was loaded from the database
  154. * @param $post_id (mixed) the $post_id from which the value was loaded
  155. * @param $field (array) the field array holding all the field options
  156. *
  157. * @return $value (mixed) the modified value
  158. */
  159. function format_value( $value, $post_id, $field ) {
  160. // bail early if no value
  161. if ( empty( $value ) ) {
  162. return $value;
  163. }
  164. // get link
  165. $link = $this->get_link( $value );
  166. // format value
  167. if ( $field['return_format'] == 'url' ) {
  168. return $link['url'];
  169. }
  170. // return link
  171. return $link;
  172. }
  173. /*
  174. * validate_value
  175. *
  176. * description
  177. *
  178. * @type function
  179. * @date 11/02/2014
  180. * @since 5.0.0
  181. *
  182. * @param $post_id (int)
  183. * @return $post_id (int)
  184. */
  185. function validate_value( $valid, $value, $field, $input ) {
  186. // bail early if not required
  187. if ( ! $field['required'] ) {
  188. return $valid;
  189. }
  190. // URL is required
  191. if ( empty( $value ) || empty( $value['url'] ) ) {
  192. return false;
  193. }
  194. // return
  195. return $valid;
  196. }
  197. /*
  198. * update_value()
  199. *
  200. * This filter is appied to the $value before it is updated in the db
  201. *
  202. * @type filter
  203. * @since 3.6
  204. * @date 23/01/13
  205. *
  206. * @param $value - the value which will be saved in the database
  207. * @param $post_id - the $post_id of which the value will be saved
  208. * @param $field - the field array holding all the field options
  209. *
  210. * @return $value - the modified value
  211. */
  212. function update_value( $value, $post_id, $field ) {
  213. // Check if value is an empty array and convert to empty string.
  214. if ( empty( $value ) || empty( $value['url'] ) ) {
  215. $value = '';
  216. }
  217. // return
  218. return $value;
  219. }
  220. /**
  221. * Return the schema array for the REST API.
  222. *
  223. * @param array $field
  224. * @return array
  225. */
  226. public function get_rest_schema( array $field ) {
  227. return array(
  228. 'type' => array( 'object', 'null' ),
  229. 'required' => ! empty( $field['required'] ),
  230. 'properties' => array(
  231. 'title' => array(
  232. 'type' => 'string',
  233. ),
  234. 'url' => array(
  235. 'type' => 'string',
  236. 'required' => true,
  237. 'format' => 'uri',
  238. ),
  239. 'target' => array(
  240. 'type' => 'string',
  241. ),
  242. ),
  243. );
  244. }
  245. }
  246. // initialize
  247. acf_register_field_type( 'acf_field_link' );
  248. endif; // class_exists check
  249. ?>