bp_document_forums_new_post_document_save( $post_id )

Save document when new topic or reply is saved

Description

Parameters

$post_id

(Required)

Source

File: bp-document/bp-document-filters.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
542
543
544
545
546
547
548
549
550
551
552
553
function bp_document_forums_new_post_document_save( $post_id ) {
 
    if ( ! empty( $_POST['bbp_document'] ) ) {
 
        // save activity id if it is saved in forums and enabled in platform settings.
        $main_activity_id = get_post_meta( $post_id, '_bbp_activity_id', true );
 
        // save document.
        $documents = json_decode( stripslashes( $_POST['bbp_document'] ), true );
 
        // fetch currently uploaded document ids.
        $existing_document                = array();
        $existing_document_ids            = get_post_meta( $post_id, 'bp_document_ids', true );
        $existing_document_attachment_ids = array();
        if ( ! empty( $existing_document_ids ) ) {
            $existing_document_ids = explode( ',', $existing_document_ids );
 
            foreach ( $existing_document_ids as $existing_document_id ) {
                $existing_document[ $existing_document_id ] = new BP_Document( $existing_document_id );
 
                if ( ! empty( $existing_document[ $existing_document_id ]->attachment_id ) ) {
                    $existing_document_attachment_ids[] = $existing_document[ $existing_document_id ]->attachment_id;
                }
            }
        }
 
        $document_ids = array();
        foreach ( $documents as $document ) {
 
            $title                = ! empty( $document['name'] ) ? $document['name'] : '';
            $attachment_id        = ! empty( $document['id'] ) ? $document['id'] : 0;
            $attached_document_id = ! empty( $document['document_id'] ) ? $document['document_id'] : 0;
            $folder_id            = ! empty( $document['folder_id'] ) ? $document['folder_id'] : 0;
            $group_id             = ! empty( $document['group_id'] ) ? $document['group_id'] : 0;
            $forum_id             = ! empty( $document['forum_id'] ) ? $document['forum_id'] : 0;
            $topic_id             = ! empty( $document['topic_id'] ) ? $document['topic_id'] : 0;
            $reply_id             = ! empty( $document['reply_id'] ) ? $document['reply_id'] : 0;
            $menu_order           = ! empty( $document['menu_order'] ) ? $document['menu_order'] : 0;
 
            if ( ! empty( $existing_document_attachment_ids ) ) {
                $index = array_search( $attachment_id, $existing_document_attachment_ids );
                if ( ! empty( $attachment_id ) && $index !== false && ! empty( $existing_document[ $attached_document_id ] ) ) {
 
                    $existing_document[ $attached_document_id ]->menu_order = $menu_order;
                    $existing_document[ $attached_document_id ]->save();
 
                    unset( $existing_document_ids[ $index ] );
                    $document_ids[] = $attached_document_id;
                    continue;
                }
            }
 
            if ( 0 === $reply_id && bbp_get_reply_post_type() === get_post_type( $post_id ) ) {
                $reply_id = $post_id;
                $topic_id = bbp_get_reply_topic_id( $reply_id );
                $forum_id = bbp_get_topic_forum_id( $topic_id );
            } elseif ( 0 === $topic_id && bbp_get_topic_post_type() === get_post_type( $post_id ) ) {
                $topic_id = $post_id;
                $forum_id = bbp_get_topic_forum_id( $topic_id );
            } elseif ( 0 === $forum_id && bbp_get_forum_post_type() === get_post_type( $post_id ) ) {
                $forum_id = $post_id;
            }
 
            $attachment_data = get_post( $document['id'] );
            $file            = get_attached_file( $document['id'] );
            $file_type       = wp_check_filetype( $file );
            $file_name       = basename( $file );
 
            $document_id = bp_document_add(
                array(
                    'attachment_id' => $attachment_id,
                    'title'         => $title,
                    'folder_id'     => $folder_id,
                    'group_id'      => $group_id,
                    'privacy'       => 'forums',
                    'error_type'    => 'wp_error',
                    'menu_order'    => $menu_order,
                )
            );
 
            if ( ! is_wp_error( $document_id ) && ! empty( $document_id ) ) {
                $document_ids[] = $document_id;
 
                // save document meta.
                bp_document_update_meta( $document_id, 'forum_id', $forum_id );
                bp_document_update_meta( $document_id, 'topic_id', $topic_id );
                bp_document_update_meta( $document_id, 'reply_id', $reply_id );
                bp_document_update_meta( $document_id, 'file_name', $file_name );
                bp_document_update_meta( $document_id, 'extension', '.' . $file_type['ext'] );
 
                // save document is saved in attachment.
                update_post_meta( $attachment_id, 'bp_document_saved', true );
            }
        }
 
        $document_ids = implode( ',', $document_ids );
 
        // Save all attachment ids in forums post meta.
        update_post_meta( $post_id, 'bp_document_ids', $document_ids );
 
        // save document meta for activity.
        if ( ! empty( $main_activity_id ) && bp_is_active( 'activity' ) ) {
            bp_activity_update_meta( $main_activity_id, 'bp_document_ids', $document_ids );
        }
 
        // delete documents which were not saved or removed from form.
        if ( ! empty( $existing_document_ids ) ) {
            foreach ( $existing_document_ids as $document_id ) {
                bp_document_delete( array( 'id' => $document_id ) );
            }
        }
    }
}

Changelog

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