BP_REST_Group_Membership_Endpoint::create_item( WP_REST_Request $request )
Add member to a group.
Description
Parameters
- $request
-
(Required) Full data about the request.
Return
(WP_REST_Response) | WP_Error
Source
File: bp-groups/classes/class-bp-rest-group-membership-endpoint.php
public function create_item( $request ) { $user = bp_rest_get_user( $request['user_id'] ); $group = $this->groups_endpoint->get_group_object( $request['group_id'] ); if ( ! $request['context'] || 'view' === $request['context'] ) { if ( ! groups_join_group( $group->id, $user->ID ) ) { return new WP_Error( 'bp_rest_group_member_failed_to_join', __( 'Could not join the group.', 'buddyboss' ), array( 'status' => 500, ) ); } // Get the group member. $group_member = new BP_Groups_Member( $user->ID, $group->id ); } else { $role = $request['role']; $group_id = $group->id; $group_member = new BP_Groups_Member( $user->ID, $group_id ); // Add member to the group. $group_member->group_id = $group_id; $group_member->user_id = $user->ID; $group_member->is_admin = 0; $group_member->date_modified = bp_core_current_time(); $group_member->is_confirmed = 1; $saved = $group_member->save(); if ( ! $saved ) { return new WP_Error( 'bp_rest_group_member_failed_to_join', __( 'Could not add member to the group.', 'buddyboss' ), array( 'status' => 500, ) ); } // If new role set, promote it too. if ( $saved && 'member' !== $role ) { // Make sure to update the group role. if ( groups_promote_member( $user->ID, $group_id, $role ) ) { $group_member = new BP_Groups_Member( $user->ID, $group_id ); } } } // Setting context. $request->set_param( 'context', 'edit' ); $retval = $this->prepare_response_for_collection( $this->prepare_item_for_response( $group_member, $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a member is added to a group via the REST API. * * @param WP_User $user The user. * @param BP_Groups_Member $group_member The group member object. * @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_members_create_item', $user, $group_member, $group, $response, $request ); return $response; }
Changelog
Version | Description |
---|---|
0.1.0 | Introduced. |
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.