REST API: Allow explicit HEAD callbacks.

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

Fixes #34841.


git-svn-id: https://develop.svn.wordpress.org/trunk@36535 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan McCue 2016-02-16 05:50:21 +00:00
parent 66fc70f0e1
commit 10bfe70f06
2 changed files with 28 additions and 1 deletions

View File

@ -820,7 +820,11 @@ class WP_REST_Server {
$callback = $handler['callback'];
$response = null;
$checked_method = 'HEAD' === $method ? 'GET' : $method;
// Fallback to GET method if no HEAD method is registered.
$checked_method = $method;
if ( 'HEAD' === $method && empty( $handler['methods']['HEAD'] ) ) {
$checked_method = 'GET';
}
if ( empty( $handler['methods'][ $checked_method ] ) ) {
continue;
}

View File

@ -138,6 +138,29 @@ class Tests_REST_Server extends WP_Test_REST_TestCase {
$this->assertEquals( 200, $response->get_status() );
}
/**
* Plugins should be able to register explicit HEAD callbacks before the
* GET callback.
*
* @depends test_head_request_handled_by_get
*/
public function test_explicit_head_callback() {
register_rest_route( 'head-request', '/test', array(
array(
'methods' => array( 'HEAD' ),
'callback' => '__return_true',
),
array(
'methods' => array( 'GET' ),
'callback' => '__return_false',
'permission_callback' => array( $this, 'permission_denied' ),
),
));
$request = new WP_REST_Request( 'HEAD', '/head-request/test' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}
/**
* Pass a capability which the user does not have, this should
* result in a 403 error.