Skip to content

Commit

Permalink
Messages: add pagination headers to the response
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Apr 22, 2024
1 parent 4ab7bd1 commit bb7a281
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions includes/bp-messages/classes/class-bp-rest-messages-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,14 @@ public function get_item( $request ) {
'recipients_per_page' => $request->get_param( 'recipients_per_page' ),
'page' => $request->get_param( 'messages_page' ),
'per_page' => $request->get_param( 'messages_per_page' ),
'order' => $request->get_param( 'order' ),
'user_id' => $request->get_param( 'user_id' ),
);

$user_id = bp_loggedin_user_id();
if ( ! empty( $request->get_param( 'user_id' ) ) ) {
$user_id = $request->get_param( 'user_id' );
if ( empty( $args['user_id'] ) ) {
$args['user_id'] = bp_loggedin_user_id();
}

$args['user_id'] = $user_id;

/**
* Filter the query arguments for the request.
*
Expand All @@ -258,7 +257,7 @@ public function get_item( $request ) {

$thread = new BP_Messages_Thread(
$request->get_param( 'id' ),
$request->get_param( 'order' ),
'ASC', // not used.
$args
);

Expand All @@ -269,6 +268,7 @@ public function get_item( $request ) {
);

$response = rest_ensure_response( $retval );
$response = bp_rest_response_add_total_headers( $response, $thread->messages_total_count, $args['per_page'] );

/**
* Fires after a thread is fetched via the REST API.
Expand Down
4 changes: 4 additions & 0 deletions tests/testcases/messages/test-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ public function test_get_thread_messages_paginated() {

$this->assertEquals( 200, $response->get_status() );

$headers = $response->get_headers();
$this->assertEquals( 12, $headers['X-WP-Total'] );
$this->assertEquals( 12, $headers['X-WP-TotalPages'] );

$all_data = current( $response->get_data() );

$this->assertCount( 1, $all_data['messages'] );
Expand Down

0 comments on commit bb7a281

Please sign in to comment.