acf-rest-api-functions.php
1 week ago
class-acf-rest-api.php
3 days 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 month ago
index.php
1 year ago
acf-rest-api-functions.php
245 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 | * Replaces User subfield data with REST-safe values. |
| 62 | * |
| 63 | * @param mixed $formatted_value The formatted parent value. |
| 64 | * @param mixed $raw_value The raw parent value. |
| 65 | * @param array $sub_fields The parent field's subfields. |
| 66 | * @param string $output_property The subfield property used as the output key. |
| 67 | * @return mixed |
| 68 | */ |
| 69 | function scf_rest_sanitize_user_sub_fields( $formatted_value, $raw_value, $sub_fields, $output_property ) { |
| 70 | if ( ! is_array( $formatted_value ) || ! is_array( $sub_fields ) ) { |
| 71 | return $formatted_value; |
| 72 | } |
| 73 | |
| 74 | $raw_value = is_array( $raw_value ) ? $raw_value : array(); |
| 75 | |
| 76 | foreach ( $sub_fields as $sub_field ) { |
| 77 | if ( ! is_array( $sub_field ) ) { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | $output_key = array_key_exists( $output_property, $sub_field ) |
| 82 | ? $sub_field[ $output_property ] |
| 83 | : $sub_field['name'] ?? ''; |
| 84 | if ( '' === $output_key || ! array_key_exists( $output_key, $formatted_value ) ) { |
| 85 | continue; |
| 86 | } |
| 87 | |
| 88 | $raw_key = $sub_field['key'] ?? ''; |
| 89 | $raw_sub_value = '' !== $raw_key && array_key_exists( $raw_key, $raw_value ) |
| 90 | ? $raw_value[ $raw_key ] |
| 91 | : null; |
| 92 | |
| 93 | $formatted_value[ $output_key ] = scf_rest_sanitize_user_data( |
| 94 | $formatted_value[ $output_key ], |
| 95 | $raw_sub_value, |
| 96 | $sub_field |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return $formatted_value; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Replaces formatted User field data with REST-safe values based on the field definition. |
| 105 | * |
| 106 | * @param mixed $formatted_value The formatted field value. |
| 107 | * @param mixed $raw_value The raw field value. |
| 108 | * @param array $field The field array. |
| 109 | * @return mixed |
| 110 | */ |
| 111 | function scf_rest_sanitize_user_data( $formatted_value, $raw_value, $field ) { |
| 112 | if ( ! is_array( $field ) || empty( $field['type'] ) ) { |
| 113 | return $formatted_value; |
| 114 | } |
| 115 | |
| 116 | if ( 'user' === $field['type'] ) { |
| 117 | if ( ! $formatted_value ) { |
| 118 | return $formatted_value; |
| 119 | } |
| 120 | |
| 121 | if ( ! empty( $field['multiple'] ) && is_array( $formatted_value ) ) { |
| 122 | $user_ids = array(); |
| 123 | foreach ( $formatted_value as $user ) { |
| 124 | $user_ids[] = (int) acf_idval( $user ); |
| 125 | } |
| 126 | |
| 127 | return $user_ids; |
| 128 | } |
| 129 | |
| 130 | return (int) acf_idval( $formatted_value ); |
| 131 | } |
| 132 | |
| 133 | if ( ! is_array( $formatted_value ) ) { |
| 134 | return $formatted_value; |
| 135 | } |
| 136 | |
| 137 | $is_clone = 'clone' === $field['type']; |
| 138 | $is_group = 'group' === $field['type']; |
| 139 | |
| 140 | if ( $is_group || $is_clone ) { |
| 141 | $output_property = $is_clone ? '__name' : '_name'; |
| 142 | |
| 143 | return scf_rest_sanitize_user_sub_fields( |
| 144 | $formatted_value, |
| 145 | $raw_value, |
| 146 | $field['sub_fields'] ?? array(), |
| 147 | $output_property |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | if ( 'repeater' === $field['type'] ) { |
| 152 | $raw_value = is_array( $raw_value ) ? $raw_value : array(); |
| 153 | |
| 154 | foreach ( $formatted_value as $row_index => $formatted_row ) { |
| 155 | $raw_row = array_key_exists( $row_index, $raw_value ) ? $raw_value[ $row_index ] : array(); |
| 156 | |
| 157 | $formatted_value[ $row_index ] = scf_rest_sanitize_user_sub_fields( |
| 158 | $formatted_row, |
| 159 | $raw_row, |
| 160 | $field['sub_fields'] ?? array(), |
| 161 | '_name' |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | return $formatted_value; |
| 166 | } |
| 167 | |
| 168 | if ( 'flexible_content' === $field['type'] ) { |
| 169 | $raw_value = is_array( $raw_value ) ? $raw_value : array(); |
| 170 | |
| 171 | foreach ( $formatted_value as $row_index => $formatted_row ) { |
| 172 | if ( ! is_array( $formatted_row ) ) { |
| 173 | continue; |
| 174 | } |
| 175 | |
| 176 | $raw_row = array_key_exists( $row_index, $raw_value ) && is_array( $raw_value[ $row_index ] ) |
| 177 | ? $raw_value[ $row_index ] |
| 178 | : array(); |
| 179 | $layout_name = $formatted_row['acf_fc_layout'] ?? $raw_row['acf_fc_layout'] ?? ''; |
| 180 | |
| 181 | foreach ( $field['layouts'] ?? array() as $layout ) { |
| 182 | if ( ! isset( $layout['name'] ) || $layout_name !== $layout['name'] ) { |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | $formatted_value[ $row_index ] = scf_rest_sanitize_user_sub_fields( |
| 187 | $formatted_row, |
| 188 | $raw_row, |
| 189 | $layout['sub_fields'] ?? array(), |
| 190 | '_name' |
| 191 | ); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | return $formatted_value; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Applies standard field formatting while keeping User values safe for REST output. |
| 202 | * |
| 203 | * This does not apply the public acf/rest/format_value_for_rest filter. |
| 204 | * |
| 205 | * @param mixed $value The raw field value. |
| 206 | * @param string|int $post_id The post ID of the current object. |
| 207 | * @param array $field The field array. |
| 208 | * @return mixed |
| 209 | */ |
| 210 | function scf_rest_format_standard_value( $value, $post_id, $field ) { |
| 211 | $value_formatted = acf_format_value( $value, $post_id, $field ); |
| 212 | return scf_rest_sanitize_user_data( $value_formatted, $value, $field ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Format a given field's value for output in the REST API. |
| 217 | * |
| 218 | * @param $value |
| 219 | * @param $post_id |
| 220 | * @param $field |
| 221 | * @param string $format 'light' for normal REST API formatting or 'standard' to apply ACF's normal field formatting. |
| 222 | * @return mixed |
| 223 | */ |
| 224 | function acf_format_value_for_rest( $value, $post_id, $field, $format = 'light' ) { |
| 225 | if ( 'standard' === $format ) { |
| 226 | $value_formatted = scf_rest_format_standard_value( $value, $post_id, $field ); |
| 227 | } else { |
| 228 | $type = acf_get_field_type( $field['type'] ); |
| 229 | $value_formatted = $type->format_value_for_rest( $value, $post_id, $field ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Filter the formatted value for a given field. |
| 234 | * |
| 235 | * @param mixed $value_formatted The formatted value. |
| 236 | * @param string|int $post_id The post ID of the current object. |
| 237 | * @param array $field The field array. |
| 238 | * @param mixed $value The raw/unformatted value. |
| 239 | * @param string $format The format applied to the field value. |
| 240 | */ |
| 241 | return apply_filters( 'acf/rest/format_value_for_rest', $value_formatted, $post_id, $field, $value, $format ); |
| 242 | } |
| 243 | |
| 244 | acf_add_filter_variations( 'acf/rest/format_value_for_rest', array( 'type', 'name', 'key' ), 2 ); |
| 245 |