BP_REST_Invites_Endpoint

Email Invites endpoints.

Description

Source

File: bp-invites/classes/class-bp-rest-invites-endpoint.php

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
class BP_REST_Invites_Endpoint extends WP_REST_Controller {
 
    /**
     * Constructor.
     *
     * @since 0.1.0
     */
    public function __construct() {
        $this->namespace = bp_rest_namespace() . '/' . bp_rest_version();
        $this->rest_base = 'invites';
    }
 
    /**
     * 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' ),
            )
        );
 
        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . '/(?P<id>[\d]+)',
            array(
                'args'   => array(
                    'id' => array(
                        'description' => __( 'A unique numeric ID for the member invitation.', 'buddyboss' ),
                        'type'        => 'integer',
                        'required'    => true,
                    ),
                ),
                array(
                    'methods'             => WP_REST_Server::DELETABLE,
                    'callback'            => array( $this, 'delete_item' ),
                    'permission_callback' => array( $this, 'delete_item_permissions_check' ),
                ),
                'schema' => array( $this, 'get_item_schema' ),
            )
        );
 
        register_rest_route(
            $this->namespace,
            '/' . $this->rest_base . '/profile-type',
            array(
                array(
                    'methods'             => WP_REST_Server::READABLE,
                    'callback'            => array( $this, 'get_invite_profile_type' ),
                    'permission_callback' => array( $this, 'get_invite_profile_type_permissions_check' ),
                ),
                'schema' => array( $this, 'get_item_schema' ),
            )
        );
    }
 
    /**
     * Sent Invites.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return WP_REST_Response | WP_Error List of bp-invite post's object data.
     * @since 0.1.0
     *
     * @api            {GET} /wp-json/buddyboss/v1/invites Sent Invites
     * @apiName        GetBBInvites
     * @apiGroup       Email Invites
     * @apiDescription Retrieve Sent Invites.
     * @apiVersion     1.0.0
     * @apiPermission  LoggedInUser
     * @apiParam {Number} [page=1] Current page of the collection.
     * @apiParam {Number} [per_page=10] Maximum number of items to be returned in result set.
     * @apiParam {String=asc,desc} [order=desc] Designates ascending or descending order of invites.
     * @apiParam {String=date,ID,rand} [orderby=date] Sort retrieved invites by parameter.
     */
    public function get_items( $request ) {
 
        $args = array(
            'post_type'      => bp_get_invite_post_type(),
            'author'         => (int) get_current_user_id(),
            'paged'          => ( ! empty( $request['page'] ) ? $request['page'] : '' ),
            'posts_per_page' => ( ! empty( $request['per_page'] ) ? $request['per_page'] : '' ),
            'orderby'        => ( ! empty( $request['orderby'] ) ? $request['orderby'] : 'date' ),
            'order'          => ( ! empty( $request['order'] ) ? $request['order'] : 'desc' ),
        );
 
        $invites_query = new WP_Query( $args );
 
        $sent_invites = ( ! empty( $invites_query->posts ) ? $invites_query->posts : array() );
 
        $retval = array();
        foreach ( $sent_invites as $invite ) {
            $retval[] = $this->prepare_response_for_collection(
                $this->prepare_item_for_response( $invite, $request )
            );
        }
 
        wp_reset_postdata();
 
        $response = rest_ensure_response( $retval );
        $response = bp_rest_response_add_total_headers( $response, $invites_query->found_posts, $args['posts_per_page'] );
 
        /**
         * Fires after a list of sent invites is fetched via the REST API.
         *
         * @param array            $sent_invites Fetched Invites.
         * @param WP_REST_Response $response     The response data.
         * @param WP_REST_Request  $request      The request sent to the API.
         *
         * @since 0.1.0
         */
        do_action( 'bp_rest_invites_get_items', $sent_invites, $response, $request );
 
        return $response;
    }
 
    /**
     * Check if a given request has access to invites items.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return bool|WP_Error
     * @since 0.1.0
     */
    public function get_items_permissions_check( $request ) {
        $retval = true;
 
        if ( ! is_user_logged_in() ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you need to be logged in to view invites.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        if ( true === $retval && ! ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you don\'t have permission to view invites.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        /**
         * Filter the invites `get_items` permissions check.
         *
         * @param bool|WP_Error   $retval  Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_invites_get_items_permissions_check', $retval, $request );
    }
 
    /**
     * Create an Invites/Send Invites.
     *
     * @param WP_REST_Request $request Full data about the request.
     *
     * @return WP_REST_Response | WP_Error
     * @since 0.1.0
     *
     * @api            {POST} /wp-json/buddyboss/v1/invites Send Invites
     * @apiName        CreateBBInvites
     * @apiGroup       Email Invites
     * @apiDescription Create an Invites/Send Invites.
     * @apiVersion     1.0.0
     * @apiPermission  LoggedInUser
     * @apiParam {Array} fields Fields array with name, email_id and profile_type to create an invites.
     * @apiParam {string} [email_subject] Subject for invite a member.
     * @apiParam {String} [email_content] Content for invite a member.
     */
    public function create_item( $request ) {
 
        $fields = $request->get_param( 'fields' );
 
        if ( empty( $fields ) ) {
            return new WP_Error(
                'bp_rest_required_fields',
                __( 'Sorry, you need to set the fields parameter.', 'buddyboss' ),
                array(
                    'status' => 400,
                )
            );
        }
 
        $invite_exists_array = array();
        $failed_invite       = array();
 
        $bp = buddypress();
 
        if ( ! empty( $fields ) ) {
            foreach ( $fields as $k => $field ) {
                if (
                    isset( $field['name'] )
                    && isset( $field['email_id'] )
                    && '' !== $field['name']
                    && '' !== $field['name']
                    && '' !== $field['email_id']
                    && is_email( $field['email_id'] )
                ) {
                    if ( email_exists( (string) $field['email_id'] ) ) {
                        $invite_exists_array[] = $field['email_id'];
                    } else {
                        $invite_correct_array[] = array(
                            'name'        => $field['name'],
                            'email'       => $field['email_id'],
                            'member_type' => ( isset( $field['profile_type'] ) && ! empty( $field['profile_type'] ) ) ? $field['profile_type'] : '',
                        );
                    }
                } else {
                    $invite_wrong_array[] = array(
                        'name'        => ( isset( $field['name'] ) ? $field['name'] : '' ),
                        'email'       => ( isset( $field['email_id'] ) ? $field['email_id'] : '' ),
                        'member_type' => ( isset( $field['profile_type'] ) ? $field['profile_type'] : '' ),
                    );
                }
            }
        }
 
        $invitations_ids = array();
 
        $query_string = array();
        if ( ! empty( $invite_correct_array ) ) {
 
            require trailingslashit( buddypress()->plugin_dir . 'bp-invites/actions' ) . '/invites.php';
 
            foreach ( $invite_correct_array as $key => $value ) {
 
                $_POST = array();
 
                $email          = $value['email'];
                $name           = $value['name'];
                $member_type    = $value['member_type'];
                $query_string[] = $email;
                $inviter_name   = bp_core_get_user_displayname( bp_loggedin_user_id() );
 
                if ( true === bp_disable_invite_member_email_subject() ) {
                    $subject = $request->get_param( 'email_subject' );
                    if ( empty( $subject ) ) {
                        $subject = stripslashes( wp_strip_all_tags( bp_get_member_invitation_subject() ) );
                    } else {
                        $_POST['bp_member_invites_custom_subject'] = $subject;
                    }
                } else {
                    $subject = stripslashes( wp_strip_all_tags( bp_get_member_invitation_subject() ) );
                }
 
                if ( true === bp_disable_invite_member_email_content() ) {
                    $message = $request->get_param( 'email_content' );
                    if ( empty( $message ) ) {
                        $message = stripslashes( wp_strip_all_tags( bp_get_member_invitation_message() ) );
                    } else {
                        $_POST['bp_member_invites_custom_content'] = $message;
                    }
                } else {
                    $message = stripslashes( wp_strip_all_tags( bp_get_member_invitation_message() ) );
                }
 
                $message .= '
 
' . bp_get_member_invites_wildcard_replace( stripslashes( wp_strip_all_tags( bp_get_invites_member_invite_url() ) ), $email );
 
                $inviter_name = bp_core_get_user_displayname( bp_loggedin_user_id() );
                $site_name    = get_bloginfo( 'name' );
                $inviter_url  = bp_loggedin_user_domain();
 
                $email_encode = rawurlencode( $email );
 
                // set post variable.
                $_POST['custom_user_email'] = $email;
 
                // Set both variable which will use in email.
                $_POST['custom_user_name']   = $name;
                $_POST['custom_user_avatar'] = apply_filters( 'bp_sent_invite_email_avatar', buddypress()->plugin_url . 'bp-core/images/mystery-man.jpg' );
 
                $accept_link = add_query_arg(
                    array(
                        'bp-invites' => 'accept-member-invitation',
                        'email'      => $email_encode,
                        // phpcs:ignore
                        'inviter'    => base64_encode( bp_loggedin_user_id() ),
                    ),
                    bp_get_root_domain() . '/' . bp_get_signup_slug() . '/'
                );
                $accept_link = apply_filters( 'bp_member_invitation_accept_url', $accept_link );
                $args        = array(
                    'tokens' => array(
                        'inviter.name' => $inviter_name,
                        'inviter.url'  => $inviter_url,
                        'invitee.url'  => $accept_link,
                    ),
                );
 
                /**
                 * Remove Recipients avatar and name.
                 *
                 */
                add_filter( 'bp_email_get_salutation', '__return_false' );
 
                $insert_post_args = array(
                    'post_author'  => $bp->loggedin_user->id,
                    'post_content' => $message,
                    'post_title'   => $subject,
                    'post_status'  => 'publish',
                    'post_type'    => bp_get_invite_post_type(),
                );
 
                $post_id = wp_insert_post( $insert_post_args );
 
                if ( ! empty( $post_id ) || ! is_wp_error( $post_id ) ) {
                    $invitations_ids[] = $post_id;
 
                    // Send invitation email.
                    bp_send_email( 'invites-member-invite', $email, $args );
 
                    // Save a blank bp_ia_accepted post_meta.
                    update_post_meta( $post_id, 'bp_member_invites_accepted', '' );
                    update_post_meta( $post_id, '_bp_invitee_email', $email );
                    update_post_meta( $post_id, '_bp_invitee_name', $name );
                    update_post_meta( $post_id, '_bp_inviter_name', $inviter_name );
                    update_post_meta( $post_id, '_bp_invitee_status', 0 );
                    update_post_meta( $post_id, '_bp_invitee_member_type', $member_type );
                } else {
                    $failed_invite[] = $value;
                }
            }
        }
 
        if ( empty( $invitations_ids ) ) {
            return new WP_Error(
                'bp_rest_no_invitation_send',
                __( 'Sorry, No any invitation has been send.', 'buddyboss' ),
                array(
                    'status' => 400,
                )
            );
        }
 
        $send_invitations = get_posts(
            array(
                'post_type' => bp_get_invite_post_type(),
                'include'   => $invitations_ids,
            )
        );
 
        $retval = array(
            'data'   => array(),
            'exists' => $invite_exists_array,
            'failed' => wp_list_pluck( array_filter( $failed_invite ), 'email' ),
        );
 
        foreach ( $send_invitations as $invite ) {
            $retval['data'][] = $this->prepare_response_for_collection(
                $this->prepare_item_for_response( $invite, $request )
            );
        }
 
        $response = rest_ensure_response( $retval );
 
        /**
         * Fires after a list of invites has been send via the REST API.
         *
         * @param WP_REST_Response $response The response data.
         * @param WP_REST_Request  $request  The request sent to the API.
         *
         * @since 0.1.0
         */
        do_action( 'bp_rest_invites_get_items', $response, $request );
 
        return $response;
    }
 
    /**
     * Checks if a given request has access to create an invites.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return bool|WP_Error
     * @since 0.1.0
     */
    public function create_item_permissions_check( $request ) {
        $retval = true;
 
        if ( ! is_user_logged_in() ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you need to be logged in to create invites.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        if ( true === $retval && ! ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you don\'t have permission to create invites.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        /**
         * Filter the invites `create_item` permissions check.
         *
         * @param bool|WP_Error   $retval  Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_invites_create_item_permissions_check', $retval, $request );
    }
 
    /**
     * Delete a invites.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return WP_REST_Response | WP_Error
     * @since 0.1.0
     *
     * @api            {DELETE} /wp-json/buddyboss/v1/invites/:id Revoke Invite
     * @apiName        DeleteBBInvites
     * @apiGroup       Email Invites
     * @apiDescription Remoke Invites.
     * @apiVersion     1.0.0
     * @apiPermission  LoggedInUser
     * @apiParam {Number} id A unique numeric ID for the member invitation.
     */
    public function delete_item( $request ) {
        // Setting context.
        $request->set_param( 'context', 'edit' );
 
        $invite = get_post( $request['id'] );
 
        $previous = $this->prepare_response_for_collection(
            $this->prepare_item_for_response( $invite, $request )
        );
 
        $success = wp_delete_post( $invite->ID );
 
        // Build the response.
        $response = new WP_REST_Response();
        $response->set_data(
            array(
                'deleted'  => ( ! empty( $success ) && ! is_wp_error( $success ) ? true : $success ),
                'previous' => $previous,
            )
        );
 
        /**
         * Fires after a invitation is deleted via the REST API.
         *
         * @param object           $invite   The deleted invitation.
         * @param WP_REST_Response $response The response data.
         * @param WP_REST_Request  $request  The request sent to the API.
         *
         * @since 0.1.0
         */
        do_action( 'bp_rest_invites_delete_item', $invite, $response, $request );
 
        return $response;
    }
 
    /**
     * Check if a given request has access to delete an invite.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return bool|WP_Error
     * @since 0.1.0
     */
    public function delete_item_permissions_check( $request ) {
        $retval = true;
 
        if ( ! is_user_logged_in() ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you need to be logged in to revoke invite.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        if ( true === $retval && ! ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you don\'t have permission to revoke invite.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        $invite = get_post( $request['id'] );
 
        if ( true === $retval && empty( $invite->ID ) ) {
            $retval = new WP_Error(
                'bp_rest_invite_invalid_id',
                __( 'Invalid invite ID.', 'buddyboss' ),
                array(
                    'status' => 404,
                )
            );
        }
 
        if ( true === $retval && ( ! isset( $invite->post_type ) || 'bp-invite' !== $invite->post_type ) ) {
            $retval = new WP_Error(
                'bp_rest_invite_invalid_id',
                __( 'Invalid invite ID.', 'buddyboss' ),
                array(
                    'status' => 404,
                )
            );
        }
 
        /**
         * Filter the invites `delete_item` permissions check.
         *
         * @param bool|WP_Error   $retval  Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_invites_delete_item_permissions_check', $retval, $request );
    }
 
    /**
     * Sent Invites Profile Type.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return WP_REST_Response | WP_Error List of bp-invite profile types
     * @since 0.1.0
     *
     * @api            {GET} /wp-json/buddyboss/v1/invites/profile-type Invites Profile Type
     * @apiName        GetBBInvitesProfileType
     * @apiGroup       Email Invites
     * @apiDescription Retrieve Sent Invites Profile Type.
     * @apiVersion     1.0.0
     * @apiPermission  LoggedInUser
     */
    public function get_invite_profile_type( $request ) {
 
        $member_types = array();
 
        if ( function_exists( 'bp_check_member_send_invites_tab_member_type_allowed' ) && true === bp_check_member_send_invites_tab_member_type_allowed() ) {
            $current_user              = bp_loggedin_user_id();
            $member_type               = bp_get_member_type( $current_user );
            $member_type_post_id       = bp_member_type_post_by_type( $member_type );
            $get_selected_member_types = get_post_meta( $member_type_post_id, '_bp_member_type_allowed_member_type_invite', true );
            if ( isset( $get_selected_member_types ) && ! empty( $get_selected_member_types ) ) {
                $member_types = $get_selected_member_types;
            } else {
                $member_types = bp_get_active_member_types();
            }
        }
 
        $retval = array();
 
        if ( ! empty( $member_types ) ) {
            foreach ( $member_types as $type ) {
                $name     = bp_get_member_type_key( $type );
                $type_obj = bp_get_member_type_object( $name );
                if ( ! empty( $type_obj ) ) {
                    $member_type = $type_obj->labels['singular_name'];
                }
 
                $retval[] = array(
                    'value' => $name,
                    'label' => esc_html( $member_type ),
                );
            }
        }
 
        $response = rest_ensure_response( $retval );
 
        /**
         * Fires after a list of sent invites profile type is fetched via the REST API.
         *
         * @param WP_REST_Response $response     The response data.
         * @param WP_REST_Request  $request      The request sent to the API.
         *
         * @since 0.1.0
         */
        do_action( 'bp_rest_invites_get_invite_profile_type', $response, $request );
 
        return $response;
    }
 
    /**
     * Check if a given request has access to invites profile type items.
     *
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return bool|WP_Error
     * @since 0.1.0
     */
    public function get_invite_profile_type_permissions_check( $request ) {
        $retval = true;
 
        if ( ! is_user_logged_in() ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you need to be logged in to view invites profile type.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        if ( true === $retval && ! ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) ) {
            $retval = new WP_Error(
                'bp_rest_authorization_required',
                __( 'Sorry, you don\'t have permission to view invites profile type.', 'buddyboss' ),
                array(
                    'status' => rest_authorization_required_code(),
                )
            );
        }
 
        /**
         * Filter the invites `get_invite_profile_type` permissions check.
         *
         * @param bool|WP_Error   $retval  Returned value.
         * @param WP_REST_Request $request The request sent to the API.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_invites_get_invite_profile_type_permissions_check', $retval, $request );
    }
 
    /**
     * Prepares Invite data for return as an object.
     *
     * @param WP_Post         $item    bp-invite post object.
     * @param WP_REST_Request $request Full details about the request.
     *
     * @return WP_REST_Response
     * @since 0.1.0
     */
    public function prepare_item_for_response( $item, $request ) {
        $data = array(
            'id'             => $item->ID,
            'name'           => get_post_meta( $item->ID, '_bp_invitee_name', true ),
            'email'          => get_post_meta( $item->ID, '_bp_invitee_email', true ),
            'date'           => $this->prepare_date_response( $item->post_date_gmt, $item->post_date ),
            'date_gmt'       => $this->prepare_date_response( $item->post_date_gmt ),
            'status'         => '',
            'invitee-status' => (bool) get_post_meta( $item->ID, '_bp_invitee_status', true ),
            'revoke-invite'  => false,
        );
 
        if (
            function_exists( 'bp_allow_custom_registration' )
            && function_exists( 'bp_custom_register_page_url' )
            && bp_allow_custom_registration()
            && '' !== bp_custom_register_page_url()
        ) {
            $data['status'] = ( '1' === get_post_meta( $item->ID, '_bp_invitee_status', true ) ) ? __( 'Registered', 'buddyboss' ) : __( 'Invited', 'buddyboss' );
        } else {
            $data['status']        = ( '1' === get_post_meta( $item->ID, '_bp_invitee_status', true ) ) ? __( 'Registered', 'buddyboss' ) : __( 'Revoke Invite', 'buddyboss' );
            $data['revoke-invite'] = ( '1' === get_post_meta( $item->ID, '_bp_invitee_status', true ) ) ? false : true;
        }
 
        $context  = ! empty( $request['context'] ) ? $request['context'] : 'view';
        $data     = $this->add_additional_fields_to_object( $data, $request );
        $data     = $this->filter_response_by_context( $data, $context );
        $response = rest_ensure_response( $data );
 
        /**
         * Filter a invite value returned from the API.
         *
         * @param WP_REST_Response $response The response data.
         * @param WP_REST_Request  $request  Request used to generate the response.
         * @param WP_Post          $item     bp-invite post object.
         *
         * @since 0.1.0
         */
        return apply_filters( 'bp_rest_invites_prepare_value', $response, $request, $item );
    }
 
    /**
     * Edit some arguments for the endpoint's CREATABLE and EDITABLE methods.
     *
     * @param string $method Optional. HTTP method of the request.
     *
     * @return array Endpoint arguments.
     * @since 0.1.0
     */
    public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
        $args = WP_REST_Controller::get_endpoint_args_for_item_schema( $method );
        $key  = 'create_item';
 
        if ( WP_REST_Server::CREATABLE === $method ) {
            $args = array(
                'fields' => array(
                    'description' => __( 'Fields array with name and email_id to create an invites.', 'buddyboss' ),
                    'type'        => 'array',
                    'items'       => array( 'type' => 'object' ),
                    'required'    => true,
                    'properties'  => array(
                        'name'     => array(
                            'description'       => __( 'Recipient Name for the invite.', 'buddyboss' ),
                            'type'              => 'string',
                            'required'          => true,
                            'sanitize_callback' => 'sanitize_text_field',
                            'validate_callback' => 'rest_validate_request_arg',
                        ),
                        'email_id' => array(
                            'description'       => __( 'Recipient Email for the invite.', 'buddyboss' ),
                            'type'              => 'string',
                            'required'          => true,
                            'sanitize_callback' => 'sanitize_text_field',
                            'validate_callback' => 'rest_validate_request_arg',
                        ),
                    ),
                ),
            );
 
            if ( true === bp_check_member_send_invites_tab_member_type_allowed() ) {
                $args['fields']['description']                = __( 'Fields array with name, email_id and profile_type to create an invites.', 'buddyboss' );
                $args['fields']['properties']['profile_type'] = array(
                    'description'       => __( 'Profile Type for the invite.', 'buddyboss' ),
                    'type'              => 'string',
                    'required'          => true,
                    'sanitize_callback' => 'sanitize_text_field',
                    'validate_callback' => 'rest_validate_request_arg',
                );
            }
 
            if ( true === bp_disable_invite_member_email_subject() ) {
                $args['email_subject'] = array(
                    'description'       => __( 'Subject for invite a member.', 'buddyboss' ),
                    'type'              => 'string',
                    'default'           => stripslashes( wp_strip_all_tags( bp_get_member_invitation_subject() ) ),
                    'validate_callback' => 'rest_validate_request_arg',
                );
            }
 
            if ( true === bp_disable_invite_member_email_content() ) {
                $args['email_content'] = array(
                    'description'       => __( 'Content for invite a member.', 'buddyboss' ),
                    'type'              => 'string',
                    'default'           => bp_get_member_invites_wildcard_replace( bp_get_member_invitation_message() ),
                    'validate_callback' => 'rest_validate_request_arg',
                );
            }
        }
 
        /**
         * Filters the method query arguments.
         *
         * @param array  $args   Query arguments.
         * @param string $method HTTP method of the request.
         *
         * @since 0.1.0
         */
        return apply_filters( "bp_rest_invites_{$key}_query_arguments", $args, $method );
    }
 
    /**
     * Get the invite schema, conforming to JSON Schema.
     *
     * @return array
     * @since 0.1.0
     */
    public function get_item_schema() {
        $schema = array(
            '$schema'    => 'http://json-schema.org/draft-04/schema#',
            'title'      => 'bp_invites',
            'type'       => 'object',
            'properties' => array(
                'id'             => array(
                    'description' => __( 'Unique identifier for the invite.', 'buddyboss' ),
                    'type'        => 'integer',
                    'context'     => array( 'embed', 'view', 'edit' ),
                    'readonly'    => true,
                ),
                'name'           => array(
                    'description' => __( 'Member\'s name.', 'buddyboss' ),
                    'type'        => 'string',
                    'context'     => array( 'embed', 'view', 'edit' ),
                    'readonly'    => true,
                ),
                'email'          => array(
                    'description' => __( 'Member\'s email address', 'buddyboss' ),
                    'type'        => 'string',
                    'context'     => array( 'embed', 'view', 'edit' ),
                    'readonly'    => true,
                ),
                'date'           => array(
                    'description' => __( 'The date the object was published, in the site\'s timezone.', 'buddyboss' ),
                    'type'        => 'string',
                    'format'      => 'date-time',
                    'context'     => array( 'embed', 'view', 'edit' ),
                ),
                'date_gmt'       => array(
                    'description' => __( 'The date the object was published, as GMT.', 'buddyboss' ),
                    'type'        => 'string',
                    'format'      => 'date-time',
                    'context'     => array( 'embed', 'view', 'edit' ),
                ),
                'status'         => array(
                    'description' => __( 'Status to perform on it.', 'buddyboss' ),
                    'type'        => 'string',
                    'context'     => array( 'embed', 'view', 'edit' ),
                ),
                'invitee-status' => array(
                    'description' => __( 'Whether invitee is registered or not.', 'buddyboss' ),
                    'type'        => 'boolean',
                    'context'     => array( 'embed', 'view', 'edit' ),
                ),
                'revoke-invite'  => array(
                    'description' => __( 'Whether revoke invite or not.', 'buddyboss' ),
                    'type'        => 'boolean',
                    'context'     => array( 'embed', 'view', 'edit' ),
                ),
            ),
        );
 
        /**
         * Filters the Invites schema.
         *
         * @param array $schema The endpoint schema.
         */
        return apply_filters( 'bp_rest_invites_schema', $this->add_additional_fields_schema( $schema ) );
    }
 
    /**
     * Get the query params for collections of invites.
     *
     * @return array
     * @since 0.1.0
     */
    public function get_collection_params() {
        $params                       = parent::get_collection_params();
        $params['context']['default'] = 'view';
 
        unset( $params['search'] );
 
        $params['order'] = array(
            'description'       => __( 'Designates ascending or descending order of invites.', 'buddyboss' ),
            'default'           => 'desc',
            'type'              => 'string',
            'enum'              => array( 'asc', 'desc' ),
            'sanitize_callback' => 'sanitize_key',
            'validate_callback' => 'rest_validate_request_arg',
        );
 
        $params['orderby'] = array(
            'description'       => __( 'Sort retrieved invites by parameter.', 'buddyboss' ),
            'type'              => 'array',
            'items'             => array(
                'type' => 'string',
                'enum' => array(
                    'date',
                    'ID',
                    'rand',
                ),
            ),
            'sanitize_callback' => 'bp_rest_sanitize_string_list',
            'validate_callback' => 'rest_validate_request_arg',
        );
 
        /**
         * Filters the collection query params.
         *
         * @param array $params Query params.
         */
        return apply_filters( 'bp_rest_invites_collection_params', $params );
    }
 
    /**
     * Check the post_date_gmt or modified_gmt and prepare any post or
     * modified date for single post output.
     *
     * @param string      $date_gmt GMT date format.
     * @param string|null $date     forum date.
     *
     * @return string|null ISO8601/RFC3339 formatted datetime.
     */
    protected function prepare_date_response( $date_gmt, $date = null ) {
        // Use the date if passed.
        if ( isset( $date ) ) {
            return mysql_to_rfc3339( $date );
        }
 
        // Return null if $date_gmt is empty/zeros.
        if ( '0000-00-00 00:00:00' === $date_gmt ) {
            return null;
        }
 
        // Return the formatted datetime.
        return mysql_to_rfc3339( $date_gmt );
    }
}

Changelog

Changelog
Version Description
0.1.0 Introduced.

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.