Skip to content

Commit 0945d70

Browse files
author
zarcode
committed
sentbox
1 parent f932e29 commit 0945d70

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# A JSON REST API for BuddyPress
22

3-
1.0.0 Started with Activity
4-
1.0.1 Messages : Inbox, OutBox and Compose Messages
3+
This plugin extends Version 2.0 of the [WP REST API](https://github.com/WP-API/WP-API/blob/develop/README.md).
54

6-
This plugin extends Version 2.0 of the [WP REST API](https://github.com/WP-API/WP-API/blob/develop/README.md).
5+
## Reference
6+
* Messages
7+
* Activity

endpoints/bp-api-message.php

+44-15
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ class BP_API_Messages extends WP_REST_Controller {
66
* Register the routes for the objects of the controller.
77
*/
88
public function register_routes() {
9+
910
register_rest_route( BP_API_SLUG, '/messages', array(
1011
array(
1112
'methods' => WP_REST_Server::READABLE,
1213
'callback' => array( $this, 'get_items' ),
13-
'permission_callback' => array( $this, 'bp_messages_permission' )
14+
'permission_callback' => array( $this, 'bp_messages_permission' ),
15+
'args' => array(
16+
'context' => array(
17+
'default' => 'view',
18+
),
19+
'box' => array(
20+
'default' => 'inbox'
21+
),
22+
'filter' => array(),
23+
)
1424
),
1525
array(
1626
'methods' => WP_REST_Server::CREATABLE,
@@ -101,9 +111,17 @@ public function create_item( $request ) {
101111
* @return array|WP_Error
102112
*/
103113
public function get_items( $request ) {
114+
$args = array();
115+
$args['box'] = $request['box'];
116+
117+
if ( isset( $request['filter'] ) ) {
118+
$args = array_merge( $args, $request['filter'] );
119+
unset( $args['filter'] );
120+
}
121+
104122
global $messages_template;
105123
$data = array();
106-
if ( bp_has_message_threads( bp_ajax_querystring( 'messages' ) ) ) {
124+
if ( bp_has_message_threads($args) ) {
107125
while ( bp_message_threads() ) : bp_message_thread();
108126
$single_msg=array(
109127
'id' => $messages_template->thread->thread_id,
@@ -118,14 +136,25 @@ public function get_items( $request ) {
118136
'excerpt' => bp_get_message_thread_excerpt(),
119137
'content' => bp_get_message_thread_content()
120138
);
121-
$links = array(
122-
'self' => array(
123-
'href' => rest_url( sprintf( BP_API_SLUG.'/messages/%d', $messages_template->thread->thread_id ) ),
124-
),
125-
'collection' => array(
126-
'href' => rest_url( BP_API_SLUG.'/messages/' ),
127-
)
128-
);
139+
if($args['box'] == 'sentbox') {
140+
foreach($messages_template->thread->recipients as $user=> $userdata){
141+
if ( (int) $user !== bp_loggedin_user_id() ) {
142+
$single_msg['to'][$user]['name'] = bp_core_get_username( $user );
143+
}
144+
}
145+
}
146+
$links['self'] = array(
147+
'href' => rest_url( sprintf( BP_API_SLUG.'/messages/%d', $messages_template->thread->thread_id ) ),
148+
);
149+
if($args['box'] == 'sentbox') {
150+
$links['collection'] = array(
151+
'href' => rest_url( BP_API_SLUG.'/messages?box=sentbox' ),
152+
);
153+
} else {
154+
$links['collection'] = array(
155+
'href' => rest_url( BP_API_SLUG.'/messages/' ),
156+
);
157+
}
129158
$single_msg['_links']=$links;
130159
$data[]=$single_msg;
131160
endwhile;
@@ -157,16 +186,16 @@ public function get_item( $request ) {
157186
} else {
158187
foreach( (array) $thread_template->thread->recipients as $recipient ) {
159188
if ( (int) $recipient->user_id !== bp_loggedin_user_id() ) {
160-
$recipient_link = bp_core_get_user_displayname( $recipient->user_id );
189+
$recipient_name = bp_core_get_user_displayname( $recipient->user_id );
161190

162-
if ( empty( $recipient_link ) ) {
163-
$recipient_link = __( 'Deleted User', BP_API_PLUGIN_SLUG );
191+
if ( empty( $recipient_name ) ) {
192+
$recipient_name = __( 'Deleted User', BP_API_PLUGIN_SLUG );
164193
}
165194

166-
$recipient_links[] = $recipient_link;
195+
$recipient_names[] = $recipient_name;
167196
}
168197
}
169-
$data['thread_title'] = sprintf( __('Conversation between %s and you.' ), implode(',', $recipient_links) );
198+
$data['thread_title'] = sprintf( __('Conversation between %s and you.' ), implode(',', $recipient_names) );
170199
while ( bp_thread_messages() ) : bp_thread_the_message();
171200

172201
$data['thread_msg'][$thread_template->message->id] = (array)$thread_template->message;

0 commit comments

Comments
 (0)