Make WordPress Core


Ignore:
Timestamp:
02/13/2010 10:35:11 PM (15 years ago)
Author:
ryan
Message:

Move blogs table related functions to blogs.php. Include it before ms-settings.php so that caching API can be used instead of direct queries. see #11644

File:
1 edited

Legend:

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

    r13116 r13125  
    55 * @package WordPress
    66 */
    7 
    8 // @todo use update_blog_details
    9 function wpmu_update_blogs_date() {
    10     global $wpdb;
    11 
    12     $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
    13     refresh_blog_details( $wpdb->blogid );
    14 
    15     do_action( 'wpmu_blog_updated', $wpdb->blogid );
    16 }
    17 
    18 function get_blogaddress_by_id( $blog_id ) {
    19     $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
    20     return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
    21 }
    22 
    23 function get_blogaddress_by_name( $blogname ) {
    24     global $current_site;
    25 
    26     if ( is_subdomain_install() ) {
    27         if ( $blogname == 'main' )
    28             $blogname = 'www';
    29         return esc_url( 'http://' . $blogname . '.' . $current_site->domain . $current_site->path );
    30     } else {
    31         return esc_url( 'http://' . $current_site->domain . $current_site->path . $blogname . '/' );
    32     }
    33 }
    34 
    35 function get_blogaddress_by_domain( $domain, $path ){
    36     if ( is_subdomain_install() ) {
    37         $url = "http://".$domain.$path;
    38     } else {
    39         if ( $domain != $_SERVER['HTTP_HOST'] ) {
    40             $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
    41             $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
    42             // we're not installing the main blog
    43             if ( $blogname != 'www.' )
    44                 $url .= $blogname . '/';
    45         } else { // main blog
    46             $url = 'http://' . $domain . $path;
    47         }
    48     }
    49     return esc_url( $url );
    50 }
    517
    528function get_sitestats() {
     
    7935
    8036    return false;
    81 }
    82 
    83 function get_id_from_blogname( $name ) {
    84     global $wpdb, $current_site;
    85     $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
    86     if ( $blog_id )
    87         return $blog_id;
    88 
    89     if ( is_subdomain_install() ) {
    90         $domain = $name . '.' . $current_site->domain;
    91         $path = $current_site->path;
    92     } else {
    93         $domain = $current_site->domain;
    94         $path = $current_site->path . $name . '/';
    95     }
    96     $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
    97     wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
    98     return $blog_id;
    99 }
    100 
    101 /**
    102  * Retrieve the details for a blog from the blogs table and blog options.
    103  *
    104  * @since 3.0
    105  * @param int $blog_id Blog ID
    106  * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
    107  * @return object Blog details.
    108  */
    109 function get_blog_details( $blog_id, $get_all = true ) {
    110     global $wpdb;
    111 
    112     if ( !is_numeric( $blog_id ) )
    113         $blog_id = get_id_from_blogname( $blog_id );
    114 
    115     $blog_id = (int) $blog_id;
    116 
    117     $all = $get_all == true ? '' : 'short';
    118     $details = wp_cache_get( $blog_id . $all, 'blog-details' );
    119 
    120     if ( $details ) {
    121         if ( ! is_object( $details ) ) {
    122             if ( $details == -1 )
    123                 return false;
    124             else
    125                 // Clear old pre-serialized objects. Cache clients do better with that.
    126                 wp_cache_delete( $blog_id . $all, 'blog-details' );
    127         }
    128         return $details;
    129     }
    130 
    131     $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
    132     if ( ! $details ) {
    133         wp_cache_set( $blog_id . $all, -1, 'blog-details' );
    134         return false;
    135     }
    136 
    137     if ( ! $get_all ) {
    138         wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    139         return $details;
    140     }
    141 
    142     $details->blogname      = get_blog_option( $blog_id, 'blogname' );
    143     $details->siteurl       = get_blog_option( $blog_id, 'siteurl' );
    144     $details->post_count    = get_blog_option( $blog_id, 'post_count' );
    145 
    146     $details = apply_filters( 'blog_details', $details );
    147 
    148     wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    149 
    150     $key = md5( $details->domain . $details->path );
    151     wp_cache_set( $key, $details, 'blog-lookup' );
    152 
    153     return $details;
    154 }
    155 
    156 /**
    157  * Clear the blog details cache.
    158  *
    159  * @since 3.0
    160  *
    161  * @param int $blog_id Blog ID
    162  */
    163 function refresh_blog_details( $blog_id ) {
    164     $blog_id = (int) $blog_id;
    165     $details = get_blog_details( $blog_id, false );
    166 
    167     wp_cache_delete( $blog_id , 'blog-details' );
    168     wp_cache_delete( $blog_id . 'short' , 'blog-details' );
    169     wp_cache_delete( md5( $details->domain . $details->path )  , 'blog-lookup' );
    170     wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
    171     wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
    172 }
    173 
    174 /**
    175  * Update the details for a blog. Updates the blogs table for a given blog id.
    176  *
    177  * @since 3.0
    178  *
    179  * @param int $blog_id Blog ID
    180  * @param array $details Array of details keyed by blogs table field names.
    181  * @return bool True if update succeeds, false otherwise.
    182  */
    183 function update_blog_details( $blog_id, $details = array() ) {
    184     global $wpdb;
    185 
    186     if ( empty($details) )
    187         return false;
    188 
    189     if ( is_object($details) )
    190         $details = get_object_vars($details);
    191 
    192     $current_details = get_blog_details($blog_id, false);
    193     if ( empty($current_details) )
    194         return false;
    195 
    196     $current_details = get_object_vars($current_details);
    197 
    198     $details = array_merge($current_details, $details);
    199     $details['last_updated'] = current_time('mysql', true);
    200 
    201     $update_details = array();
    202     $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
    203     foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
    204         $update_details[$field] = $details[$field];
    205 
    206     $result = $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
    207 
    208     // If spam status changed, issue actions.
    209     if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
    210         if ( $details[ 'spam' ] == 1 )
    211             do_action( "make_spam_blog", $blog_id );
    212         else
    213             do_action( "make_ham_blog", $blog_id );
    214     }
    215 
    216     if ( isset($details[ 'public' ]) )
    217         update_blog_option( $blog_id, 'blog_public', $details[ 'public' ], false );
    218 
    219     refresh_blog_details($blog_id);
    220 
    221     return true;
    222 }
    223 
    224 /**
    225  * Retrieve option value based on setting name and blog_id.
    226  *
    227  * If the option does not exist or does not have a value, then the return value
    228  * will be false. This is useful to check whether you need to install an option
    229  * and is commonly used during installation of plugin options and to test
    230  * whether upgrading is required.
    231  *
    232  * There is a filter called 'blog_option_$option' with the $option being
    233  * replaced with the option name. The filter takes two parameters. $value and
    234  * $blog_id. It returns $value.
    235  * The 'option_$option' filter in get_option() is not called.
    236  *
    237  * @since NA
    238  * @package WordPress MU
    239  * @subpackage Option
    240  * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
    241  *
    242  * @param int $blog_id is the id of the blog.
    243  * @param string $setting Name of option to retrieve. Should already be SQL-escaped
    244  * @param string $default (optional) Default value returned if option not found.
    245  * @return mixed Value set for the option.
    246  */
    247 function get_blog_option( $blog_id, $setting, $default = false ) {
    248     global $wpdb;
    249 
    250     $key = $blog_id."-".$setting."-blog_option";
    251     $value = wp_cache_get( $key, "site-options" );
    252     if ( $value == null ) {
    253         if ( $blog_id == $wpdb->blogid ) {
    254             $value = get_option( $setting, $default );
    255             $notoptions = wp_cache_get( 'notoptions', 'options' );
    256             if ( isset( $notoptions[$setting] ) )
    257                 wp_cache_set( $key, 'noop', 'site-options' );
    258             elseif ( $value == false )
    259                 wp_cache_set( $key, 'falsevalue', 'site-options' );
    260             else
    261                 wp_cache_set( $key, $value, 'site-options' );
    262         } else {
    263             $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
    264             $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
    265             if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
    266                 $value = $row->option_value;
    267                 if ( $value == false )
    268                     wp_cache_set( $key, 'falsevalue', 'site-options' );
    269                 else
    270                     wp_cache_set( $key, $value, 'site-options' );
    271             } else { // option does not exist, so we must cache its non-existence
    272                 wp_cache_set( $key, 'noop', 'site-options' );
    273                 $value = $default;
    274             }
    275         }
    276     } elseif ( $value == 'noop' ) {
    277         $value = $default;
    278     } elseif ( $value == 'falsevalue' ) {
    279         $value = false;
    280     }
    281     // If home is not set use siteurl.
    282     if ( 'home' == $setting && '' == $value )
    283         return get_blog_option( $blog_id, 'siteurl' );
    284 
    285     if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
    286         $value = untrailingslashit( $value );
    287 
    288     if (! @unserialize( $value ) )
    289         $value = stripslashes( $value );
    290 
    291     return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
    292 }
    293 
    294 function add_blog_option( $id, $key, $value ) {
    295     $id = (int) $id;
    296 
    297     switch_to_blog($id);
    298     add_option( $key, $value );
    299     restore_current_blog();
    300     wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
    301 }
    302 
    303 function delete_blog_option( $id, $key ) {
    304     $id = (int) $id;
    305 
    306     switch_to_blog($id);
    307     delete_option( $key );
    308     restore_current_blog();
    309     wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
    310 }
    311 
    312 function update_blog_option( $id, $key, $value, $refresh = true ) {
    313     $id = (int) $id;
    314 
    315     switch_to_blog($id);
    316     update_option( $key, $value );
    317     restore_current_blog();
    318 
    319     if ( $refresh == true )
    320         refresh_blog_details( $id );
    321     wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
    322 }
    323 
    324 function switch_to_blog( $new_blog, $validate = false ) {
    325     global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
    326 
    327     if ( empty($new_blog) )
    328         $new_blog = $blog_id;
    329 
    330     if ( $validate && ! get_blog_details( $new_blog ) )
    331         return false;
    332 
    333     if ( empty($switched_stack) )
    334         $switched_stack = array();
    335 
    336     $switched_stack[] = $blog_id;
    337 
    338     /* If we're switching to the same blog id that we're on,
    339     * set the right vars, do the associated actions, but skip
    340     * the extra unnecessary work */
    341     if ( $blog_id == $new_blog ) {
    342         do_action( 'switch_blog', $blog_id, $blog_id );
    343         $switched = true;
    344         return true;
    345     }
    346 
    347     $wpdb->set_blog_id($new_blog);
    348     $table_prefix = $wpdb->prefix;
    349     $prev_blog_id = $blog_id;
    350     $blog_id = $new_blog;
    351 
    352     if ( is_object( $wp_roles ) ) {
    353         $wpdb->suppress_errors();
    354         if ( method_exists( $wp_roles ,'_init' ) )
    355             $wp_roles->_init();
    356         elseif ( method_exists( $wp_roles, '__construct' ) )
    357             $wp_roles->__construct();
    358         $wpdb->suppress_errors( false );
    359     }
    360 
    361     if ( is_object( $current_user ) )
    362         $current_user->for_blog( $blog_id );
    363 
    364     if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
    365         $global_groups = $wp_object_cache->global_groups;
    366     else
    367         $global_groups = false;
    368 
    369     wp_cache_init();
    370     if ( function_exists('wp_cache_add_global_groups') ) {
    371         if ( is_array( $global_groups ) )
    372             wp_cache_add_global_groups( $global_groups );
    373         else
    374             wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient', 'global-posts' ) );
    375         wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
    376     }
    377 
    378     do_action('switch_blog', $blog_id, $prev_blog_id);
    379     $switched = true;
    380     return true;
    381 }
    382 
    383 function restore_current_blog() {
    384     global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
    385 
    386     if ( !$switched )
    387         return false;
    388 
    389     if ( !is_array( $switched_stack ) )
    390         return false;
    391 
    392     $blog = array_pop( $switched_stack );
    393     if ( $blog_id == $blog ) {
    394         do_action( 'switch_blog', $blog, $blog );
    395         /* If we still have items in the switched stack, consider ourselves still 'switched' */
    396         $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
    397         return true;
    398     }
    399 
    400     $wpdb->set_blog_id($blog);
    401     $prev_blog_id = $blog_id;
    402     $blog_id = $blog;
    403     $table_prefix = $wpdb->prefix;
    404 
    405     if ( is_object( $wp_roles ) ) {
    406         $wpdb->suppress_errors();
    407         if ( method_exists( $wp_roles ,'_init' ) )
    408             $wp_roles->_init();
    409         elseif ( method_exists( $wp_roles, '__construct' ) )
    410             $wp_roles->__construct();
    411         $wpdb->suppress_errors( false );
    412     }
    413 
    414     if ( is_object( $current_user ) )
    415         $current_user->for_blog( $blog_id );
    416 
    417     if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
    418         $global_groups = $wp_object_cache->global_groups;
    419     else
    420         $global_groups = false;
    421 
    422     wp_cache_init();
    423     if ( function_exists('wp_cache_add_global_groups') ) {
    424         if ( is_array( $global_groups ) )
    425             wp_cache_add_global_groups( $global_groups );
    426         else
    427             wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient' ) );
    428         wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
    429     }
    430 
    431     do_action('switch_blog', $blog_id, $prev_blog_id);
    432 
    433     /* If we still have items in the switched stack, consider ourselves still 'switched' */
    434     $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
    435     return true;
    43637}
    43738
     
    551152}
    552153
    553 function is_archived( $id ) {
    554     return get_blog_status($id, 'archived');
    555 }
    556 
    557 function update_archived( $id, $archived ) {
    558     update_blog_status($id, 'archived', $archived);
    559     return $archived;
    560 }
    561 
    562 /**
    563  * Update a blog details field.
    564  *
    565  * @since 3.0
    566  *
    567  * @param int $blog_id BLog ID
    568  * @param string $pref A field name
    569  * @param string $value Value for $pref
    570  * @param bool $refresh Whether to refresh the blog details cache. Default is true.
    571  */
    572 function update_blog_status( $blog_id, $pref, $value, $refresh = true ) {
    573     global $wpdb;
    574 
    575     if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
    576         return $value;
    577 
    578     $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
    579 
    580     if ( $refresh )
    581         refresh_blog_details($blog_id);
    582 
    583     if ( $pref == 'spam' ) {
    584         if ( $value == 1 )
    585             do_action( "make_spam_blog", $blog_id );
    586         else
    587             do_action( "make_ham_blog", $blog_id );
    588     }
    589 
    590     return $value;
    591 }
    592 
    593 function get_blog_status( $id, $pref ) {
    594     global $wpdb;
    595 
    596     $details = get_blog_details( $id, false );
    597     if ( $details )
    598         return $details->$pref;
    599 
    600     return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
    601 }
    602 
    603 function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
    604     global $wpdb;
    605     return $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
    606 }
    607 
    608154function get_most_active_blogs( $num = 10, $display = true ) {
    609155    $most_active = get_site_option( "most_active" );
     
    647193    }
    648194    return array_slice( $most_active, 0, $num );
    649 }
    650 
    651 function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
    652     global $wpdb;
    653 
    654     $blogs = get_site_option( "blog_list" );
    655     $update = false;
    656     if ( is_array( $blogs ) ) {
    657         if ( ( $blogs['time'] + 60 ) < time() ) { // cache for 60 seconds.
    658             $update = true;
    659         }
    660     } else {
    661         $update = true;
    662     }
    663 
    664     if ( $update == true ) {
    665         unset( $blogs );
    666         $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid), ARRAY_A );
    667 
    668         foreach ( (array) $blogs as $details ) {
    669             $blog_list[ $details['blog_id'] ] = $details;
    670             $blog_list[ $details['blog_id'] ]['postcount'] = $wpdb->get_var( "SELECT COUNT(ID) FROM " . $wpdb->base_prefix . $details['blog_id'] . "_posts WHERE post_status='publish' AND post_type='post'" );
    671         }
    672         unset( $blogs );
    673         $blogs = $blog_list;
    674         update_site_option( "blog_list", $blogs );
    675     }
    676 
    677     if ( false == is_array( $blogs ) )
    678         return array();
    679 
    680     if ( $num == 'all' )
    681         return array_slice( $blogs, $start, count( $blogs ) );
    682     else
    683         return array_slice( $blogs, $start, $num );
    684195}
    685196
Note: See TracChangeset for help on using the changeset viewer.