Conditionally load documents in BuddyBoss profiles

64 Views Asked by At

I am trying to make the current user's documents only accessible to the current user and admins.

I am currently trying to add a filter when you are not on your own profile, you will not see the documents. The documents are still showing however...

function debug($data) {
    $output = $data;
    if (is_array($output))
        $output = implode(',', $output);

    echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

function document_screen_restrict() {
        if ( bp_is_my_profile() != 1 ) {
            debug("not my documents, SHOULD NOT SEE");
            add_filter("document_screen",'__return_false');
            debug(did_action( 'document_screen' ));
            return;
        }else{
            debug("My documents, SEE");
            remove_filter("document_screen",'__return_false');
            debug(did_action( 'document_screen' ));

        }
}

add_action('wp','document_screen_restrict',10);
1

There are 1 best solutions below

0
On BEST ANSWER

It looks like this is working

function debug($data) {
$output = $data;
if (is_array($output))
    $output = implode(',', $output);

echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

function document_screen_restrict($arg) {
    $admin=is_admin();
    debug($admin);
    if ( bp_is_my_profile() ) {
        debug("My documents, SEE");
        return $arg;
    }elseif ( $admin ){
        debug("I am Admin, SEE");
        return $arg;
    }else{
        debug("not my documents, SHOULD NOT SEE");
        return;
    }
}

add_filter('bp_document_get','document_screen_restrict');

However if you have the url you can still download it if you're not logged in. This is how to stop the download conditionally if your not logged in. When you enter the document link in the url it is now redirecting you to the main page to log in, if you already are then it will just download per usual.

add_filter( 'bb_media_user_can_access', 'remove_document_download_globally_bb', 10, 4 );

function remove_document_download_globally_bb( $data, $id, $type, $media_group_id ){

  if( 'document' == $type || 'folder' == $type ){

    if ( bp_is_my_profile() ) {
        $data['can_download'] = true;
    }elseif ( current_user_can('manage_options') ){
        $data['can_download'] = true;
    }else{
        $data['can_download'] = false;
    }
  }

  return $data;

}