Aucune description

acf-rest-api-functions.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Get the REST API schema for a given field.
  4. *
  5. * @param array $field
  6. * @return array
  7. */
  8. function acf_get_field_rest_schema( array $field ) {
  9. $type = acf_get_field_type( $field['type'] );
  10. $schema = $type->get_rest_schema( $field );
  11. /**
  12. * Filter the REST API schema for a given field.
  13. *
  14. * @param array $schema The field schema array.
  15. * @param array $field The field array.
  16. */
  17. return (array) apply_filters( 'acf/rest/get_field_schema', $schema, $field );
  18. }
  19. acf_add_filter_variations( 'acf/rest/get_field_schema', array( 'type', 'name', 'key' ), 1 );
  20. /**
  21. * Get the REST API field links for a given field. The links are appended to the REST response under the _links property
  22. * and provide API resource links to related objects. If a link is marked as 'embeddable', WordPress can load the resource
  23. * in the main request under the _embedded property when the request contains the _embed URL parameter.
  24. *
  25. * @see \acf_field::get_rest_links()
  26. * @see https://developer.wordpress.org/rest-api/using-the-rest-api/linking-and-embedding/
  27. *
  28. * @param string|int $post_id
  29. * @param array $field
  30. * @return array
  31. */
  32. function acf_get_field_rest_links( $post_id, array $field ) {
  33. $value = acf_get_value( $post_id, $field );
  34. $type = acf_get_field_type( $field['type'] );
  35. $links = $type->get_rest_links( $value, $post_id, $field );
  36. /**
  37. * Filter the REST API links for a given field.
  38. *
  39. * @param array $links
  40. * @param string|int $post_id
  41. * @param array $field
  42. * @param mixed $value
  43. */
  44. return (array) apply_filters( 'acf/rest/get_field_links', $links, $post_id, $field, $value );
  45. }
  46. acf_add_filter_variations( 'acf/rest/get_field_links', array( 'type', 'name', 'key' ), 2 );
  47. /**
  48. * Format a given field's value for output in the REST API.
  49. *
  50. * @param $value
  51. * @param $post_id
  52. * @param $field
  53. * @param string $format 'light' for normal REST API formatting or 'standard' to apply ACF's normal field formatting.
  54. * @return mixed
  55. */
  56. function acf_format_value_for_rest( $value, $post_id, $field, $format = 'light' ) {
  57. if ( $format === 'standard' ) {
  58. $value_formatted = acf_format_value( $value, $post_id, $field );
  59. } else {
  60. $type = acf_get_field_type( $field['type'] );
  61. $value_formatted = $type->format_value_for_rest( $value, $post_id, $field );
  62. }
  63. /**
  64. * Filter the formatted value for a given field.
  65. *
  66. * @param mixed $value_formatted The formatted value.
  67. * @param string|int $post_id The post ID of the current object.
  68. * @param array $field The field array.
  69. * @param mixed $value The raw/unformatted value.
  70. * @param string $format The format applied to the field value.
  71. */
  72. return apply_filters( 'acf/rest/format_value_for_rest', $value_formatted, $post_id, $field, $value, $format );
  73. }
  74. acf_add_filter_variations( 'acf/rest/format_value_for_rest', array( 'type', 'name', 'key' ), 2 );