WordPress.org

Make WordPress Core

Changeset 36535


Ignore:
Timestamp:
02/16/2016 05:50:21 AM (6 years ago)
Author:
rmccue
Message:

REST API: Allow explicit HEAD callbacks.

HEAD callbacks can now be registered independently, with the GET
callback still used as a fallback.

Fixes #34841.

Location:
trunk
Files:
2 edited

Legend:

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

    r36534 r36535  
    821821                $response = null;
    822822
    823                 $checked_method = 'HEAD' === $method ? 'GET' : $method;
     823                // Fallback to GET method if no HEAD method is registered.
     824                $checked_method = $method;
     825                if ( 'HEAD' === $method && empty( $handler['methods']['HEAD'] ) ) {
     826                    $checked_method = 'GET';
     827                }
    824828                if ( empty( $handler['methods'][ $checked_method ] ) ) {
    825829                    continue;
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r36533 r36535  
    140140
    141141    /**
     142     * Plugins should be able to register explicit HEAD callbacks before the
     143     * GET callback.
     144     *
     145     * @depends test_head_request_handled_by_get
     146     */
     147    public function test_explicit_head_callback() {
     148        register_rest_route( 'head-request', '/test', array(
     149            array(
     150                'methods' => array( 'HEAD' ),
     151                'callback' => '__return_true',
     152            ),
     153            array(
     154                'methods' => array( 'GET' ),
     155                'callback' => '__return_false',
     156                'permission_callback' => array( $this, 'permission_denied' ),
     157            ),
     158        ));
     159        $request = new WP_REST_Request( 'HEAD', '/head-request/test' );
     160        $response = $this->server->dispatch( $request );
     161        $this->assertEquals( 200, $response->get_status() );
     162    }
     163
     164    /**
    142165     * Pass a capability which the user does not have, this should
    143166     * result in a 403 error.
Note: See TracChangeset for help on using the changeset viewer.