Skip to content
This repository has been archived by the owner on Sep 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2428 from BE-Webdesign/#2424-mixed-usage-of-rest-url
Browse files Browse the repository at this point in the history
#2424 mixed usage of rest url
  • Loading branch information
joehoyle committed Aug 17, 2016
2 parents 9f24684 + cda055b commit 9cb8702
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/endpoints/class-wp-rest-attachments-controller.php
Expand Up @@ -140,7 +140,7 @@ public function create_item( $request ) {
$response = $this->prepare_item_for_response( $attachment, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $id ) ) );

/**
* Fires after a single attachment is created or updated via the REST API.
Expand Down
14 changes: 7 additions & 7 deletions lib/endpoints/class-wp-rest-comments-controller.php
Expand Up @@ -193,7 +193,7 @@ public function get_items( $request ) {
$response->header( 'X-WP-Total', $total_comments );
$response->header( 'X-WP-TotalPages', $max_pages );

$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $request['page'] > 1 ) {
$prev_page = $request['page'] - 1;
if ( $prev_page > $max_pages ) {
Expand Down Expand Up @@ -378,7 +378,7 @@ public function create_item( $request ) {
$response = $this->prepare_item_for_response( $comment, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) );

/**
* Fires after a comment is created or updated via the REST API.
Expand Down Expand Up @@ -605,16 +605,16 @@ public function prepare_item_for_response( $comment, $request ) {
protected function prepare_links( $comment ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_ID ) ),
'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_ID ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);

if ( 0 !== (int) $comment->user_id ) {
$links['author'] = array(
'href' => rest_url( '/wp/v2/users/' . $comment->user_id ),
'href' => rest_url( 'wp/v2/users/' . $comment->user_id ),
'embeddable' => true,
);
}
Expand All @@ -626,7 +626,7 @@ protected function prepare_links( $comment ) {
$base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;

$links['up'] = array(
'href' => rest_url( '/wp/v2/' . $base . '/' . $comment->comment_post_ID ),
'href' => rest_url( 'wp/v2/' . $base . '/' . $comment->comment_post_ID ),
'embeddable' => true,
'post_type' => $post->post_type,
);
Expand All @@ -635,7 +635,7 @@ protected function prepare_links( $comment ) {

if ( 0 !== (int) $comment->comment_parent ) {
$links['in-reply-to'] = array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_parent ) ),
'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_parent ) ),
'embeddable' => true,
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/endpoints/class-wp-rest-post-statuses-controller.php
Expand Up @@ -155,9 +155,9 @@ public function prepare_item_for_response( $status, $request ) {
$response = rest_ensure_response( $data );

if ( 'publish' === $status->name ) {
$response->add_link( 'archives', rest_url( '/wp/v2/posts' ) );
$response->add_link( 'archives', rest_url( 'wp/v2/posts' ) );
} else {
$response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( '/wp/v2/posts' ) ) );
$response->add_link( 'archives', add_query_arg( 'status', $status->name, rest_url( 'wp/v2/posts' ) ) );
}

/**
Expand Down
12 changes: 6 additions & 6 deletions lib/endpoints/class-wp-rest-posts-controller.php
Expand Up @@ -185,7 +185,7 @@ public function get_items( $request ) {
unset( $request_params['filter']['posts_per_page'] );
unset( $request_params['filter']['paged'] );
}
$base = add_query_arg( $request_params, rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
$base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );

if ( $page > 1 ) {
$prev_page = $page - 1;
Expand Down Expand Up @@ -349,7 +349,7 @@ public function create_item( $request ) {
$response = $this->prepare_item_for_response( $post, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $post_id ) ) );

return $response;
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ public function prepare_item_for_response( $post, $request ) {
* @return array Links for the given post.
*/
protected function prepare_links( $post ) {
$base = sprintf( '/%s/%s', $this->namespace, $this->rest_base );
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );

// Entity meta
$links = array(
Expand All @@ -1249,20 +1249,20 @@ protected function prepare_links( $post ) {
'href' => rest_url( $base ),
),
'about' => array(
'href' => rest_url( '/wp/v2/types/' . $this->post_type ),
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
),
);

if ( ( in_array( $post->post_type, array( 'post', 'page' ) ) || post_type_supports( $post->post_type, 'author' ) )
&& ! empty( $post->post_author ) ) {
$links['author'] = array(
'href' => rest_url( '/wp/v2/users/' . $post->post_author ),
'href' => rest_url( 'wp/v2/users/' . $post->post_author ),
'embeddable' => true,
);
};

if ( in_array( $post->post_type, array( 'post', 'page' ) ) || post_type_supports( $post->post_type, 'comments' ) ) {
$replies_url = rest_url( '/wp/v2/comments' );
$replies_url = rest_url( 'wp/v2/comments' );
$replies_url = add_query_arg( 'post', $post->ID, $replies_url );
$links['replies'] = array(
'href' => $replies_url,
Expand Down
2 changes: 1 addition & 1 deletion lib/endpoints/class-wp-rest-revisions-controller.php
Expand Up @@ -213,7 +213,7 @@ public function prepare_item_for_response( $post, $request ) {
$response = rest_ensure_response( $data );

if ( ! empty( $data['parent'] ) ) {
$response->add_link( 'parent', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
$response->add_link( 'parent', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->parent_base, $data['parent'] ) ) );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/endpoints/class-wp-rest-terms-controller.php
Expand Up @@ -182,7 +182,7 @@ public function get_items( $request ) {
$max_pages = ceil( $total_terms / $per_page );
$response->header( 'X-WP-TotalPages', (int) $max_pages );

$base = add_query_arg( $request->get_query_params(), rest_url( '/' . $this->namespace . '/' . $this->rest_base ) );
$base = add_query_arg( $request->get_query_params(), rest_url( $this->namespace . '/' . $this->rest_base ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
Expand Down Expand Up @@ -376,7 +376,7 @@ public function create_item( $request ) {
$response = $this->prepare_item_for_response( $term, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( '/' . $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
$response->header( 'Location', rest_url( $this->namespace . '/' . $this->rest_base . '/' . $term->term_id ) );
return $response;
}

Expand Down Expand Up @@ -613,7 +613,7 @@ public function prepare_item_for_response( $item, $request ) {
* @return array Links for the given term.
*/
protected function prepare_links( $term ) {
$base = '/' . $this->namespace . '/' . $this->rest_base;
$base = $this->namespace . '/' . $this->rest_base;
$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $term->term_id ),
Expand Down
10 changes: 5 additions & 5 deletions lib/endpoints/class-wp-rest-users-controller.php
Expand Up @@ -168,7 +168,7 @@ public function get_items( $request ) {
$max_pages = ceil( $total_users / $per_page );
$response->header( 'X-WP-TotalPages', (int) $max_pages );

$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ) );
$base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
Expand Down Expand Up @@ -250,7 +250,7 @@ public function get_current_item( $request ) {
$user = wp_get_current_user();
$response = $this->prepare_item_for_response( $user, $request );
$response = rest_ensure_response( $response );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $current_user_id ) ) );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $current_user_id ) ) );
$response->set_status( 302 );

return $response;
Expand Down Expand Up @@ -337,7 +337,7 @@ public function create_item( $request ) {
$response = $this->prepare_item_for_response( $user, $request );
$response = rest_ensure_response( $response );
$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $user_id ) ) );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $user_id ) ) );

return $response;
}
Expand Down Expand Up @@ -594,10 +594,10 @@ public function prepare_item_for_response( $user, $request ) {
protected function prepare_links( $user ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $user->ID ) ),
'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $user->ID ) ),
),
'collection' => array(
'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
),
);

Expand Down
10 changes: 8 additions & 2 deletions tests/class-wp-test-rest-controller-testcase.php
Expand Up @@ -6,7 +6,7 @@ abstract class WP_Test_REST_Controller_Testcase extends WP_Test_REST_TestCase {

public function setUp() {
parent::setUp();

add_filter( 'rest_url', array( $this, 'filter_rest_url_for_leading_slash' ), 10, 2 );
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new WP_Test_Spy_REST_Server;
Expand All @@ -15,7 +15,7 @@ public function setUp() {

public function tearDown() {
parent::tearDown();

remove_filter( 'rest_url', array( $this, 'test_rest_url_for_leading_slash' ), 10, 2 );
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$wp_rest_server = null;
Expand All @@ -39,4 +39,10 @@ abstract public function test_prepare_item();

abstract public function test_get_item_schema();

public function filter_rest_url_for_leading_slash( $url, $path ) {
// Make sure path for rest_url has a leading slash for proper resolution.
$this->assertTrue( 0 === strpos( $path, '/' ) );

return $url;
}
}
12 changes: 6 additions & 6 deletions tests/test-rest-categories-controller.php
Expand Up @@ -443,7 +443,7 @@ public function test_get_terms_pagination_headers() {
$this->assertCount( 10, $response->get_data() );
$next_link = add_query_arg( array(
'page' => 2,
), rest_url( '/wp/v2/categories' ) );
), rest_url( 'wp/v2/categories' ) );
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
// 3rd page
Expand All @@ -459,11 +459,11 @@ public function test_get_terms_pagination_headers() {
$this->assertCount( 10, $response->get_data() );
$prev_link = add_query_arg( array(
'page' => 2,
), rest_url( '/wp/v2/categories' ) );
), rest_url( 'wp/v2/categories' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$next_link = add_query_arg( array(
'page' => 4,
), rest_url( '/wp/v2/categories' ) );
), rest_url( 'wp/v2/categories' ) );
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
// Last page
$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
Expand All @@ -475,7 +475,7 @@ public function test_get_terms_pagination_headers() {
$this->assertCount( 1, $response->get_data() );
$prev_link = add_query_arg( array(
'page' => 5,
), rest_url( '/wp/v2/categories' ) );
), rest_url( 'wp/v2/categories' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
// Out of bounds
Expand All @@ -488,7 +488,7 @@ public function test_get_terms_pagination_headers() {
$this->assertCount( 0, $response->get_data() );
$prev_link = add_query_arg( array(
'page' => 6,
), rest_url( '/wp/v2/categories' ) );
), rest_url( 'wp/v2/categories' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
}
Expand Down Expand Up @@ -759,7 +759,7 @@ public function test_prepare_taxonomy_term_child() {
$this->assertEquals( 1, $data['parent'] );

$links = $response->get_links();
$this->assertEquals( rest_url( '/wp/v2/categories/1' ), $links['up'][0]['href'] );
$this->assertEquals( rest_url( 'wp/v2/categories/1' ), $links['up'][0]['href'] );
}

public function test_get_item_schema() {
Expand Down
10 changes: 5 additions & 5 deletions tests/test-rest-tags-controller.php
Expand Up @@ -353,7 +353,7 @@ public function test_get_terms_pagination_headers() {
$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
$next_link = add_query_arg( array(
'page' => 2,
), rest_url( '/wp/v2/tags' ) );
), rest_url( 'wp/v2/tags' ) );
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
// 3rd page
Expand All @@ -368,11 +368,11 @@ public function test_get_terms_pagination_headers() {
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg( array(
'page' => 2,
), rest_url( '/wp/v2/tags' ) );
), rest_url( 'wp/v2/tags' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$next_link = add_query_arg( array(
'page' => 4,
), rest_url( '/wp/v2/tags' ) );
), rest_url( 'wp/v2/tags' ) );
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
// Last page
$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
Expand All @@ -383,7 +383,7 @@ public function test_get_terms_pagination_headers() {
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg( array(
'page' => 5,
), rest_url( '/wp/v2/tags' ) );
), rest_url( 'wp/v2/tags' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
// Out of bounds
Expand All @@ -395,7 +395,7 @@ public function test_get_terms_pagination_headers() {
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg( array(
'page' => 6,
), rest_url( '/wp/v2/tags' ) );
), rest_url( 'wp/v2/tags' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
}
Expand Down
14 changes: 7 additions & 7 deletions tests/test-rest-users-controller.php
Expand Up @@ -159,7 +159,7 @@ public function test_get_items_pagination_headers() {
$this->assertEquals( 5, $headers['X-WP-TotalPages'] );
$next_link = add_query_arg( array(
'page' => 2,
), rest_url( '/wp/v2/users' ) );
), rest_url( 'wp/v2/users' ) );
$this->assertFalse( stripos( $headers['Link'], 'rel="prev"' ) );
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
// 3rd page
Expand All @@ -174,11 +174,11 @@ public function test_get_items_pagination_headers() {
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg( array(
'page' => 2,
), rest_url( '/wp/v2/users' ) );
), rest_url( 'wp/v2/users' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$next_link = add_query_arg( array(
'page' => 4,
), rest_url( '/wp/v2/users' ) );
), rest_url( 'wp/v2/users' ) );
$this->assertContains( '<' . $next_link . '>; rel="next"', $headers['Link'] );
// Last page
$request = new WP_REST_Request( 'GET', '/wp/v2/users' );
Expand All @@ -189,7 +189,7 @@ public function test_get_items_pagination_headers() {
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg( array(
'page' => 5,
), rest_url( '/wp/v2/users' ) );
), rest_url( 'wp/v2/users' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
// Out of bounds
Expand All @@ -201,7 +201,7 @@ public function test_get_items_pagination_headers() {
$this->assertEquals( 6, $headers['X-WP-TotalPages'] );
$prev_link = add_query_arg( array(
'page' => 6,
), rest_url( '/wp/v2/users' ) );
), rest_url( 'wp/v2/users' ) );
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
$this->assertFalse( stripos( $headers['Link'], 'rel="next"' ) );
}
Expand Down Expand Up @@ -233,7 +233,7 @@ public function test_get_items_page() {
$prev_link = add_query_arg( array(
'per_page' => 5,
'page' => 1,
), rest_url( '/wp/v2/users' ) );
), rest_url( 'wp/v2/users' ) );
$headers = $response->get_headers();
$this->assertContains( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
}
Expand Down Expand Up @@ -535,7 +535,7 @@ public function test_get_current_user() {

$headers = $response->get_headers();
$this->assertArrayHasKey( 'Location', $headers );
$this->assertEquals( rest_url( '/wp/v2/users/' . $this->user ), $headers['Location'] );
$this->assertEquals( rest_url( 'wp/v2/users/' . $this->user ), $headers['Location'] );
}

public function test_get_current_user_without_permission() {
Expand Down

0 comments on commit 9cb8702

Please sign in to comment.