From be371a7431c685ea9a53fdb8e1778dd87ff6d84d Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Thu, 20 Feb 2020 00:53:43 +0000 Subject: [PATCH] REST API: Fix links format in OPTIONS requests for non-variable routes. Props nsundberg, johnwatkins0, birgire. Fixes #49149. git-svn-id: https://develop.svn.wordpress.org/trunk@47326 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/class-wp-rest-server.php | 6 ++++- tests/phpunit/tests/rest-api/rest-server.php | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index efda2321b7..12de6c55f0 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -1288,7 +1288,11 @@ class WP_REST_Server { // For non-variable routes, generate links. if ( strpos( $route, '{' ) === false ) { $data['_links'] = array( - 'self' => rest_url( $route ), + 'self' => array( + array( + 'href' => rest_url( $route ), + ), + ), ); } } diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php index 755f0296ea..b724221891 100644 --- a/tests/phpunit/tests/rest-api/rest-server.php +++ b/tests/phpunit/tests/rest-api/rest-server.php @@ -1020,6 +1020,29 @@ class Tests_REST_Server extends WP_Test_REST_TestCase { $this->assertContains( 'test/another', $namespaces ); } + /** + * @ticket 49147 + */ + public function test_get_data_for_non_variable_route_includes_links() { + $expected = array( + 'self' => array( + array( 'href' => rest_url( 'wp/v2/posts' ) ), + ), + ); + + $actual = rest_get_server()->get_data_for_route( + '/wp/v2/posts', + array( + array( + 'methods' => array( 'OPTIONS' => 1 ), + 'show_in_index' => true, + ), + ) + ); + + $this->assertEquals( $expected, $actual['_links'] ); + } + public function test_x_robot_tag_header_on_requests() { $request = new WP_REST_Request( 'GET', '/', array() );