@@ -6,11 +6,21 @@ class BP_API_Messages extends WP_REST_Controller {
6
6
* Register the routes for the objects of the controller.
7
7
*/
8
8
public function register_routes () {
9
+
9
10
register_rest_route ( BP_API_SLUG , '/messages ' , array (
10
11
array (
11
12
'methods ' => WP_REST_Server::READABLE ,
12
13
'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
+ )
14
24
),
15
25
array (
16
26
'methods ' => WP_REST_Server::CREATABLE ,
@@ -101,9 +111,17 @@ public function create_item( $request ) {
101
111
* @return array|WP_Error
102
112
*/
103
113
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
+
104
122
global $ messages_template ;
105
123
$ data = array ();
106
- if ( bp_has_message_threads ( bp_ajax_querystring ( ' messages ' ) ) ) {
124
+ if ( bp_has_message_threads ($ args ) ) {
107
125
while ( bp_message_threads () ) : bp_message_thread ();
108
126
$ single_msg =array (
109
127
'id ' => $ messages_template ->thread ->thread_id ,
@@ -118,14 +136,25 @@ public function get_items( $request ) {
118
136
'excerpt ' => bp_get_message_thread_excerpt (),
119
137
'content ' => bp_get_message_thread_content ()
120
138
);
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
+ }
129
158
$ single_msg ['_links ' ]=$ links ;
130
159
$ data []=$ single_msg ;
131
160
endwhile ;
@@ -157,16 +186,16 @@ public function get_item( $request ) {
157
186
} else {
158
187
foreach ( (array ) $ thread_template ->thread ->recipients as $ recipient ) {
159
188
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 );
161
190
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 );
164
193
}
165
194
166
- $ recipient_links [] = $ recipient_link ;
195
+ $ recipient_names [] = $ recipient_name ;
167
196
}
168
197
}
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 ) );
170
199
while ( bp_thread_messages () ) : bp_thread_the_message ();
171
200
172
201
$ data ['thread_msg ' ][$ thread_template ->message ->id ] = (array )$ thread_template ->message ;
0 commit comments