wpseek.com
Eine auf WordPress spezialiserte Suchmaschine für Entwickler und Theme-Autoren
rest_filter_response_fields › WordPress Function
Seit4.8.0
Veraltetn/v
› rest_filter_response_fields ( $response, $server, $request )
Parameter: (3) |
|
Gibt zurück: |
|
Definiert in: |
|
Codex: |
Filters the REST API response to include only an allow-listed set of response object fields.
Ähnliche Funktionen: rest_filter_response_by_context, register_rest_field, rest_ensure_response, sanitize_post_field, get_oembed_response_data
Quellcode
function rest_filter_response_fields( $response, $server, $request ) { if ( ! isset( $request['_fields'] ) || $response->is_error() ) { return $response; } $data = $response->get_data(); $fields = wp_parse_list( $request['_fields'] ); if ( 0 === count( $fields ) ) { return $response; } // Trim off outside whitespace from the comma delimited list. $fields = array_map( 'trim', $fields ); // Create nested array of accepted field hierarchy. $fields_as_keyed = array(); foreach ( $fields as $field ) { $parts = explode( '.', $field ); $ref = &$fields_as_keyed; while ( count( $parts ) > 1 ) { $next = array_shift( $parts ); if ( isset( $ref[ $next ] ) && true === $ref[ $next ] ) { // Skip any sub-properties if their parent prop is already marked for inclusion. break 2; } $ref[ $next ] = isset( $ref[ $next ] ) ? $ref[ $next ] : array(); $ref = &$ref[ $next ]; } $last = array_shift( $parts ); $ref[ $last ] = true; } if ( wp_is_numeric_array( $data ) ) { $new_data = array(); foreach ( $data as $item ) { $new_data[] = _rest_array_intersect_key_recursive( $item, $fields_as_keyed ); } } else { $new_data = _rest_array_intersect_key_recursive( $data, $fields_as_keyed ); } $response->set_data( $new_data ); return $response; }