class-acf-field-post_object.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. <?php
  2. if ( ! class_exists( 'acf_field_post_object' ) ) :
  3. class acf_field_post_object 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 = 'post_object';
  19. $this->label = __( 'Post Object', 'acf' );
  20. $this->category = 'relational';
  21. $this->defaults = array(
  22. 'post_type' => array(),
  23. 'taxonomy' => array(),
  24. 'allow_null' => 0,
  25. 'multiple' => 0,
  26. 'return_format' => 'object',
  27. 'ui' => 1,
  28. );
  29. // extra
  30. add_action( 'wp_ajax_acf/fields/post_object/query', array( $this, 'ajax_query' ) );
  31. add_action( 'wp_ajax_nopriv_acf/fields/post_object/query', array( $this, 'ajax_query' ) );
  32. }
  33. /*
  34. * ajax_query
  35. *
  36. * description
  37. *
  38. * @type function
  39. * @date 24/10/13
  40. * @since 5.0.0
  41. *
  42. * @param $post_id (int)
  43. * @return $post_id (int)
  44. */
  45. function ajax_query() {
  46. // validate
  47. if ( ! acf_verify_ajax() ) {
  48. die();
  49. }
  50. // get choices
  51. $response = $this->get_ajax_query( $_POST );
  52. // return
  53. acf_send_ajax_results( $response );
  54. }
  55. /*
  56. * get_ajax_query
  57. *
  58. * This function will return an array of data formatted for use in a select2 AJAX response
  59. *
  60. * @type function
  61. * @date 15/10/2014
  62. * @since 5.0.9
  63. *
  64. * @param $options (array)
  65. * @return (array)
  66. */
  67. function get_ajax_query( $options = array() ) {
  68. // defaults
  69. $options = acf_parse_args(
  70. $options,
  71. array(
  72. 'post_id' => 0,
  73. 's' => '',
  74. 'field_key' => '',
  75. 'paged' => 1,
  76. )
  77. );
  78. // load field
  79. $field = acf_get_field( $options['field_key'] );
  80. if ( ! $field ) {
  81. return false;
  82. }
  83. // vars
  84. $results = array();
  85. $args = array();
  86. $s = false;
  87. $is_search = false;
  88. // paged
  89. $args['posts_per_page'] = 20;
  90. $args['paged'] = $options['paged'];
  91. // search
  92. if ( $options['s'] !== '' ) {
  93. // strip slashes (search may be integer)
  94. $s = wp_unslash( strval( $options['s'] ) );
  95. // update vars
  96. $args['s'] = $s;
  97. $is_search = true;
  98. }
  99. // post_type
  100. if ( ! empty( $field['post_type'] ) ) {
  101. $args['post_type'] = acf_get_array( $field['post_type'] );
  102. } else {
  103. $args['post_type'] = acf_get_post_types();
  104. }
  105. // taxonomy
  106. if ( ! empty( $field['taxonomy'] ) ) {
  107. // vars
  108. $terms = acf_decode_taxonomy_terms( $field['taxonomy'] );
  109. // append to $args
  110. $args['tax_query'] = array();
  111. // now create the tax queries
  112. foreach ( $terms as $k => $v ) {
  113. $args['tax_query'][] = array(
  114. 'taxonomy' => $k,
  115. 'field' => 'slug',
  116. 'terms' => $v,
  117. );
  118. }
  119. }
  120. // filters
  121. $args = apply_filters( 'acf/fields/post_object/query', $args, $field, $options['post_id'] );
  122. $args = apply_filters( 'acf/fields/post_object/query/name=' . $field['name'], $args, $field, $options['post_id'] );
  123. $args = apply_filters( 'acf/fields/post_object/query/key=' . $field['key'], $args, $field, $options['post_id'] );
  124. // get posts grouped by post type
  125. $groups = acf_get_grouped_posts( $args );
  126. // bail early if no posts
  127. if ( empty( $groups ) ) {
  128. return false;
  129. }
  130. // loop
  131. foreach ( array_keys( $groups ) as $group_title ) {
  132. // vars
  133. $posts = acf_extract_var( $groups, $group_title );
  134. // data
  135. $data = array(
  136. 'text' => $group_title,
  137. 'children' => array(),
  138. );
  139. // convert post objects to post titles
  140. foreach ( array_keys( $posts ) as $post_id ) {
  141. $posts[ $post_id ] = $this->get_post_title( $posts[ $post_id ], $field, $options['post_id'], $is_search );
  142. }
  143. // order posts by search
  144. if ( $is_search && empty( $args['orderby'] ) && isset( $args['s'] ) ) {
  145. $posts = acf_order_by_search( $posts, $args['s'] );
  146. }
  147. // append to $data
  148. foreach ( array_keys( $posts ) as $post_id ) {
  149. $data['children'][] = $this->get_post_result( $post_id, $posts[ $post_id ] );
  150. }
  151. // append to $results
  152. $results[] = $data;
  153. }
  154. // optgroup or single
  155. $post_type = acf_get_array( $args['post_type'] );
  156. if ( count( $post_type ) == 1 ) {
  157. $results = $results[0]['children'];
  158. }
  159. // vars
  160. $response = array(
  161. 'results' => $results,
  162. 'limit' => $args['posts_per_page'],
  163. );
  164. // return
  165. return $response;
  166. }
  167. /*
  168. * get_post_result
  169. *
  170. * This function will return an array containing id, text and maybe description data
  171. *
  172. * @type function
  173. * @date 7/07/2016
  174. * @since 5.4.0
  175. *
  176. * @param $id (mixed)
  177. * @param $text (string)
  178. * @return (array)
  179. */
  180. function get_post_result( $id, $text ) {
  181. // vars
  182. $result = array(
  183. 'id' => $id,
  184. 'text' => $text,
  185. );
  186. // look for parent
  187. $search = '| ' . __( 'Parent', 'acf' ) . ':';
  188. $pos = strpos( $text, $search );
  189. if ( $pos !== false ) {
  190. $result['description'] = substr( $text, $pos + 2 );
  191. $result['text'] = substr( $text, 0, $pos );
  192. }
  193. // return
  194. return $result;
  195. }
  196. /*
  197. * get_post_title
  198. *
  199. * This function returns the HTML for a result
  200. *
  201. * @type function
  202. * @date 1/11/2013
  203. * @since 5.0.0
  204. *
  205. * @param $post (object)
  206. * @param $field (array)
  207. * @param $post_id (int) the post_id to which this value is saved to
  208. * @return (string)
  209. */
  210. function get_post_title( $post, $field, $post_id = 0, $is_search = 0 ) {
  211. // get post_id
  212. if ( ! $post_id ) {
  213. $post_id = acf_get_form_data( 'post_id' );
  214. }
  215. // vars
  216. $title = acf_get_post_title( $post, $is_search );
  217. // filters
  218. $title = apply_filters( 'acf/fields/post_object/result', $title, $post, $field, $post_id );
  219. $title = apply_filters( 'acf/fields/post_object/result/name=' . $field['_name'], $title, $post, $field, $post_id );
  220. $title = apply_filters( 'acf/fields/post_object/result/key=' . $field['key'], $title, $post, $field, $post_id );
  221. // return
  222. return $title;
  223. }
  224. /*
  225. * render_field()
  226. *
  227. * Create the HTML interface for your field
  228. *
  229. * @param $field - an array holding all the field's data
  230. *
  231. * @type action
  232. * @since 3.6
  233. * @date 23/01/13
  234. */
  235. function render_field( $field ) {
  236. // Change Field into a select
  237. $field['type'] = 'select';
  238. $field['ui'] = 1;
  239. $field['ajax'] = 1;
  240. $field['choices'] = array();
  241. // load posts
  242. $posts = $this->get_posts( $field['value'], $field );
  243. if ( $posts ) {
  244. foreach ( array_keys( $posts ) as $i ) {
  245. // vars
  246. $post = acf_extract_var( $posts, $i );
  247. // append to choices
  248. $field['choices'][ $post->ID ] = $this->get_post_title( $post, $field );
  249. }
  250. }
  251. // render
  252. acf_render_field( $field );
  253. }
  254. /*
  255. * render_field_settings()
  256. *
  257. * Create extra options for your field. This is rendered when editing a field.
  258. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  259. *
  260. * @type action
  261. * @since 3.6
  262. * @date 23/01/13
  263. *
  264. * @param $field - an array holding all the field's data
  265. */
  266. function render_field_settings( $field ) {
  267. acf_render_field_setting(
  268. $field,
  269. array(
  270. 'label' => __( 'Filter by Post Type', 'acf' ),
  271. 'instructions' => '',
  272. 'type' => 'select',
  273. 'name' => 'post_type',
  274. 'choices' => acf_get_pretty_post_types(),
  275. 'multiple' => 1,
  276. 'ui' => 1,
  277. 'allow_null' => 1,
  278. 'placeholder' => __( 'All post types', 'acf' ),
  279. )
  280. );
  281. acf_render_field_setting(
  282. $field,
  283. array(
  284. 'label' => __( 'Filter by Taxonomy', 'acf' ),
  285. 'instructions' => '',
  286. 'type' => 'select',
  287. 'name' => 'taxonomy',
  288. 'choices' => acf_get_taxonomy_terms(),
  289. 'multiple' => 1,
  290. 'ui' => 1,
  291. 'allow_null' => 1,
  292. 'placeholder' => __( 'All taxonomies', 'acf' ),
  293. )
  294. );
  295. acf_render_field_setting(
  296. $field,
  297. array(
  298. 'label' => __( 'Return Format', 'acf' ),
  299. 'instructions' => '',
  300. 'type' => 'radio',
  301. 'name' => 'return_format',
  302. 'choices' => array(
  303. 'object' => __( 'Post Object', 'acf' ),
  304. 'id' => __( 'Post ID', 'acf' ),
  305. ),
  306. 'layout' => 'horizontal',
  307. )
  308. );
  309. acf_render_field_setting(
  310. $field,
  311. array(
  312. 'label' => __( 'Select multiple values?', 'acf' ),
  313. 'instructions' => '',
  314. 'name' => 'multiple',
  315. 'type' => 'true_false',
  316. 'ui' => 1,
  317. )
  318. );
  319. }
  320. /**
  321. * Renders the field settings used in the "Validation" tab.
  322. *
  323. * @since 6.0
  324. *
  325. * @param array $field The field settings array.
  326. * @return void
  327. */
  328. function render_field_validation_settings( $field ) {
  329. acf_render_field_setting(
  330. $field,
  331. array(
  332. 'label' => __( 'Allow Null?', 'acf' ),
  333. 'instructions' => '',
  334. 'name' => 'allow_null',
  335. 'type' => 'true_false',
  336. 'ui' => 1,
  337. )
  338. );
  339. }
  340. /*
  341. * load_value()
  342. *
  343. * This filter is applied to the $value after it is loaded from the db
  344. *
  345. * @type filter
  346. * @since 3.6
  347. * @date 23/01/13
  348. *
  349. * @param $value (mixed) the value found in the database
  350. * @param $post_id (mixed) the $post_id from which the value was loaded
  351. * @param $field (array) the field array holding all the field options
  352. * @return $value
  353. */
  354. function load_value( $value, $post_id, $field ) {
  355. // ACF4 null
  356. if ( $value === 'null' ) {
  357. return false;
  358. }
  359. // return
  360. return $value;
  361. }
  362. /*
  363. * format_value()
  364. *
  365. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  366. *
  367. * @type filter
  368. * @since 3.6
  369. * @date 23/01/13
  370. *
  371. * @param $value (mixed) the value which was loaded from the database
  372. * @param $post_id (mixed) the $post_id from which the value was loaded
  373. * @param $field (array) the field array holding all the field options
  374. *
  375. * @return $value (mixed) the modified value
  376. */
  377. function format_value( $value, $post_id, $field ) {
  378. // numeric
  379. $value = acf_get_numeric( $value );
  380. // bail early if no value
  381. if ( empty( $value ) ) {
  382. return false;
  383. }
  384. // load posts if needed
  385. if ( $field['return_format'] == 'object' ) {
  386. $value = $this->get_posts( $value, $field );
  387. }
  388. // convert back from array if neccessary
  389. if ( ! $field['multiple'] && is_array( $value ) ) {
  390. $value = current( $value );
  391. }
  392. // return value
  393. return $value;
  394. }
  395. /*
  396. * update_value()
  397. *
  398. * This filter is appied to the $value before it is updated in the db
  399. *
  400. * @type filter
  401. * @since 3.6
  402. * @date 23/01/13
  403. *
  404. * @param $value - the value which will be saved in the database
  405. * @param $post_id - the $post_id of which the value will be saved
  406. * @param $field - the field array holding all the field options
  407. *
  408. * @return $value - the modified value
  409. */
  410. function update_value( $value, $post_id, $field ) {
  411. // Bail early if no value.
  412. if ( empty( $value ) ) {
  413. return $value;
  414. }
  415. // Format array of values.
  416. // - ensure each value is an id.
  417. // - Parse each id as string for SQL LIKE queries.
  418. if ( acf_is_sequential_array( $value ) ) {
  419. $value = array_map( 'acf_idval', $value );
  420. $value = array_map( 'strval', $value );
  421. // Parse single value for id.
  422. } else {
  423. $value = acf_idval( $value );
  424. }
  425. // Return value.
  426. return $value;
  427. }
  428. /*
  429. * get_posts
  430. *
  431. * This function will return an array of posts for a given field value
  432. *
  433. * @type function
  434. * @date 13/06/2014
  435. * @since 5.0.0
  436. *
  437. * @param $value (array)
  438. * @return $value
  439. */
  440. function get_posts( $value, $field ) {
  441. // numeric
  442. $value = acf_get_numeric( $value );
  443. // bail early if no value
  444. if ( empty( $value ) ) {
  445. return false;
  446. }
  447. // get posts
  448. $posts = acf_get_posts(
  449. array(
  450. 'post__in' => $value,
  451. 'post_type' => $field['post_type'],
  452. )
  453. );
  454. // return
  455. return $posts;
  456. }
  457. /**
  458. * Validates post object fields updated via the REST API.
  459. *
  460. * @param bool $valid
  461. * @param int $value
  462. * @param array $field
  463. *
  464. * @return bool|WP_Error
  465. */
  466. public function validate_rest_value( $valid, $value, $field ) {
  467. if ( is_null( $value ) ) {
  468. return $valid;
  469. }
  470. $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
  471. $data = array( 'param' => $param );
  472. $value = is_array( $value ) ? $value : array( $value );
  473. $invalid_posts = array();
  474. $post_type_errors = array();
  475. $taxonomy_errors = array();
  476. foreach ( $value as $post_id ) {
  477. if ( is_string( $post_id ) ) {
  478. continue;
  479. }
  480. $post_type = get_post_type( $post_id );
  481. if ( ! $post_type ) {
  482. $invalid_posts[] = $post_id;
  483. continue;
  484. }
  485. if (
  486. is_array( $field['post_type'] ) &&
  487. ! empty( $field['post_type'] ) &&
  488. ! in_array( $post_type, $field['post_type'] )
  489. ) {
  490. $post_type_errors[] = $post_id;
  491. }
  492. if ( is_array( $field['taxonomy'] ) && ! empty( $field['taxonomy'] ) ) {
  493. $found = false;
  494. foreach ( $field['taxonomy'] as $taxonomy_term ) {
  495. $decoded = acf_decode_taxonomy_term( $taxonomy_term );
  496. if ( $decoded && is_object_in_term( $post_id, $decoded['taxonomy'], $decoded['term'] ) ) {
  497. $found = true;
  498. break;
  499. }
  500. }
  501. if ( ! $found ) {
  502. $taxonomy_errors[] = $post_id;
  503. }
  504. }
  505. }
  506. if ( count( $invalid_posts ) ) {
  507. $error = sprintf(
  508. __( '%1$s must have a valid post ID.', 'acf' ),
  509. $param
  510. );
  511. $data['value'] = $invalid_posts;
  512. return new WP_Error( 'rest_invalid_param', $error, $data );
  513. }
  514. if ( count( $post_type_errors ) ) {
  515. $error = sprintf(
  516. _n(
  517. '%1$s must be of post type %2$s.',
  518. '%1$s must be of one of the following post types: %2$s',
  519. count( $field['post_type'] ),
  520. 'acf'
  521. ),
  522. $param,
  523. count( $field['post_type'] ) > 1 ? implode( ', ', $field['post_type'] ) : $field['post_type'][0]
  524. );
  525. $data['value'] = $post_type_errors;
  526. return new WP_Error( 'rest_invalid_param', $error, $data );
  527. }
  528. if ( count( $taxonomy_errors ) ) {
  529. $error = sprintf(
  530. _n(
  531. '%1$s must have term %2$s.',
  532. '%1$s must have one of the following terms: %2$s',
  533. count( $field['taxonomy'] ),
  534. 'acf'
  535. ),
  536. $param,
  537. count( $field['taxonomy'] ) > 1 ? implode( ', ', $field['taxonomy'] ) : $field['taxonomy'][0]
  538. );
  539. $data['value'] = $taxonomy_errors;
  540. return new WP_Error( 'rest_invalid_param', $error, $data );
  541. }
  542. return $valid;
  543. }
  544. /**
  545. * Return the schema array for the REST API.
  546. *
  547. * @param array $field
  548. * @return array
  549. */
  550. public function get_rest_schema( array $field ) {
  551. $schema = array(
  552. 'type' => array( 'integer', 'array', 'null' ),
  553. 'required' => ! empty( $field['required'] ),
  554. 'items' => array(
  555. 'type' => 'integer',
  556. ),
  557. );
  558. if ( empty( $field['allow_null'] ) ) {
  559. $schema['minItems'] = 1;
  560. }
  561. if ( empty( $field['multiple'] ) ) {
  562. $schema['maxItems'] = 1;
  563. }
  564. return $schema;
  565. }
  566. /**
  567. * @see \acf_field::get_rest_links()
  568. * @param mixed $value The raw (unformatted) field value.
  569. * @param int|string $post_id
  570. * @param array $field
  571. * @return array
  572. */
  573. public function get_rest_links( $value, $post_id, array $field ) {
  574. $links = array();
  575. if ( empty( $value ) ) {
  576. return $links;
  577. }
  578. foreach ( (array) $value as $object_id ) {
  579. if ( ! $post_type = get_post_type( $object_id ) ) {
  580. continue;
  581. }
  582. if ( ! $post_type_object = get_post_type_object( $post_type ) ) {
  583. continue;
  584. }
  585. $rest_base = acf_get_object_type_rest_base( $post_type_object );
  586. $links[] = array(
  587. 'rel' => $post_type_object->name === 'attachment' ? 'acf:attachment' : 'acf:post',
  588. 'href' => rest_url( sprintf( '/wp/v2/%s/%s', $rest_base, $object_id ) ),
  589. 'embeddable' => true,
  590. );
  591. }
  592. return $links;
  593. }
  594. /**
  595. * Apply basic formatting to prepare the value for default REST output.
  596. *
  597. * @param mixed $value
  598. * @param string|int $post_id
  599. * @param array $field
  600. * @return mixed
  601. */
  602. public function format_value_for_rest( $value, $post_id, array $field ) {
  603. return acf_format_numerics( $value );
  604. }
  605. }
  606. // initialize
  607. acf_register_field_type( 'acf_field_post_object' );
  608. endif; // class_exists check