WordPress.org

Make WordPress Core

Changeset 36533


Ignore:
Timestamp:
02/16/2016 02:18:34 AM (2 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.

Location:
trunk
Files:
3 edited

Legend:

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

    r35671 r36533  
    257257        return $error;
    258258    }
     259
     260    /**
     261     * Get the CURIEs (compact URIs) used for relations.
     262     *
     263     * @return array
     264     */
     265    public function get_curies() {
     266        $curies = array(
     267            array(
     268                'name' => 'wp',
     269                'href' => 'https://api.w.org/{rel}',
     270                'templated' => true,
     271            ),
     272        );
     273
     274        /**
     275         * Filter extra CURIEs available on API responses.
     276         *
     277         * CURIEs allow a shortened version of URI relations. This allows a more
     278         * usable form for custom relations than using the full URI. These work
     279         * similarly to how XML namespaces work.
     280         *
     281         * Registered CURIES need to specify a name and URI template. This will
     282         * automatically transform URI relations into their shortened version.
     283         * The shortened relation follows the format `{name}:{rel}`. `{rel}` in
     284         * the URI template will be replaced with the `{rel}` part of the
     285         * shortened relation.
     286         *
     287         * For example, a CURIE with name `example` and URI template
     288         * `http://w.org/{rel}` would transform a `http://w.org/term` relation
     289         * into `example:term`.
     290         *
     291         * Well-behaved clients should expand and normalise these back to their
     292         * full URI relation, however some naive clients may not resolve these
     293         * correctly, so adding new CURIEs may break backwards compatibility.
     294         *
     295         * @param array $additional Additional CURIEs to register with the API.
     296         */
     297        $additional = apply_filters( 'rest_response_link_curies', array() );
     298        return array_merge( $curies, $additional );
     299    }
    259300}
  • 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
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r36531 r36533  
    401401    }
    402402
     403    public function test_link_curies() {
     404        $response = new WP_REST_Response();
     405        $response->add_link( 'https://api.w.org/term', 'http://example.com/' );
     406
     407        $data = $this->server->response_to_data( $response, false );
     408        $links = $data['_links'];
     409
     410        $this->assertArrayHasKey( 'wp:term', $links );
     411        $this->assertArrayHasKey( 'curies', $links );
     412    }
     413
     414    public function test_custom_curie_link() {
     415        $response = new WP_REST_Response();
     416        $response->add_link( 'http://mysite.com/contact.html', 'http://example.com/' );
     417
     418        add_filter( 'rest_response_link_curies', array( $this, 'add_custom_curie' ) );
     419
     420        $data = $this->server->response_to_data( $response, false );
     421        $links = $data['_links'];
     422
     423        $this->assertArrayHasKey( 'my_site:contact', $links );
     424        $this->assertArrayHasKey( 'curies', $links );
     425    }
     426
     427    /**
     428     * Helper callback to add a new custom curie via a filter.
     429     *
     430     * @param array $curies
     431     * @return array
     432     */
     433    public function add_custom_curie( $curies ) {
     434        $curies[] = array(
     435            'name'      => 'my_site',
     436            'href'      => 'http://mysite.com/{rel}.html',
     437            'templated' => true,
     438        );
     439        return $curies;
     440    }
     441
    403442    /**
    404443     * @depends test_link_embedding
Note: See TracChangeset for help on using the changeset viewer.