class-acf-field-relationship.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. <?php
  2. if ( ! class_exists( 'acf_field_relationship' ) ) :
  3. class acf_field_relationship 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 = 'relationship';
  19. $this->label = __( 'Relationship', 'acf' );
  20. $this->category = 'relational';
  21. $this->defaults = array(
  22. 'post_type' => array(),
  23. 'taxonomy' => array(),
  24. 'min' => 0,
  25. 'max' => 0,
  26. 'filters' => array( 'search', 'post_type', 'taxonomy' ),
  27. 'elements' => array(),
  28. 'return_format' => 'object',
  29. );
  30. // extra
  31. add_action( 'wp_ajax_acf/fields/relationship/query', array( $this, 'ajax_query' ) );
  32. add_action( 'wp_ajax_nopriv_acf/fields/relationship/query', array( $this, 'ajax_query' ) );
  33. }
  34. /*
  35. * input_admin_enqueue_scripts
  36. *
  37. * description
  38. *
  39. * @type function
  40. * @date 16/12/2015
  41. * @since 5.3.2
  42. *
  43. * @param $post_id (int)
  44. * @return $post_id (int)
  45. */
  46. function input_admin_enqueue_scripts() {
  47. // localize
  48. acf_localize_text(
  49. array(
  50. // 'Minimum values reached ( {min} values )' => __('Minimum values reached ( {min} values )', 'acf'),
  51. 'Maximum values reached ( {max} values )' => __( 'Maximum values reached ( {max} values )', 'acf' ),
  52. 'Loading' => __( 'Loading', 'acf' ),
  53. 'No matches found' => __( 'No matches found', 'acf' ),
  54. )
  55. );
  56. }
  57. /*
  58. * ajax_query
  59. *
  60. * description
  61. *
  62. * @type function
  63. * @date 24/10/13
  64. * @since 5.0.0
  65. *
  66. * @param $post_id (int)
  67. * @return $post_id (int)
  68. */
  69. function ajax_query() {
  70. // validate
  71. if ( ! acf_verify_ajax() ) {
  72. die();
  73. }
  74. // get choices
  75. $response = $this->get_ajax_query( $_POST );
  76. // return
  77. acf_send_ajax_results( $response );
  78. }
  79. /*
  80. * get_ajax_query
  81. *
  82. * This function will return an array of data formatted for use in a select2 AJAX response
  83. *
  84. * @type function
  85. * @date 15/10/2014
  86. * @since 5.0.9
  87. *
  88. * @param $options (array)
  89. * @return (array)
  90. */
  91. function get_ajax_query( $options = array() ) {
  92. // defaults
  93. $options = wp_parse_args(
  94. $options,
  95. array(
  96. 'post_id' => 0,
  97. 's' => '',
  98. 'field_key' => '',
  99. 'paged' => 1,
  100. 'post_type' => '',
  101. 'taxonomy' => '',
  102. )
  103. );
  104. // load field
  105. $field = acf_get_field( $options['field_key'] );
  106. if ( ! $field ) {
  107. return false;
  108. }
  109. // vars
  110. $results = array();
  111. $args = array();
  112. $s = false;
  113. $is_search = false;
  114. // paged
  115. $args['posts_per_page'] = 20;
  116. $args['paged'] = intval( $options['paged'] );
  117. // search
  118. if ( $options['s'] !== '' ) {
  119. // strip slashes (search may be integer)
  120. $s = wp_unslash( strval( $options['s'] ) );
  121. // update vars
  122. $args['s'] = $s;
  123. $is_search = true;
  124. }
  125. // post_type
  126. if ( ! empty( $options['post_type'] ) ) {
  127. $args['post_type'] = acf_get_array( $options['post_type'] );
  128. } elseif ( ! empty( $field['post_type'] ) ) {
  129. $args['post_type'] = acf_get_array( $field['post_type'] );
  130. } else {
  131. $args['post_type'] = acf_get_post_types();
  132. }
  133. // taxonomy
  134. if ( ! empty( $options['taxonomy'] ) ) {
  135. // vars
  136. $term = acf_decode_taxonomy_term( $options['taxonomy'] );
  137. // tax query
  138. $args['tax_query'] = array();
  139. // append
  140. $args['tax_query'][] = array(
  141. 'taxonomy' => $term['taxonomy'],
  142. 'field' => 'slug',
  143. 'terms' => $term['term'],
  144. );
  145. } elseif ( ! empty( $field['taxonomy'] ) ) {
  146. // vars
  147. $terms = acf_decode_taxonomy_terms( $field['taxonomy'] );
  148. // append to $args
  149. $args['tax_query'] = array(
  150. 'relation' => 'OR',
  151. );
  152. // now create the tax queries
  153. foreach ( $terms as $k => $v ) {
  154. $args['tax_query'][] = array(
  155. 'taxonomy' => $k,
  156. 'field' => 'slug',
  157. 'terms' => $v,
  158. );
  159. }
  160. }
  161. // filters
  162. $args = apply_filters( 'acf/fields/relationship/query', $args, $field, $options['post_id'] );
  163. $args = apply_filters( 'acf/fields/relationship/query/name=' . $field['name'], $args, $field, $options['post_id'] );
  164. $args = apply_filters( 'acf/fields/relationship/query/key=' . $field['key'], $args, $field, $options['post_id'] );
  165. // get posts grouped by post type
  166. $groups = acf_get_grouped_posts( $args );
  167. // bail early if no posts
  168. if ( empty( $groups ) ) {
  169. return false;
  170. }
  171. // loop
  172. foreach ( array_keys( $groups ) as $group_title ) {
  173. // vars
  174. $posts = acf_extract_var( $groups, $group_title );
  175. // data
  176. $data = array(
  177. 'text' => $group_title,
  178. 'children' => array(),
  179. );
  180. // convert post objects to post titles
  181. foreach ( array_keys( $posts ) as $post_id ) {
  182. $posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'] );
  183. }
  184. // order posts by search
  185. if ( $is_search && empty( $args['orderby'] ) && isset( $args['s'] ) ) {
  186. $posts = acf_order_by_search( $posts, $args['s'] );
  187. }
  188. // append to $data
  189. foreach ( array_keys( $posts ) as $post_id ) {
  190. $data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ] );
  191. }
  192. // append to $results
  193. $results[] = $data;
  194. }
  195. // add as optgroup or results
  196. if ( count( $args['post_type'] ) == 1 ) {
  197. $results = $results[0]['children'];
  198. }
  199. // vars
  200. $response = array(
  201. 'results' => $results,
  202. 'limit' => $args['posts_per_page'],
  203. );
  204. // return
  205. return $response;
  206. }
  207. /*
  208. * get_post_result
  209. *
  210. * This function will return an array containing id, text and maybe description data
  211. *
  212. * @type function
  213. * @date 7/07/2016
  214. * @since 5.4.0
  215. *
  216. * @param $id (mixed)
  217. * @param $text (string)
  218. * @return (array)
  219. */
  220. function get_post_result( $id, $text ) {
  221. // vars
  222. $result = array(
  223. 'id' => $id,
  224. 'text' => $text,
  225. );
  226. // return
  227. return $result;
  228. }
  229. /*
  230. * get_post_title
  231. *
  232. * This function returns the HTML for a result
  233. *
  234. * @type function
  235. * @date 1/11/2013
  236. * @since 5.0.0
  237. *
  238. * @param $post (object)
  239. * @param $field (array)
  240. * @param $post_id (int) the post_id to which this value is saved to
  241. * @return (string)
  242. */
  243. function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
  244. // get post_id
  245. if ( ! $post_id ) {
  246. $post_id = acf_get_form_data( 'post_id' );
  247. }
  248. // vars
  249. $title = acf_get_post_title( $post, $is_search );
  250. // featured_image
  251. if ( acf_in_array( 'featured_image', $field['elements'] ) ) {
  252. // vars
  253. $class = 'thumbnail';
  254. $thumbnail = acf_get_post_thumbnail( $post->ID, array( 17, 17 ) );
  255. // icon
  256. if ( $thumbnail['type'] == 'icon' ) {
  257. $class .= ' -' . $thumbnail['type'];
  258. }
  259. // append
  260. $title = '<div class="' . $class . '">' . $thumbnail['html'] . '</div>' . $title;
  261. }
  262. // filters
  263. $title = apply_filters( 'acf/fields/relationship/result', $title, $post, $field, $post_id );
  264. $title = apply_filters( 'acf/fields/relationship/result/name=' . $field['_name'], $title, $post, $field, $post_id );
  265. $title = apply_filters( 'acf/fields/relationship/result/key=' . $field['key'], $title, $post, $field, $post_id );
  266. // return
  267. return $title;
  268. }
  269. /*
  270. * render_field()
  271. *
  272. * Create the HTML interface for your field
  273. *
  274. * @param $field - an array holding all the field's data
  275. *
  276. * @type action
  277. * @since 3.6
  278. * @date 23/01/13
  279. */
  280. function render_field( $field ) {
  281. // vars
  282. $post_type = acf_get_array( $field['post_type'] );
  283. $taxonomy = acf_get_array( $field['taxonomy'] );
  284. $filters = acf_get_array( $field['filters'] );
  285. // filters
  286. $filter_count = count( $filters );
  287. $filter_post_type_choices = array();
  288. $filter_taxonomy_choices = array();
  289. // post_type filter
  290. if ( in_array( 'post_type', $filters ) ) {
  291. $filter_post_type_choices = array(
  292. '' => __( 'Select post type', 'acf' ),
  293. ) + acf_get_pretty_post_types( $post_type );
  294. }
  295. // taxonomy filter
  296. if ( in_array( 'taxonomy', $filters ) ) {
  297. $term_choices = array();
  298. $filter_taxonomy_choices = array(
  299. '' => __( 'Select taxonomy', 'acf' ),
  300. );
  301. // check for specific taxonomy setting
  302. if ( $taxonomy ) {
  303. $terms = acf_get_encoded_terms( $taxonomy );
  304. $term_choices = acf_get_choices_from_terms( $terms, 'slug' );
  305. // if no terms were specified, find all terms
  306. } else {
  307. // restrict taxonomies by the post_type selected
  308. $term_args = array();
  309. if ( $post_type ) {
  310. $term_args['taxonomy'] = acf_get_taxonomies(
  311. array(
  312. 'post_type' => $post_type,
  313. )
  314. );
  315. }
  316. // get terms
  317. $terms = acf_get_grouped_terms( $term_args );
  318. $term_choices = acf_get_choices_from_grouped_terms( $terms, 'slug' );
  319. }
  320. // append term choices
  321. $filter_taxonomy_choices = $filter_taxonomy_choices + $term_choices;
  322. }
  323. // div attributes
  324. $atts = array(
  325. 'id' => $field['id'],
  326. 'class' => "acf-relationship {$field['class']}",
  327. 'data-min' => $field['min'],
  328. 'data-max' => $field['max'],
  329. 'data-s' => '',
  330. 'data-paged' => 1,
  331. 'data-post_type' => '',
  332. 'data-taxonomy' => '',
  333. );
  334. ?>
  335. <div <?php echo acf_esc_attrs( $atts ); ?>>
  336. <?php
  337. acf_hidden_input(
  338. array(
  339. 'name' => $field['name'],
  340. 'value' => '',
  341. )
  342. );
  343. ?>
  344. <?php
  345. /* filters */
  346. if ( $filter_count ) :
  347. ?>
  348. <div class="filters -f<?php echo esc_attr( $filter_count ); ?>">
  349. <?php
  350. /* search */
  351. if ( in_array( 'search', $filters ) ) :
  352. ?>
  353. <div class="filter -search">
  354. <?php
  355. acf_text_input(
  356. array(
  357. 'placeholder' => __( 'Search...', 'acf' ),
  358. 'data-filter' => 's',
  359. )
  360. );
  361. ?>
  362. </div>
  363. <?php
  364. endif;
  365. /* post_type */
  366. if ( in_array( 'post_type', $filters ) ) :
  367. ?>
  368. <div class="filter -post_type">
  369. <?php
  370. acf_select_input(
  371. array(
  372. 'choices' => $filter_post_type_choices,
  373. 'data-filter' => 'post_type',
  374. )
  375. );
  376. ?>
  377. </div>
  378. <?php
  379. endif;
  380. /* post_type */
  381. if ( in_array( 'taxonomy', $filters ) ) :
  382. ?>
  383. <div class="filter -taxonomy">
  384. <?php
  385. acf_select_input(
  386. array(
  387. 'choices' => $filter_taxonomy_choices,
  388. 'data-filter' => 'taxonomy',
  389. )
  390. );
  391. ?>
  392. </div>
  393. <?php endif; ?>
  394. </div>
  395. <?php endif; ?>
  396. <div class="selection">
  397. <div class="choices">
  398. <ul class="acf-bl list choices-list"></ul>
  399. </div>
  400. <div class="values">
  401. <ul class="acf-bl list values-list">
  402. <?php
  403. if ( ! empty( $field['value'] ) ) :
  404. // get posts
  405. $posts = acf_get_posts(
  406. array(
  407. 'post__in' => $field['value'],
  408. 'post_type' => $field['post_type'],
  409. )
  410. );
  411. // loop
  412. foreach ( $posts as $post ) :
  413. ?>
  414. <li>
  415. <?php
  416. acf_hidden_input(
  417. array(
  418. 'name' => $field['name'] . '[]',
  419. 'value' => $post->ID,
  420. )
  421. );
  422. ?>
  423. <span tabindex="0" data-id="<?php echo esc_attr( $post->ID ); ?>" class="acf-rel-item acf-rel-item-remove">
  424. <?php echo acf_esc_html( $this->get_post_title( $post, $field ) ); ?>
  425. <a href="#" class="acf-icon -minus small dark" data-name="remove_item"></a>
  426. </span>
  427. </li>
  428. <?php endforeach; ?>
  429. <?php endif; ?>
  430. </ul>
  431. </div>
  432. </div>
  433. </div>
  434. <?php
  435. }
  436. /*
  437. * render_field_settings()
  438. *
  439. * Create extra options for your field. This is rendered when editing a field.
  440. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  441. *
  442. * @type action
  443. * @since 3.6
  444. * @date 23/01/13
  445. *
  446. * @param $field - an array holding all the field's data
  447. */
  448. function render_field_settings( $field ) {
  449. acf_render_field_setting(
  450. $field,
  451. array(
  452. 'label' => __( 'Filter by Post Type', 'acf' ),
  453. 'instructions' => '',
  454. 'type' => 'select',
  455. 'name' => 'post_type',
  456. 'choices' => acf_get_pretty_post_types(),
  457. 'multiple' => 1,
  458. 'ui' => 1,
  459. 'allow_null' => 1,
  460. 'placeholder' => __( 'All post types', 'acf' ),
  461. )
  462. );
  463. acf_render_field_setting(
  464. $field,
  465. array(
  466. 'label' => __( 'Filter by Taxonomy', 'acf' ),
  467. 'instructions' => '',
  468. 'type' => 'select',
  469. 'name' => 'taxonomy',
  470. 'choices' => acf_get_taxonomy_terms(),
  471. 'multiple' => 1,
  472. 'ui' => 1,
  473. 'allow_null' => 1,
  474. 'placeholder' => __( 'All taxonomies', 'acf' ),
  475. )
  476. );
  477. acf_render_field_setting(
  478. $field,
  479. array(
  480. 'label' => __( 'Filters', 'acf' ),
  481. 'instructions' => '',
  482. 'type' => 'checkbox',
  483. 'name' => 'filters',
  484. 'choices' => array(
  485. 'search' => __( 'Search', 'acf' ),
  486. 'post_type' => __( 'Post Type', 'acf' ),
  487. 'taxonomy' => __( 'Taxonomy', 'acf' ),
  488. ),
  489. )
  490. );
  491. acf_render_field_setting(
  492. $field,
  493. array(
  494. 'label' => __( 'Return Format', 'acf' ),
  495. 'instructions' => '',
  496. 'type' => 'radio',
  497. 'name' => 'return_format',
  498. 'choices' => array(
  499. 'object' => __( 'Post Object', 'acf' ),
  500. 'id' => __( 'Post ID', 'acf' ),
  501. ),
  502. 'layout' => 'horizontal',
  503. )
  504. );
  505. }
  506. /**
  507. * Renders the field settings used in the "Validation" tab.
  508. *
  509. * @since 6.0
  510. *
  511. * @param array $field The field settings array.
  512. * @return void
  513. */
  514. function render_field_validation_settings( $field ) {
  515. $field['min'] = empty( $field['min'] ) ? '' : $field['min'];
  516. $field['max'] = empty( $field['max'] ) ? '' : $field['max'];
  517. acf_render_field_setting(
  518. $field,
  519. array(
  520. 'label' => __( 'Minimum posts', 'acf' ),
  521. 'instructions' => '',
  522. 'type' => 'number',
  523. 'name' => 'min',
  524. )
  525. );
  526. acf_render_field_setting(
  527. $field,
  528. array(
  529. 'label' => __( 'Maximum posts', 'acf' ),
  530. 'instructions' => '',
  531. 'type' => 'number',
  532. 'name' => 'max',
  533. )
  534. );
  535. }
  536. /**
  537. * Renders the field settings used in the "Presentation" tab.
  538. *
  539. * @since 6.0
  540. *
  541. * @param array $field The field settings array.
  542. * @return void
  543. */
  544. function render_field_presentation_settings( $field ) {
  545. acf_render_field_setting(
  546. $field,
  547. array(
  548. 'label' => __( 'Elements', 'acf' ),
  549. 'instructions' => __( 'Selected elements will be displayed in each result', 'acf' ),
  550. 'type' => 'checkbox',
  551. 'name' => 'elements',
  552. 'choices' => array(
  553. 'featured_image' => __( 'Featured Image', 'acf' ),
  554. ),
  555. )
  556. );
  557. }
  558. /*
  559. * format_value()
  560. *
  561. * This filter is applied to the $value after it is loaded from the db and before it is returned to the template
  562. *
  563. * @type filter
  564. * @since 3.6
  565. * @date 23/01/13
  566. *
  567. * @param $value (mixed) the value which was loaded from the database
  568. * @param $post_id (mixed) the $post_id from which the value was loaded
  569. * @param $field (array) the field array holding all the field options
  570. *
  571. * @return $value (mixed) the modified value
  572. */
  573. function format_value( $value, $post_id, $field ) {
  574. // bail early if no value
  575. if ( empty( $value ) ) {
  576. return $value;
  577. }
  578. // force value to array
  579. $value = acf_get_array( $value );
  580. // convert to int
  581. $value = array_map( 'intval', $value );
  582. // load posts if needed
  583. if ( $field['return_format'] == 'object' ) {
  584. // get posts
  585. $value = acf_get_posts(
  586. array(
  587. 'post__in' => $value,
  588. 'post_type' => $field['post_type'],
  589. )
  590. );
  591. }
  592. // return
  593. return $value;
  594. }
  595. /*
  596. * validate_value
  597. *
  598. * description
  599. *
  600. * @type function
  601. * @date 11/02/2014
  602. * @since 5.0.0
  603. *
  604. * @param $post_id (int)
  605. * @return $post_id (int)
  606. */
  607. function validate_value( $valid, $value, $field, $input ) {
  608. // default
  609. if ( empty( $value ) || ! is_array( $value ) ) {
  610. $value = array();
  611. }
  612. // min
  613. if ( count( $value ) < $field['min'] ) {
  614. $valid = _n( '%1$s requires at least %2$s selection', '%1$s requires at least %2$s selections', $field['min'], 'acf' );
  615. $valid = sprintf( $valid, $field['label'], $field['min'] );
  616. }
  617. // return
  618. return $valid;
  619. }
  620. /**
  621. * update_value()
  622. *
  623. * This filter is applied to the $value before it is updated in the db
  624. *
  625. * @type filter
  626. * @since 3.6
  627. * @date 23/01/13
  628. *
  629. * @param $value - the value which will be saved in the database
  630. * @param $post_id - the $post_id of which the value will be saved
  631. * @param $field - the field array holding all the field options
  632. *
  633. * @return $value - the modified value
  634. */
  635. function update_value( $value, $post_id, $field ) {
  636. // Bail early if no value.
  637. if ( empty( $value ) ) {
  638. return $value;
  639. }
  640. // Format array of values.
  641. // - ensure each value is an id.
  642. // - Parse each id as string for SQL LIKE queries.
  643. if ( acf_is_sequential_array( $value ) ) {
  644. $value = array_map( 'acf_idval', $value );
  645. $value = array_map( 'strval', $value );
  646. // Parse single value for id.
  647. } else {
  648. $value = acf_idval( $value );
  649. }
  650. // Return value.
  651. return $value;
  652. }
  653. /**
  654. * Validates relationship fields updated via the REST API.
  655. *
  656. * @param bool $valid
  657. * @param int $value
  658. * @param array $field
  659. *
  660. * @return bool|WP_Error
  661. */
  662. public function validate_rest_value( $valid, $value, $field ) {
  663. return acf_get_field_type( 'post_object' )->validate_rest_value( $valid, $value, $field );
  664. }
  665. /**
  666. * Return the schema array for the REST API.
  667. *
  668. * @param array $field
  669. * @return array
  670. */
  671. public function get_rest_schema( array $field ) {
  672. $schema = array(
  673. 'type' => array( 'integer', 'array', 'null' ),
  674. 'required' => ! empty( $field['required'] ),
  675. 'items' => array(
  676. 'type' => 'integer',
  677. ),
  678. );
  679. if ( empty( $field['allow_null'] ) ) {
  680. $schema['minItems'] = 1;
  681. }
  682. if ( ! empty( $field['min'] ) ) {
  683. $schema['minItems'] = (int) $field['min'];
  684. }
  685. if ( ! empty( $field['max'] ) ) {
  686. $schema['maxItems'] = (int) $field['max'];
  687. }
  688. return $schema;
  689. }
  690. /**
  691. * @see \acf_field::get_rest_links()
  692. * @param mixed $value The raw (unformatted) field value.
  693. * @param int|string $post_id
  694. * @param array $field
  695. * @return array
  696. */
  697. public function get_rest_links( $value, $post_id, array $field ) {
  698. $links = array();
  699. if ( empty( $value ) ) {
  700. return $links;
  701. }
  702. foreach ( (array) $value as $object_id ) {
  703. if ( ! $post_type = get_post_type( $object_id ) or ! $post_type = get_post_type_object( $post_type ) ) {
  704. continue;
  705. }
  706. $rest_base = acf_get_object_type_rest_base( $post_type );
  707. $links[] = array(
  708. 'rel' => $post_type->name === 'attachment' ? 'acf:attachment' : 'acf:post',
  709. 'href' => rest_url( sprintf( '/wp/v2/%s/%s', $rest_base, $object_id ) ),
  710. 'embeddable' => true,
  711. );
  712. }
  713. return $links;
  714. }
  715. /**
  716. * Apply basic formatting to prepare the value for default REST output.
  717. *
  718. * @param mixed $value
  719. * @param string|int $post_id
  720. * @param array $field
  721. * @return mixed
  722. */
  723. public function format_value_for_rest( $value, $post_id, array $field ) {
  724. return acf_format_numerics( $value );
  725. }
  726. }
  727. // initialize
  728. acf_register_field_type( 'acf_field_relationship' );
  729. endif; // class_exists check
  730. ?>