WordPress.org

Make WordPress Core


Ignore:
Timestamp:
05/04/2020 02:44:44 AM (19 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support more JSON Schemas when filtering a response by context.

The array type, multi-types, and the additional properties keyword are now supported. Additionally, the filter recurses to an infinite depth.

Fixes #48819.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r47511 r47758  
    289289     * @since 4.7.0
    290290     *
    291      * @param array  $data    Response data to fiter.
     291     * @param array  $data    Response data to filter.
    292292     * @param string $context Context defined in the schema.
    293293     * @return array Filtered response.
     
    297297        $schema = $this->get_item_schema();
    298298
    299         foreach ( $data as $key => $value ) {
    300             if ( empty( $schema['properties'][ $key ] ) || empty( $schema['properties'][ $key ]['context'] ) ) {
    301                 continue;
    302             }
    303 
    304             if ( ! in_array( $context, $schema['properties'][ $key ]['context'], true ) ) {
    305                 unset( $data[ $key ] );
    306                 continue;
    307             }
    308 
    309             if ( 'object' === $schema['properties'][ $key ]['type'] && ! empty( $schema['properties'][ $key ]['properties'] ) ) {
    310                 foreach ( $schema['properties'][ $key ]['properties'] as $attribute => $details ) {
    311                     if ( empty( $details['context'] ) ) {
    312                         continue;
    313                     }
    314 
    315                     if ( ! in_array( $context, $details['context'], true ) ) {
    316                         if ( isset( $data[ $key ][ $attribute ] ) ) {
    317                             unset( $data[ $key ][ $attribute ] );
    318                         }
    319                     }
    320                 }
    321             }
    322         }
    323 
    324         return $data;
     299        return rest_filter_response_by_context( $data, $schema, $context );
    325300    }
    326301
Note: See TracChangeset for help on using the changeset viewer.