bp_attachments_cover_image_ajax_upload()

Ajax Upload and set a cover photo

Description

Return

(string|null) A json object containing success data if the upload succeeded, error message otherwise.

Source

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

1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
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
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
function bp_attachments_cover_image_ajax_upload() {
    if ( ! bp_is_post_request() ) {
        wp_die();
    }
 
    check_admin_referer( 'bp-uploader' );
 
    // Sending the json response will be different if the current Plupload runtime is html4.
    $is_html4 = ! empty( $_POST['html4' ] );
 
    if ( empty( $_POST['bp_params'] ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    $bp_params = bp_parse_args( $_POST['bp_params'], array(
        'object'  => 'user',
        'item_id' => bp_loggedin_user_id(),
    ), 'attachments_cover_image_ajax_upload' );
 
    $bp_params['item_id'] = (int) $bp_params['item_id'];
    $bp_params['object']  = sanitize_text_field( $bp_params['object'] );
 
    // We need the object to set the uploads dir filter.
    if ( empty( $bp_params['object'] ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    // Capability check.
    if ( ! bp_attachments_current_user_can( 'edit_cover_image', $bp_params ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    $bp          = buddypress();
    $needs_reset = array();
 
    // Member's cover photo.
    if ( 'user' === $bp_params['object'] ) {
        $object_data = array( 'dir' => 'members', 'component' => 'xprofile' );
 
        if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) {
            $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user );
            $bp->displayed_user->id = $bp_params['item_id'];
        }
 
    // Group's cover photo.
    } elseif ( 'group' === $bp_params['object'] ) {
        $object_data = array( 'dir' => 'groups', 'component' => 'groups' );
 
        if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) {
            $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group );
            $bp->groups->current_group = groups_get_group( $bp_params['item_id'] );
        }
 
    // Other object's cover photo.
    } else {
        $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', array(), $bp_params['object'] );
    }
 
    // Stop here in case of a missing parameter for the object.
    if ( empty( $object_data['dir'] ) || empty( $object_data['component'] ) ) {
        bp_attachments_json_response( false, $is_html4 );
    }
 
    /**
     * Filters whether or not to handle cover photo uploading.
     *
     * If you want to override this function, make sure you return an array with the 'result' key set.
     *
     * @since BuddyPress 2.5.1
     *
     * @param array $value
     * @param array $bp_params
     * @param array $needs_reset Stores original value of certain globals we need to revert to later.
     * @param array $object_data
     */
    $pre_filter = apply_filters( 'bp_attachments_pre_cover_image_ajax_upload', array(), $bp_params, $needs_reset, $object_data );
    if ( isset( $pre_filter['result'] ) ) {
        bp_attachments_json_response( $pre_filter['result'], $is_html4, $pre_filter );
    }
 
    $cover_image_attachment = new BP_Attachment_Cover_Image();
    $uploaded = $cover_image_attachment->upload( $_FILES );
 
    // Reset objects.
    if ( ! empty( $needs_reset ) ) {
        if ( ! empty( $needs_reset['component'] ) ) {
            $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value'];
        } else {
            $bp->{$needs_reset['key']} = $needs_reset['value'];
        }
    }
 
    if ( ! empty( $uploaded['error'] ) ) {
        // Upload error response.
        bp_attachments_json_response( false, $is_html4, array(
            'type'    => 'upload_error',
            'message' => sprintf( __( 'Upload Error: %s', 'buddyboss' ), $uploaded['error'] ),
        ) );
    }
 
    $error_message = __( 'There was a problem uploading the cover photo.', 'buddyboss' );
 
    $bp_attachments_uploads_dir = bp_attachments_cover_image_upload_dir();
 
    // The BP Attachments Uploads Dir is not set, stop.
    if ( ! $bp_attachments_uploads_dir ) {
        bp_attachments_json_response( false, $is_html4, array(
            'type'    => 'upload_error',
            'message' => $error_message,
        ) );
    }
 
    $cover_subdir = $object_data['dir'] . '/' . $bp_params['item_id'] . '/cover-image';
    $cover_dir    = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $cover_subdir;
 
    if ( 1 === validate_file( $cover_dir ) || ! is_dir( $cover_dir ) ) {
        // Upload error response.
        bp_attachments_json_response( false, $is_html4, array(
            'type'    => 'upload_error',
            'message' => $error_message,
        ) );
    }
 
    /*
     * Generate the cover photo so that it fit to feature's dimensions
     *
     * Unlike the avatar, uploading and generating the cover photo is happening during
     * the same Ajax request, as we already instantiated the BP_Attachment_Cover_Image
     * class, let's use it.
     */
    $cover = bp_attachments_cover_image_generate_file( array(
        'file'            => $uploaded['file'],
        'component'       => $object_data['component'],
        'cover_image_dir' => $cover_dir
    ), $cover_image_attachment );
 
    if ( ! $cover ) {
        bp_attachments_json_response( false, $is_html4, array(
            'type'    => 'upload_error',
            'message' => $error_message,
        ) );
    }
 
    $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename'];
 
    // 1 is success.
    $feedback_code = 1;
 
    // 0 is the size warning.
    if ( $cover['is_too_small'] ) {
        $feedback_code = 0;
    }
 
    // Set the name of the file.
    $name = $_FILES['file']['name'];
    $name_parts = pathinfo( $name );
    $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) );
 
    /**
     * Fires if the new cover photo was successfully uploaded.
     *
     * The dynamic portion of the hook will be xprofile in case of a user's
     * cover photo, groups in case of a group's cover photo. For instance:
     * Use add_action( 'xprofile_cover_image_uploaded' ) to run your specific
     * code once the user has set his cover photo.
     *
     * @since BuddyPress 2.4.0
     * @since BuddyPress 3.0.0 Added $cover_url, $name, $feedback_code arguments.
     *
     * @param int    $item_id       Inform about the item id the cover photo was set for.
     * @param string $name          Filename.
     * @param string $cover_url     URL to the image.
     * @param int    $feedback_code If value not 1, an error occured.
     */
    do_action(
        $object_data['component'] . '_cover_image_uploaded',
        (int) $bp_params['item_id'],
        $name,
        $cover_url,
        $feedback_code
    );
 
    // Finally return the cover photo url to the UI.
    bp_attachments_json_response( true, $is_html4, array(
        'name'          => $name,
        'url'           => $cover_url,
        'feedback_code' => $feedback_code,
    ) );
}

Changelog

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