BP_REST_Group_Invites_Endpoint
Group Invites endpoints.
Description
Use /groups/{group_id}/invites Use /groups/{group_id}/invites/{user_id}
Source
File: bp-groups/classes/class-bp-rest-group-invites-endpoint.php
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 | class BP_REST_Group_Invites_Endpoint extends WP_REST_Controller { /** * Reuse some parts of the BP_REST_Groups_Endpoint class. * * @since 0.1.0 * * @var BP_REST_Groups_Endpoint */ protected $groups_endpoint ; /** * Constructor. * * @since 0.1.0 */ public function __construct() { $this -> namespace = bp_rest_namespace() . '/' . bp_rest_version(); $this ->rest_base = buddypress()->groups->id . '/invites' ; $this ->groups_endpoint = new BP_REST_Groups_Endpoint(); $this ->group_members_endpoint = new BP_REST_Group_Membership_Endpoint(); } /** * 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<invite_id>[\d]+)' , array ( 'args' => array ( 'invite_id' => array ( 'description' => __( 'A unique numeric ID for the group invitation.' , 'buddyboss' ), 'type' => 'integer' , ), ), array ( 'methods' => WP_REST_Server::READABLE, 'callback' => array ( $this , 'get_item' ), 'permission_callback' => array ( $this , 'get_item_permissions_check' ), 'args' => array ( 'context' => $this ->get_context_param( array ( 'default' => 'view' , ) ), ), ), array ( 'methods' => WP_REST_Server::EDITABLE, 'callback' => array ( $this , 'update_item' ), 'permission_callback' => array ( $this , 'update_item_permissions_check' ), ), 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' ), ) ); } /** * Retrieve group invitations. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/groups/invites Invites * @apiName GetBBGroupsInvites * @apiGroup Groups * @apiDescription Retrieve invites for group * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} [page] Current page of the collection. * @apiParam {Number} [per_page=10] Maximum number of items to be returned in result set. * @apiParam {String} [group_id] ID of the group to limit results to. * @apiParam {String} [user_id] Return only invitations extended to this user. * @apiParam {Number} [inviter_id] Return only invitations extended by this user. * @apiParam {String=draft,sent,all} [invite_sent=sent] Limit result set to invites that have been sent, not sent, or include all. */ public function get_items( $request ) { $args = array ( 'item_id' => $request [ 'group_id' ], 'user_id' => $request [ 'user_id' ], 'invite_sent' => $request [ 'invite_sent' ], 'per_page' => $request [ 'per_page' ], 'page' => $request [ 'page' ], ); /** * Inviter_id is a special case, because 0 can be meaningful for requests, * but if it is zero for invitations, we can safely ignore it and should. * So, only apply non-zero inviter_ids. */ if ( $request [ 'inviter_id' ] ) { $args [ 'inviter_id' ] = $request [ 'inviter_id' ]; } // If the query is not restricted by group, user or inviter, limit it to the current user, if not an admin. if ( ! $args [ 'item_id' ] && ! $args [ 'user_id' ] && ! $args [ 'inviter_id' ] && ! bp_current_user_can( 'bp_moderate' ) ) { $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. * * @since 0.1.0 */ $args = apply_filters( 'bp_rest_group_invites_get_items_query_args' , $args , $request ); // Get invites. $invites_data = groups_get_invites( $args ); $retval = array (); foreach ( $invites_data as $invitation ) { if ( $invitation instanceof BP_Invitation ) { $retval [] = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $invitation , $request ) ); } } $response = rest_ensure_response( $retval ); $response = bp_rest_response_add_total_headers( $response , count ( $invites_data ), $args [ 'per_page' ] ); /** * Fires after a list of group invites are fetched via the REST API. * * @param array $invites_data Invited users from the group. * @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_group_invites_get_items' , $invites_data , $response , $request ); return $response ; } /** * Check if a given request has access to group invitations. * * @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; $user_id = bp_loggedin_user_id(); $user_id_arg = $request [ 'user_id' ]; $group = $this ->groups_endpoint->get_group_object( $request [ 'group_id' ] ); $inviter = bp_rest_get_user( $request [ 'inviter_id' ] ); // If the query is not restricted by group or user, limit it to the current user, if not an admin. if ( ! $request [ 'group_id' ] && ! $request [ 'user_id' ] && ! bp_current_user_can( 'bp_moderate' ) ) { $user_id_arg = $user_id ; } $user = bp_rest_get_user( $user_id_arg ); if ( ! $user_id ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to see the group invitations.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } // If a group ID has been passed, check that it is valid. if ( true === $retval && $request [ 'group_id' ] && ! $group instanceof BP_Groups_Group ) { $retval = new WP_Error( 'bp_rest_group_invalid_id' , __( 'Invalid group ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // If a user ID has been passed, check that it is valid. if ( true === $retval && $user_id_arg && ! $user instanceof WP_User ) { $retval = new WP_Error( 'bp_rest_member_invalid_id' , __( 'Invalid member ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // If an inviter ID has been passed, check that it is valid. if ( true === $retval && $request [ 'inviter_id' ] && ! $inviter instanceof WP_User ) { $retval = new WP_Error( 'bp_rest_member_invalid_id' , __( 'Invalid member ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } /** * Filter the group invites `get_items` permissions check. * * @param bool|WP_Error $retval Whether the request can continue. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_get_items_permissions_check' , $retval , $request ); } /** * Fetch a specific group invitation by ID. * * @param WP_REST_Request $request Full data about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {GET} /wp-json/buddyboss/v1/groups/:invite_id Invite * @apiName GetBBGroupsInvite * @apiGroup Groups * @apiDescription Retrieve single invitation. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} invite_id A unique numeric ID for the group invitation. */ public function get_item( $request ) { $invite = $this ->fetch_single_invite( $request [ 'invite_id' ] ); $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $invite , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a membership request is fetched via the REST API. * * @param BP_Invitation $invite Invitation object. * @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_group_invite_get_item' , $invite , $response , $request ); return $response ; } /** * Check if a given request has access to fetch group invitation. * * @param WP_REST_Request $request Full details about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function get_item_permissions_check( $request ) { $user_id = bp_loggedin_user_id(); $invite = $this ->fetch_single_invite( $request [ 'invite_id' ] ); $retval = true; if ( ! $user_id ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to see the group invitations.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval && ! $invite ) { $retval = new WP_Error( 'bp_rest_group_invite_invalid_id' , __( 'Invalid group invitation ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } /** * Filter the group membership request `get_item` permissions check. * * @param bool|WP_Error $retval Whether the request can continue. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_get_item_permissions_check' , $retval , $request ); } /** * Invite a member to a group. * * @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/groups/invites Create Group Invite * @apiName CreateBBGroupsInvites * @apiGroup Groups * @apiDescription Create group invitation. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} user_id The ID of the user who is invited to join the Group. * @apiParam {Number} [inviter_id=1] The ID of the user who made the invite. * @apiParam {Number} group_id The ID of the group to which the user has been invited. * @apiParam {String} [message] The optional message to send to the invited user. * @apiParam {Boolean} [send_invite=true] Whether the invite should be sent to the invitee. */ public function create_item( $request ) { $inviter_id_arg = $request [ 'inviter_id' ] ? $request [ 'inviter_id' ] : bp_loggedin_user_id(); $group = $this ->groups_endpoint->get_group_object( $request [ 'group_id' ] ); $user = bp_rest_get_user( $request [ 'user_id' ] ); $inviter = bp_rest_get_user( $inviter_id_arg ); $invite_id = groups_invite_user( array ( 'user_id' => $user ->ID, 'group_id' => $group ->id, 'inviter_id' => $inviter ->ID, 'send_invite' => isset( $request [ 'invite_sent' ] ) ? (bool) $request [ 'invite_sent' ] : 1, 'content' => $request [ 'message' ], ) ); if ( ! $invite_id ) { return new WP_Error( 'bp_rest_group_invite_cannot_create_item' , __( 'Could not invite member to the group.' , 'buddyboss' ), array ( 'status' => 500, ) ); } $invite = new BP_Invitation( $invite_id ); // Set context. $request ->set_param( 'context' , 'edit' ); $retval = $this ->prepare_response_for_collection( $this ->prepare_item_for_response( $invite , $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a member is invited to a group via the REST API. * * @param BP_Invitation $invite The invitation object. * @param WP_User $user The invited user. * @param WP_User $inviter The inviter user. * @param BP_Groups_Group $group The group object. * @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_group_invites_create_item' , $invite , $user , $inviter , $group , $response , $request ); return $response ; } /** * Checks if a given request has access to invite a member to a group. * * @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 ) { $inviter_id_arg = $request [ 'inviter_id' ] ? $request [ 'inviter_id' ] : bp_loggedin_user_id(); $group = $this ->groups_endpoint->get_group_object( $request [ 'group_id' ] ); $user = bp_rest_get_user( $request [ 'user_id' ] ); $inviter = bp_rest_get_user( $inviter_id_arg ); $retval = true; if ( ! is_user_logged_in() ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to create an invitation.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval && empty ( $group ->id ) ) { $retval = new WP_Error( 'bp_rest_group_invalid_id' , __( 'Invalid group ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } if ( true === $retval && ( empty ( $user ->ID ) || empty ( $inviter ->ID ) || $user ->ID === $inviter ->ID ) ) { $retval = new WP_Error( 'bp_rest_member_invalid_id' , __( 'Invalid member ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // Only a site admin or the user herself can extend invites. if ( true === $retval && ! bp_current_user_can( 'bp_moderate' ) && bp_loggedin_user_id() !== $inviter_id_arg ) { $retval = new WP_Error( 'bp_rest_group_invite_cannot_create_item' , __( 'Sorry, you are not allowed to create the invitation as requested.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the group invites `create_item` permissions check. * * @param bool|WP_Error $retval Whether the request can continue. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_create_item_permissions_check' , $retval , $request ); } /** * Accept a group invitation. * * @param WP_REST_Request $request Full details about the request. * * @return WP_REST_Response | WP_Error * @since 0.1.0 * * @api {PATCH} /wp-json/buddyboss/v1/groups/invites/:invite_id Update Group Invite * @apiName UpdateBBGroupsInvite * @apiGroup Groups * @apiDescription Update group invitation. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} invite_id A unique numeric ID for the group invitation. */ public function update_item( $request ) { $request ->set_param( 'context' , 'edit' ); $invite = $this ->fetch_single_invite( $request [ 'invite_id' ] ); $accept = groups_accept_invite( $invite ->user_id, $invite ->item_id ); if ( ! $accept ) { return new WP_Error( 'bp_rest_group_invite_cannot_update_item' , __( 'Could not accept group invitation.' , 'buddyboss' ), array ( 'status' => 500, ) ); } $accepted_member = new BP_Groups_Member( $invite ->user_id, $invite ->item_id ); $retval = $this ->prepare_response_for_collection( $this ->group_members_endpoint->prepare_item_for_response( $accepted_member , $request ) ); $response = rest_ensure_response( $retval ); $group = $this ->groups_endpoint->get_group_object( $invite ->item_id ); /** * Fires after a group invite is accepted via the REST API. * * @param BP_Groups_Member $accepted_member Accepted group member. * @param BP_Groups_Group $group The group object. * @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_group_invites_update_item' , $accepted_member , $group , $response , $request ); return $response ; } /** * Check if a given request has access to accept a group invitation. * * @param WP_REST_Request $request Full details about the request. * * @return bool|WP_Error * @since 0.1.0 */ public function update_item_permissions_check( $request ) { $retval = true; $user_id = bp_loggedin_user_id(); $invite = $this ->fetch_single_invite( $request [ 'invite_id' ] ); if ( ! $user_id ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to see the group invitations.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval && ! $invite ) { $retval = new WP_Error( 'bp_rest_group_invite_invalid_id' , __( 'Invalid group invitation ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // Only the invitee or a site admin should be able to accept an invitation. if ( true === $retval && ! bp_current_user_can( 'bp_moderate' ) && $user_id !== $invite ->user_id ) { $retval = new WP_Error( 'bp_rest_group_invite_cannot_update_item' , __( 'Sorry, you are not allowed to accept the invitation as requested.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the group invites `update_item` permissions check. * * @param bool|WP_Error $retval Whether the request can continue. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_update_item_permissions_check' , $retval , $request ); } /** * Remove (reject/delete) a group invitation. * * @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/groups/invites/:invite_id Delete Group Invite * @apiName DeleteBBGroupsInvite * @apiGroup Groups * @apiDescription Delete group invitation. * @apiVersion 1.0.0 * @apiPermission LoggedInUser * @apiParam {Number} invite_id A unique numeric ID for the group invitation. */ public function delete_item( $request ) { $request ->set_param( 'context' , 'edit' ); $user_id = bp_loggedin_user_id(); $invite = $this ->fetch_single_invite( $request [ 'invite_id' ] ); // Set the invite response before it is deleted. $previous = $this ->prepare_item_for_response( $invite , $request ); /** * If this change is being initiated by the invited user, * use the `reject` function. */ if ( $user_id === $invite ->user_id ) { $deleted = groups_reject_invite( $invite ->user_id, $invite ->item_id, $invite ->inviter_id ); /** * Otherwise, this change is being initiated by a group admin, site admin, * or the inviter, and we should use the `uninvite` function. */ } else { $deleted = groups_uninvite_user( $invite ->user_id, $invite ->item_id, $invite ->inviter_id ); } if ( ! $deleted ) { return new WP_Error( 'bp_rest_group_invite_cannot_delete_item' , __( 'Could not delete group invitation.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } // Build the response. $response = new WP_REST_Response(); $response ->set_data( array ( 'deleted' => true, 'previous' => $previous ->get_data(), ) ); $user = bp_rest_get_user( $invite ->user_id ); $group = $this ->groups_endpoint->get_group_object( $invite ->item_id ); /** * Fires after a group invite is deleted via the REST API. * * @param WP_User $user The invited user. * @param BP_Groups_Group $group The group object. * @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_group_invites_delete_item' , $user , $group , $response , $request ); return $response ; } /** * Check if a given request has access to delete a group invitation. * * @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; $user_id = bp_loggedin_user_id(); $invite = $this ->fetch_single_invite( $request [ 'invite_id' ] ); if ( ! $user_id ) { $retval = new WP_Error( 'bp_rest_authorization_required' , __( 'Sorry, you need to be logged in to see the group invitations.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } if ( true === $retval && ! $invite ) { $retval = new WP_Error( 'bp_rest_group_invite_invalid_id' , __( 'Invalid group invitation ID.' , 'buddyboss' ), array ( 'status' => 404, ) ); } // The inviter, the invitee, group admins, and site admins can all delete invites. if ( true === $retval && ! bp_current_user_can( 'bp_moderate' ) && ! in_array( $user_id , array ( $invite ->user_id, $invite ->inviter_id ), true ) && ! groups_is_user_admin( $user_id , $invite ->item_id ) ) { $retval = new WP_Error( 'bp_rest_group_invite_cannot_delete_item' , __( 'Sorry, you are not allowed to delete the invitation as requested.' , 'buddyboss' ), array ( 'status' => rest_authorization_required_code(), ) ); } /** * Filter the group invites `delete_item` permissions check. * * @param bool|WP_Error $retval Whether the request can continue. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_delete_item_permissions_check' , $retval , $request ); } /** * Prepares group invitation data to return as an object. * * @param BP_Invitation $invite Invite 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( $invite , $request ) { $data = array ( 'id' => $invite ->id, 'user_id' => $invite ->user_id, 'invite_sent' => $invite ->invite_sent, 'inviter_id' => $invite ->inviter_id, 'group_id' => $invite ->item_id, 'date_modified' => bp_rest_prepare_date_response( $invite ->date_modified ), 'type' => $invite ->type, 'message' => array ( 'raw' => $invite ->content, 'rendered' => apply_filters( 'the_content' , $invite ->content ), ), ); $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 ); $response ->add_links( $this ->prepare_links( $invite ) ); /** * Filter a group 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 BP_Invitation $invite The invite object. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_prepare_value' , $response , $request , $invite ); } /** * Prepare links for the request. * * @param BP_Invitation $invite Invite object. * * @return array Links for the given plugin. * @since 0.1.0 */ protected function prepare_links( $invite ) { $base = sprintf( '/%s/%s/' , $this -> namespace , $this ->rest_base ); $url = $base . $invite ->id; // Entity meta. $links = array ( 'self' => array ( 'href' => rest_url( $url ), ), 'collection' => array ( 'href' => rest_url( $base ), ), 'user' => array ( 'href' => rest_url( bp_rest_get_user_url( $invite ->user_id ) ), 'embeddable' => true, ), ); /** * Filter links prepared for the REST response. * * @param array $links The prepared links of the REST response. * @param stdClass $invite Invite object. * * @since 0.1.0 */ return apply_filters( 'bp_rest_group_invites_prepare_links' , $links , $invite ); } /** * Helper function to fetch a single group invite. * * @param int $invite_id The ID of the invitation you wish to fetch. * * @return BP_Invitation|bool $invite Invitation if found, false otherwise. * @since 0.1.0 */ public function fetch_single_invite( $invite_id = 0 ) { $invites = groups_get_invites( array ( 'id' => $invite_id ) ); if ( $invites ) { return current( $invites ); } else { return false; } } /** * Edit the type of the some properties for the CREATABLE & 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 = 'get_item' ; if ( WP_REST_Server::CREATABLE === $method || WP_REST_Server::EDITABLE === $method ) { $key = 'create_item' ; $args [ 'message' ][ 'type' ] = 'string' ; $args [ 'message' ][ 'description' ] = __( 'The optional message to send to the invited user.' , 'buddyboss' ); $args [ 'send_invite' ] = $args [ 'invite_sent' ]; $args [ 'inviter_id' ][ 'default' ] = bp_loggedin_user_id(); $args [ 'group_id' ][ 'required' ] = true; $args [ 'user_id' ][ 'required' ] = true; // Remove arguments not needed for the CREATABLE transport method. unset( $args [ 'message' ][ 'properties' ], $args [ 'invite_sent' ], $args [ 'date_modified' ], $args [ 'type' ] ); $args [ 'send_invite' ][ 'description' ] = __( 'Whether the invite should be sent to the invitee.' , 'buddyboss' ); $args [ 'send_invite' ][ 'default' ] = true; if ( WP_REST_Server::EDITABLE === $method ) { $key = 'update_item' ; } } elseif ( WP_REST_Server::DELETABLE === $method ) { $key = 'delete_item' ; } /** * 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_group_invites_{$key}_query_arguments" , $args , $method ); } /** * Get the group invite schema, conforming to JSON Schema. * * @return array * @since 0.1.0 */ public function get_item_schema() { $schema = array ( 'title' => 'bp_group_invites' , 'type' => 'object' , 'properties' => array ( 'id' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'A unique numeric ID for the BP Invitation object.' , 'buddyboss' ), 'type' => 'integer' , 'readonly' => true, ), 'user_id' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'The ID of the user who is invited to join the Group.' , 'buddyboss' ), 'type' => 'integer' , ), 'invite_sent' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'Whether the invite has been sent to the invitee.' , 'buddyboss' ), 'type' => 'boolean' , ), 'inviter_id' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'The ID of the user who made the invite.' , 'buddyboss' ), 'type' => 'integer' , ), 'group_id' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'The ID of the group to which the user has been invited.' , 'buddyboss' ), 'type' => 'integer' , ), 'date_modified' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( "The date the object was created or last updated, in the site's timezone." , 'buddyboss' ), 'type' => 'string' , 'format' => 'date-time' , ), 'type' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'Invitation or request.' , 'buddyboss' ), 'type' => 'string' , 'enum' => array ( 'invite' , 'request' ), 'default' => 'invite' , ), 'message' => array ( 'context' => array ( 'view' , 'edit' ), 'description' => __( 'The raw and rendered versions for the content of the message.' , 'buddyboss' ), 'type' => 'object' , 'arg_options' => array ( 'sanitize_callback' => null, 'validate_callback' => null, ), 'properties' => array ( 'raw' => array ( 'description' => __( 'Content for the object, as it exists in the database.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'view' , 'edit' ), ), 'rendered' => array ( 'description' => __( 'HTML content for the object, transformed for display.' , 'buddyboss' ), 'type' => 'string' , 'context' => array ( 'view' , 'edit' ), 'readonly' => true, ), ), ), ), ); /** * Filters the group invites schema. * * @param array $schema The endpoint schema. */ return apply_filters( 'bp_rest_group_invites_schema' , $this ->add_additional_fields_schema( $schema ) ); } /** * Get the query params for collections of group invites. * * @return array * @since 0.1.0 */ public function get_collection_params() { $params = parent::get_collection_params(); $params [ 'context' ][ 'default' ] = 'view' ; // Remove the search param. unset( $params [ 'search' ] ); $params [ 'group_id' ] = array ( 'description' => __( 'ID of the group to limit results to.' , 'buddyboss' ), 'required' => false, 'default' => 0, 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'user_id' ] = array ( 'description' => __( 'Return only invitations extended to this user.' , 'buddyboss' ), 'required' => false, 'default' => 0, 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'inviter_id' ] = array ( 'description' => __( 'Return only invitations extended by this user.' , 'buddyboss' ), 'required' => false, 'default' => 0, 'type' => 'integer' , 'sanitize_callback' => 'absint' , 'validate_callback' => 'rest_validate_request_arg' , ); $params [ 'invite_sent' ] = array ( 'description' => __( 'Limit result set to invites that have been sent, not sent, or include all.' , 'buddyboss' ), 'default' => 'sent' , 'type' => 'string' , 'sanitize_callback' => 'sanitize_text_field' , 'validate_callback' => 'rest_validate_request_arg' , 'enum' => array ( 'draft' , 'sent' , 'all' ), ); /** * Filters the collection query params. * * @param array $params Query params. */ return apply_filters( 'bp_rest_group_invites_collection_params' , $params ); } } |
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
Methods
- __construct — Constructor.
- create_item — Invite a member to a group.
- create_item_permissions_check — Checks if a given request has access to invite a member to a group.
- create_multiple_item — Invite multiple member to a group.
- create_multiple_item_permissions_check — Checks if a given request has access to invite a multiple member to a group.
- delete_item — Remove (reject/delete) a group invitation.
- delete_item_permissions_check — Check if a given request has access to delete a group invitation.
- fetch_single_invite — Helper function to fetch a single group invite.
- get_collection_params — Get the query params for collections of group invites.
- get_endpoint_args_for_item_schema — Edit the type of the some properties for the CREATABLE & EDITABLE methods.
- get_item — Fetch a specific group invitation by ID.
- get_item_permissions_check — Check if a given request has access to fetch group invitation.
- get_item_schema — Get the group invite schema, conforming to JSON Schema.
- get_items — Retrieve group invitations.
- get_items_permissions_check — Check if a given request has access to group invitations.
- prepare_item_for_response — Prepares group invitation data to return as an object.
- prepare_links — Prepare links for the request.
- register_routes — Register the component routes.
- update_item — Accept a group invitation.
- update_item_permissions_check — Check if a given request has access to accept a group invitation.
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.