acf-rest-api-functions.php 3.0 KB

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