BP_REST_Attachments_Member_Avatar_Endpoint::delete_item( WP_REST_Request $request )

Delete an existing member avatar.

Description

Parameters

$request

(Required) Full details about the request.

Return

(WP_REST_Response) | WP_Error

Source

File: bp-members/classes/class-bp-rest-attachments-member-avatar-endpoint.php

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
public function delete_item( $request ) {
    $request->set_param( 'context', 'edit' );
    $user_id = (int) $this->user->ID;
 
    if ( ! bp_get_user_has_avatar( $user_id ) ) {
        return new WP_Error(
            'bp_rest_attachments_member_avatar_no_uploaded_avatar',
            __( 'Sorry, there are no uploaded avatars for this user on this site.', 'buddyboss' ),
            array(
                'status' => 404,
            )
        );
    }
 
    $args = array();
 
    foreach ( array( 'full', 'thumb' ) as $type ) {
        $args[ $type ] = bp_core_fetch_avatar(
            array(
                'object'  => $this->object,
                'type'    => $type,
                'item_id' => $user_id,
                'html'    => false,
            )
        );
    }
 
    // Get the avatar object before deleting it.
    $avatar = $this->get_avatar_object( $args );
 
    $deleted = bp_core_delete_existing_avatar(
        array(
            'object'  => $this->object,
            'item_id' => $user_id,
        )
    );
 
    if ( ! $deleted ) {
        return new WP_Error(
            'bp_rest_attachments_member_avatar_delete_failed',
            __( 'Sorry, there was a problem deleting the avatar.', 'buddyboss' ),
            array(
                'status' => 500,
            )
        );
    }
 
    // Build the response.
    $response = new WP_REST_Response();
    $response->set_data(
        array(
            'deleted'  => true,
            'previous' => $avatar,
        )
    );
 
    /**
     * Fires after a member avatar is deleted via the REST API.
     *
     * @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_attachments_member_avatar_delete_item', $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.