[ Index ]

PHP Cross Reference of WordPress 3.0 beta 1

[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/wp-includes/ -> ms-blogs.php (source)

   1  <?php
   2  
   3  /**
   4   * Site/blog functions that work with the blogs table and related data.
   5   *
   6   * @package WordPress
   7   * @subpackage Multisite
   8   * @since 3.0.0
   9   */
  10  
  11  // @todo use update_blog_details
  12  function wpmu_update_blogs_date() {
  13      global $wpdb;
  14  
  15      $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
  16      refresh_blog_details( $wpdb->blogid );
  17  
  18      do_action( 'wpmu_blog_updated', $wpdb->blogid );
  19  }
  20  
  21  function get_blogaddress_by_id( $blog_id ) {
  22      $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
  23      return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
  24  }
  25  
  26  function get_blogaddress_by_name( $blogname ) {
  27      global $current_site;
  28  
  29      if ( is_subdomain_install() ) {
  30          if ( $blogname == 'main' )
  31              $blogname = 'www';
  32          return esc_url( 'http://' . $blogname . '.' . $current_site->domain . $current_site->path );
  33      } else {
  34          return esc_url( 'http://' . $current_site->domain . $current_site->path . $blogname . '/' );
  35      }
  36  }
  37  
  38  function get_blogaddress_by_domain( $domain, $path ){
  39      if ( is_subdomain_install() ) {
  40          $url = "http://".$domain.$path;
  41      } else {
  42          if ( $domain != $_SERVER['HTTP_HOST'] ) {
  43              $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
  44              $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
  45              // we're not installing the main blog
  46              if ( $blogname != 'www.' )
  47                  $url .= $blogname . '/';
  48          } else { // main blog
  49              $url = 'http://' . $domain . $path;
  50          }
  51      }
  52      return esc_url( $url );
  53  }
  54  
  55  function get_id_from_blogname( $name ) {
  56      global $wpdb, $current_site;
  57      $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
  58      if ( $blog_id )
  59          return $blog_id;
  60  
  61      if ( is_subdomain_install() ) {
  62          $domain = $name . '.' . $current_site->domain;
  63          $path = $current_site->path;
  64      } else {
  65          $domain = $current_site->domain;
  66          $path = $current_site->path . $name . '/';
  67      }
  68      $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
  69      wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
  70      return $blog_id;
  71  }
  72  
  73  /**
  74   * Retrieve the details for a blog from the blogs table and blog options.
  75   *
  76   * @since 3.0.0
  77   * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against.
  78   * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
  79   * @return object Blog details.
  80   */
  81  function get_blog_details( $fields, $get_all = true ) {
  82      global $wpdb;
  83  
  84      if ( is_array($fields ) ) {
  85          if ( isset($fields['blog_id']) ) {
  86              $blog_id = $fields['blog_id'];
  87          } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
  88              $key = md5( $fields['domain'] . $fields['path'] );
  89              $blog = wp_cache_get($key, 'blog-lookup');
  90              if ( false !== $blog )
  91                  return $blog;
  92              $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
  93              if ( $blog ) {
  94                  wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  95                  $blog_id = $blog->blog_id;
  96              } else {
  97                  return false;
  98              }
  99          } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
 100              $key = md5( $fields['domain'] );
 101              $blog = wp_cache_get($key, 'blog-lookup');
 102              if ( false !== $blog )
 103                  return $blog;
 104              $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
 105              if ( $blog ) {
 106                  wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
 107                  $blog_id = $blog->blog_id;
 108              } else {
 109                  return false;
 110              }
 111          } else {
 112              return false;
 113          }
 114      } else {
 115          if ( !is_numeric( $fields ) )
 116              $blog_id = get_id_from_blogname( $fields );
 117          else
 118              $blog_id = $fields;
 119      }
 120  
 121      $blog_id = (int) $blog_id;
 122  
 123      $all = $get_all == true ? '' : 'short';
 124      $details = wp_cache_get( $blog_id . $all, 'blog-details' );
 125  
 126      if ( $details ) {
 127          if ( ! is_object( $details ) ) {
 128              if ( $details == -1 )
 129                  return false;
 130              else
 131                  // Clear old pre-serialized objects. Cache clients do better with that.
 132                  wp_cache_delete( $blog_id . $all, 'blog-details' );
 133          }
 134          return $details;
 135      }
 136  
 137      // Try the other cache.
 138      if ( $get_all ) {
 139          $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
 140      } else {
 141          $details = wp_cache_get( $blog_id, 'blog-details' );
 142          // If short was requested and full cache is set, we can return.
 143          if ( $details ) {
 144              if ( ! is_object( $details ) ) {
 145                  if ( $details == -1 )
 146                      return false;
 147                  else
 148                      // Clear old pre-serialized objects. Cache clients do better with that.
 149                      wp_cache_delete( $blog_id . $all, 'blog-details' );
 150              }
 151              return $details;
 152          }
 153      }
 154  
 155      if ( !$details ) {
 156          $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d", $blog_id ) );
 157          if ( ! $details ) {
 158              // Set the full cache.
 159              wp_cache_set( $blog_id, -1, 'blog-details' );
 160              return false;
 161          }
 162      }
 163  
 164      if ( ! $get_all ) {
 165          wp_cache_set( $blog_id . $all, $details, 'blog-details' );
 166          return $details;
 167      }
 168  
 169      $details->blogname        = get_blog_option( $blog_id, 'blogname' );
 170      $details->siteurl        = get_blog_option( $blog_id, 'siteurl' );
 171      $details->post_count    = get_blog_option( $blog_id, 'post_count' );
 172  
 173      $details = apply_filters( 'blog_details', $details );
 174  
 175      wp_cache_set( $blog_id . $all, $details, 'blog-details' );
 176  
 177      $key = md5( $details->domain . $details->path );
 178      wp_cache_set( $key, $details, 'blog-lookup' );
 179  
 180      return $details;
 181  }
 182  
 183  /**
 184   * Clear the blog details cache.
 185   *
 186   * @since 3.0.0
 187   *
 188   * @param int $blog_id Blog ID
 189   */
 190  function refresh_blog_details( $blog_id ) {
 191      $blog_id = (int) $blog_id;
 192      $details = get_blog_details( $blog_id, false );
 193  
 194      wp_cache_delete( $blog_id , 'blog-details' );
 195      wp_cache_delete( $blog_id . 'short' , 'blog-details' );
 196      wp_cache_delete( md5( $details->domain . $details->path )  , 'blog-lookup' );
 197      wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
 198      wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
 199  }
 200  
 201  /**
 202   * Update the details for a blog. Updates the blogs table for a given blog id.
 203   *
 204   * @since 3.0.0
 205   *
 206   * @param int $blog_id Blog ID
 207   * @param array $details Array of details keyed by blogs table field names.
 208   * @return bool True if update succeeds, false otherwise.
 209   */
 210  function update_blog_details( $blog_id, $details = array() ) {
 211      global $wpdb;
 212  
 213      if ( empty($details) )
 214          return false;
 215  
 216      if ( is_object($details) )
 217          $details = get_object_vars($details);
 218  
 219      $current_details = get_blog_details($blog_id, false);
 220      if ( empty($current_details) )
 221          return false;
 222  
 223      $current_details = get_object_vars($current_details);
 224  
 225      $details = array_merge($current_details, $details);
 226      $details['last_updated'] = current_time('mysql', true);
 227  
 228      $update_details = array();
 229      $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
 230      foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
 231          $update_details[$field] = $details[$field];
 232  
 233      $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
 234  
 235      // If spam status changed, issue actions.
 236      if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
 237          if ( $details[ 'spam' ] == 1 )
 238              do_action( "make_spam_blog", $blog_id );
 239          else
 240              do_action( "make_ham_blog", $blog_id );
 241      }
 242  
 243      if ( isset($details[ 'public' ]) )
 244          update_blog_option( $blog_id, 'blog_public', $details[ 'public' ], false );
 245  
 246      refresh_blog_details($blog_id);
 247  
 248      return true;
 249  }
 250  
 251  /**
 252   * Retrieve option value based on setting name and blog_id.
 253   *
 254   * If the option does not exist or does not have a value, then the return value
 255   * will be false. This is useful to check whether you need to install an option
 256   * and is commonly used during installation of plugin options and to test
 257   * whether upgrading is required.
 258   *
 259   * There is a filter called 'blog_option_$option' with the $option being
 260   * replaced with the option name. The filter takes two parameters. $value and
 261   * $blog_id. It returns $value.
 262   * The 'option_$option' filter in get_option() is not called.
 263   *
 264   * @since NA
 265   * @package WordPress MU
 266   * @subpackage Option
 267   * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
 268   *
 269   * @param int $blog_id is the id of the blog.
 270   * @param string $setting Name of option to retrieve. Should already be SQL-escaped
 271   * @param string $default (optional) Default value returned if option not found.
 272   * @return mixed Value set for the option.
 273   */
 274  function get_blog_option( $blog_id, $setting, $default = false ) {
 275      global $wpdb;
 276  
 277      $key = $blog_id."-".$setting."-blog_option";
 278      $value = wp_cache_get( $key, "site-options" );
 279      if ( $value == null ) {
 280          if ( $blog_id == $wpdb->blogid ) {
 281              $value = get_option( $setting, $default );
 282              $notoptions = wp_cache_get( 'notoptions', 'options' );
 283              if ( isset( $notoptions[$setting] ) ) {
 284                  wp_cache_set( $key, 'noop', 'site-options' );
 285                  $value = $default;
 286              } elseif ( $value == false ) {
 287                  wp_cache_set( $key, 'falsevalue', 'site-options' );
 288              } else {
 289                  wp_cache_set( $key, $value, 'site-options' );
 290              }
 291              return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
 292          } else {
 293              $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
 294              $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
 295              if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
 296                  $value = $row->option_value;
 297                  if ( $value == false )
 298                      wp_cache_set( $key, 'falsevalue', 'site-options' );
 299                  else
 300                      wp_cache_set( $key, $value, 'site-options' );
 301              } else { // option does not exist, so we must cache its non-existence
 302                  wp_cache_set( $key, 'noop', 'site-options' );
 303                  $value = $default;
 304              }
 305          }
 306      } elseif ( $value == 'noop' ) {
 307          $value = $default;
 308      } elseif ( $value == 'falsevalue' ) {
 309          $value = false;
 310      }
 311      // If home is not set use siteurl.
 312      if ( 'home' == $setting && '' == $value )
 313          return get_blog_option( $blog_id, 'siteurl' );
 314  
 315      if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
 316          $value = untrailingslashit( $value );
 317  
 318      if (! @unserialize( $value ) )
 319          $value = stripslashes( $value );
 320  
 321      return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
 322  }
 323  
 324  function add_blog_option( $id, $key, $value ) {
 325      $id = (int) $id;
 326  
 327      switch_to_blog($id);
 328      add_option( $key, $value );
 329      restore_current_blog();
 330      wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
 331  }
 332  
 333  function delete_blog_option( $id, $key ) {
 334      $id = (int) $id;
 335  
 336      switch_to_blog($id);
 337      delete_option( $key );
 338      restore_current_blog();
 339      wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
 340  }
 341  
 342  function update_blog_option( $id, $key, $value, $refresh = true ) {
 343      $id = (int) $id;
 344  
 345      switch_to_blog($id);
 346      update_option( $key, $value );
 347      restore_current_blog();
 348  
 349      if ( $refresh == true )
 350          refresh_blog_details( $id );
 351      wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
 352  }
 353  
 354  function switch_to_blog( $new_blog, $validate = false ) {
 355      global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
 356  
 357      if ( empty($new_blog) )
 358          $new_blog = $blog_id;
 359  
 360      if ( $validate && ! get_blog_details( $new_blog ) )
 361          return false;
 362  
 363      if ( empty($switched_stack) )
 364          $switched_stack = array();
 365  
 366      $switched_stack[] = $blog_id;
 367  
 368      /* If we're switching to the same blog id that we're on,
 369      * set the right vars, do the associated actions, but skip
 370      * the extra unnecessary work */
 371      if ( $blog_id == $new_blog ) {
 372          do_action( 'switch_blog', $blog_id, $blog_id );
 373          $switched = true;
 374          return true;
 375      }
 376  
 377      $wpdb->set_blog_id($new_blog);
 378      $table_prefix = $wpdb->prefix;
 379      $prev_blog_id = $blog_id;
 380      $blog_id = $new_blog;
 381  
 382      if ( is_object( $wp_roles ) ) {
 383          $wpdb->suppress_errors();
 384          if ( method_exists( $wp_roles ,'_init' ) )
 385              $wp_roles->_init();
 386          elseif ( method_exists( $wp_roles, '__construct' ) )
 387              $wp_roles->__construct();
 388          $wpdb->suppress_errors( false );
 389      }
 390  
 391      if ( is_object( $current_user ) )
 392          $current_user->for_blog( $blog_id );
 393  
 394      if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
 395          $global_groups = $wp_object_cache->global_groups;
 396      else
 397          $global_groups = false;
 398  
 399      wp_cache_init();
 400      if ( function_exists('wp_cache_add_global_groups') ) {
 401          if ( is_array( $global_groups ) )
 402              wp_cache_add_global_groups( $global_groups );
 403          else
 404              wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient', 'global-posts' ) );
 405          wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
 406      }
 407  
 408      do_action('switch_blog', $blog_id, $prev_blog_id);
 409      $switched = true;
 410      return true;
 411  }
 412  
 413  function restore_current_blog() {
 414      global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
 415  
 416      if ( !$switched )
 417          return false;
 418  
 419      if ( !is_array( $switched_stack ) )
 420          return false;
 421  
 422      $blog = array_pop( $switched_stack );
 423      if ( $blog_id == $blog ) {
 424          do_action( 'switch_blog', $blog, $blog );
 425          /* If we still have items in the switched stack, consider ourselves still 'switched' */
 426          $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
 427          return true;
 428      }
 429  
 430      $wpdb->set_blog_id($blog);
 431      $prev_blog_id = $blog_id;
 432      $blog_id = $blog;
 433      $table_prefix = $wpdb->prefix;
 434  
 435      if ( is_object( $wp_roles ) ) {
 436          $wpdb->suppress_errors();
 437          if ( method_exists( $wp_roles ,'_init' ) )
 438              $wp_roles->_init();
 439          elseif ( method_exists( $wp_roles, '__construct' ) )
 440              $wp_roles->__construct();
 441          $wpdb->suppress_errors( false );
 442      }
 443  
 444      if ( is_object( $current_user ) )
 445          $current_user->for_blog( $blog_id );
 446  
 447      if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
 448          $global_groups = $wp_object_cache->global_groups;
 449      else
 450          $global_groups = false;
 451  
 452      wp_cache_init();
 453      if ( function_exists('wp_cache_add_global_groups') ) {
 454          if ( is_array( $global_groups ) )
 455              wp_cache_add_global_groups( $global_groups );
 456          else
 457              wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient' ) );
 458          wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
 459      }
 460  
 461      do_action('switch_blog', $blog_id, $prev_blog_id);
 462  
 463      /* If we still have items in the switched stack, consider ourselves still 'switched' */
 464      $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
 465      return true;
 466  }
 467  
 468  function is_archived( $id ) {
 469      return get_blog_status($id, 'archived');
 470  }
 471  
 472  function update_archived( $id, $archived ) {
 473      update_blog_status($id, 'archived', $archived);
 474      return $archived;
 475  }
 476  
 477  /**
 478   * Update a blog details field.
 479   *
 480   * @since 3.0.0
 481   *
 482   * @param int $blog_id BLog ID
 483   * @param string $pref A field name
 484   * @param string $value Value for $pref
 485   * @param bool $refresh Whether to refresh the blog details cache. Default is true.
 486   */
 487  function update_blog_status( $blog_id, $pref, $value, $refresh = true ) {
 488      global $wpdb;
 489  
 490      if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
 491          return $value;
 492  
 493      $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
 494  
 495      if ( $refresh )
 496          refresh_blog_details($blog_id);
 497  
 498      if ( $pref == 'spam' ) {
 499          if ( $value == 1 )
 500              do_action( "make_spam_blog", $blog_id );
 501          else
 502              do_action( "make_ham_blog", $blog_id );
 503      }
 504  
 505      return $value;
 506  }
 507  
 508  function get_blog_status( $id, $pref ) {
 509      global $wpdb;
 510  
 511      $details = get_blog_details( $id, false );
 512      if ( $details )
 513          return $details->$pref;
 514  
 515      return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
 516  }
 517  
 518  function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
 519      global $wpdb;
 520      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 );
 521  }
 522  
 523  function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
 524      global $wpdb;
 525  
 526      $blogs = get_site_option( "blog_list" );
 527      $update = false;
 528      if ( is_array( $blogs ) ) {
 529          if ( ( $blogs['time'] + 60 ) < time() ) { // cache for 60 seconds.
 530              $update = true;
 531          }
 532      } else {
 533          $update = true;
 534      }
 535  
 536      if ( $update == true ) {
 537          unset( $blogs );
 538          $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 );
 539  
 540          foreach ( (array) $blogs as $details ) {
 541              $blog_list[ $details['blog_id'] ] = $details;
 542              $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'" );
 543          }
 544          unset( $blogs );
 545          $blogs = $blog_list;
 546          update_site_option( "blog_list", $blogs );
 547      }
 548  
 549      if ( false == is_array( $blogs ) )
 550          return array();
 551  
 552      if ( $num == 'all' )
 553          return array_slice( $blogs, $start, count( $blogs ) );
 554      else
 555          return array_slice( $blogs, $start, $num );
 556  }
 557  
 558  ?>


Generated: Mon Apr 5 14:26:09 2010 Cross-referenced by PHPXref 0.7