BP_REST_Group_Membership_Endpoint:: create_item (WP_REST_Request美元的请求

向群组中添加成员。

描述

参数

美元的请求

WP_REST_Request(必需)关于请求的完整数据。

返回

(WP_REST_Response)| WP_Error

文件:bp-groups /类/ class-bp-rest-group-membership-endpoint.php

Public function create_item($request) {$user = bp_rest_get_user($request['user_id']);$group = $this-> group_endpoint ->get_group_object($request['group_id']);如果(!美元请求(“上下文”)| |“视图”= = = $请求(“上下文”)){如果!group_join_group ($group->id, $user-> id)) {return new WP_Error('bp_rest_group_member_failed_to_join', __('无法加入组。', 'buddyboss'), array('status' => 500,));} //获取组成员。$group_member = new bp_group_member ($user->ID, $group-> ID);} else {$role = $request['role'];group_id =美元集团- > id;$group_member = new bp_group_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; }

更新日志

更新日志
版本 描述
0.1.0 介绍了。

问题吗?

我们总是很乐意帮助您解决代码或其他问题!搜索我们的开发人员文档联络支持,或与我们联系销售团队