Support » Plugin: Private groups » Private Forum Posts Are Publicly Searchable

  • Resolved Wasca

    (@wasca)


    Hi Guys

    I have a Private group created in bbpress and have added a new topic. I’ve just found that I’m able to search for private forum topics and get results. The links don’t work on the search result which is good, but I’d prefer that private forum topics not be displayed in public searches.

    Also I would like to make sure that only members of a private forum can search for posts in the private forums they belong to.

    Would appreciate some assistance on this.

    BTW, great plugin!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Robin W

    (@robin-w)

    is this a search within the bbpress search on forum pages or a site wide search or a say google search?

    Thread Starter Wasca

    (@wasca)

    @robin-w The search is done using the wordpress search however I have a plugin called Relevanssi and I’m making use of this code https://www.relevanssi.com/knowledge-base/relevanssi-bbpress/

    Plugin Author Robin W

    (@robin-w)

    99% suspect that is the issue – do a search that produces a wrong result, then deactivate the relevanssi plugin/code and try again to prove.

    I only filter on the bbpress search function.

    Thread Starter Wasca

    (@wasca)

    Spoken to the Relevanssi plugin creator, here is what he said.

    Whether a post is showed or not can be determined in many spots, but Relevanssi does it using the “relevanssi_post_ok” filter. That filter is applied to all posts and has two parameters: a boolean value and the post ID. If the filter returns “true”, the post is fine, but if it returns “false”, the post is not included in the results.

    You can find the default function relevanssi_default_post_ok() in lib/common.php. It should block the private posts from appearing in results, unless the current user has the permission to read the private post, but it’s quite possible bbP Private Groups uses a different mechanism.

    To cover that, you need to create your own “relevanssi_post_ok” filter that runs after the default one (so priority 11 or higher) and checks if the current user should be allowed to see the post.

    So looks like I need to create a new filter.

    How can I determine if the current user has permissions to read a private forum post?

    Plugin Author Robin W

    (@robin-w)

    thanks for posting this.

    if you are ok with code, then several places you could hook to, depending on what is set within wordpress when the relevanssi post id called.

    I’d look at this first :

    function private_groups_check_can_user_view_post() {
    //uses $post_id and $post_type to get the forum ($forum_id) that the post belongs to
        global $wp_query;
    
        // Get Forum Id for the current post    
        $post_id = $wp_query->post->ID;
        $post_type = $wp_query->get('post_type');
    	
    	if (bbp_is_topic_super_sticky($post_id)) return true;
    	
    	
        $forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
    //then call the function that checks if the user can view this forum, and hence this post
        if (private_groups_can_user_view_post_id($forum_id))
            return true;
    }

    If that’s no good (ie wp-query isn’t set to the relevant post) then you could hook directly to

    private_groups_get_forum_id_from_post_id($post_id, $post_type); which is called by that.

    If you all fine with that, do please post back the answer – it will help others and I would look to add it to the plugin as an option.

    If I am expecting too much of you, then please come back, and I’ll try and work some code out, but may need you to do some digging for me !

    Thread Starter Wasca

    (@wasca)

    This function I wrote seemed to work for me, but I’m a bit of a hack, more than happy for any one to refine it.

    /*-----------------------------------------------------------------------------------*/
    /*
    /*     Filter search results so that private forum posts are not displayed
    /*     to members who do not belong to the particular private group.
    /*
    /*-----------------------------------------------------------------------------------*/
    
    add_filter('relevanssi_post_ok', 'rlv_filter_private_forum_posts', 11, 2);
    
    function rlv_filter_private_forum_posts($post_ok, $post_id) { 
    	
    	//Default user can view results
    	$user_can_view = 1;
    	
    	$post = get_post($post_id);
    	
    	//Get the post type
    	$post_type = $post->post_type; 
    	
    	//Get the forum id the post belongs to (Only if it's a topic or reply)
    	if ( $post_type == 'reply' || $post_type == 'topic' ) {
    	
    		$forum_id = private_groups_get_forum_id_from_post_id($post_id, $post_type);
    	
    	}
    		
    	//Check if current user can view the forum that the post belongs to.
    	if ( private_groups_can_user_view_post_id($forum_id) === false ) {
    	  
    		$user_can_view = 0;
    	
    	}	
            
    	//Filter the search results for forum topics or replies based on the users forum permission.
    	if ( $post_type == 'reply' || $post_type == 'topic' ) {
    	
    		if ( $user_can_view == 0 ) {
    		
    			$post_ok = false;	
    		
    		} else {
    			
    			$post_ok = true;
    		
    		}
    	
    	}
      
      return $post_ok;
    }
    • This reply was modified 7 years, 4 months ago by Wasca.
    Plugin Author Robin W

    (@robin-w)

    thanks for posting the solution, and glad it is working

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Private Forum Posts Are Publicly Searchable’ is closed to new replies.