bp_document_move_folder_to_folder( $folder_id,  $destination_folder_id,  $group_id )

This function will move folder to another destination folder id.

Description

Parameters

$folder_id

(Required)

$destination_folder_id

(Required)

$group_id

(Required)

Return

(bool)

Source

File: bp-document/bp-document-functions.php

2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
function bp_document_move_folder_to_folder( $folder_id, $destination_folder_id, $group_id = 0 ) {
 
    global $wpdb, $bp;
 
    if ( '' === $folder_id || '' === $destination_folder_id ) {
        return false;
    }
 
    if ( (int) $folder_id > 0 ) {
        $has_access = bp_folder_user_can_edit( $folder_id );
        if ( ! $has_access ) {
            return false;
        }
    }
 
    if ( (int) $destination_folder_id > 0 ) {
        $has_destination_access = bp_folder_user_can_edit( $destination_folder_id );
        if ( ! $has_destination_access ) {
            return false;
        }
    }
 
    if ( ! $group_id ) {
        $get_folder = new BP_Document_Folder( $folder_id );
        if ( $get_folder->group_id > 0 ) {
            $group_id = $get_folder->group_id;
        }
    }
 
    $destination_privacy = 'loggedin';
    if ( $group_id > 0 ) {
        $destination_privacy = 'grouponly';
    } elseif ( $destination_folder_id > 0 ) {
        $destination_folder  = BP_Document_Folder::get_folder_data( array( $destination_folder_id ) );
        $destination_privacy = $destination_folder[0]->privacy;
 
        // Update modify date for destination folder.
        $destination_folder_update                = new BP_Document_Folder( $destination_folder_id );
        $destination_folder_update->date_modified = bp_core_current_time();
        $destination_folder_update->save();
    }
 
    // Update main parent folder.
    $folder                = new BP_Document_Folder( $folder_id );
    $folder->privacy       = $destination_privacy;
    $folder->parent        = $destination_folder_id;
    $folder->date_modified = bp_core_current_time();
    $folder->save();
 
    // Get all the documents of main folder.
    $document_ids = bp_document_get_folder_document_ids( $folder_id );
    if ( ! empty( $document_ids ) ) {
        foreach ( $document_ids as $id ) {
            // Update privacy of the document.
            $query_update_document = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET privacy = %s WHERE id = %d", $destination_privacy, $id ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
            $query                 = $wpdb->query( $query_update_document );
 
            // Update document activity privacy.
            $document = new BP_Document( $id );
            if ( ! empty( $document ) && ! empty( $document->attachment_id ) ) {
                $post_attachment = $document->attachment_id;
                $activity_id     = get_post_meta( $post_attachment, 'bp_document_parent_activity_id', true );
                if ( ! empty( $activity_id ) && bp_is_active( 'activity' ) ) {
                    $activity = new BP_Activity_Activity( (int) $activity_id );
                    if ( bp_activity_user_can_delete( $activity ) ) {
                        $activity->privacy = $destination_privacy;
                        $activity->save();
                    }
                }
            }
        }
    }
 
    // Update privacy for all child folders.
    $get_children = bp_document_get_folder_children( $folder_id );
 
    foreach ( $get_children as $child ) {
        $query_update_child = $wpdb->prepare( "UPDATE {$bp->document->table_name_folder} SET privacy = %s WHERE id = %d", $destination_privacy, $child ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
        $query              = $wpdb->query( $query_update_child );
 
        // Get all the documents of particular folder.
        $document_ids = bp_document_get_folder_document_ids( $child );
 
        if ( ! empty( $document_ids ) ) {
            foreach ( $document_ids as $id ) {
 
                // Update privacy of the document.
                $query_update_document = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET privacy = %s WHERE id = %d", $destination_privacy, $id ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
                $query                 = $wpdb->query( $query_update_document );
 
                // Update document activity privacy.
                $document = new BP_Document( $id );
                if ( ! empty( $document ) && ! empty( $document->attachment_id ) ) {
                    $post_attachment = $document->attachment_id;
                    $activity_id     = get_post_meta( $post_attachment, 'bp_document_parent_activity_id', true );
                    if ( ! empty( $activity_id ) && bp_is_active( 'activity' ) ) {
                        $activity = new BP_Activity_Activity( (int) $activity_id );
                        if ( bp_activity_user_can_delete( $activity ) ) {
                            $activity->privacy = $destination_privacy;
                            $activity->save();
                        }
                    }
                }
            }
        }
    }
 
    return true;
}

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.