bp_document_update_privacy( int $document_id, string $privacy = '', string $type = 'folder' )

Update document privacy with nested level.

Description

Parameters

$document_id

(Required) Document/Folder ID.

$privacy

(Optional) Privacy term to update.

Default value: ''

$type

(Optional) Current type for the document.

Default value: 'folder'

Return

(bool)

Source

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

2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
function bp_document_update_privacy( $document_id = 0, $privacy = '', $type = 'folder' ) {
 
    global $wpdb, $bp;
 
    if ( '' === $document_id || '' === $privacy ) {
        return false;
    }
 
    if ( ! is_user_logged_in() ) {
        return false;
    }
 
    if ( 'folder' === $type ) {
 
        if ( (int) $document_id > 0 ) {
            $has_access = bp_folder_user_can_edit( $document_id );
            if ( ! $has_access ) {
                return false;
            }
        }
 
        $q = $wpdb->prepare( "UPDATE {$bp->document->table_name_folder} SET privacy = %s, date_modified = %s WHERE id = %d", $privacy, bp_core_current_time(), $document_id );  // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
        $wpdb->query( $q );
 
        // Get main folder's child folders.
        $get_children = bp_document_get_folder_children( $document_id );
        if ( ! empty( $get_children ) ) {
            foreach ( $get_children as $child ) {
                $query_child_privacy = $wpdb->prepare( "UPDATE {$bp->document->table_name_folder} SET privacy = %s WHERE id = %d", $privacy, $child ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
                $wpdb->query( $query_child_privacy );
 
                // Get current folder's documents.
                $child_document_ids = bp_document_get_folder_document_ids( $child );
                if ( ! empty( $child_document_ids ) ) {
                    foreach ( $child_document_ids as $child_document_id ) {
                        $child_document_query = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET privacy = %s WHERE id = %d", $privacy, $child_document_id );
                        $wpdb->query( $child_document_query );
 
                        $document = new BP_Document( $child_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 = $privacy;
                                    $activity->save();
                                }
                            }
                        }
                    }
                }
            }
        }
 
        // Get main folder's documents.
        $get_document_ids = bp_document_get_folder_document_ids( $document_id );
        if ( ! empty( $get_document_ids ) ) {
            foreach ( $get_document_ids as $document_id ) {
                $document_query = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET privacy = %s WHERE id = %d", $privacy, $document_id );
                $wpdb->query( $document_query );
 
                $document = new BP_Document( $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 = $privacy;
                            $activity->save();
                        }
                    }
                }
            }
        }
    } else {
 
        if ( (int) $document_id > 0 ) {
            $has_access = bp_document_user_can_edit( $document_id );
            if ( ! $has_access ) {
                return false;
            }
        }
 
        $document_query = $wpdb->prepare( "UPDATE {$bp->document->table_name} SET privacy = %s, date_modified = %s WHERE id = %d", $privacy, bp_core_current_time(), $document_id );
        $wpdb->query( $document_query );
 
        $document = new BP_Document( $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 ( bp_is_active( 'activity' ) && ! empty( $activity_id ) ) {
                $activity = new BP_Activity_Activity( (int) $activity_id );
                if ( bp_activity_user_can_delete( $activity ) ) {
                    $activity->privacy = $privacy;
                    $activity->save();
                }
            }
        }
    }
}

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.