Make WordPress Core


Ignore:
Timestamp:
02/16/2016 02:18:34 AM (6 years ago)
Author:
rmccue
Message:

REST API: Add support for CURIEs.

CURIEs are Compact URIs, which provide a more usable way to use
custom relations in the API. The wp CURIE is registered by default
for https://api.w.org/ URI relations.

Fixes #34729.
Props joehoyle.

File:
1 edited

Legend:

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

    r35773 r36533  
    461461        // Convert links to part of the data.
    462462        $data = array();
     463        $curies = $response->get_curies();
     464        $used_curies = array();
     465
    463466        foreach ( $links as $rel => $items ) {
     467
     468            // Convert $rel URIs to their compact versions if they exist.
     469            foreach ( $curies as $curie ) {
     470                $href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) );
     471                if ( strpos( $rel, $href_prefix ) !== 0 ) {
     472                    continue;
     473                }
     474                $used_curies[ $curie['name'] ] = $curie;
     475
     476                // Relation now changes from '$uri' to '$curie:$relation'
     477                $rel_regex = str_replace( '\{rel\}', '([\w]+)', preg_quote( $curie['href'], '!' ) );
     478                preg_match( '!' . $rel_regex . '!', $rel, $matches );
     479                if ( $matches ) {
     480                    $rel = $curie['name'] . ':' . $matches[1];
     481                }
     482                break;
     483            }
     484
    464485            $data[ $rel ] = array();
    465486
     
    469490                $data[ $rel ][] = $attributes;
    470491            }
     492        }
     493
     494        // Push the curies onto the start of the links array.
     495        if ( $used_curies ) {
     496            $data = array_merge( array( 'curies' => array_values( $used_curies ) ), $data );
    471497        }
    472498
Note: See TracChangeset for help on using the changeset viewer.