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