Make WordPress Core

Changeset 15671


Ignore:
Timestamp:
09/27/2010 08:26:36 PM (14 years ago)
Author:
ryan
Message:

Admin bar, first pass. see #14772

Location:
trunk
Files:
9 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/ms-functions.php

    r15621 r15671  
    2929
    3030    return false;
    31 }
    32 
    33 function get_blogs_of_user( $id, $all = false ) {
    34     global $wpdb;
    35 
    36     $cache_suffix = $all ? '_all' : '_short';
    37     $return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
    38     if ( $return )
    39         return apply_filters( 'get_blogs_of_user', $return, $id, $all );
    40 
    41     $user = get_userdata( (int) $id );
    42     if ( !$user )
    43         return false;
    44 
    45     $blogs = $match = array();
    46     $prefix_length = strlen($wpdb->base_prefix);
    47     foreach ( (array) $user as $key => $value ) {
    48         if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
    49             continue;
    50         if ( substr($key, -12, 12) != 'capabilities' )
    51             continue;
    52         if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
    53             if ( count( $match ) > 2 )
    54                 $blog_id = $match[ 2 ];
    55             else
    56                 $blog_id = 1;
    57             $blog = get_blog_details( $blog_id );
    58             if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
    59                 $blogs[ $blog_id ]->userblog_id = $blog_id;
    60                 $blogs[ $blog_id ]->blogname        = $blog->blogname;
    61                 $blogs[ $blog_id ]->domain      = $blog->domain;
    62                 $blogs[ $blog_id ]->path            = $blog->path;
    63                 $blogs[ $blog_id ]->site_id     = $blog->site_id;
    64                 $blogs[ $blog_id ]->siteurl     = $blog->siteurl;
    65             }
    66         }
    67     }
    68 
    69     wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 );
    70     return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
    7131}
    7232
     
    367327}
    368328
    369 function is_blog_user( $blog_id = 0 ) {
    370     global $wpdb;
    371  
    372     $current_user = wp_get_current_user();
    373     if ( !$blog_id )
    374         $blog_id = $wpdb->blogid;
    375 
    376     $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
    377 
    378     if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
    379         return true;
    380 
    381     return false;
    382 }
    383 
    384329function is_email_address_unsafe( $user_email ) {
    385330    $banned_names = get_site_option( 'banned_email_domains' );
  • trunk/wp-includes/user.php

    r15590 r15671  
    576576
    577577/**
     578 * Get the blogs a user belong to.
     579 *
     580 * $since 3.0.0
     581 *
     582 * @param int $id User Id
     583 * @param bool $all Whether to retrieve all blog details or an abbreviated set of details. Default is abbreviated.
     584 * @return array A list of the user's blogs.
     585 */
     586function get_blogs_of_user( $id, $all = false ) {
     587    global $wpdb;
     588
     589    if ( !is_multisite() ) {
     590        global $blog_id;
     591        $blogs = array();
     592        $blogs[ $blog_id ]->userblog_id = $blog_id;
     593        $blogs[ $blog_id ]->blogname = get_option('blogname');
     594        $blogs[ $blog_id ]->domain = '';
     595        $blogs[ $blog_id ]->path = '';
     596        $blogs[ $blog_id ]->site_id = 1;
     597        $blogs[ $blog_id ]->siteurl = get_option('siteurl');
     598        return $blogs;
     599    }
     600
     601    $cache_suffix = $all ? '_all' : '_short';
     602    $return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
     603    if ( $return )
     604        return apply_filters( 'get_blogs_of_user', $return, $id, $all );
     605
     606    $user = get_userdata( (int) $id );
     607    if ( !$user )
     608        return false;
     609
     610    $blogs = $match = array();
     611    $prefix_length = strlen($wpdb->base_prefix);
     612    foreach ( (array) $user as $key => $value ) {
     613        if ( $prefix_length && substr($key, 0, $prefix_length) != $wpdb->base_prefix )
     614            continue;
     615        if ( substr($key, -12, 12) != 'capabilities' )
     616            continue;
     617        if ( preg_match( '/^' . $wpdb->base_prefix . '((\d+)_)?capabilities$/', $key, $match ) ) {
     618            if ( count( $match ) > 2 )
     619                $blog_id = $match[ 2 ];
     620            else
     621                $blog_id = 1;
     622            $blog = get_blog_details( $blog_id );
     623            if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
     624                $blogs[ $blog_id ]->userblog_id = $blog_id;
     625                $blogs[ $blog_id ]->blogname        = $blog->blogname;
     626                $blogs[ $blog_id ]->domain      = $blog->domain;
     627                $blogs[ $blog_id ]->path            = $blog->path;
     628                $blogs[ $blog_id ]->site_id     = $blog->site_id;
     629                $blogs[ $blog_id ]->siteurl     = $blog->siteurl;
     630            }
     631        }
     632    }
     633
     634    wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 );
     635    return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
     636}
     637
     638function get_ordered_blogs_of_user( $user_id, $visibility = true ) {
     639    $newblogs      = $ordered = array();
     640    $blogs         = get_blogs_of_user( $user_id );
     641    $order_meta    = get_user_meta( $user_id, 'blog_order' );
     642    $visible_meta  = get_user_meta( $user_id, 'blog_visibility' );
     643   
     644    $order = $order_meta;
     645    if ( !is_array( $order ) )
     646        $order = array();
     647
     648    $visible = $visible_meta;
     649    if ( !is_array( $visible ) )
     650        $visible = array();
     651   
     652    // Index the blogs by userblog_id and set the visibility flag
     653    // Visibility is on by default, unless a linked site then off
     654    foreach ( $blogs AS $blog ) {
     655        $blog->visible = true;
     656
     657        if ( isset( $visible[$blog->userblog_id] ) )
     658            $blog->visible = $visible[$blog->userblog_id];
     659
     660        $newblogs[$blog->userblog_id] = $blog;
     661    }
     662
     663    // Add the blogs to our list by order
     664    foreach ( (array)$order AS $id ) {
     665        // A previous change was saving the entire blog details into ordered, not just the blog ID - this detects it
     666        if ( is_object( $id ) && isset( $id->userblog_id ) )
     667            $id = $id->userblog_id;
     668           
     669        if ( is_numeric( $id ) && isset( $newblogs[intval( $id )] ) ) {
     670            $ordered[$id] = $newblogs[$id];
     671            unset( $newblogs[$id] );
     672        }
     673    }
     674
     675    // Add any blog not yet ordered to the end
     676    foreach ( $newblogs AS $blog ) {
     677        $ordered[$blog->userblog_id] = $blog;
     678    }
     679
     680    // If we're only interested in visible blogs then remove the rest
     681    if ( $visibility ) {
     682        foreach ( (array)$ordered AS $pos => $blog ) {
     683            if ( $blog->visible == false )
     684                unset( $ordered[$pos] );
     685        }
     686    }
     687
     688/*
     689    // Set the order and visible options if the user doesn't have any,
     690    // but rate limit it so that the global DB doesn't get hammered
     691    if ( !is_array( $order_meta ) && ( 1 == mt_rand( 1, 10 ) ) )
     692        update_usermeta( $user_id, 'blog_order', array() );
     693
     694    if ( !is_array( $visible_meta ) && ( 1 == mt_rand( 1, 10 ) ) )
     695        update_usermeta( $user_id, 'blog_visibility', array() );
     696*/
     697
     698    return apply_filters( 'ordered_blogs', $ordered );
     699}
     700
     701function set_blog_visibility( $blog_id, $visible ) {
     702    global $current_user;
     703
     704    if ( is_blog_user( $blog_id ) ) {
     705        $visibility = get_user_meta( $current_user->ID, 'blog_visibility' );
     706        if ( !is_array( $visibility ) )
     707            $visibility = array();
     708
     709        $visibility[$blog_id] = $visible;
     710
     711        update_user_meta( $current_user->ID, 'blog_visibility', $visibility );
     712    }
     713}
     714
     715/**
     716 * Checks if the current user belong to a given blog.
     717 *
     718 * @since 3.0.0
     719 *
     720 * @param int $blog_id Blog ID
     721 * @return bool True if the current users belong to $blog_id, false if not.
     722 */
     723function is_blog_user( $blog_id = 0 ) {
     724    global $wpdb;
     725 
     726    $current_user = wp_get_current_user();
     727    if ( !$blog_id )
     728        $blog_id = $wpdb->blogid;
     729
     730    $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
     731
     732    if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
     733        return true;
     734
     735    return false;
     736}
     737
     738/**
    578739 * Add meta data field to a user.
    579740 *
  • trunk/wp-settings.php

    r15638 r15671  
    134134require( ABSPATH . WPINC . '/nav-menu.php' );
    135135require( ABSPATH . WPINC . '/nav-menu-template.php' );
     136require( ABSPATH . WPINC . '/admin-bar.php' );
    136137
    137138// Load multisite-specific files.
Note: See TracChangeset for help on using the changeset viewer.