Skip to content

Commit

Permalink
Introduce a new method to prepare recipients
Browse files Browse the repository at this point in the history
`BP_REST_Messages_Endpoint::prepare_recipient_for_response()` appends the user link and the user avatars (full & thumb).
  • Loading branch information
imath committed Jul 5, 2019
1 parent c8858f8 commit 52534e8
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
57 changes: 56 additions & 1 deletion includes/bp-messages/classes/class-bp-rest-messages-endpoint.php
Expand Up @@ -771,6 +771,56 @@ public function prepare_message_for_response( $message, $request ) {
return apply_filters( 'bp_rest_messages_prepare_message_value', $data, $request );
}

/**
* Prepares recipient data for the REST response.
*
* @since 0.1.0
*
* @param object $recipient The recipient object.
* @param WP_REST_Request $request Full details about the request.
* @return array The recipient data for the REST response.
*/
public function prepare_recipient_for_response( $recipient, $request ) {
$data = array(
'id' => (int) $recipient->id,
'user_id' => (int) $recipient->user_id,
'user_link' => esc_url( bp_core_get_user_domain( $recipient->user_id ) ),
);

// Fetch the user avatar urls (Full & thumb).
if ( true === buddypress()->avatar->show_avatars ) {
foreach ( array( 'full', 'thumb' ) as $type ) {
$data['user_avatars'][ $type ] = bp_core_fetch_avatar(
array(
'item_id' => $recipient->user_id,
'html' => false,
'type' => $type,
)
);
}
}

$data = array_merge(
$data,
array(
'thread_id' => (int) $recipient->thread_id,
'unread_count' => (int) $recipient->unread_count,
'sender_only' => (int) $recipient->sender_only,
'is_deleted' => (int) $recipient->is_deleted,
)
);

/**
* Filter a recipient value returned from the API.
*
* @since 0.1.0
*
* @param array $data The recipient value for the REST response.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'bp_rest_messages_prepare_recipient_value', $data, $request );
}

/**
* Prepares thread data for return as an object.
*
Expand Down Expand Up @@ -805,7 +855,7 @@ public function prepare_item_for_response( $thread, $request ) {
'date' => bp_rest_prepare_date_response( $thread->last_message_date ),
'unread_count' => ! empty( $thread->unread_count ) ? $thread->unread_count : 0,
'sender_ids' => $thread->sender_ids,
'recipients' => $thread->recipients,
'recipients' => array(),
'messages' => array(),
);

Expand All @@ -814,6 +864,11 @@ public function prepare_item_for_response( $thread, $request ) {
$data['messages'][] = $this->prepare_message_for_response( $message, $request );
}

// Loop through recipients to prepare them for the response.
foreach ( $thread->recipients as $recipient ) {
$data['recipients'][ $recipient->user_id ] = $this->prepare_recipient_for_response( $recipient, $request );
}

// Pluck starred message ids.
$data['starred_message_ids'] = array_keys( array_filter( wp_list_pluck( $data['messages'], 'is_starred', 'id' ) ) );

Expand Down
43 changes: 43 additions & 0 deletions tests/messages/test-controller.php
Expand Up @@ -825,4 +825,47 @@ public function test_additional_fields_for_specific_message_updated() {

$GLOBALS['wp_rest_additional_fields'] = $registered_fields;
}

/**
* @group prepare_recipient_for_response
*/
public function test_prepare_prepare_recipient_for_response() {
$u1 = $this->factory->user->create();
$u2 = $this->factory->user->create();
$u3 = $this->factory->user->create();

$m = $this->bp_factory->message->create_and_get( array(
'sender_id' => $u1,
'recipients' => array( $u2, $u3 ),
'subject' => 'Foo',
'content' => 'Content',
) );

$this->bp->set_current_user( $u2 );

$request = new WP_REST_Request( 'GET', $this->endpoint_url . '/' . $m->thread_id );

$request->set_param( 'context', 'view' );
$response = $this->server->dispatch( $request );

$get_data = $response->get_data();
$recipients = $get_data[0]['recipients'];
$expended = array();

foreach( array( $u1, $u2, $u3 ) as $user_id ) {
$this->assertEquals( esc_url( bp_core_get_user_domain( $user_id ) ), $recipients[ $user_id ]['user_link'] );

foreach ( array( 'full', 'thumb' ) as $type ) {
$expected['user_avatars'][ $type ] = bp_core_fetch_avatar(
array(
'item_id' => $user_id ,
'html' => false,
'type' => $type,
)
);

$this->assertEquals( $expected['user_avatars'][ $type ], $recipients[ $user_id ]['user_avatars'][ $type ] );
}
}
}
}

0 comments on commit 52534e8

Please sign in to comment.