Нет описания

class-acf-field-post_object.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  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. // default_value
  268. acf_render_field_setting(
  269. $field,
  270. array(
  271. 'label' => __( 'Filter by Post Type', 'acf' ),
  272. 'instructions' => '',
  273. 'type' => 'select',
  274. 'name' => 'post_type',
  275. 'choices' => acf_get_pretty_post_types(),
  276. 'multiple' => 1,
  277. 'ui' => 1,
  278. 'allow_null' => 1,
  279. 'placeholder' => __( 'All post types', 'acf' ),
  280. )
  281. );
  282. // default_value
  283. acf_render_field_setting(
  284. $field,
  285. array(
  286. 'label' => __( 'Filter by Taxonomy', 'acf' ),
  287. 'instructions' => '',
  288. 'type' => 'select',
  289. 'name' => 'taxonomy',
  290. 'choices' => acf_get_taxonomy_terms(),
  291. 'multiple' => 1,
  292. 'ui' => 1,
  293. 'allow_null' => 1,
  294. 'placeholder' => __( 'All taxonomies', 'acf' ),
  295. )
  296. );
  297. // allow_null
  298. acf_render_field_setting(
  299. $field,
  300. array(
  301. 'label' => __( 'Allow Null?', 'acf' ),
  302. 'instructions' => '',
  303. 'name' => 'allow_null',
  304. 'type' => 'true_false',
  305. 'ui' => 1,
  306. )
  307. );
  308. // multiple
  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. // return_format
  320. acf_render_field_setting(
  321. $field,
  322. array(
  323. 'label' => __( 'Return Format', 'acf' ),
  324. 'instructions' => '',
  325. 'type' => 'radio',
  326. 'name' => 'return_format',
  327. 'choices' => array(
  328. 'object' => __( 'Post Object', 'acf' ),
  329. 'id' => __( 'Post ID', 'acf' ),
  330. ),
  331. 'layout' => 'horizontal',
  332. )
  333. );
  334. }
  335. /*
  336. * load_value()
  337. *
  338. * This filter is applied to the $value after it is loaded from the db
  339. *
  340. * @type filter
  341. * @since 3.6
  342. * @date 23/01/13
  343. *
  344. * @param $value (mixed) the value found in the database
  345. * @param $post_id (mixed) the $post_id from which the value was loaded
  346. * @param $field (array) the field array holding all the field options
  347. * @return $value
  348. */
  349. function load_value( $value, $post_id, $field ) {
  350. // ACF4 null
  351. if ( $value === 'null' ) {
  352. return false;
  353. }
  354. // return
  355. return $value;
  356. }
  357. /*
  358. * format_value()
  359. *
  360. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  361. *
  362. * @type filter
  363. * @since 3.6
  364. * @date 23/01/13
  365. *
  366. * @param $value (mixed) the value which was loaded from the database
  367. * @param $post_id (mixed) the $post_id from which the value was loaded
  368. * @param $field (array) the field array holding all the field options
  369. *
  370. * @return $value (mixed) the modified value
  371. */
  372. function format_value( $value, $post_id, $field ) {
  373. // numeric
  374. $value = acf_get_numeric( $value );
  375. // bail early if no value
  376. if ( empty( $value ) ) {
  377. return false;
  378. }
  379. // load posts if needed
  380. if ( $field['return_format'] == 'object' ) {
  381. $value = $this->get_posts( $value, $field );
  382. }
  383. // convert back from array if neccessary
  384. if ( ! $field['multiple'] && is_array( $value ) ) {
  385. $value = current( $value );
  386. }
  387. // return value
  388. return $value;
  389. }
  390. /*
  391. * update_value()
  392. *
  393. * This filter is appied to the $value before it is updated in the db
  394. *
  395. * @type filter
  396. * @since 3.6
  397. * @date 23/01/13
  398. *
  399. * @param $value - the value which will be saved in the database
  400. * @param $post_id - the $post_id of which the value will be saved
  401. * @param $field - the field array holding all the field options
  402. *
  403. * @return $value - the modified value
  404. */
  405. function update_value( $value, $post_id, $field ) {
  406. // Bail early if no value.
  407. if ( empty( $value ) ) {
  408. return $value;
  409. }
  410. // Format array of values.
  411. // - ensure each value is an id.
  412. // - Parse each id as string for SQL LIKE queries.
  413. if ( acf_is_sequential_array( $value ) ) {
  414. $value = array_map( 'acf_idval', $value );
  415. $value = array_map( 'strval', $value );
  416. // Parse single value for id.
  417. } else {
  418. $value = acf_idval( $value );
  419. }
  420. // Return value.
  421. return $value;
  422. }
  423. /*
  424. * get_posts
  425. *
  426. * This function will return an array of posts for a given field value
  427. *
  428. * @type function
  429. * @date 13/06/2014
  430. * @since 5.0.0
  431. *
  432. * @param $value (array)
  433. * @return $value
  434. */
  435. function get_posts( $value, $field ) {
  436. // numeric
  437. $value = acf_get_numeric( $value );
  438. // bail early if no value
  439. if ( empty( $value ) ) {
  440. return false;
  441. }
  442. // get posts
  443. $posts = acf_get_posts(
  444. array(
  445. 'post__in' => $value,
  446. 'post_type' => $field['post_type'],
  447. )
  448. );
  449. // return
  450. return $posts;
  451. }
  452. /**
  453. * Validates post object fields updated via the REST API.
  454. *
  455. * @param bool $valid
  456. * @param int $value
  457. * @param array $field
  458. *
  459. * @return bool|WP_Error
  460. */
  461. public function validate_rest_value( $valid, $value, $field ) {
  462. if ( is_null( $value ) ) {
  463. return $valid;
  464. }
  465. $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
  466. $data = array( 'param' => $param );
  467. $value = is_array( $value ) ? $value : array( $value );
  468. $invalid_posts = array();
  469. $post_type_errors = array();
  470. $taxonomy_errors = array();
  471. foreach ( $value as $post_id ) {
  472. if ( is_string( $post_id ) ) {
  473. continue;
  474. }
  475. $post_type = get_post_type( $post_id );
  476. if ( ! $post_type ) {
  477. $invalid_posts[] = $post_id;
  478. continue;
  479. }
  480. if (
  481. is_array( $field['post_type'] ) &&
  482. ! empty( $field['post_type'] ) &&
  483. ! in_array( $post_type, $field['post_type'] )
  484. ) {
  485. $post_type_errors[] = $post_id;
  486. }
  487. if ( is_array( $field['taxonomy'] ) && ! empty( $field['taxonomy'] ) ) {
  488. $found = false;
  489. foreach ( $field['taxonomy'] as $taxonomy_term ) {
  490. $decoded = acf_decode_taxonomy_term( $taxonomy_term );
  491. if ( $decoded && is_object_in_term( $post_id, $decoded['taxonomy'], $decoded['term'] ) ) {
  492. $found = true;
  493. break;
  494. }
  495. }
  496. if ( ! $found ) {
  497. $taxonomy_errors[] = $post_id;
  498. }
  499. }
  500. }
  501. if ( count( $invalid_posts ) ) {
  502. $error = sprintf(
  503. __( '%1$s must have a valid post ID.', 'acf' ),
  504. $param
  505. );
  506. $data['value'] = $invalid_posts;
  507. return new WP_Error( 'rest_invalid_param', $error, $data );
  508. }
  509. if ( count( $post_type_errors ) ) {
  510. $error = sprintf(
  511. _n(
  512. '%1$s must be of post type %2$s.',
  513. '%1$s must be of one of the following post types: %2$s',
  514. count( $field['post_type'] ),
  515. 'acf'
  516. ),
  517. $param,
  518. count( $field['post_type'] ) > 1 ? implode( ', ', $field['post_type'] ) : $field['post_type'][0]
  519. );
  520. $data['value'] = $post_type_errors;
  521. return new WP_Error( 'rest_invalid_param', $error, $data );
  522. }
  523. if ( count( $taxonomy_errors ) ) {
  524. $error = sprintf(
  525. _n(
  526. '%1$s must have term %2$s.',
  527. '%1$s must have one of the following terms: %2$s',
  528. count( $field['taxonomy'] ),
  529. 'acf'
  530. ),
  531. $param,
  532. count( $field['taxonomy'] ) > 1 ? implode( ', ', $field['taxonomy'] ) : $field['taxonomy'][0]
  533. );
  534. $data['value'] = $taxonomy_errors;
  535. return new WP_Error( 'rest_invalid_param', $error, $data );
  536. }
  537. return $valid;
  538. }
  539. /**
  540. * Return the schema array for the REST API.
  541. *
  542. * @param array $field
  543. * @return array
  544. */
  545. public function get_rest_schema( array $field ) {
  546. $schema = array(
  547. 'type' => array( 'integer', 'array', 'null' ),
  548. 'required' => ! empty( $field['required'] ),
  549. 'items' => array(
  550. 'type' => 'integer',
  551. ),
  552. );
  553. if ( empty( $field['allow_null'] ) ) {
  554. $schema['minItems'] = 1;
  555. }
  556. if ( empty( $field['multiple'] ) ) {
  557. $schema['maxItems'] = 1;
  558. }
  559. return $schema;
  560. }
  561. /**
  562. * @see \acf_field::get_rest_links()
  563. * @param mixed $value The raw (unformatted) field value.
  564. * @param int|string $post_id
  565. * @param array $field
  566. * @return array
  567. */
  568. public function get_rest_links( $value, $post_id, array $field ) {
  569. $links = array();
  570. if ( empty( $value ) ) {
  571. return $links;
  572. }
  573. foreach ( (array) $value as $object_id ) {
  574. if ( ! $post_type = get_post_type( $object_id ) ) {
  575. continue;
  576. }
  577. if ( ! $post_type_object = get_post_type_object( $post_type ) ) {
  578. continue;
  579. }
  580. $rest_base = acf_get_object_type_rest_base( $post_type_object );
  581. $links[] = array(
  582. 'rel' => $post_type_object->name === 'attachment' ? 'acf:attachment' : 'acf:post',
  583. 'href' => rest_url( sprintf( '/wp/v2/%s/%s', $rest_base, $object_id ) ),
  584. 'embeddable' => true,
  585. );
  586. }
  587. return $links;
  588. }
  589. /**
  590. * Apply basic formatting to prepare the value for default REST output.
  591. *
  592. * @param mixed $value
  593. * @param string|int $post_id
  594. * @param array $field
  595. * @return mixed
  596. */
  597. public function format_value_for_rest( $value, $post_id, array $field ) {
  598. return acf_format_numerics( $value );
  599. }
  600. }
  601. // initialize
  602. acf_register_field_type( 'acf_field_post_object' );
  603. endif; // class_exists check