BP_REST_XProfile_Repeater_Endpoint:: update_item (WP_REST_Request美元的请求

重新订购一个新的中继器组。

描述

参数

美元的请求

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

返回

(WP_REST_Response | WP_Error)

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

public function update_item($request){//设置上下文。$request->set_param('context', 'edit');//在删除字段组之前获取字段组$field_group = xprofile_get_field_group((int) $request['id']);if (empty($field_group->id)) {return new WP_Error('bp_rest_invalid_id', __('无效的字段组id。', 'buddyboss'), array('status' => 404,));} $user_id = get_current_user_id();如果(!$user_id) {return new WP_Error('bp_rest_authorization_required', __('对不起,您不允许更新您的配置文件重复字段。', 'buddyboss'), array('status' => rest_authorization_required_code(),));} $field_group = $this->group_fields_endpoint->get_xprofile_field_group_object($request);字段= field_group美元- >字段; $final_fields = array(); // Get fields and its repeater fields and set with the field. if ( ! empty( $fields ) ) { foreach ( $fields as $field ) { $sub_fields = $this->xprofile_fields_endpoint->get_repeater_fields_data( $field, $request ); foreach ( $sub_fields as $key => $sub_field ) { $field_object = $this->xprofile_fields_endpoint->get_xprofile_field_object( $sub_field['id'] ); $final_fields[ $key ][ $field_object->id ] = array( 'id' => $field_object->id, 'type' => $field_object->type, 'value' => $sub_field['value']['raw'], ); } } } $final_fields = array_filter( $final_fields ); $form_data = $request->get_param( 'fields' ); if ( ! empty( $form_data ) ) { foreach ( $form_data as $k => $v ) { foreach ( $v as $id => $value ) { $field_object = $this->xprofile_fields_endpoint->get_xprofile_field_object( $id ); $form_data[ $k ][ $id ] = array( 'id' => $field_object->id, 'type' => $field_object->type, 'value' => $value, ); } } } ksort( $final_fields ); ksort( $form_data ); if ( ! $this->array_equal( $final_fields, $form_data ) ) { return new WP_Error( 'bp_rest_invalid_fields', __( 'Sorry, Fields are not matched with original field set to reorder.', 'buddyboss' ), array( 'status' => 404, ) ); } if ( ! empty( $final_fields ) && ! empty( $form_data ) ) { foreach ( $final_fields as $key => $value ) { $array_1 = array_column( $value, 'id' ); $data = array_column( $form_data[ $key ], 'value' ); $final_fields[ $key ] = array_combine( $array_1, $data ); } } $return = array(); array_walk( $final_fields, function( $a ) use ( &$return ) { $return = $return + $a; } ); $updated = true; $errors = array(); if ( ! empty( $return ) ) { foreach ( $return as $field_id => $value ) { $field = xprofile_get_field( $field_id ); if ( 'checkbox' === $field->type || 'socialnetworks' === $field->type || 'multiselectbox' === $field->type ) { if ( is_serialized( $value ) ) { $value = maybe_unserialize( $value ); } $value = json_decode( wp_json_encode( $value ), true ); if ( ! is_array( $value ) ) { $value = (array) $value; } } $validation = $this->xprofile_update_endpoint->validate_update( $field_id, $user_id, $value ); if ( ! empty( $validation ) ) { $updated = false; $errors[ $field_id ] = $validation; } } if ( true === $updated ) { foreach ( $return as $field_id => $value ) { $field = xprofile_get_field( $field_id ); if ( 'checkbox' === $field->type || 'socialnetworks' === $field->type || 'multiselectbox' === $field->type ) { if ( is_serialized( $value ) ) { $value = maybe_unserialize( $value ); } $value = json_decode( wp_json_encode( $value ), true ); if ( ! is_array( $value ) ) { $value = (array) $value; } } xprofile_set_field_data( $field_id, $user_id, $value, $field->is_required ); } } } /** * Clear cache when creating a new field set. * - from xprofile_clear_profile_field_object_cache(); */ // Clear default visibility level cache. wp_cache_delete( 'default_visibility_levels', 'bp_xprofile' ); // Modified fields can alter parent group status, in particular when // the group goes from empty to non-empty. Bust its cache, as well as // the global 'all' cache. wp_cache_delete( 'all', 'bp_xprofile_groups' ); wp_cache_delete( $field_group->id, 'bp_xprofile_groups' ); $field_group = $this->group_fields_endpoint->get_xprofile_field_group_object( $request ); $retval = $this->group_fields_endpoint->prepare_response_for_collection( $this->group_fields_endpoint->prepare_item_for_response( $field_group, $request ) ); $response = new WP_REST_Response(); $response->set_data( array( 'updated' => $updated, 'error' => $errors, 'data' => $retval, ) ); /** * Fires after a XProfile repeater fields created via the REST API. * * @since 0.1.0 * * @param BP_XProfile_Group $field_group Deleted field group. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. */ do_action( 'bp_rest_xprofile_repeater_fields_create_item', $field_group, $response, $request ); return $response; }

更新日志

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

问题吗?

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