Make WordPress Core

Ticket #49538: 49538.diff

File 49538.diff, 2.4 KB (added by iandunn, 5 years ago)

WIP

  • wp-includes/rest-api/class-wp-rest-server.php

     
    561561                        return $data;
    562562                }
    563563
     564                $requested_fields = $this->get_requested_embedded_fields();
    564565                $embedded = array();
    565566
    566567                foreach ( $data['_links'] as $rel => $links ) {
     
    574575                                        continue;
    575576                                }
    576577
     578                                // Ignore relationships that weren't requested.
     579                                if ( $requested_fields && ! array_key_exists( $rel, $requested_fields ) ) {
     580                                        continue;
     581                                }
     582
     583                                // Add the requested fields to the soon-to-be-dispatched request.
     584                                if ( array_key_exists( $rel, $requested_fields ) ) {
     585                                        $item['href'] = add_query_arg(
     586                                                '_fields',
     587                                                implode( ',', $requested_fields[ $rel ] ),
     588                                                $item['href']
     589                                        );
     590                                }
     591
    577592                                // Run through our internal routing and serve.
    578593                                $request = WP_REST_Request::from_url( $item['href'] );
    579594                                if ( ! $request ) {
     
    610625        }
    611626
    612627        /**
     628         * todo
     629         *
     630         * @return array
     631         */
     632        protected function get_requested_embedded_fields() {
     633                $raw_fields = isset( $_GET['_fields'] ) ? wp_parse_list( $_GET['_fields'] ) : '';
     634                // todo sanitize
     635
     636                // handle back-compat?
     637                        // some people may already using _fields and _embed in order to limit the main response, but might expect full embedded results to stay
     638
     639                // add tests
     640                        // success
     641                        // nested
     642                                // _embedded.author.avatar_urls.24 doesn't work yet
     643                        // handle case where nothing was requested
     644                        // where something was requested but doesn't exist -probably nothing to do here, just test
     645                        // what else?
     646
     647                if ( empty( $raw_fields ) ) {
     648                        return array();
     649                }
     650
     651                foreach ( $raw_fields as $raw_field ) {
     652                        if ( '_embedded.' !== substr( $raw_field, 0, 10 ) ) {
     653                                continue;
     654                        }
     655
     656                        $rel_with_nested    = str_replace( '_embedded.', '', $raw_field );
     657                        $delimiter_position = strpos( $rel_with_nested, '.' );
     658                        $rel                = substr( $rel_with_nested, 0, $delimiter_position );
     659
     660                        if ( false !== $delimiter_position ) {
     661                                $nested_field = substr( $rel_with_nested, $delimiter_position + 1 );
     662
     663                                $requested_fields[ $rel ][] = $nested_field;
     664                        } else {
     665                                $requested_fields[ $rel ] = array();
     666                        }
     667                }
     668
     669                return $requested_fields;
     670        }
     671
     672        /**
    613673         * Wraps the response in an envelope.
    614674         *
    615675         * The enveloping technique is used to work around browser/client