Make WordPress Core


Ignore:
Timestamp:
02/09/2020 08:52:06 PM (7 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Introduce selective link embedding.

Previously the _embed flag would embed all embeddable links in a response even if only a subset of the links were necessary. Now, a list of link relations can be passed in the _embed parameter to restrict the list of embedded objects.

Props rheinardkorf, adamsilverstein, jnylen0, cklosows, chrisvanpatten, TimothyBlynJacobs.
Fixes #39696.

File:
1 edited

Legend:

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

    r47138 r47224  
    399399
    400400                        // Embed links inside the request.
    401                         $result = $this->response_to_data( $result, isset( $_GET['_embed'] ) );
     401                        $embed  = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
     402                        $result = $this->response_to_data( $result, $embed );
    402403
    403404                        /**
     
    451452         *
    452453         * @since 4.4.0
     454         * @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
    453455         *
    454456         * @param WP_REST_Response $response Response object.
    455          * @param bool             $embed    Whether links should be embedded.
     457         * @param bool|string[]    $embed    Whether to embed all links, a filtered list of link relations, or no links.
    456458         * @return array {
    457459         *     Data with sub-requests embedded.
     
    474476                        // Determine if this is a numeric array.
    475477                        if ( wp_is_numeric_array( $data ) ) {
    476                                 $data = array_map( array( $this, 'embed_links' ), $data );
     478                                foreach ( $data as $key => $item ) {
     479                                        $data[ $key ] = $this->embed_links( $item, $embed );
     480                                }
    477481                        } else {
    478                                 $data = $this->embed_links( $data );
     482                                $data = $this->embed_links( $data, $embed );
    479483                        }
    480484                        $this->embed_cache = array();
     
    572576         *
    573577         * @since 4.4.0
    574          *
    575          * @param array $data Data from the request.
     578         * @since 5.4.0 The $embed parameter can now contain a list of link relations to include.
     579         *
     580         * @param array         $data  Data from the request.
     581         * @param bool|string[] $embed Whether to embed all links or a filtered list of link relations.
    576582         * @return array {
    577583         *     Data with sub-requests embedded.
     
    581587         * }
    582588         */
    583         protected function embed_links( $data ) {
     589        protected function embed_links( $data, $embed = true ) {
    584590                if ( empty( $data['_links'] ) ) {
    585591                        return $data;
     
    589595
    590596                foreach ( $data['_links'] as $rel => $links ) {
     597                        // If a list of relations was specified, and the link relation is not in the whitelist, don't process the link.
     598                        if ( is_array( $embed ) && ! in_array( $rel, $embed, true ) ) {
     599                                continue;
     600                        }
     601
    591602                        $embeds = array();
    592603
Note: See TracChangeset for help on using the changeset viewer.