BP_REST_Friends_Endpoint:: create_item (WP_REST_Request美元的请求

结交新朋友。

描述

参数

美元的请求

WP_REST_Request(必需)请求的全部细节。

返回

(WP_REST_Response)| WP_Error

文件:bp-friends /类/ class-bp-rest-friends-endpoint.php

Public function create_item($request) {$request->set_param('context', 'edit');$request['initiator_id'] = get_user_by('id', $request['initiator_id']);$ user_id = get_user_by('id', $request[' user_id ']); / /用户id//检查用户是否有效。如果(!美元initiator_id | | !$friend_id) {return new WP_Error('bp_rest_friends_create_item_failed', __('确认用户是否有效存在问题。', 'buddyboss'), array('status' => 404,));} //检查用户是否为好友或是否有好友请求。if ('not_friends' !== friends_check_friendship_status($initiator_id->ID, $friend_id->ID)) {return new WP_Error('bp_rest_friends_create_item_failed', __('那些用户已经是朋友或最近发送了友谊请求(s)。', 'buddyboss'), array('status' => 500,));} $is_moderator = bp_current_user_can('bp_moderate'); // Only admins can create friendship requests for other people. if ( ! in_array( bp_loggedin_user_id(), array( $initiator_id->ID, $friend_id->ID ), true ) && ! $is_moderator ) { return new WP_Error( 'bp_rest_friends_create_item_failed', __( 'You are not allowed to perform this action.', 'buddyboss' ), array( 'status' => 403, ) ); } // Only admins can force a friendship request. $force = false; if ( true === $request->get_param( 'force' ) && $is_moderator ) { $force = true; } // Adding friendship. if ( ! friends_add_friend( $initiator_id->ID, $friend_id->ID, $request['force'] ) ) { return new WP_Error( 'bp_rest_friends_create_item_failed', __( 'There was an error trying to create the friendship.', 'buddyboss' ), array( 'status' => 404, ) ); } // Get friendship. $friendship = $this->get_friendship_object( BP_Friends_Friendship::get_friendship_id( $initiator_id->ID, $friend_id->ID ) ); if ( ! $friendship || empty( $friendship->id ) ) { return new WP_Error( 'bp_rest_invalid_id', __( 'Friendship does not exist.', 'buddyboss' ), array( 'status' => 404, ) ); } $retval = $this->prepare_response_for_collection( $this->prepare_item_for_response( $friendship, $request ) ); $response = rest_ensure_response( $retval ); /** * Fires after a friendship is created via the REST API. * * @param BP_Friends_Friendship $friendship The friendship object. * @param WP_REST_Response $retval The response data. * @param WP_REST_Request $request The request sent to the API. * * @since 0.1.0 */ do_action( 'bp_rest_friends_create_item', $friendship, $response, $request ); return $response; }

更新日志

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

问题吗?

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