WP_REST_Controller::filter_response_by_context( array $data, string $context )

Filters a response based on the context defined in the schema.


Description Description


Parameters Parameters

$data

(array) (Required) Response data to fiter.

$context

(string) (Required) Context defined in the schema.


Top ↑

Return Return

(array) Filtered response.


Top ↑

Source Source

File: wp-includes/rest-api/endpoints/class-wp-rest-controller.php

	public function filter_response_by_context( $data, $context ) {

		$schema = $this->get_item_schema();

		foreach ( $data as $key => $value ) {
			if ( empty( $schema['properties'][ $key ] ) || empty( $schema['properties'][ $key ]['context'] ) ) {
				continue;
			}

			if ( ! in_array( $context, $schema['properties'][ $key ]['context'], true ) ) {
				unset( $data[ $key ] );
				continue;
			}

			if ( 'object' === $schema['properties'][ $key ]['type'] && ! empty( $schema['properties'][ $key ]['properties'] ) ) {
				foreach ( $schema['properties'][ $key ]['properties'] as $attribute => $details ) {
					if ( empty( $details['context'] ) ) {
						continue;
					}

					if ( ! in_array( $context, $details['context'], true ) ) {
						if ( isset( $data[ $key ][ $attribute ] ) ) {
							unset( $data[ $key ][ $attribute ] );
						}
					}
				}
			}
		}

		return $data;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.