BP_REST_XProfile_Repeater_Endpoint::update_item( WP_REST_Request $request )
Reorder a new Repeater Group.
Description
Parameters
- $request
-
(Required) Full data about the request.
Return
(WP_REST_Response|WP_Error)
Source
File: bp-xprofile/classes/class-bp-rest-xprofile-repeater-endpoint.php
public function update_item( $request ) {
// Setting context.
$request->set_param( 'context', 'edit' );
// Get the field group before it's deleted.
$field_group = xprofile_get_field_group( (int) $request['id'] );
if ( empty( $field_group->id ) ) {
return new WP_Error(
'bp_rest_invalid_id',
__( 'Invalid field group ID.', 'buddyboss' ),
array(
'status' => 404,
)
);
}
$user_id = get_current_user_id();
if ( ! $user_id ) {
return new WP_Error(
'bp_rest_authorization_required',
__( 'Sorry, you are not allowed to update your profile repeater fields.', 'buddyboss' ),
array(
'status' => rest_authorization_required_code(),
)
);
}
$field_group = $this->group_fields_endpoint->get_xprofile_field_group_object( $request );
$fields = $field_group->fields;
$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;
}
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.