bp_nouveau_ajax_media_album_save()

Save album

Description

Return

(string) HTML

Source

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

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
function bp_nouveau_ajax_media_album_save() {
    $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['title'] ) ) {
        $response['feedback'] = sprintf(
            '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
            esc_html__( 'Please enter title of album.', 'buddyboss' )
        );
 
        wp_send_json_error( $response );
    }
 
    // save media
    $id       = ! empty( $_POST['album_id'] ) ? $_POST['album_id'] : false;
    $group_id = ! empty( $_POST['group_id'] ) ? $_POST['group_id'] : false;
    $title    = $_POST['title'];
    $privacy  = ! empty( $_POST['privacy'] ) ? $_POST['privacy'] : 'public';
 
    $album_id = bp_album_add( array( 'id' => $id, 'title' => $title, 'privacy' => $privacy, 'group_id' => $group_id ) );
 
    if ( ! $album_id ) {
        $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 create the album.', 'buddyboss' )
        );
        wp_send_json_error( $response );
    }
 
    // save media
    $medias = $_POST['medias'];
    if ( ! empty( $medias ) ) {
 
        foreach ( $medias as $media ) {
            $activity_id = false;
            // make an activity for the media
            if ( bp_is_active( 'activity' ) ) {
                $activity_id = bp_activity_post_update( array( 'hide_sitewide' => true, 'privacy' => 'media' ) );
 
                if ( $activity_id ) {
                    // update activity meta
                    bp_activity_update_meta( $activity_id, 'bp_media_activity', '1' );
                }
            }
 
            $media_id = bp_media_add( array(
                'attachment_id' => $media['id'],
                'title'         => $media['name'],
                'activity_id'   => $activity_id,
                'album_id'      => $album_id,
                'group_id'      => $group_id,
                'privacy'       => $privacy,
                'error_type'    => 'wp_error'
            ) );
 
            if ( is_wp_error( $media_id ) ) {
                $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 add the media.', 'buddyboss' )
                );
                wp_send_json_error( $response );
            }
 
            //save media is saved in attachment
            update_post_meta( $media['id'], 'bp_media_saved', true );
        }
    }
 
    if ( ! empty( $group_id ) && bp_is_active( 'groups' ) ) {
        $group_link = bp_get_group_permalink( groups_get_group( $group_id ) );
        $redirect_url = trailingslashit( $group_link . '/albums/' . $album_id );
    } else {
        $redirect_url = trailingslashit( bp_loggedin_user_domain() . bp_get_media_slug() . '/albums/' . $album_id );
    }
 
    wp_send_json_success( array(
        'redirect_url'     => $redirect_url,
    ) );
}

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.