The MU forums have moved to WordPress.org

Only show information to site admins of site_id 1? (11 posts)

  1. maxaud
    Member
    Posted 14 years ago #

    I'd like to only show certain information to users if they are a 'site admin' for site_id 1 (not blog_id).

    I'd image the best way to do this would be to query the wp_sitemeta table for meta_value where site_id=1 and meta_key=site_admins

    I haven't tested this code, and I don't know mysql/php all too well so I'm not sure if this would work, I'll try back here once I'm able to test. Does anyone see any issues with the code below so far? or is there a better way to do this?

    <?php
    //get list of siteadmins
    $querystr = "
    SELECT sadmin.*
    FROM $wpdb->wp_sitemeta sadmin
    WHERE sadmin.site_id = '1'
    AND sadmin.meta_key = 'site_admins'
    ORDER BY sadmin.meta_value DESC
    ";
    
    $siteadmins = $wpdb->get_results($querystr);
    
    //get current user username
    global $current_user;
    get_currentuserinfo();
    $curuname = $current_user->user_login;
    
    //if the currently logged in user is a site admin show info
    if (in_array($curuname, $siteadmins)) {
    echo("This user is a siteadmin for the main site");
    }
    ?>

    How would I make a function out of this for a conditional statement?

    Thanks in advance for any help.

  2. maxaud
    Member
    Posted 14 years ago #

    I was able to figure this out. Basically modified the is_site_admin function to check against only site_id=1

  3. DeannaS
    Member
    Posted 14 years ago #

    I think you're confused. (Or, I'm confused as to what you're trying to accomplish.) The site is the entire group of blogs. is_site_admin doesn't check against any particular blog - it checks against the entire site. If you mess with is_site_admin there are some pretty far-reaching consequences, and hacking the core is pretty much the last thing you want to do.

  4. andrea_r
    Moderator
    Posted 14 years ago #

    Do you have multiple sites? That's the only reason I could see this as remotely useful.

  5. maxaud
    Member
    Posted 14 years ago #

    Deanna, thank you for your concerns about the consequences of editing is_site_admin.

    Andrea, you're correct that I'm running multiple sites.
    I have multiple sites running on one WordPress MU installation.

    With it like this I have a problem with 'site admins' of other sites being able to edit 'site admins' of other sites (since the user table is shared between all blogs). So what I'm doing is creating a 'super site admin' basically that can edit anything and then restricting regular 'site admins' to not be able to edit each other.

    For the time being, the 'super site admins' are determined by what users I have set up as 'site admins' of the main blog.

    I'm doing this by using actions and filters so no core files are being edited.
    My is_super_site_admin() function checks against site admins of the main site instead of editing the is_site_admin() function.

    Does anyone see any issues with this, or any problems I may run into?

    Thanks.
    Dustin Dempsey

  6. maxaud
    Member
    Posted 14 years ago #

    One thing I am looking for now is how I can get an array of all site admins from the database(from all sites). I'm believe that I'm getting it as a multi-dimensional array but I don't know how to assign a variable to that.

    I hope that makes sense.

  7. maxaud
    Member
    Posted 14 years ago #

    Here is the array that I'm getting:
    array(2) { [0]=> object(stdClass)#100 (1) { ["meta_value"]=> string(39) "a:2:{i:0;s:5:"adminuser";i:1;s:6:"anotheruser";}" } [1]=> object(stdClass)#74 (1) { ["meta_value"]=> string(41) "a:2:{i:0;s:5:"adminuser";i:1;s:8:"testuser";}" } }

    How would I do a check to see if user 'testuser' is in the generated array?

    I tried in_array() and I tried a multidimensional array search.

  8. andrea_r
    Moderator
    Posted 14 years ago #

    Are you using the free multisite plugin that's out there?

  9. maxaud
    Member
    Posted 14 years ago #

    yes.

  10. andrea_r
    Moderator
    Posted 14 years ago #

    It's a flaw in that particular plugin. Contact the dev, see if he'll fix it.

  11. maxaud
    Member
    Posted 14 years ago #

    I am going to.

    I was first going to see if I can find a solution and submit that when I contact him.

    I believe I found out what I was looking for
    See any flaws in this?

    <?php
    	function site_admin_exists( $user_login = false ) {
    
    		// is user is site admin on current site
    		if (is_site_admin( $user_login )) {
    			return true;
    		}
    
    		// From object to array.
    		function to_array($data) {
    		    if (is_object($data)) $data = get_object_vars($data);
    		    return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
    		}
    
    		// connect to database
    		global $wpdb;
    
    		// what we are looking for
    		$key = "site_admins";
    
    		// get an array containing all site admin information
    		$site_admin_list = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s", $key));
    
    		// if we got information continue
    		if($site_admin_list) {
    			if( is_array( $site_admin_list ) && in_array( $user_login, $site_admin_list ) ) {
    				return true;
    			}
    			// start loop
    			foreach($site_admin_list as $siteadminnamearray) {
    				// convert object to array
    				if (is_object($siteadminnamearray)) {
    					$siteadminnamearray = to_array($siteadminnamearray);
    				}
    				if( is_array( $siteadminnamearray ) && in_array( $user_login, $siteadminnamearray ) ) {
    					return true;
    				}
    				if(is_array( $siteadminnamearray ) ) {
    					// start second loop
    					foreach($siteadminnamearray as $siteadminname) {
    						// match names to site admins
    						$siteadminname = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $siteadminname, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
    						if( is_array( $siteadminname) && in_array( $user_login, $siteadminname) ) {
    							return true;
    						}
    					}
    				}
    			}
    		}
    		return false;
    	}
    
    	if (site_admin_exists()) {
    		echo "currently logged in user is a site admin";
    	} 
    
    	if (site_admin_exists('username')) {
    		echo "username is a site admin";
    	}
    
    ?>

About this Topic