Make WordPress Core

Changeset 37903


Ignore:
Timestamp:
06/29/2016 01:56:32 AM (7 years ago)
Author:
rachelbaker
Message:

REST API: Include auto-discovery Link header when serving API requests.

The Link header allows clients to verify if a site has made the REST API available, as well as indicating how to access it.

Props danielbachhuber.
Fixes #35580.

Location:
trunk
Files:
2 edited

Legend:

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

    r37726 r37903  
    228228        $this->send_header( 'Content-Type', $content_type . '; charset=' . get_option( 'blog_charset' ) );
    229229        $this->send_header( 'X-Robots-Tag', 'noindex' );
     230
     231        $api_root = get_rest_url();
     232        if ( ! empty( $api_root ) ) {
     233            $this->send_header( 'Link', '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"' );
     234        }
    230235
    231236        /*
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r37726 r37903  
    727727    }
    728728
     729    public function test_link_header_on_requests() {
     730        $api_root = get_rest_url();
     731
     732        $request = new WP_REST_Request( 'GET', '/', array() );
     733
     734        $result = $this->server->serve_request('/');
     735        $headers = $this->server->sent_headers;
     736
     737        $this->assertEquals( '<' . esc_url_raw( $api_root ) . '>; rel="https://api.w.org/"', $headers['Link'] );
     738    }
     739
    729740    public function test_nocache_headers_on_authenticated_requests() {
    730741        $editor = self::factory()->user->create( array( 'role' => 'editor' ) );
Note: See TracChangeset for help on using the changeset viewer.