/
class-bp-rest-messages-endpoint.php
1522 lines (1361 loc) · 47.9 KB
/
class-bp-rest-messages-endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* BP REST: BP_REST_Messages_Endpoint class
*
* @package BuddyPress
* @since 0.1.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Messages endpoints.
*
* /messages/
* /messages/{thread_id}
* /messages/starred/{message_id}
*
* @since 0.1.0
*/
class BP_REST_Messages_Endpoint extends WP_REST_Controller {
/**
* Constructor.
*
* @since 0.1.0
*/
public function __construct() {
$this->namespace = bp_rest_namespace() . '/' . bp_rest_version();
$this->rest_base = buddypress()->messages->id;
}
/**
* Register the component routes.
*
* @since 0.1.0
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
// Attention: (?P<id>[\d]+) is the placeholder for **Thread** ID, not the Message ID one.
$thread_endpoint = '/' . $this->rest_base . '/(?P<id>[\d]+)';
register_rest_route(
$this->namespace,
$thread_endpoint,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::READABLE ),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::DELETABLE ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
// Register the starred route.
if ( bp_is_active( 'messages', 'star' ) ) {
// Attention: (?P<id>[\d]+) is the placeholder for **Message** ID, not the Thread ID one.
$starred_endpoint = '/' . $this->rest_base . '/' . bp_get_messages_starred_slug() . '/(?P<id>[\d]+)';
register_rest_route(
$this->namespace,
$starred_endpoint,
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_starred' ),
'permission_callback' => array( $this, 'update_starred_permissions_check' ),
),
'schema' => array( $this, 'get_item_schema' ),
)
);
}
}
/**
* Retrieve threads.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response
*/
public function get_items( $request ) {
$args = array(
'user_id' => $request->get_param( 'user_id' ),
'box' => $request->get_param( 'box' ),
'type' => $request->get_param( 'type' ),
'page' => $request->get_param( 'page' ),
'per_page' => $request->get_param( 'per_page' ),
'search_terms' => $request->get_param( 'search' ),
'recipients_page' => $request->get_param( 'recipients_page' ),
'recipients_per_page' => $request->get_param( 'recipients_per_page' ),
'messages_page' => $request->get_param( 'messages_page' ),
'messages_per_page' => $request->get_param( 'messages_per_page' ),
);
// Include the meta_query for starred messages.
if ( 'starred' === $args['box'] ) {
$args['meta_query'] = array( // phpcs:ignore
array(
'key' => 'starred_by_user',
'value' => $args['user_id'],
),
);
}
/**
* Filter the query arguments for the request.
*
* @since 0.1.0
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request sent to the API.
*/
$args = apply_filters( 'bp_rest_messages_get_items_query_args', $args, $request );
// Actually, query it.
$messages_box = new BP_Messages_Box_Template( $args );
$retval = array();
foreach ( (array) $messages_box->threads as $thread ) {
$retval[] = $this->prepare_response_for_collection(
$this->prepare_item_for_response( $thread, $request )
);
}
$response = rest_ensure_response( $retval );
$response = bp_rest_response_add_total_headers( $response, $messages_box->total_thread_count, $args['per_page'] );
/**
* Fires after a thread is fetched via the REST API.
*
* @since 0.1.0
*
* @param BP_Messages_Box_Template $messages_box Fetched thread.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'bp_rest_messages_get_items', $messages_box, $response, $request );
return $response;
}
/**
* Check if a given request has access to thread items.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full data about the request.
* @return true|WP_Error
*/
public function get_items_permissions_check( $request ) {
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
if ( is_user_logged_in() ) {
$user = bp_rest_get_user( $request->get_param( 'user_id' ) );
if ( ! $user instanceof WP_User ) {
$retval = new WP_Error(
'bp_rest_invalid_id',
__( 'Invalid member ID.', 'buddypress' ),
array(
'status' => 404,
)
);
} elseif ( (int) bp_loggedin_user_id() === $user->ID || bp_current_user_can( 'bp_moderate' ) ) {
$retval = true;
} else {
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you cannot view the messages.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
}
/**
* Filter the messages `get_items` permissions check.
*
* @since 0.1.0
*
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_messages_get_items_permissions_check', $retval, $request );
}
/**
* Get a single thread.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_REST_Response
*/
public function get_item( $request ) {
$args = array(
'recipients_page' => $request->get_param( 'recipients_page' ),
'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' ),
);
if ( empty( $args['user_id'] ) ) {
$args['user_id'] = bp_loggedin_user_id();
}
/**
* Filter the query arguments for the request.
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request sent to the API.
*/
$args = apply_filters( 'bp_rest_messages_get_item_query_args', $args, $request );
$thread = new BP_Messages_Thread(
$request->get_param( 'id' ),
'ASC', // not used.
$args
);
$retval = array(
$this->prepare_response_for_collection(
$this->prepare_item_for_response( $thread, $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.
*
* @since 0.1.0
*
* @param BP_Messages_Thread $thread Thread object.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'bp_rest_messages_get_item', $thread, $response, $request );
return $response;
}
/**
* Check if a given request has access to a thread item.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full data about the request.
* @return true|WP_Error
*/
public function get_item_permissions_check( $request ) {
$error = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to see this thread.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
$retval = $error;
$user_id = bp_loggedin_user_id();
if ( ! empty( $request->get_param( 'user_id' ) ) ) {
$user_id = $request->get_param( 'user_id' );
}
$id = $request->get_param( 'id' );
if ( is_user_logged_in() ) {
$thread = BP_Messages_Thread::is_valid( $id );
if ( empty( $thread ) ) {
$retval = new WP_Error(
'bp_rest_invalid_id',
__( 'Sorry, this thread does not exist.', 'buddypress' ),
array(
'status' => 404,
)
);
} elseif ( bp_current_user_can( 'bp_moderate' ) || messages_check_thread_access( $id, $user_id ) ) {
$retval = true;
}
}
/**
* Filter the messages `get_item` permissions check.
*
* @since 0.1.0
*
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_messages_get_item_permissions_check', $retval, $request );
}
/**
* Init a Messages Thread or add a reply to an existing Thread.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function create_item( $request ) {
// Setting context.
$request->set_param( 'context', 'edit' );
// Create the message or the reply.
$thread_id = messages_new_message( $this->prepare_item_for_database( $request ) );
// Validate it created a Thread or was added to it.
if ( false === $thread_id ) {
return new WP_Error(
'bp_rest_messages_create_failed',
__( 'There was an error trying to create the message.', 'buddypress' ),
array(
'status' => 500,
)
);
}
// Make sure to get the newest message to update REST Additional fields.
$thread = $this->get_thread_object( $thread_id );
$last_message = wp_list_filter( $thread->messages, array( 'id' => $thread->last_message_id ) );
$last_message = reset( $last_message );
$fields_update = $this->update_additional_fields_for_object( $last_message, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
$retval = array(
$this->prepare_response_for_collection(
$this->prepare_item_for_response( $thread, $request )
),
);
$response = rest_ensure_response( $retval );
/**
* Fires after a message is created via the REST API.
*
* @since 0.1.0
*
* @param BP_Messages_Thread $thread Thread object.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'bp_rest_messages_create_item', $thread, $response, $request );
return $response;
}
/**
* Check if a given request has access to create a message.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error
*/
public function create_item_permissions_check( $request ) {
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
if ( ! is_user_logged_in() ) {
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you need to be logged in to create a message.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
} else {
$thread_id = (int) $request->get_param( 'id' );
// It's an existing thread.
if ( $thread_id ) {
if ( bp_current_user_can( 'bp_moderate' ) || ( messages_is_valid_thread( $thread_id ) && messages_check_thread_access( $thread_id ) ) ) {
$retval = true;
}
} else {
// It's a new thread.
$retval = true;
}
}
/**
* Filter the messages `create_item` permissions check.
*
* @since 0.1.0
*
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_messages_create_item_permissions_check', $retval, $request );
}
/**
* Update metadata for one of the messages of the thread.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function update_item( $request ) {
// Setting context.
$request->set_param( 'context', 'edit' );
// Updated user id.
$updated_user_id = bp_loggedin_user_id();
if ( ! empty( $request->get_param( 'user_id' ) ) ) {
$updated_user_id = $request->get_param( 'user_id' );
}
// Get the thread.
$thread = $this->get_thread_object( $request->get_param( 'id' ), $updated_user_id );
$error = new WP_Error(
'bp_rest_messages_update_failed',
__( 'There was an error trying to update the message.', 'buddypress' ),
array(
'status' => 500,
)
);
// Is someone updating the thread status?
$thread_status_update = ( (bool) $request->get_param( 'read' ) || (bool) $request->get_param( 'unread' ) );
// Mark thread as read.
if ( true === (bool) $request->get_param( 'read' ) ) {
messages_mark_thread_read( $thread->thread_id, $updated_user_id );
}
// Mark thread as unread.
if ( true === (bool) $request->get_param( 'unread' ) ) {
messages_mark_thread_unread( $thread->thread_id, $updated_user_id );
}
// By default use the last message.
$message_id = $thread->last_message_id;
if ( $request->get_param( 'message_id' ) ) {
$message_id = $request->get_param( 'message_id' );
}
$updated_message = wp_list_filter( $thread->messages, array( 'id' => $message_id ) );
$updated_message = reset( $updated_message );
/**
* Filter here to allow more users to edit the message meta (eg: the recipients).
*
* @since 0.1.0
*
* @param boolean $value Whether the user can edit the message meta.
* By default: only the sender and a community moderator can.
* @param BP_Messages_Message $updated_message The updated message object.
* @param WP_REST_Request $request Full details about the request.
*/
$can_edit_item_meta = apply_filters(
'bp_rest_messages_can_edit_item_meta',
bp_loggedin_user_id() === $updated_message->sender_id || bp_current_user_can( 'bp_moderate' ),
$updated_message,
$request
);
// The message must exist in the thread, and the logged in user must be the sender.
if (
false === $thread_status_update
&& (
! isset( $updated_message->id )
|| ! $updated_message->id
|| ! $can_edit_item_meta
)
) {
return $error;
}
$fields_update = $this->update_additional_fields_for_object( $updated_message, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}
// Get the updated thread object.
$thread = $this->get_thread_object( $thread->thread_id, $updated_user_id );
$retval = array(
$this->prepare_response_for_collection(
$this->prepare_item_for_response( $thread, $request )
),
);
$response = rest_ensure_response( $retval );
/**
* Fires after a thread or a message is updated via the REST API.
*
* @since 0.1.0
*
* @param BP_Messages_Message $updated_message The updated message.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'bp_rest_messages_update_item', $updated_message, $response, $request );
return $response;
}
/**
* Check if a given request has access to update a message.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error
*/
public function update_item_permissions_check( $request ) {
$retval = $this->get_item_permissions_check( $request );
/**
* Filter the message `update_item` permissions check.
*
* @since 0.1.0
*
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_messages_update_item_permissions_check', $retval, $request );
}
/**
* Adds or removes the message from the current user's starred box.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function update_starred( $request ) {
$message = $this->get_message_object( $request->get_param( 'id' ) );
$user_id = bp_loggedin_user_id();
$result = false;
$action = 'star';
$info = __( 'Sorry, you cannot add the message to your starred box.', 'buddypress' );
if ( bp_messages_is_message_starred( $message->id, $user_id ) ) {
$action = 'unstar';
$info = __( 'Sorry, you cannot remove the message from your starred box.', 'buddypress' );
}
$result = bp_messages_star_set_action(
array(
'user_id' => $user_id,
'message_id' => $message->id,
'action' => $action,
)
);
if ( ! $result ) {
return new WP_Error(
'bp_rest_user_cannot_update_starred_message',
$info,
array(
'status' => 500,
)
);
}
// Prepare the message for the REST response.
$data = array(
$this->prepare_response_for_collection(
$this->prepare_message_for_response( $message, $request )
),
);
$response = rest_ensure_response( $data );
/**
* Fires after a message is starred/unstarred via the REST API.
*
* @since 0.1.0
*
* @param BP_Messages_Message $message Message object.
* @param string $action Informs about the update performed.
* Possible values are `star` or `unstar`.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'bp_rest_message_update_starred_item', $message, $action, $response, $request );
return $response;
}
/**
* Check if a given request has access to update user starred messages.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error
*/
public function update_starred_permissions_check( $request ) {
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to star/unstar messages.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
if ( ! is_user_logged_in() ) {
$retval = new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you need to be logged in to star/unstar a message.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
} else {
$thread_id = messages_get_message_thread_id( $request->get_param( 'id' ) ); // This is a message id.
if ( empty( $thread_id ) ) {
return new WP_Error(
'bp_rest_invalid_id',
__( 'Sorry, the thread of this message does not exist.', 'buddypress' ),
array( 'status' => 404 )
);
}
if ( messages_check_thread_access( $thread_id ) ) {
$retval = true;
}
}
/**
* Filter the message `update_starred` permissions check.
*
* @since 0.1.0
*
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_messages_update_starred_permissions_check', $retval, $request );
}
/**
* Delete a thread.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error
*/
public function delete_item( $request ) {
// Setting context.
$request->set_param( 'context', 'edit' );
$user_id = bp_loggedin_user_id();
if ( ! empty( $request->get_param( 'user_id' ) ) ) {
$user_id = $request->get_param( 'user_id' );
}
// Get the thread before it's deleted.
$thread = $this->get_thread_object( $request->get_param( 'id' ), $user_id );
$previous = $this->prepare_item_for_response( $thread, $request );
// Check the user is one of the recipients.
if ( ! in_array( $user_id, wp_parse_id_list( wp_list_pluck( $thread->get_recipients(), 'user_id' ) ), true ) ) {
return new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
// Delete a thread.
if ( false === messages_delete_thread( $thread->thread_id, $user_id ) ) {
return new WP_Error(
'bp_rest_messages_delete_thread_failed',
__( 'There was an error trying to delete the thread.', 'buddypress' ),
array(
'status' => 500,
)
);
}
// Build the response.
$response = new WP_REST_Response();
$response->set_data(
array(
'deleted' => true,
'previous' => $previous->get_data(),
)
);
/**
* Fires after a thread is deleted via the REST API.
*
* @since 0.1.0
*
* @param BP_Messages_Thread $thread The thread object.
* @param WP_REST_Response $response The response data.
* @param WP_REST_Request $request The request sent to the API.
*/
do_action( 'bp_rest_messages_delete_item', $thread, $response, $request );
return $response;
}
/**
* Check if a given request has access to delete a thread.
*
* @since 0.1.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error
*/
public function delete_item_permissions_check( $request ) {
$retval = $this->get_item_permissions_check( $request );
/**
* Filter the thread `delete_item` permissions check.
*
* @since 0.1.0
*
* @param true|WP_Error $retval Returned value.
* @param WP_REST_Request $request The request sent to the API.
*/
return apply_filters( 'bp_rest_messages_delete_item_permissions_check', $retval, $request );
}
/**
* Prepare a message for create.
*
* @since 6.0.0
*
* @param WP_REST_Request $request The request sent to the API.
* @return stdClass
*/
protected function prepare_item_for_database( $request ) {
$prepared_thread = new stdClass();
$schema = $this->get_item_schema();
$thread = $this->get_thread_object( $request->get_param( 'id' ) );
if ( ! empty( $schema['properties']['id'] ) && ! empty( $request->get_param( 'id' ) ) ) {
$prepared_thread->thread_id = $request->get_param( 'id' );
} elseif ( ! empty( $thread->thread_id ) ) {
$prepared_thread->thread_id = $thread->thread_id;
}
if ( ! empty( $schema['properties']['sender_id'] ) && ! empty( $request->get_param( 'sender_id' ) ) ) {
$prepared_thread->sender_id = $thread->sender_id;
} elseif ( ! empty( $thread->sender_id ) ) {
$prepared_thread->sender_id = $thread->sender_id;
} else {
$prepared_thread->sender_id = bp_loggedin_user_id();
}
if ( ! empty( $schema['properties']['message'] ) && ! empty( $request->get_param( 'message' ) ) ) {
$prepared_thread->content = $request->get_param( 'message' );
} elseif ( ! empty( $thread->message ) ) {
$prepared_thread->message = $thread->message;
}
if ( ! empty( $schema['properties']['subject'] ) && ! empty( $request->get_param( 'subject' ) ) ) {
$prepared_thread->subject = $request->get_param( 'subject' );
} elseif ( ! empty( $thread->subject ) ) {
$prepared_thread->subject = $thread->subject;
}
if ( ! empty( $schema['properties']['recipients'] ) && ! empty( $request->get_param( 'recipients' ) ) ) {
$prepared_thread->recipients = $request->get_param( 'recipients' );
} elseif ( ! empty( $thread->recipients ) ) {
$prepared_thread->recipients = wp_parse_id_list( wp_list_pluck( $thread->recipients, 'user_id' ) );
}
/**
* Filters a message before it is inserted via the REST API.
*
* @since 6.0.0
*
* @param stdClass $prepared_thread An object prepared for inserting into the database.
* @param WP_REST_Request $request Request object.
*/
return apply_filters( 'bp_rest_message_pre_insert_value', $prepared_thread, $request );
}
/**
* Prepares message data for the REST response.
*
* @since 0.1.0
*
* @param BP_Messages_Message $message The Message object.
* @param WP_REST_Request $request Full details about the request.
* @return array The Message data for the REST response.
*/
public function prepare_message_for_response( $message, $request ) {
$data = array(
'id' => (int) $message->id,
'thread_id' => (int) $message->thread_id,
'sender_id' => (int) $message->sender_id,
'subject' => array(
'raw' => $message->subject,
'rendered' => apply_filters( 'bp_get_message_thread_subject', $message->subject ),
),
'message' => array(
'raw' => $message->message,
'rendered' => apply_filters( 'bp_get_the_thread_message_content', $message->message ),
),
'date_sent' => bp_rest_prepare_date_response( $message->date_sent ),
);
if ( bp_is_active( 'messages', 'star' ) ) {
$user_id = bp_loggedin_user_id();
if ( ! empty( $request->get_param( 'user_id' ) ) ) {
$user_id = (int) $request->get_param( 'user_id' );
}
$data['is_starred'] = bp_messages_is_message_starred( $data['id'], $user_id );
}
// Add REST Fields (BP Messages meta) data.
$data = $this->add_additional_fields_to_object( $data, $request );
/**
* Filter a message value returned from the API.
*
* @since 0.1.0
*
* @param array $data The message value for the REST response.
* @param BP_Messages_Message $message The Message object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'bp_rest_message_prepare_value', $data, $message, $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 ) {
$display_name = '';
$user_info = get_userdata( (int) $recipient->user_id );
if ( $user_info instanceof WP_User && ! empty( $user_info->display_name ) ) {
$display_name = (string) $user_info->display_name;
}
$data = array(
'id' => (int) $recipient->id,
'is_deleted' => (int) $recipient->is_deleted,
'name' => $display_name,
'sender_only' => (int) $recipient->sender_only,
'thread_id' => (int) $recipient->thread_id,
'unread_count' => (int) $recipient->unread_count,
'user_id' => (int) $recipient->user_id,
'user_link' => esc_url( bp_members_get_user_url( $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,
)
);
}
}
/**
* Filter a recipient value returned from the API.
*
* @since 0.1.0
*
* @param array $data The recipient value for the REST response.
* @param object $recipient The recipient object.
* @param WP_REST_Request $request Request used to generate the response.
*/
return apply_filters( 'bp_rest_messages_prepare_recipient_value', $data, $recipient, $request );
}
/**
* Prepares thread data for return as an object.
*
* @since 0.1.0
*
* @param BP_Messages_Thread $thread The thread object.
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response
*/
public function prepare_item_for_response( $thread, $request ) {
$excerpt = '';
if ( isset( $thread->last_message_content ) ) {
$excerpt = wp_strip_all_tags( bp_create_excerpt( $thread->last_message_content, 75 ) );
}
$data = array(
'id' => (int) $thread->thread_id,
'message_id' => (int) $thread->last_message_id,
'last_sender_id' => (int) $thread->last_sender_id,
'subject' => array(
'raw' => $thread->last_message_subject,
'rendered' => apply_filters( 'bp_get_message_thread_subject', $thread->last_message_subject ),
),
'excerpt' => array(
'raw' => $excerpt,
'rendered' => apply_filters( 'bp_get_message_thread_excerpt', $excerpt ),
),
'message' => array(
'raw' => $thread->last_message_content,
'rendered' => apply_filters( 'bp_get_message_thread_content', $thread->last_message_content ),
),
'date' => bp_rest_prepare_date_response( $thread->last_message_date, get_date_from_gmt( $thread->last_message_date ) ),
'date_gmt' => bp_rest_prepare_date_response( $thread->last_message_date ),
'unread_count' => ! empty( $thread->unread_count ) ? absint( $thread->unread_count ) : 0,
'sender_ids' => (array) $thread->sender_ids,
'recipients' => array(),
'messages' => array(),
);
// Loop through messages to prepare them for the response.
foreach ( $thread->messages as $message ) {
$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'][] = $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' ) ) );
$context = ! empty( $request->get_param( 'context' ) ) ? $request->get_param( 'context' ) : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$response = rest_ensure_response( $data );
// Add prepare links.
$response->add_links( $this->prepare_links( $thread ) );
/**
* Filter a thread value returned from the API.