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

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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

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.