Skip to content

Commit

Permalink
Use groups_$action_member() function to manage group membership
Browse files Browse the repository at this point in the history
  • Loading branch information
imath authored May 25, 2024
1 parent 989a805 commit 89ded5b
Showing 1 changed file with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,14 @@ public function create_item_permissions_check( $request ) {
* @return WP_REST_Response|WP_Error
*/
public function update_item( $request ) {
$user = bp_rest_get_user( $request->get_param( 'user_id' ) );
$group = $this->groups_endpoint->get_group_object( $request->get_param( 'group_id' ) );
$action = $request->get_param( 'action' );
$role = $request->get_param( 'role' );
$group_id = $group->id;
$group_member = new BP_Groups_Member( $user->ID, $group_id );
$user = bp_rest_get_user( $request->get_param( 'user_id' ) );
$group = $this->groups_endpoint->get_group_object( $request->get_param( 'group_id' ) );
$action = $request->get_param( 'action' );
$role = $request->get_param( 'role' );
$group_id = $group->id;

if ( 'promote' === $action ) {
if ( ! $group_member->promote( $role ) ) {
if ( ! groups_promote_member( $user->ID, $group_id, $role, bp_loggedin_user_id() ) ) {
return new WP_Error(
'bp_rest_group_member_failed_to_promote',
__( 'Could not promote member.', 'buddypress' ),
Expand All @@ -408,7 +407,7 @@ public function update_item( $request ) {
);
}
} elseif ( 'demote' === $action && 'member' !== $role ) {
if ( ! $group_member->promote( $role ) ) {
if ( ! groups_promote_member( $user->ID, $group_id, $role, bp_loggedin_user_id() ) ) {
return new WP_Error(
'bp_rest_group_member_failed_to_demote',
__( 'Could not demote member.', 'buddypress' ),
Expand All @@ -418,7 +417,7 @@ public function update_item( $request ) {
);
}
} elseif ( in_array( $action, array( 'demote', 'ban', 'unban' ), true ) ) {
if ( ! $group_member->$action() ) {
if ( ! call_user_func( 'groups_' . $action . '_member', $user->ID, $group_id, bp_loggedin_user_id() ) ) {
$messages = array(
'demote' => __( 'Could not demote member from the group.', 'buddypress' ),
'ban' => __( 'Could not ban member from the group.', 'buddypress' ),
Expand All @@ -435,6 +434,9 @@ public function update_item( $request ) {
}
}

// Get updated group member.
$group_member = new BP_Groups_Member( $user->ID, $group_id );

// Setting context.
$request->set_param( 'context', 'edit' );

Expand Down

0 comments on commit 89ded5b

Please sign in to comment.