WordPress.org

Make WordPress Core

Changeset 37041


Ignore:
Timestamp:
03/22/2016 12:15:49 AM (4 years ago)
Author:
joehoyle
Message:

REST API: Provide better method for generating CURIEs

In [36533] CURIEs were added to the API responses for the link relation URIs, this makes
it a lot easier for clients to look up links by relation. That patch was functional, but
broke on edge cases such as embedded responses and collection items with links in the items.

This patch instead takes a less obtrusive approach by creating a new get_compact_response_links
to compliment get_response_links making both old and new functionality available.

Also the regex for curie relations has been relaxed to .+ as rel names can have any uri-valid charector in it.

Fixes #34729.

File:
1 edited

Legend:

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

    r37031 r37041  
    422422    public function response_to_data( $response, $embed ) {
    423423        $data  = $response->get_data();
    424         $links = $this->get_response_links( $response );
     424        $links = $this->get_compact_response_links( $response );
    425425
    426426        if ( ! empty( $links ) ) {
     
    455455    public static function get_response_links( $response ) {
    456456        $links = $response->get_links();
    457 
    458457        if ( empty( $links ) ) {
    459458            return array();
     
    462461        // Convert links to part of the data.
    463462        $data = array();
     463        foreach ( $links as $rel => $items ) {
     464            $data[ $rel ] = array();
     465
     466            foreach ( $items as $item ) {
     467                $attributes = $item['attributes'];
     468                $attributes['href'] = $item['href'];
     469                $data[ $rel ][] = $attributes;
     470            }
     471        }
     472
     473        return $data;
     474    }
     475
     476    /**
     477     * Retrieves the CURIEs (compact URIs) used for relations.
     478     *
     479     * Extracts the links from a response into a structured hash, suitable for
     480     * direct output.
     481     *
     482     * @since 4.5.0
     483     * @access public
     484     * @static
     485     *
     486     * @param WP_REST_Response $response Response to extract links from.
     487     * @return array Map of link relation to list of link hashes.
     488     */
     489    public static function get_compact_response_links( $response ) {
     490        $links = self::get_response_links( $response );
     491
     492        if ( empty( $links ) ) {
     493            return array();
     494        }
     495
    464496        $curies = $response->get_curies();
    465497        $used_curies = array();
     
    473505                    continue;
    474506                }
    475                 $used_curies[ $curie['name'] ] = $curie;
    476507
    477508                // Relation now changes from '$uri' to '$curie:$relation'
    478                 $rel_regex = str_replace( '\{rel\}', '([\w]+)', preg_quote( $curie['href'], '!' ) );
     509                $rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) );
    479510                preg_match( '!' . $rel_regex . '!', $rel, $matches );
    480511                if ( $matches ) {
    481                     $rel = $curie['name'] . ':' . $matches[1];
    482                 }
    483                 break;
    484             }
    485 
    486             $data[ $rel ] = array();
    487 
    488             foreach ( $items as $item ) {
    489                 $attributes = $item['attributes'];
    490                 $attributes['href'] = $item['href'];
    491                 $data[ $rel ][] = $attributes;
     512                    $new_rel = $curie['name'] . ':' . $matches[1];
     513                    $used_curies[ $curie['name'] ] = $curie;
     514                    $links[ $new_rel ] = $items;
     515                    unset( $links[ $rel ] );
     516                    break;
     517                }
    492518            }
    493519        }
     
    495521        // Push the curies onto the start of the links array.
    496522        if ( $used_curies ) {
    497             $data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data );
    498         }
    499 
    500         return $data;
     523            $links['curies'] = array_values( $used_curies );
     524        }
     525
     526        return $links;
    501527    }
    502528
Note: See TracChangeset for help on using the changeset viewer.