bp_nouveau_ajax_media_move_to_album()

Move media to album

Description

Return

(string) HTML

Source

File: bp-templates/bp-nouveau/includes/media/ajax.php

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
function bp_nouveau_ajax_media_move_to_album() {
    $response = array(
        'feedback' => sprintf(
            '<div class="bp-feedback error bp-ajax-message"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
            esc_html__( 'There was a problem performing this action. Please try again.', 'buddyboss' )
        ),
    );
 
    // Bail if not a POST action.
    if ( ! bp_is_post_request() ) {
        wp_send_json_error( $response );
    }
 
    if ( empty( $_POST['_wpnonce'] ) ) {
        wp_send_json_error( $response );
    }
 
    // Use default nonce
    $nonce = $_POST['_wpnonce'];
    $check = 'bp_nouveau_media';
 
    // Nonce check!
    if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
        wp_send_json_error( $response );
    }
 
    if ( empty( $_POST['medias'] ) ) {
        $response['feedback'] = sprintf(
            '<div class="bp-feedback error">%s</div>',
            esc_html__( 'Please upload media before saving.', 'buddyboss' )
        );
 
        wp_send_json_error( $response );
    }
 
    if ( empty( $_POST['album_id'] ) ) {
        $response['feedback'] = sprintf(
            '<div class="bp-feedback error">%s</div>',
            esc_html__( 'Please provide album to move media.', 'buddyboss' )
        );
 
        wp_send_json_error( $response );
    }
 
    $album_privacy = 'public';
    $albums = bp_album_get_specific( array( 'album_ids' => array( $_POST['album_id'] ) ) );
    if ( ! empty( $albums['albums'] ) ) {
        $album = array_pop( $albums['albums'] );
        $album_privacy = $album->privacy;
    }
 
    // save media
    $medias = $_POST['medias'];
    $media_ids = array();
    foreach( $medias as $media_id ) {
 
        $media_obj           = new BP_Media( $media_id );
        $media_obj->album_id = (int) $_POST['album_id'];
        $media_obj->group_id = ! empty( $_POST['group_id'] ) ? (int) $_POST['group_id'] : false;
        $media_obj->privacy  = $media_obj->group_id ? 'grouponly' : $album_privacy;
 
        if ( ! $media_obj->save() ) {
            $response['feedback'] = sprintf(
                '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
                esc_html__( 'There was a problem when trying to move the media.', 'buddyboss' )
            );
 
            wp_send_json_error( $response );
        }
 
        $media_ids[] = $media_id;
    }
 
    $media = '';
    if ( ! empty( $media_ids ) ) {
        ob_start();
        if ( bp_has_media( array( 'include' => implode( ',', $media_ids ) ) ) ) {
            while ( bp_media() ) {
                bp_the_media();
                bp_get_template_part( 'media/entry' );
            }
        }
        $media = ob_get_contents();
        ob_end_clean();
    }
 
    wp_send_json_success( array(
        'media' => $media,
    ) );
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.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.