Pro Sites - Restrict Access by URI and Pro Site Level

I need some help modifying the following code. I would like to add a second conditional. As it stands, the code currently makes access to specific URIs available to admins with Pro Sites. I would like to further restrict this to admins of Pro Sites with a specific level.

function prosites_hide_buddypress_uri() {

	$userid = get_current_user_id();
	$uri = $_SERVER['REQUEST_URI'];
	$not_allowed = array("/^\/members\/$/", "/friends\/*/", "/^\/forums\/*/", "/\/site-tracking\/create\//", "/\/members\/[^\/]*\/forums\//"); /*** , "/\/members\/[^\/]*\/settings\//" ***/

if (!is_pro_user($userid) ) {

		foreach($not_allowed as $check)
		{
		if(preg_match($check, $uri))
		{
			wp_redirect(home_url("/upgrade/"));
			exit;
		}
	}

}else{

}
}

add_action("init", "prosites_hide_buddypress_uri");
  • Kasia Swiderska

    Hello Chris,

    There is function that checks if site have level but there is no function that checks that for user (because site has level, user can have many subsites with different levels).
    If you take a look on this code https://premium.wpmudev.org/forums/topic/pro-sites-buddypress-restrict-access-to-features#post-739516 it shows how to get list of the user blog IDs and then you can check those ids with function

    function is_pro_site($blog_id = false, $level = false)

    you will find code for this function in file pro-sites.php in the line 1796.

    kind regards,
    Kasia

  • Chris

    Hi Kasia,

    That thread is actually one of my own old threads. However, I never got the code to work. Up until now, I've just been using the second code below to limit access based upon pro site status. I've made some changes to my business model and will need to further restrict by pro site level. I tried combining Aristath's code with the other code in my functions.php file, but I'm having trouble. I hope you can spot the errors in my code below:

    function ari_prosites_maxlevel( $user ) {
    	//is there a user to check?
    	global $user, $wpdb;
    	if ( $user ) { $user_id = $user->ID;}
    	$blog_id = $wpdb->blogid;
    
    	// No need t proceed if no user
    	if ( ! isset( $user_id ) ) {	return;}
    
    	// Get the email of the current user
    	$user_email = $user->user_email;
    
    	// Get the blogs of this user
    	$user_blogs = get_blogs_of_user( $user_id );
    	foreach ( $user_blogs as $user_blog ) {
    
    		// user_site_ids is an array because the user can have multiple sites
    		$user_site_ids = array();
    
    		// Get the ID of the blog
    		$blog_id = $user_blog->userblog_id;
    		// Switch to the blog
    		switch_to_blog( $blog_id );
    		// Retrieve the e-mail of the blog administrator
    		$admin_email = get_option( 'admin_email' );
    
    		// Check if the current user is the admin of that blog
    		if ( $admin_email == $user_email ) {
    			// Add this site to the $user_site_ids array
    			$user_site_ids[] = $blog_id;
    		}
    		restore_current_blog();
        }
    
        if ( ! empty( $user_site_ids ) ) {
        	foreach ( $user_site_ids as $user_site_id ) {
    
        		$sql = "SELECT level FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = '$user_site_id'";
        		$level = $wpdb->get_var( $sql );
    
        		if ( ! isset( $max_level ) ) {
        			// There is no other level defined, so set this one as max
        			$max_level = $level;
        		} else {
        			// If the current level is greater than the already defined $max_level
        			// change $max_level to $level and proceed.
        			$max_level = ( $level > $max_level ) ? $level : $max_level;
        		}
            }
        }
        return ( isset( $max_level ) ) ? $max_level : false;
    }

    Then, I tried to combine it with the is_pro_user function like this:

    function prosites_hide_buddypress_uri() {
    
    	$current_user = wp_get_current_user();
    	$max_level = ari_prosites_maxlevel( $current_user );
    	$userid = get_current_user_id();
    	$uri = $_SERVER['REQUEST_URI'];
    	$not_allowed = array("/^\/members\/$/", "/friends\/*/", "/^\/forums\/*/", "/\/site-tracking\/create\//", "/\/members\/[^\/]*\/forums\//"); /*** , "/\/members\/[^\/]*\/settings\//" ***/
    
    if (!is_pro_user($userid) || $max_level <= 1) {
    
    		foreach($not_allowed as $check)
    		{
    		if(preg_match($check, $uri))
    		{
    			wp_redirect(home_url("/upgrade/"));
    			exit;
    		}
    	}
    }else{
    }
    }
    
    add_action("init", "prosites_hide_buddypress_uri");

    I'm not sure why this wasn't working. Can you see any errors in the code?

    Thanks,
    Chris

  • Rupok

    Hi Chris

    Hope you had a wonderful day.

    if (!is_pro_user($userid) || $max_level <= 1) {
    Can you check if $max_level is returning anything? You can simply print it to check the assigned value. You are comparing it with integer. So if $max_level = "mango", it won't work.

    Please let us know your result. We will be glad to investigate further if necessary.

    Have a nice day. Cheers!