I need something that will honor the protected status of search results

Do you guys have any recommendations on site search plugins (like 'Search Everything') that would work with Membership Pro 2? I need something that will honor the protected status of results... that one does not appear to. I essentially want to search custom fields and document meta.

I was provided with this link - https://en-gb.wordpress.org/plugins/relevanssi/

I managed to get Relevanssi working... but the problem I'm having is that is it not honoring protected media in results... protected pages seem ok...

It basically is giving out the full unprotected link to media

  • Mahlamusa

    Hello Paul,

    I hope you are doing great today and thank you for contacting us. I am sorry for the late reply from our end. Please note that posting on the same thread consecutively will push your thread to the bottom of our queue and that will result in a delayed response, this is because our system treats each post as a reply and if a post has a recent reply it goes to the bottom of the queue and must wait its turn again.

    Thank you for digging and finding a useful filter that you can use to achieve what you wanted to achieve. Here is a possible solution to your needs. Paste the following code to your theme's functions.php file.

    add_filter('relevanssi_post_ok', 'relevanssi_exclude_membership_protected_posts', 99, 2 );
    
    function relevanssi_exclude_membership_protected_posts( $show_hide, $post_id ) {
    	$post_id = $post_id ? $post_id : get_the_ID();
    	$memberships = MS_Model_Membership::get_memberships();
    	$available_for = array();
    
    	foreach ( $memberships as $membership ) {
    	 	$rule = $membership->get_rule( MS_Rule_Post::RULE_ID ); //MS_Rule_Page, MS_Rule_CptItem
    	 	if ( true === $rule->has_access( $post_id ) ) {
    			                       $available_for[$membership->id] = $membership->name;
    	 	}
    	}
    
    	//if protected return false to exclude from results
    	if ( !empty( $available_for ) && is_array( $available_for ) ){
    	 	return false;
    	}
    	return $show_hide;
    }

    Basically what this code should do is take the post_id made available by the 'relevanssi_post_ok' filter and get all memberships protecting that post, if there are memberships protecting the post then it will return false to tell relavanssi to hide this post, if there are no memberships, the code will return the true/false value given to it by the hook as the $show_hide variable.

    This code should work as is but haven't done any testing on it. Please test it and let us know if you have any issues with the code snippet.

    Have a nice day.

    Cheers,
    Mahlamusa

  • Paul V

    some thoughts on this... It seems that I can get the relevanssi_post_ok filter to work if I check for something simple, like if the result is an attachment, for an example.

    What I need then, is a function to check a post's status against the current membership status of the logged in user. Also If a user is not logged in, and there is a membership status, then the post should be hidden as well.

    So something to the effect of

    Get the membership level of post ;
    If membership level exists {
         if user is logged in AND has access {
              return default show_hide;
         }else{
              return false show_hide ;
         }
    }else{
         return  default show_hide;
    }
  • Mahlamusa

    Hello Paul.

    I hope you are doing great today. I have done a couple of tests and modified the code to be as follow:

    add_action('relevanssi_post_ok', 'relevanssi_exclude_membership_protected_posts', 99, 2);
    
    function relevanssi_exclude_membership_protected_posts(  $show_hide, $post_id) { //
    	if ( !isset( $post_id ) || $post_id <= 0 ) {
    		global $post;
    		$post_id = $post->ID;
    	}
    	$memberships = MS_Model_Membership::get_memberships();
    
    	//check page protection
    	foreach ( $memberships as $membership ) {
    		$rule = $membership->get_rule( MS_Rule_Page::RULE_ID );
    		if ( true === $rule->has_access( $post_id ) && member_has_access( $post_id) ) {
    			return true;
    		}
    	}
    
    	//check post protection
    	foreach ( $memberships as $membership ) {
    		$rule = $membership->get_rule( MS_Rule_Post::RULE_ID );
    		if ( true === $rule->has_access( $post_id ) && member_has_access( $post_id) ) {
    			return true;
    		}
    	}
    
    	//check custom post type protection
    	foreach ( $memberships as $membership ) {
    		$rule = $membership->get_rule( MS_Rule_CptItem::RULE_ID );
    		if ( true === $rule->has_access( $post_id ) && member_has_access( $post_id) ) {
    			return true;
    		}
    	}
    
    	return $show_hide;
    }
    
    function member_has_access( $post_id){
    	$api = ms_api();
    	$member = $api->get_current_member();
    	if ( $member->wp_user && $member->subscriptions ) {
    		foreach ( $member->subscriptions as $subscription ) {
    			$membership = $subscription->get_membership();
    			return $membership->has_access_to_post( $post_id );
    		}
    	}
    	return false;
    }

    I have tested the code on protected posts and it seems to work on my side. The new code will check if the page is protected and the member has access, if the member has access then it should allow and if there is no logged in user, protected posts will be hidden from the search results.

    You can modify the code to suite your specific needs but I think it works for the initial requirement you specified.

    I hope this help, please let us know if you need any help and we will always be more than happy to assist in any way possible.

    Have a nice day.

    Cheers,
    Mahlamusa