bp_avatar_ajax_set()

Ajax set an avatar for a given object and item id.

Description

Return

(string|null) A JSON object containing success data if the crop/capture succeeded error message otherwise.

Source

File: bp-core/bp-core-avatars.php

1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
function bp_avatar_ajax_set() {
    if ( ! bp_is_post_request() ) {
        wp_send_json_error();
    }
 
    // Check the nonce.
    check_admin_referer( 'bp_avatar_cropstore', 'nonce' );
 
    $avatar_data = wp_parse_args( $_POST, array(
        'crop_w' => bp_core_avatar_full_width(),
        'crop_h' => bp_core_avatar_full_height(),
        'crop_x' => 0,
        'crop_y' => 0
    ) );
 
    if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) || empty( $avatar_data['original_file'] ) ) {
        wp_send_json_error();
    }
 
    // Capability check.
    if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) {
        wp_send_json_error();
    }
 
    if ( ! empty( $avatar_data['type'] ) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object'] ) {
        $webcam_avatar = false;
 
        if ( ! empty( $avatar_data['original_file'] ) ) {
            $webcam_avatar = str_replace( array( 'data:image/png;base64,', ' ' ), array( '', '+' ), $avatar_data['original_file'] );
            $webcam_avatar = base64_decode( $webcam_avatar );
        }
 
        if ( ! bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'] ) ) {
            wp_send_json_error( array(
                'feedback_code' => 1
            ) );
 
        } else {
            $return = array(
                'avatar' => esc_url( bp_core_fetch_avatar( array(
                    'object'  => $avatar_data['object'],
                    'item_id' => $avatar_data['item_id'],
                    'html'    => false,
                    'type'    => 'full',
                ) ) ),
                'feedback_code' => 2,
                'item_id'       => $avatar_data['item_id'],
            );
 
            /**
             * Fires if the new avatar was successfully captured.
             *
             * @since BuddyPress 1.1.0 Used to inform the avatar was successfully cropped
             * @since BuddyPress 2.3.4 Add two new parameters to inform about the user id and
             *              about the way the avatar was set (eg: 'crop' or 'camera')
             *              Move the action at the right place, once the avatar is set
             * @since BuddyPress 2.8.0 Added the `$avatar_data` parameter.
             *
             * @param string $item_id     Inform about the user id the avatar was set for.
             * @param string $type        Inform about the way the avatar was set ('camera').
             * @param array  $avatar_data Array of parameters passed to the avatar handler.
             */
            do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data );
 
            wp_send_json_success( $return );
        }
 
        return;
    }
 
    $original_file = str_replace( bp_core_avatar_url(), '', $avatar_data['original_file'] );
 
    // Set avatars dir & feedback part.
    if ( 'user' === $avatar_data['object'] ) {
        $avatar_dir = 'avatars';
 
    // Defaults to object-avatars dir.
    } else {
        $avatar_dir = sanitize_key( $avatar_data['object'] ) . '-avatars';
    }
 
    // Crop args.
    $r = array(
        'item_id'       => $avatar_data['item_id'],
        'object'        => $avatar_data['object'],
        'avatar_dir'    => $avatar_dir,
        'original_file' => $original_file,
        'crop_w'        => $avatar_data['crop_w'],
        'crop_h'        => $avatar_data['crop_h'],
        'crop_x'        => $avatar_data['crop_x'],
        'crop_y'        => $avatar_data['crop_y']
    );
 
    // Handle crop.
    if ( bp_core_avatar_handle_crop( $r ) ) {
        $return = array(
            'avatar' => esc_url( bp_core_fetch_avatar( array(
                'object'  => $avatar_data['object'],
                'item_id' => $avatar_data['item_id'],
                'html'    => false,
                'type'    => 'full',
            ) ) ),
            'feedback_code' => 2,
            'item_id'       => $avatar_data['item_id'],
        );
 
        if ( 'user' === $avatar_data['object'] ) {
            /** This action is documented in bp-core/bp-core-avatars.php */
            do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
        } elseif ( 'group' === $avatar_data['object'] ) {
            /** This action is documented in bp-groups/bp-groups-screens.php */
            do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r );
        }
 
        wp_send_json_success( $return );
    } else {
        wp_send_json_error( array(
            'feedback_code' => 1,
        ) );
    }
}

Changelog

Changelog
Version Description
BuddyPress 2.3.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.