[ Index ]

WordPress Cross Reference

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Multisite WordPress API
   4   *
   5   * @package WordPress
   6   * @subpackage Multisite
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Gets the network's site and user counts.
  12   *
  13   * @since MU 1.0
  14   * @uses get_blog_count()
  15   * @uses get_user_count()
  16   *
  17   * @return array Site and user count for the network.
  18   */
  19  function get_sitestats() {
  20      $stats = array(
  21          'blogs' => get_blog_count(),
  22          'users' => get_user_count(),
  23      );
  24  
  25      return $stats;
  26  }
  27  
  28  /**
  29   * Get the admin for a domain/path combination.
  30   *
  31   * @since MU 1.0
  32   *
  33   * @param string $sitedomain Optional. Site domain.
  34   * @param string $path Optional. Site path.
  35   * @return array The network admins
  36   */
  37  function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  38      global $wpdb;
  39  
  40      if ( ! $sitedomain )
  41          $site_id = $wpdb->siteid;
  42      else
  43          $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path ) );
  44  
  45      if ( $site_id )
  46          return $wpdb->get_results( $wpdb->prepare( "SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = sm.meta_value AND sm.site_id = %d", $site_id ), ARRAY_A );
  47  
  48      return false;
  49  }
  50  
  51  /**
  52   * Get one of a user's active blogs
  53   *
  54   * Returns the user's primary blog, if they have one and
  55   * it is active. If it's inactive, function returns another
  56   * active blog of the user. If none are found, the user
  57   * is added as a Subscriber to the Dashboard Blog and that blog
  58   * is returned.
  59   *
  60   * @since MU 1.0
  61   * @uses get_blogs_of_user()
  62   * @uses add_user_to_blog()
  63   * @uses get_blog_details()
  64   *
  65   * @param int $user_id The unique ID of the user
  66   * @return object The blog object
  67   */
  68  function get_active_blog_for_user( $user_id ) {
  69      global $wpdb;
  70      $blogs = get_blogs_of_user( $user_id );
  71      if ( empty( $blogs ) )
  72          return null;
  73  
  74      if ( !is_multisite() )
  75          return $blogs[$wpdb->blogid];
  76  
  77      $primary_blog = get_user_meta( $user_id, 'primary_blog', true );
  78      $first_blog = current($blogs);
  79      if ( false !== $primary_blog ) {
  80          if ( ! isset( $blogs[ $primary_blog ] ) ) {
  81              update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  82              $primary = get_blog_details( $first_blog->userblog_id );
  83          } else {
  84              $primary = get_blog_details( $primary_blog );
  85          }
  86      } else {
  87          //TODO Review this call to add_user_to_blog too - to get here the user must have a role on this blog?
  88          add_user_to_blog( $first_blog->userblog_id, $user_id, 'subscriber' );
  89          update_user_meta( $user_id, 'primary_blog', $first_blog->userblog_id );
  90          $primary = $first_blog;
  91      }
  92  
  93      if ( ( ! is_object( $primary ) ) || ( $primary->archived == 1 || $primary->spam == 1 || $primary->deleted == 1 ) ) {
  94          $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  95          $ret = false;
  96          if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  97              foreach ( (array) $blogs as $blog_id => $blog ) {
  98                  if ( $blog->site_id != $wpdb->siteid )
  99                      continue;
 100                  $details = get_blog_details( $blog_id );
 101                  if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
 102                      $ret = $blog;
 103                      if ( get_user_meta( $user_id , 'primary_blog', true ) != $blog_id )
 104                          update_user_meta( $user_id, 'primary_blog', $blog_id );
 105                      if ( !get_user_meta($user_id , 'source_domain', true) )
 106                          update_user_meta( $user_id, 'source_domain', $blog->domain );
 107                      break;
 108                  }
 109              }
 110          } else {
 111              return null;
 112          }
 113          return $ret;
 114      } else {
 115          return $primary;
 116      }
 117  }
 118  
 119  /**
 120   * The number of active users in your installation.
 121   *
 122   * The count is cached and updated twice daily. This is not a live count.
 123   *
 124   * @since MU 2.7
 125   *
 126   * @return int
 127   */
 128  function get_user_count() {
 129      return get_site_option( 'user_count' );
 130  }
 131  
 132  /**
 133   * The number of active sites on your installation.
 134   *
 135   * The count is cached and updated twice daily. This is not a live count.
 136   *
 137   * @since MU 1.0
 138   *
 139   * @param int $network_id Deprecated, not supported.
 140   * @return int
 141   */
 142  function get_blog_count( $network_id = 0 ) {
 143      if ( func_num_args() )
 144          _deprecated_argument( __FUNCTION__, '3.1' );
 145  
 146      return get_site_option( 'blog_count' );
 147  }
 148  
 149  /**
 150   * Get a blog post from any site on the network.
 151   *
 152   * @since MU 1.0
 153   *
 154   * @param int $blog_id ID of the blog.
 155   * @param int $post_id ID of the post you're looking for.
 156   * @return WP_Post|null WP_Post on success or null on failure
 157   */
 158  function get_blog_post( $blog_id, $post_id ) {
 159      switch_to_blog( $blog_id );
 160      $post = get_post( $post_id );
 161      restore_current_blog();
 162  
 163      return $post;
 164  }
 165  
 166  /**
 167   * Add a user to a blog.
 168   *
 169   * Use the 'add_user_to_blog' action to fire an event when
 170   * users are added to a blog.
 171   *
 172   * @since MU 1.0
 173   *
 174   * @param int $blog_id ID of the blog you're adding the user to.
 175   * @param int $user_id ID of the user you're adding.
 176   * @param string $role The role you want the user to have
 177   * @return bool
 178   */
 179  function add_user_to_blog( $blog_id, $user_id, $role ) {
 180      switch_to_blog($blog_id);
 181  
 182      $user = get_userdata( $user_id );
 183  
 184      if ( ! $user ) {
 185          restore_current_blog();
 186          return new WP_Error( 'user_does_not_exist', __( 'The requested user does not exist.' ) );
 187      }
 188  
 189      if ( !get_user_meta($user_id, 'primary_blog', true) ) {
 190          update_user_meta($user_id, 'primary_blog', $blog_id);
 191          $details = get_blog_details($blog_id);
 192          update_user_meta($user_id, 'source_domain', $details->domain);
 193      }
 194  
 195      $user->set_role($role);
 196  
 197      /**
 198       * Fires immediately after a user is added to a site.
 199       *
 200       * @since MU
 201       *
 202       * @param int    $user_id User ID.
 203       * @param string $role    User role.
 204       * @param int    $blog_id Blog ID.
 205       */
 206      do_action( 'add_user_to_blog', $user_id, $role, $blog_id );
 207      wp_cache_delete( $user_id, 'users' );
 208      restore_current_blog();
 209      return true;
 210  }
 211  
 212  /**
 213   * Remove a user from a blog.
 214   *
 215   * Use the 'remove_user_from_blog' action to fire an event when
 216   * users are removed from a blog.
 217   *
 218   * Accepts an optional $reassign parameter, if you want to
 219   * reassign the user's blog posts to another user upon removal.
 220   *
 221   * @since MU 1.0
 222   *
 223   * @param int $user_id ID of the user you're removing.
 224   * @param int $blog_id ID of the blog you're removing the user from.
 225   * @param string $reassign Optional. A user to whom to reassign posts.
 226   * @return bool
 227   */
 228  function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
 229      global $wpdb;
 230      switch_to_blog($blog_id);
 231      $user_id = (int) $user_id;
 232      /**
 233       * Fires before a user is removed from a site.
 234       *
 235       * @since MU
 236       *
 237       * @param int $user_id User ID.
 238       * @param int $blog_id Blog ID.
 239       */
 240      do_action( 'remove_user_from_blog', $user_id, $blog_id );
 241  
 242      // If being removed from the primary blog, set a new primary if the user is assigned
 243      // to multiple blogs.
 244      $primary_blog = get_user_meta($user_id, 'primary_blog', true);
 245      if ( $primary_blog == $blog_id ) {
 246          $new_id = '';
 247          $new_domain = '';
 248          $blogs = get_blogs_of_user($user_id);
 249          foreach ( (array) $blogs as $blog ) {
 250              if ( $blog->userblog_id == $blog_id )
 251                  continue;
 252              $new_id = $blog->userblog_id;
 253              $new_domain = $blog->domain;
 254              break;
 255          }
 256  
 257          update_user_meta($user_id, 'primary_blog', $new_id);
 258          update_user_meta($user_id, 'source_domain', $new_domain);
 259      }
 260  
 261      // wp_revoke_user($user_id);
 262      $user = get_userdata( $user_id );
 263      if ( ! $user ) {
 264          restore_current_blog();
 265          return new WP_Error('user_does_not_exist', __('That user does not exist.'));
 266      }
 267  
 268      $user->remove_all_caps();
 269  
 270      $blogs = get_blogs_of_user($user_id);
 271      if ( count($blogs) == 0 ) {
 272          update_user_meta($user_id, 'primary_blog', '');
 273          update_user_meta($user_id, 'source_domain', '');
 274      }
 275  
 276      if ( $reassign != '' ) {
 277          $reassign = (int) $reassign;
 278          $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
 279          $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
 280      }
 281  
 282      restore_current_blog();
 283  
 284      return true;
 285  }
 286  
 287  /**
 288   * Create an empty blog.
 289   *
 290   * @since MU 1.0
 291   * @uses install_blog()
 292   *
 293   * @param string $domain The new blog's domain.
 294   * @param string $path The new blog's path.
 295   * @param string $weblog_title The new blog's title.
 296   * @param int $site_id Optional. Defaults to 1.
 297   * @return int The ID of the newly created blog
 298   */
 299  function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
 300      if ( empty($path) )
 301          $path = '/';
 302  
 303      // Check if the domain has been used already. We should return an error message.
 304      if ( domain_exists($domain, $path, $site_id) )
 305          return __( '<strong>ERROR</strong>: Site URL already taken.' );
 306  
 307      // Need to back up wpdb table names, and create a new wp_blogs entry for new blog.
 308      // Need to get blog_id from wp_blogs, and create new table names.
 309      // Must restore table names at the end of function.
 310  
 311      if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
 312          return __( '<strong>ERROR</strong>: problem creating site entry.' );
 313  
 314      switch_to_blog($blog_id);
 315      install_blog($blog_id);
 316      restore_current_blog();
 317  
 318      return $blog_id;
 319  }
 320  
 321  /**
 322   * Get the permalink for a post on another blog.
 323   *
 324   * @since MU 1.0
 325   *
 326   * @param int $blog_id ID of the source blog.
 327   * @param int $post_id ID of the desired post.
 328   * @return string The post's permalink
 329   */
 330  function get_blog_permalink( $blog_id, $post_id ) {
 331      switch_to_blog( $blog_id );
 332      $link = get_permalink( $post_id );
 333      restore_current_blog();
 334  
 335      return $link;
 336  }
 337  
 338  /**
 339   * Get a blog's numeric ID from its URL.
 340   *
 341   * On a subdirectory installation like example.com/blog1/,
 342   * $domain will be the root 'example.com' and $path the
 343   * subdirectory '/blog1/'. With subdomains like blog1.example.com,
 344   * $domain is 'blog1.example.com' and $path is '/'.
 345   *
 346   * @since MU 2.6.5
 347   *
 348   * @param string $domain
 349   * @param string $path Optional. Not required for subdomain installations.
 350   * @return int 0 if no blog found, otherwise the ID of the matching blog
 351   */
 352  function get_blog_id_from_url( $domain, $path = '/' ) {
 353      global $wpdb;
 354  
 355      $domain = strtolower( $domain );
 356      $path = strtolower( $path );
 357      $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
 358  
 359      if ( $id == -1 ) // blog does not exist
 360          return 0;
 361      elseif ( $id )
 362          return (int) $id;
 363  
 364      $id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s and path = %s /* get_blog_id_from_url */", $domain, $path ) );
 365  
 366      if ( ! $id ) {
 367          wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
 368          return 0;
 369      }
 370  
 371      wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
 372  
 373      return $id;
 374  }
 375  
 376  // Admin functions
 377  
 378  /**
 379   * Checks an email address against a list of banned domains.
 380   *
 381   * This function checks against the Banned Email Domains list
 382   * at wp-admin/network/settings.php. The check is only run on
 383   * self-registrations; user creation at wp-admin/network/users.php
 384   * bypasses this check.
 385   *
 386   * @since MU
 387   *
 388   * @param string $user_email The email provided by the user at registration.
 389   * @return bool Returns true when the email address is banned.
 390   */
 391  function is_email_address_unsafe( $user_email ) {
 392      $banned_names = get_site_option( 'banned_email_domains' );
 393      if ( $banned_names && ! is_array( $banned_names ) )
 394          $banned_names = explode( "\n", $banned_names );
 395  
 396      $is_email_address_unsafe = false;
 397  
 398      if ( $banned_names && is_array( $banned_names ) ) {
 399          $banned_names = array_map( 'strtolower', $banned_names );
 400          $normalized_email = strtolower( $user_email );
 401  
 402          list( $email_local_part, $email_domain ) = explode( '@', $normalized_email );
 403  
 404          foreach ( $banned_names as $banned_domain ) {
 405              if ( ! $banned_domain )
 406                  continue;
 407  
 408              if ( $email_domain == $banned_domain ) {
 409                  $is_email_address_unsafe = true;
 410                  break;
 411              }
 412  
 413              $dotted_domain = ".$banned_domain";
 414              if ( $dotted_domain === substr( $normalized_email, -strlen( $dotted_domain ) ) ) {
 415                  $is_email_address_unsafe = true;
 416                  break;
 417              }
 418          }
 419      }
 420  
 421      /**
 422       * Filter whether an email address is unsafe.
 423       *
 424       * @since 3.5.0
 425       *
 426       * @param bool   $is_email_address_unsafe Whether the email address is "unsafe". Default false.
 427       * @param string $user_email              User email address.
 428       */
 429      return apply_filters( 'is_email_address_unsafe', $is_email_address_unsafe, $user_email );
 430  }
 431  
 432  /**
 433   * Processes new user registrations.
 434   *
 435   * Checks the data provided by the user during signup. Verifies
 436   * the validity and uniqueness of user names and user email addresses,
 437   * and checks email addresses against admin-provided domain
 438   * whitelists and blacklists.
 439   *
 440   * The hook 'wpmu_validate_user_signup' provides an easy way
 441   * to modify the signup process. The value $result, which is passed
 442   * to the hook, contains both the user-provided info and the error
 443   * messages created by the function. 'wpmu_validate_user_signup' allows
 444   * you to process the data in any way you'd like, and unset the
 445   * relevant errors if necessary.
 446   *
 447   * @since MU
 448   * @uses is_email_address_unsafe()
 449   * @uses username_exists()
 450   * @uses email_exists()
 451   *
 452   * @param string $user_name The login name provided by the user.
 453   * @param string $user_email The email provided by the user.
 454   * @return array Contains username, email, and error messages.
 455   */
 456  function wpmu_validate_user_signup($user_name, $user_email) {
 457      global $wpdb;
 458  
 459      $errors = new WP_Error();
 460  
 461      $orig_username = $user_name;
 462      $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
 463  
 464      if ( $user_name != $orig_username || preg_match( '/[^a-z0-9]/', $user_name ) ) {
 465          $errors->add( 'user_name', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
 466          $user_name = $orig_username;
 467      }
 468  
 469      $user_email = sanitize_email( $user_email );
 470  
 471      if ( empty( $user_name ) )
 472             $errors->add('user_name', __( 'Please enter a username.' ) );
 473  
 474      $illegal_names = get_site_option( 'illegal_names' );
 475      if ( is_array( $illegal_names ) == false ) {
 476          $illegal_names = array(  'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
 477          add_site_option( 'illegal_names', $illegal_names );
 478      }
 479      if ( in_array( $user_name, $illegal_names ) == true )
 480          $errors->add('user_name',  __( 'That username is not allowed.' ) );
 481  
 482      if ( is_email_address_unsafe( $user_email ) )
 483          $errors->add('user_email',  __('You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider.'));
 484  
 485      if ( strlen( $user_name ) < 4 )
 486          $errors->add('user_name',  __( 'Username must be at least 4 characters.' ) );
 487  
 488      if ( strpos( ' ' . $user_name, '_' ) != false )
 489          $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character &#8220;_&#8221;!' ) );
 490  
 491      // all numeric?
 492      if ( preg_match( '/^[0-9]*$/', $user_name ) )
 493          $errors->add('user_name', __('Sorry, usernames must have letters too!'));
 494  
 495      if ( !is_email( $user_email ) )
 496          $errors->add('user_email', __( 'Please enter a valid email address.' ) );
 497  
 498      $limited_email_domains = get_site_option( 'limited_email_domains' );
 499      if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
 500          $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
 501          if ( in_array( $emaildomain, $limited_email_domains ) == false )
 502              $errors->add('user_email', __('Sorry, that email address is not allowed!'));
 503      }
 504  
 505      // Check if the username has been used already.
 506      if ( username_exists($user_name) )
 507          $errors->add( 'user_name', __( 'Sorry, that username already exists!' ) );
 508  
 509      // Check if the email address has been used already.
 510      if ( email_exists($user_email) )
 511          $errors->add( 'user_email', __( 'Sorry, that email address is already used!' ) );
 512  
 513      // Has someone already signed up for this username?
 514      $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
 515      if ( $signup != null ) {
 516          $registered_at =  mysql2date('U', $signup->registered);
 517          $now = current_time( 'timestamp', true );
 518          $diff = $now - $registered_at;
 519          // If registered more than two days ago, cancel registration and let this signup go through.
 520          if ( $diff > 2 * DAY_IN_SECONDS )
 521              $wpdb->delete( $wpdb->signups, array( 'user_login' => $user_name ) );
 522          else
 523              $errors->add('user_name', __('That username is currently reserved but may be available in a couple of days.'));
 524      }
 525  
 526      $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
 527      if ( $signup != null ) {
 528          $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
 529          // If registered more than two days ago, cancel registration and let this signup go through.
 530          if ( $diff > 2 * DAY_IN_SECONDS )
 531              $wpdb->delete( $wpdb->signups, array( 'user_email' => $user_email ) );
 532          else
 533              $errors->add('user_email', __('That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing.'));
 534      }
 535  
 536      $result = array('user_name' => $user_name, 'orig_username' => $orig_username, 'user_email' => $user_email, 'errors' => $errors);
 537  
 538      /**
 539       * Filter the validated user registration details.
 540       *
 541       * This does not allow you to override the username or email of the user during
 542       * registration. The values are solely used for validation and error handling.
 543       *
 544       * @since MU
 545       *
 546       * @param array $result {
 547       *     The array of user name, email and the error messages.
 548       *
 549       *     @type string   $user_name     Sanitized and unique username.
 550       *     @type string   $orig_username Original username.
 551       *     @type string   $user_email    User email address.
 552       *     @type WP_Error $errors        WP_Error object containing any errors found.
 553       * }
 554       */
 555      return apply_filters( 'wpmu_validate_user_signup', $result );
 556  }
 557  
 558  /**
 559   * Processes new site registrations.
 560   *
 561   * Checks the data provided by the user during blog signup. Verifies
 562   * the validity and uniqueness of blog paths and domains.
 563   *
 564   * This function prevents the current user from registering a new site
 565   * with a blogname equivalent to another user's login name. Passing the
 566   * $user parameter to the function, where $user is the other user, is
 567   * effectively an override of this limitation.
 568   *
 569   * Filter 'wpmu_validate_blog_signup' if you want to modify
 570   * the way that WordPress validates new site signups.
 571   *
 572   * @since MU
 573   * @uses domain_exists()
 574   * @uses username_exists()
 575   *
 576   * @param string $blogname The blog name provided by the user. Must be unique.
 577   * @param string $blog_title The blog title provided by the user.
 578   * @return array Contains the new site data and error messages.
 579   */
 580  function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
 581      global $wpdb, $domain;
 582  
 583      $current_site = get_current_site();
 584      $base = $current_site->path;
 585  
 586      $blog_title = strip_tags( $blog_title );
 587      $blog_title = substr( $blog_title, 0, 50 );
 588  
 589      $errors = new WP_Error();
 590      $illegal_names = get_site_option( 'illegal_names' );
 591      if ( $illegal_names == false ) {
 592          $illegal_names = array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' );
 593          add_site_option( 'illegal_names', $illegal_names );
 594      }
 595  
 596      /*
 597       * On sub dir installs, some names are so illegal, only a filter can
 598       * spring them from jail.
 599       */
 600      if ( ! is_subdomain_install() ) {
 601          $illegal_names = array_merge(
 602              $illegal_names,
 603              /**
 604               * Filter reserved site names on a sub-directory Multisite install.
 605               *
 606               * @since 3.0.0
 607               *
 608               * @param array $subdirectory_reserved_names Array of reserved names.
 609               */
 610              apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) )
 611          );
 612      }
 613  
 614      if ( empty( $blogname ) )
 615          $errors->add('blogname', __( 'Please enter a site name.' ) );
 616  
 617      if ( preg_match( '/[^a-z0-9]+/', $blogname ) )
 618          $errors->add('blogname', __( 'Only lowercase letters (a-z) and numbers are allowed.' ) );
 619  
 620      if ( in_array( $blogname, $illegal_names ) == true )
 621          $errors->add('blogname',  __( 'That name is not allowed.' ) );
 622  
 623      if ( strlen( $blogname ) < 4 && !is_super_admin() )
 624          $errors->add('blogname',  __( 'Site name must be at least 4 characters.' ) );
 625  
 626      if ( strpos( $blogname, '_' ) !== false )
 627          $errors->add( 'blogname', __( 'Sorry, site names may not contain the character &#8220;_&#8221;!' ) );
 628  
 629      // do not allow users to create a blog that conflicts with a page on the main blog.
 630      if ( !is_subdomain_install() && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) )
 631          $errors->add( 'blogname', __( 'Sorry, you may not use that site name.' ) );
 632  
 633      // all numeric?
 634      if ( preg_match( '/^[0-9]*$/', $blogname ) )
 635          $errors->add('blogname', __('Sorry, site names must have letters too!'));
 636  
 637      /**
 638       * Filter the new site name during registration.
 639       *
 640       * The name is the site's subdomain or the site's subdirectory
 641       * path depending on the network settings.
 642       *
 643       * @since MU
 644       *
 645       * @param string $blogname Site name.
 646       */
 647      $blogname = apply_filters( 'newblogname', $blogname );
 648  
 649      $blog_title = wp_unslash(  $blog_title );
 650  
 651      if ( empty( $blog_title ) )
 652          $errors->add('blog_title', __( 'Please enter a site title.' ) );
 653  
 654      // Check if the domain/path has been used already.
 655      if ( is_subdomain_install() ) {
 656          $mydomain = $blogname . '.' . preg_replace( '|^www\.|', '', $domain );
 657          $path = $base;
 658      } else {
 659          $mydomain = "$domain";
 660          $path = $base.$blogname.'/';
 661      }
 662      if ( domain_exists($mydomain, $path, $current_site->id) )
 663          $errors->add( 'blogname', __( 'Sorry, that site already exists!' ) );
 664  
 665      if ( username_exists( $blogname ) ) {
 666          if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
 667              $errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
 668      }
 669  
 670      // Has someone already signed up for this domain?
 671      $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
 672      if ( ! empty($signup) ) {
 673          $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
 674          // If registered more than two days ago, cancel registration and let this signup go through.
 675          if ( $diff > 2 * DAY_IN_SECONDS )
 676              $wpdb->delete( $wpdb->signups, array( 'domain' => $mydomain , 'path' => $path ) );
 677          else
 678              $errors->add('blogname', __('That site is currently reserved but may be available in a couple days.'));
 679      }
 680  
 681      $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'user' => $user, 'errors' => $errors);
 682  
 683      /**
 684       * Filter site details and error messages following registration.
 685       *
 686       * @since MU
 687       *
 688       * @param array $result {
 689       *     Array of domain, path, blog name, blog title, user and error messages.
 690       *
 691       *     @type string   $domain     Domain for the site.
 692       *     @type string   $path       Path for the site. Used in subdirectory installs.
 693       *     @type string   $blogname   The unique site name (slug).
 694       *     @type string   $blog_title Blog title.
 695       *     @type string   $user       User email address.
 696       *     @type WP_Error $errors     WP_Error containing any errors found.
 697       * }
 698       */
 699      return apply_filters( 'wpmu_validate_blog_signup', $result );
 700  }
 701  
 702  /**
 703   * Record site signup information for future activation.
 704   *
 705   * @since MU
 706   * @uses wpmu_signup_blog_notification()
 707   *
 708   * @param string $domain The requested domain.
 709   * @param string $path The requested path.
 710   * @param string $title The requested site title.
 711   * @param string $user The user's requested login name.
 712   * @param string $user_email The user's email address.
 713   * @param array $meta By default, contains the requested privacy setting and lang_id.
 714   */
 715  function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() )  {
 716      global $wpdb;
 717  
 718      $key = substr( md5( time() . rand() . $domain ), 0, 16 );
 719      $meta = serialize($meta);
 720  
 721      $wpdb->insert( $wpdb->signups, array(
 722          'domain' => $domain,
 723          'path' => $path,
 724          'title' => $title,
 725          'user_login' => $user,
 726          'user_email' => $user_email,
 727          'registered' => current_time('mysql', true),
 728          'activation_key' => $key,
 729          'meta' => $meta
 730      ) );
 731  
 732      wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
 733  }
 734  
 735  /**
 736   * Record user signup information for future activation.
 737   *
 738   * This function is used when user registration is open but
 739   * new site registration is not.
 740   *
 741   * @since MU
 742   * @uses wpmu_signup_user_notification()
 743   *
 744   * @param string $user The user's requested login name.
 745   * @param string $user_email The user's email address.
 746   * @param array $meta By default, this is an empty array.
 747   */
 748  function wpmu_signup_user( $user, $user_email, $meta = array() ) {
 749      global $wpdb;
 750  
 751      // Format data
 752      $user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
 753      $user_email = sanitize_email( $user_email );
 754      $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
 755      $meta = serialize($meta);
 756  
 757      $wpdb->insert( $wpdb->signups, array(
 758          'domain' => '',
 759          'path' => '',
 760          'title' => '',
 761          'user_login' => $user,
 762          'user_email' => $user_email,
 763          'registered' => current_time('mysql', true),
 764          'activation_key' => $key,
 765          'meta' => $meta
 766      ) );
 767  
 768      wpmu_signup_user_notification($user, $user_email, $key, $meta);
 769  }
 770  
 771  /**
 772   * Notify user of signup success.
 773   *
 774   * This is the notification function used when site registration
 775   * is enabled.
 776   *
 777   * Filter 'wpmu_signup_blog_notification' to bypass this function or
 778   * replace it with your own notification behavior.
 779   *
 780   * Filter 'wpmu_signup_blog_notification_email' and
 781   * 'wpmu_signup_blog_notification_subject' to change the content
 782   * and subject line of the email sent to newly registered users.
 783   *
 784   * @since MU
 785   *
 786   * @param string $domain The new blog domain.
 787   * @param string $path The new blog path.
 788   * @param string $title The site title.
 789   * @param string $user The user's login name.
 790   * @param string $user_email The user's email address.
 791   * @param string $key The activation key created in wpmu_signup_blog()
 792   * @param array $meta By default, contains the requested privacy setting and lang_id.
 793   * @return bool
 794   */
 795  function wpmu_signup_blog_notification( $domain, $path, $title, $user, $user_email, $key, $meta = array() ) {
 796      /**
 797       * Filter whether to bypass the new site email notification.
 798       *
 799       * @since MU
 800       *
 801       * @param string|bool $domain     Site domain.
 802       * @param string      $path       Site path.
 803       * @param string      $title      Site title.
 804       * @param string      $user       User login name.
 805       * @param string      $user_email User email address.
 806       * @param string      $key        Activation key created in wpmu_signup_blog().
 807       * @param array       $meta       By default, contains the requested privacy setting and lang_id.
 808       */
 809      if ( ! apply_filters( 'wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta ) ) {
 810          return false;
 811      }
 812  
 813      // Send email with activation link.
 814      if ( !is_subdomain_install() || get_current_site()->id != 1 )
 815          $activate_url = network_site_url("wp-activate.php?key=$key");
 816      else
 817          $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key"; // @todo use *_url() API
 818  
 819      $activate_url = esc_url($activate_url);
 820      $admin_email = get_site_option( 'admin_email' );
 821      if ( $admin_email == '' )
 822          $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
 823      $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
 824      $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
 825      $message = sprintf(
 826          /**
 827           * Filter the message content of the new blog notification email.
 828           *
 829           * Content should be formatted for transmission via wp_mail().
 830           *
 831           * @since MU
 832           *
 833           * @param string $content    Content of the notification email.
 834           * @param string $domain     Site domain.
 835           * @param string $path       Site path.
 836           * @param string $title      Site title.
 837           * @param string $user       User login name.
 838           * @param string $user_email User email address.
 839           * @param string $key        Activation key created in wpmu_signup_blog().
 840           * @param array  $meta       By default, contains the requested privacy setting and lang_id.
 841           */
 842          apply_filters( 'wpmu_signup_blog_notification_email',
 843              __( "To activate your blog, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\nAfter you activate, you can visit your site here:\n\n%s" ),
 844              $domain, $path, $title, $user, $user_email, $key, $meta
 845          ),
 846          $activate_url,
 847          esc_url( "http://{$domain}{$path}" ),
 848          $key
 849      );
 850      // TODO: Don't hard code activation link.
 851      $subject = sprintf(
 852          /**
 853           * Filter the subject of the new blog notification email.
 854           *
 855           * @since MU
 856           *
 857           * @param string $subject    Subject of the notification email.
 858           * @param string $domain     Site domain.
 859           * @param string $path       Site path.
 860           * @param string $title      Site title.
 861           * @param string $user       User login name.
 862           * @param string $user_email User email address.
 863           * @param string $key        Activation key created in wpmu_signup_blog().
 864           * @param array  $meta       By default, contains the requested privacy setting and lang_id.
 865           */
 866          apply_filters( 'wpmu_signup_blog_notification_subject',
 867              __( '[%1$s] Activate %2$s' ),
 868              $domain, $path, $title, $user, $user_email, $key, $meta
 869          ),
 870          $from_name,
 871          esc_url( 'http://' . $domain . $path )
 872      );
 873      wp_mail($user_email, $subject, $message, $message_headers);
 874      return true;
 875  }
 876  
 877  /**
 878   * Notify user of signup success.
 879   *
 880   * This is the notification function used when no new site has
 881   * been requested.
 882   *
 883   * Filter 'wpmu_signup_user_notification' to bypass this function or
 884   * replace it with your own notification behavior.
 885   *
 886   * Filter 'wpmu_signup_user_notification_email' and
 887   * 'wpmu_signup_user_notification_subject' to change the content
 888   * and subject line of the email sent to newly registered users.
 889   *
 890   * @since MU
 891   *
 892   * @param string $user The user's login name.
 893   * @param string $user_email The user's email address.
 894   * @param string $key The activation key created in wpmu_signup_user()
 895   * @param array $meta By default, an empty array.
 896   * @return bool
 897   */
 898  function wpmu_signup_user_notification( $user, $user_email, $key, $meta = array() ) {
 899      /**
 900       * Filter whether to bypass the email notification for new user sign-up.
 901       *
 902       * @since MU
 903       *
 904       * @param string $user       User login name.
 905       * @param string $user_email User email address.
 906       * @param string $key        Activation key created in wpmu_signup_user().
 907       * @param array  $meta       Signup meta data.
 908       */
 909      if ( ! apply_filters( 'wpmu_signup_user_notification', $user, $user_email, $key, $meta ) )
 910          return false;
 911  
 912      // Send email with activation link.
 913      $admin_email = get_site_option( 'admin_email' );
 914      if ( $admin_email == '' )
 915          $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
 916      $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
 917      $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
 918      $message = sprintf(
 919          /**
 920           * Filter the content of the notification email for new user sign-up.
 921           *
 922           * Content should be formatted for transmission via wp_mail().
 923           *
 924           * @since MU
 925           *
 926           * @param string $content    Content of the notification email.
 927           * @param string $user       User login name.
 928           * @param string $user_email User email address.
 929           * @param string $key        Activation key created in wpmu_signup_user().
 930           * @param array  $meta       Signup meta data.
 931           */
 932          apply_filters( 'wpmu_signup_user_notification_email',
 933              __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login." ),
 934              $user, $user_email, $key, $meta
 935          ),
 936          site_url( "wp-activate.php?key=$key" )
 937      );
 938      // TODO: Don't hard code activation link.
 939      $subject = sprintf(
 940          /**
 941           * Filter the subject of the notification email of new user signup.
 942           *
 943           * @since MU
 944           *
 945           * @param string $subject    Subject of the notification email.
 946           * @param string $user       User login name.
 947           * @param string $user_email User email address.
 948           * @param string $key        Activation key created in wpmu_signup_user().
 949           * @param array  $meta       Signup meta data.
 950           */
 951          apply_filters( 'wpmu_signup_user_notification_subject',
 952              __( '[%1$s] Activate %2$s' ),
 953              $user, $user_email, $key, $meta
 954          ),
 955          $from_name,
 956          $user
 957      );
 958      wp_mail($user_email, $subject, $message, $message_headers);
 959      return true;
 960  }
 961  
 962  /**
 963   * Activate a signup.
 964   *
 965   * Hook to 'wpmu_activate_user' or 'wpmu_activate_blog' for events
 966   * that should happen only when users or sites are self-created (since
 967   * those actions are not called when users and sites are created
 968   * by a Super Admin).
 969   *
 970   * @since MU
 971   * @uses wp_generate_password()
 972   * @uses wpmu_welcome_user_notification()
 973   * @uses add_user_to_blog()
 974   * @uses wpmu_create_user()
 975   * @uses wpmu_create_blog()
 976   * @uses wpmu_welcome_notification()
 977   *
 978   * @param string $key The activation key provided to the user.
 979   * @return array An array containing information about the activated user and/or blog
 980   */
 981  function wpmu_activate_signup($key) {
 982      global $wpdb;
 983  
 984      $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
 985  
 986      if ( empty( $signup ) )
 987          return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
 988  
 989      if ( $signup->active ) {
 990          if ( empty( $signup->domain ) )
 991              return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
 992          else
 993              return new WP_Error( 'already_active', __( 'The site is already active.' ), $signup );
 994      }
 995  
 996      $meta = maybe_unserialize($signup->meta);
 997      $password = wp_generate_password( 12, false );
 998  
 999      $user_id = username_exists($signup->user_login);
1000  
1001      if ( ! $user_id )
1002          $user_id = wpmu_create_user($signup->user_login, $password, $signup->user_email);
1003      else
1004          $user_already_exists = true;
1005  
1006      if ( ! $user_id )
1007          return new WP_Error('create_user', __('Could not create user'), $signup);
1008  
1009      $now = current_time('mysql', true);
1010  
1011      if ( empty($signup->domain) ) {
1012          $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
1013  
1014          if ( isset( $user_already_exists ) )
1015              return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
1016  
1017          wpmu_welcome_user_notification( $user_id, $password, $meta );
1018          /**
1019           * Fires immediately after a new user is activated.
1020           *
1021           * @since MU
1022           *
1023           * @param int   $user_id  User ID.
1024           * @param int   $password User password.
1025           * @param array $meta     Signup meta data.
1026           */
1027          do_action( 'wpmu_activate_user', $user_id, $password, $meta );
1028          return array( 'user_id' => $user_id, 'password' => $password, 'meta' => $meta );
1029      }
1030  
1031      $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
1032  
1033      // TODO: What to do if we create a user but cannot create a blog?
1034      if ( is_wp_error($blog_id) ) {
1035          // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
1036          // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
1037          if ( 'blog_taken' == $blog_id->get_error_code() ) {
1038              $blog_id->add_data( $signup );
1039              $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
1040          }
1041          return $blog_id;
1042      }
1043  
1044      $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
1045      wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
1046      /**
1047       * Fires immediately after a site is activated.
1048       *
1049       * @since MU
1050       *
1051       * @param int    $blog_id       Blog ID.
1052       * @param int    $user_id       User ID.
1053       * @param int    $password      User password.
1054       * @param string $signup_title  Site title.
1055       * @param array  $meta          Signup meta data.
1056       */
1057      do_action( 'wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta );
1058  
1059      return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
1060  }
1061  
1062  /**
1063   * Create a user.
1064   *
1065   * This function runs when a user self-registers as well as when
1066   * a Super Admin creates a new user. Hook to 'wpmu_new_user' for events
1067   * that should affect all new users, but only on Multisite (otherwise
1068   * use 'user_register').
1069   *
1070   * @since MU
1071   * @uses wp_create_user()
1072   *
1073   * @param string $user_name The new user's login name.
1074   * @param string $password The new user's password.
1075   * @param string $email The new user's email address.
1076   * @return mixed Returns false on failure, or int $user_id on success
1077   */
1078  function wpmu_create_user( $user_name, $password, $email ) {
1079      $user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
1080  
1081      $user_id = wp_create_user( $user_name, $password, $email );
1082      if ( is_wp_error( $user_id ) )
1083          return false;
1084  
1085      // Newly created users have no roles or caps until they are added to a blog.
1086      delete_user_option( $user_id, 'capabilities' );
1087      delete_user_option( $user_id, 'user_level' );
1088  
1089      /**
1090       * Fires immediately after a new user is created.
1091       *
1092       * @since MU
1093       *
1094       * @param int $user_id User ID.
1095       */
1096      do_action( 'wpmu_new_user', $user_id );
1097  
1098      return $user_id;
1099  }
1100  
1101  /**
1102   * Create a site.
1103   *
1104   * This function runs when a user self-registers a new site as well
1105   * as when a Super Admin creates a new site. Hook to 'wpmu_new_blog'
1106   * for events that should affect all new sites.
1107   *
1108   * On subdirectory installs, $domain is the same as the main site's
1109   * domain, and the path is the subdirectory name (eg 'example.com'
1110   * and '/blog1/'). On subdomain installs, $domain is the new subdomain +
1111   * root domain (eg 'blog1.example.com'), and $path is '/'.
1112   *
1113   * @since MU
1114   * @uses domain_exists()
1115   * @uses insert_blog()
1116   * @uses wp_install_defaults()
1117   * @uses add_user_to_blog()
1118   *
1119   * @param string $domain The new site's domain.
1120   * @param string $path The new site's path.
1121   * @param string $title The new site's title.
1122   * @param int $user_id The user ID of the new site's admin.
1123   * @param array $meta Optional. Used to set initial site options.
1124   * @param int $site_id Optional. Only relevant on multi-network installs.
1125   * @return mixed Returns WP_Error object on failure, int $blog_id on success
1126   */
1127  function wpmu_create_blog( $domain, $path, $title, $user_id, $meta = array(), $site_id = 1 ) {
1128      $defaults = array( 'public' => 0 );
1129      $meta = wp_parse_args( $meta, $defaults );
1130  
1131      $domain = preg_replace( '/\s+/', '', sanitize_user( $domain, true ) );
1132  
1133      if ( is_subdomain_install() )
1134          $domain = str_replace( '@', '', $domain );
1135  
1136      $title = strip_tags( $title );
1137      $user_id = (int) $user_id;
1138  
1139      if ( empty($path) )
1140          $path = '/';
1141  
1142      // Check if the domain has been used already. We should return an error message.
1143      if ( domain_exists($domain, $path, $site_id) )
1144          return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
1145  
1146      if ( !defined('WP_INSTALLING') )
1147          define( 'WP_INSTALLING', true );
1148  
1149      if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
1150          return new WP_Error('insert_blog', __('Could not create site.'));
1151  
1152      switch_to_blog($blog_id);
1153      install_blog($blog_id, $title);
1154      wp_install_defaults($user_id);
1155  
1156      add_user_to_blog($blog_id, $user_id, 'administrator');
1157  
1158      foreach ( $meta as $key => $value ) {
1159          if ( in_array( $key, array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ) ) )
1160              update_blog_status( $blog_id, $key, $value );
1161          else
1162              update_option( $key, $value );
1163      }
1164  
1165      add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
1166      update_option( 'blog_public', (int) $meta['public'] );
1167  
1168      if ( ! is_super_admin( $user_id ) && ! get_user_meta( $user_id, 'primary_blog', true ) )
1169          update_user_meta( $user_id, 'primary_blog', $blog_id );
1170  
1171      restore_current_blog();
1172      /**
1173       * Fires immediately after a new site is created.
1174       *
1175       * @since MU
1176       *
1177       * @param int    $blog_id Blog ID.
1178       * @param int    $user_id User ID.
1179       * @param string $domain  Site domain.
1180       * @param string $path    Site path.
1181       * @param int    $site_id Site ID. Only relevant on multi-network installs.
1182       * @param array  $meta    Meta data. Used to set initial site options.
1183       */
1184      do_action( 'wpmu_new_blog', $blog_id, $user_id, $domain, $path, $site_id, $meta );
1185  
1186      return $blog_id;
1187  }
1188  
1189  /**
1190   * Notifies the network admin that a new site has been activated.
1191   *
1192   * Filter 'newblog_notify_siteadmin' to change the content of
1193   * the notification email.
1194   *
1195   * @since MU
1196   *
1197   * @param int $blog_id The new site's ID.
1198   * @return bool
1199   */
1200  function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
1201      if ( get_site_option( 'registrationnotification' ) != 'yes' )
1202          return false;
1203  
1204      $email = get_site_option( 'admin_email' );
1205      if ( is_email($email) == false )
1206          return false;
1207  
1208      $options_site_url = esc_url(network_admin_url('settings.php'));
1209  
1210      switch_to_blog( $blog_id );
1211      $blogname = get_option( 'blogname' );
1212      $siteurl = site_url();
1213      restore_current_blog();
1214  
1215      $msg = sprintf( __( 'New Site: %1$s
1216  URL: %2$s
1217  Remote IP: %3$s
1218  
1219  Disable these notifications: %4$s' ), $blogname, $siteurl, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
1220      /**
1221       * Filter the message body of the new site activation email sent
1222       * to the network administrator.
1223       *
1224       * @since MU
1225       *
1226       * @param string $msg Email body.
1227       */
1228      $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
1229  
1230      wp_mail( $email, sprintf( __( 'New Site Registration: %s' ), $siteurl ), $msg );
1231      return true;
1232  }
1233  
1234  /**
1235   * Notifies the network admin that a new user has been activated.
1236   *
1237   * Filter 'newuser_notify_siteadmin' to change the content of
1238   * the notification email.
1239   *
1240   * @since MU
1241   * @uses apply_filters() Filter newuser_notify_siteadmin to change the content of the email message
1242   *
1243   * @param int $user_id The new user's ID.
1244   * @return bool
1245   */
1246  function newuser_notify_siteadmin( $user_id ) {
1247      if ( get_site_option( 'registrationnotification' ) != 'yes' )
1248          return false;
1249  
1250      $email = get_site_option( 'admin_email' );
1251  
1252      if ( is_email($email) == false )
1253          return false;
1254  
1255      $user = get_userdata( $user_id );
1256  
1257      $options_site_url = esc_url(network_admin_url('settings.php'));
1258      $msg = sprintf(__('New User: %1$s
1259  Remote IP: %2$s
1260  
1261  Disable these notifications: %3$s'), $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] ), $options_site_url);
1262  
1263      /**
1264       * Filter the message body of the new user activation email sent
1265       * to the network administrator.
1266       *
1267       * @since MU
1268       *
1269       * @param string  $msg  Email body.
1270       * @param WP_User $user WP_User instance of the new user.
1271       */
1272      $msg = apply_filters( 'newuser_notify_siteadmin', $msg, $user );
1273      wp_mail( $email, sprintf(__('New User Registration: %s'), $user->user_login), $msg );
1274      return true;
1275  }
1276  
1277  /**
1278   * Check whether a blogname is already taken.
1279   *
1280   * Used during the new site registration process to ensure
1281   * that each blogname is unique.
1282   *
1283   * @since MU
1284   *
1285   * @param string $domain The domain to be checked.
1286   * @param string $path The path to be checked.
1287   * @param int $site_id Optional. Relevant only on multi-network installs.
1288   * @return int
1289   */
1290  function domain_exists($domain, $path, $site_id = 1) {
1291      global $wpdb;
1292      $result = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
1293      /**
1294       * Filter whether a blogname is taken.
1295       *
1296       * @since 3.5.0
1297       *
1298       * @param int|null $result  The blog_id if the blogname exists, null otherwise.
1299       * @param string   $domain  Domain to be checked.
1300       * @param string   $path    Path to be checked.
1301       * @param int      $site_id Site ID. Relevant only on multi-network installs.
1302       */
1303      return apply_filters( 'domain_exists', $result, $domain, $path, $site_id );
1304  }
1305  
1306  /**
1307   * Store basic site info in the blogs table.
1308   *
1309   * This function creates a row in the wp_blogs table and returns
1310   * the new blog's ID. It is the first step in creating a new blog.
1311   *
1312   * @since MU
1313   *
1314   * @param string $domain The domain of the new site.
1315   * @param string $path The path of the new site.
1316   * @param int $site_id Unless you're running a multi-network install, be sure to set this value to 1.
1317   * @return int The ID of the new row
1318   */
1319  function insert_blog($domain, $path, $site_id) {
1320      global $wpdb;
1321  
1322      $path = trailingslashit($path);
1323      $site_id = (int) $site_id;
1324  
1325      $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
1326      if ( ! $result )
1327          return false;
1328  
1329      $blog_id = $wpdb->insert_id;
1330      refresh_blog_details( $blog_id );
1331  
1332      wp_maybe_update_network_site_counts();
1333  
1334      return $blog_id;
1335  }
1336  
1337  /**
1338   * Install an empty blog.
1339   *
1340   * Creates the new blog tables and options. If calling this function
1341   * directly, be sure to use switch_to_blog() first, so that $wpdb
1342   * points to the new blog.
1343   *
1344   * @since MU
1345   * @uses make_db_current_silent()
1346   * @uses populate_roles()
1347   *
1348   * @param int $blog_id The value returned by insert_blog().
1349   * @param string $blog_title The title of the new site.
1350   */
1351  function install_blog( $blog_id, $blog_title = '' ) {
1352      global $wpdb, $wp_roles;
1353  
1354      // Cast for security
1355      $blog_id = (int) $blog_id;
1356  
1357      require_once ( ABSPATH . 'wp-admin/includes/upgrade.php' );
1358  
1359      $suppress = $wpdb->suppress_errors();
1360      if ( $wpdb->get_results( "DESCRIBE {$wpdb->posts}" ) )
1361          die( '<h1>' . __( 'Already Installed' ) . '</h1><p>' . __( 'You appear to have already installed WordPress. To reinstall please clear your old database tables first.' ) . '</p></body></html>' );
1362      $wpdb->suppress_errors( $suppress );
1363  
1364      $url = get_blogaddress_by_id( $blog_id );
1365  
1366      // Set everything up
1367      make_db_current_silent( 'blog' );
1368      populate_options();
1369      populate_roles();
1370      $wp_roles->_init();
1371  
1372      $url = untrailingslashit( $url );
1373  
1374      update_option( 'siteurl', $url );
1375      update_option( 'home', $url );
1376  
1377      if ( get_site_option( 'ms_files_rewriting' ) )
1378          update_option( 'upload_path', UPLOADBLOGSDIR . "/$blog_id/files" );
1379      else
1380          update_option( 'upload_path', get_blog_option( get_current_site()->blog_id, 'upload_path' ) );
1381  
1382      update_option( 'blogname', wp_unslash( $blog_title ) );
1383      update_option( 'admin_email', '' );
1384  
1385      // remove all perms
1386      $table_prefix = $wpdb->get_blog_prefix();
1387      delete_metadata( 'user', 0, $table_prefix . 'user_level',   null, true ); // delete all
1388      delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // delete all
1389  }
1390  
1391  /**
1392   * Set blog defaults.
1393   *
1394   * This function creates a row in the wp_blogs table.
1395   *
1396   * @since MU
1397   * @deprecated MU
1398   * @deprecated Use wp_install_defaults()
1399   * @uses wp_install_defaults()
1400   *
1401   * @param int $blog_id Ignored in this function.
1402   * @param int $user_id
1403   */
1404  function install_blog_defaults($blog_id, $user_id) {
1405      global $wpdb;
1406  
1407      require_once ( ABSPATH . 'wp-admin/includes/upgrade.php' );
1408  
1409      $suppress = $wpdb->suppress_errors();
1410  
1411      wp_install_defaults($user_id);
1412  
1413      $wpdb->suppress_errors( $suppress );
1414  }
1415  
1416  /**
1417   * Notify a user that their blog activation has been successful.
1418   *
1419   * Filter 'wpmu_welcome_notification' to disable or bypass.
1420   *
1421   * Filter 'update_welcome_email' and 'update_welcome_subject' to
1422   * modify the content and subject line of the notification email.
1423   *
1424   * @since MU
1425   *
1426   * @param int $blog_id
1427   * @param int $user_id
1428   * @param string $password
1429   * @param string $title The new blog's title
1430   * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
1431   * @return bool
1432   */
1433  function wpmu_welcome_notification( $blog_id, $user_id, $password, $title, $meta = array() ) {
1434      $current_site = get_current_site();
1435  
1436      /**
1437       * Filter whether to bypass the welcome email after site activation.
1438       *
1439       * Returning false disables the welcome email.
1440       *
1441       * @since MU
1442       *
1443       * @param int|bool $blog_id  Blog ID.
1444       * @param int      $user_id  User ID.
1445       * @param string   $password User password.
1446       * @param string   $title    Site title.
1447       * @param array    $meta     Signup meta data.
1448       */
1449      if ( ! apply_filters( 'wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta ) )
1450          return false;
1451  
1452      $welcome_email = get_site_option( 'welcome_email' );
1453      if ( $welcome_email == false )
1454          $welcome_email = __( 'Dear User,
1455  
1456  Your new SITE_NAME site has been successfully set up at:
1457  BLOG_URL
1458  
1459  You can log in to the administrator account with the following information:
1460  Username: USERNAME
1461  Password: PASSWORD
1462  Log in here: BLOG_URLwp-login.php
1463  
1464  We hope you enjoy your new site. Thanks!
1465  
1466  --The Team @ SITE_NAME' );
1467  
1468      $url = get_blogaddress_by_id($blog_id);
1469      $user = get_userdata( $user_id );
1470  
1471      $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
1472      $welcome_email = str_replace( 'BLOG_TITLE', $title, $welcome_email );
1473      $welcome_email = str_replace( 'BLOG_URL', $url, $welcome_email );
1474      $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
1475      $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
1476  
1477      /**
1478       * Filter the content of the welcome email after site activation.
1479       *
1480       * Content should be formatted for transmission via wp_mail().
1481       *
1482       * @since MU
1483       *
1484       * @param string $welcome_email Message body of the email.
1485       * @param int    $blog_id       Blog ID.
1486       * @param int    $user_id       User ID.
1487       * @param string $password      User password.
1488       * @param string $title         Site title.
1489       * @param array  $meta          Signup meta data.
1490       */
1491      $welcome_email = apply_filters( 'update_welcome_email', $welcome_email, $blog_id, $user_id, $password, $title, $meta );
1492      $admin_email = get_site_option( 'admin_email' );
1493  
1494      if ( $admin_email == '' )
1495          $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
1496  
1497      $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
1498      $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1499      $message = $welcome_email;
1500  
1501      if ( empty( $current_site->site_name ) )
1502          $current_site->site_name = 'WordPress';
1503  
1504      /**
1505       * Filter the subject of the welcome email after site activation.
1506       *
1507       * @since MU
1508       *
1509       * @param string $subject Subject of the email.
1510       */
1511      $subject = apply_filters( 'update_welcome_subject', sprintf( __( 'New %1$s Site: %2$s' ), $current_site->site_name, wp_unslash( $title ) ) );
1512      wp_mail($user->user_email, $subject, $message, $message_headers);
1513      return true;
1514  }
1515  
1516  /**
1517   * Notify a user that their account activation has been successful.
1518   *
1519   * Filter 'wpmu_welcome_user_notification' to disable or bypass.
1520   *
1521   * Filter 'update_welcome_user_email' and 'update_welcome_user_subject' to
1522   * modify the content and subject line of the notification email.
1523   *
1524   * @since MU
1525   *
1526   * @param int $user_id
1527   * @param string $password
1528   * @param array $meta Optional. Not used in the default function, but is passed along to hooks for customization.
1529   * @return bool
1530   */
1531  function wpmu_welcome_user_notification( $user_id, $password, $meta = array() ) {
1532      $current_site = get_current_site();
1533  
1534      /**
1535        * Filter whether to bypass the welcome email after user activation.
1536       *
1537       * Returning false disables the welcome email.
1538       *
1539       * @since MU
1540       *
1541       * @param int    $user_id  User ID.
1542       * @param string $password User password.
1543       * @param array  $meta     Signup meta data.
1544       */
1545      if ( ! apply_filters( 'wpmu_welcome_user_notification', $user_id, $password, $meta ) )
1546          return false;
1547  
1548      $welcome_email = get_site_option( 'welcome_user_email' );
1549  
1550      $user = get_userdata( $user_id );
1551  
1552      /**
1553       * Filter the content of the welcome email after user activation.
1554       *
1555       * Content should be formatted for transmission via wp_mail().
1556       *
1557       * @since MU
1558       *
1559       * @param type   $welcome_email The message body of the account activation success email.
1560       * @param int    $user_id       User ID.
1561       * @param string $password      User password.
1562       * @param array  $meta          Signup meta data.
1563       */
1564      $welcome_email = apply_filters( 'update_welcome_user_email', $welcome_email, $user_id, $password, $meta );
1565      $welcome_email = str_replace( 'SITE_NAME', $current_site->site_name, $welcome_email );
1566      $welcome_email = str_replace( 'USERNAME', $user->user_login, $welcome_email );
1567      $welcome_email = str_replace( 'PASSWORD', $password, $welcome_email );
1568      $welcome_email = str_replace( 'LOGINLINK', wp_login_url(), $welcome_email );
1569  
1570      $admin_email = get_site_option( 'admin_email' );
1571  
1572      if ( $admin_email == '' )
1573          $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
1574  
1575      $from_name = get_site_option( 'site_name' ) == '' ? 'WordPress' : esc_html( get_site_option( 'site_name' ) );
1576      $message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1577      $message = $welcome_email;
1578  
1579      if ( empty( $current_site->site_name ) )
1580          $current_site->site_name = 'WordPress';
1581  
1582      /**
1583       * Filter the subject of the welcome email after user activation.
1584       *
1585       * @since MU
1586       *
1587       * @param string $subject Subject of the email.
1588       */
1589      $subject = apply_filters( 'update_welcome_user_subject', sprintf( __( 'New %1$s User: %2$s' ), $current_site->site_name, $user->user_login) );
1590      wp_mail($user->user_email, $subject, $message, $message_headers);
1591      return true;
1592  }
1593  
1594  /**
1595   * Get the current site info.
1596   *
1597   * Returns an object containing the 'id', 'domain', 'path', and 'site_name'
1598   * properties of the site being viewed.
1599   *
1600   * @see wpmu_current_site()
1601   *
1602   * @since MU
1603   *
1604   * @return object
1605   */
1606  function get_current_site() {
1607      global $current_site;
1608      return $current_site;
1609  }
1610  
1611  /**
1612   * Get a user's most recent post.
1613   *
1614   * Walks through each of a user's blogs to find the post with
1615   * the most recent post_date_gmt.
1616   *
1617   * @since MU
1618   * @uses get_blogs_of_user()
1619   *
1620   * @param int $user_id
1621   * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
1622   */
1623  function get_most_recent_post_of_user( $user_id ) {
1624      global $wpdb;
1625  
1626      $user_blogs = get_blogs_of_user( (int) $user_id );
1627      $most_recent_post = array();
1628  
1629      // Walk through each blog and get the most recent post
1630      // published by $user_id
1631      foreach ( (array) $user_blogs as $blog ) {
1632          $prefix = $wpdb->get_blog_prefix( $blog->userblog_id );
1633          $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
1634  
1635          // Make sure we found a post
1636          if ( isset($recent_post['ID']) ) {
1637              $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
1638  
1639              // If this is the first post checked or if this post is
1640              // newer than the current recent post, make it the new
1641              // most recent post.
1642              if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) {
1643                  $most_recent_post = array(
1644                      'blog_id'        => $blog->userblog_id,
1645                      'post_id'        => $recent_post['ID'],
1646                      'post_date_gmt'    => $recent_post['post_date_gmt'],
1647                      'post_gmt_ts'    => $post_gmt_ts
1648                  );
1649              }
1650          }
1651      }
1652  
1653      return $most_recent_post;
1654  }
1655  
1656  // Misc functions
1657  
1658  /**
1659   * Get the size of a directory.
1660   *
1661   * A helper function that is used primarily to check whether
1662   * a blog has exceeded its allowed upload space.
1663   *
1664   * @since MU
1665   * @uses recurse_dirsize()
1666   *
1667   * @param string $directory
1668   * @return int
1669   */
1670  function get_dirsize( $directory ) {
1671      $dirsize = get_transient( 'dirsize_cache' );
1672      if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) )
1673          return $dirsize[ $directory ][ 'size' ];
1674  
1675      if ( false == is_array( $dirsize ) )
1676          $dirsize = array();
1677  
1678      $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
1679  
1680      set_transient( 'dirsize_cache', $dirsize, HOUR_IN_SECONDS );
1681      return $dirsize[ $directory ][ 'size' ];
1682  }
1683  
1684  /**
1685   * Get the size of a directory recursively.
1686   *
1687   * Used by get_dirsize() to get a directory's size when it contains
1688   * other directories.
1689   *
1690   * @since MU
1691   *
1692   * @param string $directory
1693   * @return int
1694   */
1695  function recurse_dirsize( $directory ) {
1696      $size = 0;
1697  
1698      $directory = untrailingslashit( $directory );
1699  
1700      if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
1701          return false;
1702  
1703      if ($handle = opendir($directory)) {
1704          while(($file = readdir($handle)) !== false) {
1705              $path = $directory.'/'.$file;
1706              if ($file != '.' && $file != '..') {
1707                  if (is_file($path)) {
1708                      $size += filesize($path);
1709                  } elseif (is_dir($path)) {
1710                      $handlesize = recurse_dirsize($path);
1711                      if ($handlesize > 0)
1712                          $size += $handlesize;
1713                  }
1714              }
1715          }
1716          closedir($handle);
1717      }
1718      return $size;
1719  }
1720  
1721  /**
1722   * Check an array of MIME types against a whitelist.
1723   *
1724   * WordPress ships with a set of allowed upload filetypes,
1725   * which is defined in wp-includes/functions.php in
1726   * get_allowed_mime_types(). This function is used to filter
1727   * that list against the filetype whitelist provided by Multisite
1728   * Super Admins at wp-admin/network/settings.php.
1729   *
1730   * @since MU
1731   *
1732   * @param array $mimes
1733   * @return array
1734   */
1735  function check_upload_mimes( $mimes ) {
1736      $site_exts = explode( ' ', get_site_option( 'upload_filetypes' ) );
1737      foreach ( $site_exts as $ext ) {
1738          foreach ( $mimes as $ext_pattern => $mime ) {
1739              if ( $ext != '' && strpos( $ext_pattern, $ext ) !== false )
1740                  $site_mimes[$ext_pattern] = $mime;
1741          }
1742      }
1743      return $site_mimes;
1744  }
1745  
1746  /**
1747   * Update a blog's post count.
1748   *
1749   * WordPress MS stores a blog's post count as an option so as
1750   * to avoid extraneous COUNTs when a blog's details are fetched
1751   * with get_blog_details(). This function is called when posts
1752   * are published to make sure the count stays current.
1753   *
1754   * @since MU
1755   */
1756  function update_posts_count( $deprecated = '' ) {
1757      global $wpdb;
1758      update_option( 'post_count', (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
1759  }
1760  
1761  /**
1762   * Logs user registrations.
1763   *
1764   * @since MU
1765   *
1766   * @param int $blog_id
1767   * @param int $user_id
1768   */
1769  function wpmu_log_new_registrations( $blog_id, $user_id ) {
1770      global $wpdb;
1771      $user = get_userdata( (int) $user_id );
1772      if ( $user )
1773          $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '', wp_unslash( $_SERVER['REMOTE_ADDR'] ) ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
1774  }
1775  
1776  /**
1777   * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
1778   *
1779   * @since 3.0.0
1780   *
1781   * @see term_id_filter
1782   *
1783   * @param int $term_id An ID for a term on the current blog.
1784   * @return int An ID from the global terms table mapped from $term_id.
1785   */
1786  function global_terms( $term_id, $deprecated = '' ) {
1787      global $wpdb;
1788      static $global_terms_recurse = null;
1789  
1790      if ( !global_terms_enabled() )
1791          return $term_id;
1792  
1793      // prevent a race condition
1794      $recurse_start = false;
1795      if ( $global_terms_recurse === null ) {
1796          $recurse_start = true;
1797          $global_terms_recurse = 1;
1798      } elseif ( 10 < $global_terms_recurse++ ) {
1799          return $term_id;
1800      }
1801  
1802      $term_id = intval( $term_id );
1803      $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
1804  
1805      $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
1806      if ( $global_id == null ) {
1807          $used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
1808          if ( null == $used_global_id ) {
1809              $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $term_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
1810              $global_id = $wpdb->insert_id;
1811              if ( empty( $global_id ) )
1812                  return $term_id;
1813          } else {
1814              $max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
1815              $max_local_id = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
1816              $new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
1817              $wpdb->insert( $wpdb->sitecategories, array( 'cat_ID' => $new_global_id, 'cat_name' => $c->name, 'category_nicename' => $c->slug ) );
1818              $global_id = $wpdb->insert_id;
1819          }
1820      } elseif ( $global_id != $term_id ) {
1821          $local_id = $wpdb->get_row( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
1822          if ( null != $local_id )
1823              $local_id = global_terms( $local_id );
1824              if ( 10 < $global_terms_recurse )
1825                  $global_id = $term_id;
1826      }
1827  
1828      if ( $global_id != $term_id ) {
1829          if ( get_option( 'default_category' ) == $term_id )
1830              update_option( 'default_category', $global_id );
1831  
1832          $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
1833          $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
1834          $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
1835  
1836          clean_term_cache($term_id);
1837      }
1838      if( $recurse_start )
1839          $global_terms_recurse = null;
1840  
1841      return $global_id;
1842  }
1843  
1844  /**
1845   * Ensure that the current site's domain is listed in the allowed redirect host list.
1846   *
1847   * @see wp_validate_redirect()
1848   * @since MU
1849   *
1850   * @return array The current site's domain
1851   */
1852  function redirect_this_site( $deprecated = '' ) {
1853      return array( get_current_site()->domain );
1854  }
1855  
1856  /**
1857   * Check whether an upload is too big.
1858   *
1859   * @since MU
1860   *
1861   * @param array $upload
1862   * @return mixed If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
1863   */
1864  function upload_is_file_too_big( $upload ) {
1865      if ( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) )
1866          return $upload;
1867  
1868      if ( strlen( $upload['bits'] )  > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) )
1869          return sprintf( __( 'This file is too big. Files must be less than %d KB in size.' ) . '<br />', get_site_option( 'fileupload_maxk', 1500 ));
1870  
1871      return $upload;
1872  }
1873  
1874  /**
1875   * Add a nonce field to the signup page.
1876   *
1877   * @since MU
1878   * @uses wp_nonce_field()
1879   */
1880  function signup_nonce_fields() {
1881      $id = mt_rand();
1882      echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
1883      wp_nonce_field('signup_form_' . $id, '_signup_form', false);
1884  }
1885  
1886  /**
1887   * Process the signup nonce created in signup_nonce_fields().
1888   *
1889   * @since MU
1890   * @uses wp_create_nonce()
1891   *
1892   * @param array $result
1893   * @return array
1894   */
1895  function signup_nonce_check( $result ) {
1896      if ( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
1897          return $result;
1898  
1899      if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
1900          wp_die( __( 'Please try again.' ) );
1901  
1902      return $result;
1903  }
1904  
1905  /**
1906   * Correct 404 redirects when NOBLOGREDIRECT is defined.
1907   *
1908   * @since MU
1909   */
1910  function maybe_redirect_404() {
1911      /**
1912       * Filter the redirect URL for 404s on the main site.
1913       *
1914       * The filter is only evaluated if the NOBLOGREDIRECT constant is defined.
1915       *
1916       * @since 3.0.0
1917       *
1918       * @param string $no_blog_redirect The redirect URL defined in NOBLOGREDIRECT.
1919       */
1920      if ( is_main_site() && is_404() && defined( 'NOBLOGREDIRECT' ) && ( $destination = apply_filters( 'blog_redirect_404', NOBLOGREDIRECT ) ) ) {
1921          if ( $destination == '%siteurl%' )
1922              $destination = network_home_url();
1923          wp_redirect( $destination );
1924          exit();
1925      }
1926  }
1927  
1928  /**
1929   * Add a new user to a blog by visiting /newbloguser/username/.
1930   *
1931   * This will only work when the user's details are saved as an option
1932   * keyed as 'new_user_x', where 'x' is the username of the user to be
1933   * added, as when a user is invited through the regular WP Add User interface.
1934   *
1935   * @since MU
1936   * @uses add_existing_user_to_blog()
1937   */
1938  function maybe_add_existing_user_to_blog() {
1939      if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) )
1940          return false;
1941  
1942      $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
1943      $key = array_pop( $parts );
1944  
1945      if ( $key == '' )
1946          $key = array_pop( $parts );
1947  
1948      $details = get_option( 'new_user_' . $key );
1949      if ( !empty( $details ) )
1950          delete_option( 'new_user_' . $key );
1951  
1952      if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) )
1953          wp_die( sprintf(__('An error occurred adding you to this site. Back to the <a href="%s">homepage</a>.'), home_url() ) );
1954  
1955      wp_die( sprintf( __( 'You have been added to this site. Please visit the <a href="%s">homepage</a> or <a href="%s">log in</a> using your username and password.' ), home_url(), admin_url() ), __( 'WordPress &rsaquo; Success' ) );
1956  }
1957  
1958  /**
1959   * Add a user to a blog based on details from maybe_add_existing_user_to_blog().
1960   *
1961   * @since MU
1962   * @uses add_user_to_blog()
1963   *
1964   * @param array $details
1965   */
1966  function add_existing_user_to_blog( $details = false ) {
1967      global $blog_id;
1968  
1969      if ( is_array( $details ) ) {
1970          $result = add_user_to_blog( $blog_id, $details[ 'user_id' ], $details[ 'role' ] );
1971          /**
1972           * Fires immediately after an existing user is added to a site.
1973           *
1974           * @since MU
1975           *
1976           * @param int   $user_id User ID.
1977           * @param mixed $result  True on success or a WP_Error object if the user doesn't exist.
1978           */
1979          do_action( 'added_existing_user', $details['user_id'], $result );
1980      }
1981      return $result;
1982  }
1983  
1984  /**
1985   * Add a newly created user to the appropriate blog
1986   *
1987   * To add a user in general, use add_user_to_blog(). This function
1988   * is specifically hooked into the wpmu_activate_user action.
1989   *
1990   * @since MU
1991   * @see add_user_to_blog()
1992   *
1993   * @param int $user_id
1994   * @param mixed $password Ignored.
1995   * @param array $meta
1996   */
1997  function add_new_user_to_blog( $user_id, $password, $meta ) {
1998      if ( !empty( $meta[ 'add_to_blog' ] ) ) {
1999          $blog_id = $meta[ 'add_to_blog' ];
2000          $role = $meta[ 'new_role' ];
2001          remove_user_from_blog($user_id, get_current_site()->blog_id); // remove user from main blog.
2002          add_user_to_blog( $blog_id, $user_id, $role );
2003          update_user_meta( $user_id, 'primary_blog', $blog_id );
2004      }
2005  }
2006  
2007  /**
2008   * Correct From host on outgoing mail to match the site domain
2009   *
2010   * @since MU
2011   */
2012  function fix_phpmailer_messageid( $phpmailer ) {
2013      $phpmailer->Hostname = get_current_site()->domain;
2014  }
2015  
2016  /**
2017   * Check to see whether a user is marked as a spammer, based on user login.
2018   *
2019   * @since MU
2020   * @uses get_user_by()
2021   *
2022   * @param string|WP_User $user Optional. Defaults to current user. WP_User object,
2023   *     or user login name as a string.
2024   * @return bool
2025   */
2026  function is_user_spammy( $user = null ) {
2027      if ( ! is_a( $user, 'WP_User' ) ) {
2028          if ( $user )
2029              $user = get_user_by( 'login', $user );
2030          else
2031              $user = wp_get_current_user();
2032      }
2033  
2034      return $user && isset( $user->spam ) && 1 == $user->spam;
2035  }
2036  
2037  /**
2038   * Update this blog's 'public' setting in the global blogs table.
2039   *
2040   * Public blogs have a setting of 1, private blogs are 0.
2041   *
2042   * @since MU
2043   * @uses update_blog_status()
2044   *
2045   * @param int $old_value
2046   * @param int $value The new public value
2047   * @return bool
2048   */
2049  function update_blog_public( $old_value, $value ) {
2050      update_blog_status( get_current_blog_id(), 'public', (int) $value );
2051  }
2052  add_action('update_option_blog_public', 'update_blog_public', 10, 2);
2053  
2054  /**
2055   * Check whether a usermeta key has to do with the current blog.
2056   *
2057   * @since MU
2058   * @uses wp_get_current_user()
2059   *
2060   * @param string $key
2061   * @param int $user_id Optional. Defaults to current user.
2062   * @param int $blog_id Optional. Defaults to current blog.
2063   * @return bool
2064   */
2065  function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
2066      global $wpdb;
2067  
2068      $current_user = wp_get_current_user();
2069      if ( $user_id == 0 )
2070          $user_id = $current_user->ID;
2071      if ( $blog_id == 0 )
2072          $blog_id = $wpdb->blogid;
2073  
2074      $local_key = $wpdb->get_blog_prefix( $blog_id ) . $key;
2075  
2076      if ( isset( $current_user->$local_key ) )
2077          return true;
2078  
2079      return false;
2080  }
2081  
2082  /**
2083   * Check whether users can self-register, based on Network settings.
2084   *
2085   * @since MU
2086   *
2087   * @return bool
2088   */
2089  function users_can_register_signup_filter() {
2090      $registration = get_site_option('registration');
2091      if ( $registration == 'all' || $registration == 'user' )
2092          return true;
2093  
2094      return false;
2095  }
2096  add_filter('option_users_can_register', 'users_can_register_signup_filter');
2097  
2098  /**
2099   * Ensure that the welcome message is not empty. Currently unused.
2100   *
2101   * @since MU
2102   *
2103   * @param string $text
2104   * @return string
2105   */
2106  function welcome_user_msg_filter( $text ) {
2107      if ( !$text ) {
2108          remove_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
2109          $text = __( 'Dear User,
2110  
2111  Your new account is set up.
2112  
2113  You can log in with the following information:
2114  Username: USERNAME
2115  Password: PASSWORD
2116  LOGINLINK
2117  
2118  Thanks!
2119  
2120  --The Team @ SITE_NAME' );
2121          update_site_option( 'welcome_user_email', $text );
2122      }
2123      return $text;
2124  }
2125  add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
2126  
2127  /**
2128   * Whether to force SSL on content.
2129   *
2130   * @since 2.8.5
2131   *
2132   * @param string|bool $force
2133   * @return bool True if forced, false if not forced.
2134   */
2135  function force_ssl_content( $force = '' ) {
2136      static $forced_content;
2137  
2138      if ( '' != $force ) {
2139          $old_forced = $forced_content;
2140          $forced_content = $force;
2141          return $old_forced;
2142      }
2143  
2144      return $forced_content;
2145  }
2146  
2147  /**
2148   * Formats a URL to use https.
2149   *
2150   * Useful as a filter.
2151   *
2152   * @since 2.8.5
2153   *
2154   * @param string URL
2155   * @return string URL with https as the scheme
2156   */
2157  function filter_SSL( $url ) {
2158      if ( ! is_string( $url ) )
2159          return get_bloginfo( 'url' ); // Return home blog url with proper scheme
2160  
2161      if ( force_ssl_content() && is_ssl() )
2162          $url = set_url_scheme( $url, 'https' );
2163  
2164      return $url;
2165  }
2166  
2167  /**
2168   * Schedule update of the network-wide counts for the current network.
2169   *
2170   * @since 3.1.0
2171   */
2172  function wp_schedule_update_network_counts() {
2173      if ( !is_main_site() )
2174          return;
2175  
2176      if ( !wp_next_scheduled('update_network_counts') && !defined('WP_INSTALLING') )
2177          wp_schedule_event(time(), 'twicedaily', 'update_network_counts');
2178  }
2179  
2180  /**
2181   *  Update the network-wide counts for the current network.
2182   *
2183   *  @since 3.1.0
2184   */
2185  function wp_update_network_counts() {
2186      wp_update_network_user_counts();
2187      wp_update_network_site_counts();
2188  }
2189  
2190  /**
2191   * Update the count of sites for the current network.
2192   *
2193   * If enabled through the 'enable_live_network_counts' filter, update the sites count
2194   * on a network when a site is created or its status is updated.
2195   *
2196   * @since 3.7.0
2197   *
2198   * @uses wp_update_network_site_counts()
2199   */
2200  function wp_maybe_update_network_site_counts() {
2201      $is_small_network = ! wp_is_large_network( 'sites' );
2202  
2203      /**
2204       * Filter whether to update network site or user counts when a new site is created.
2205       *
2206       * @since 3.7.0
2207       *
2208       * @see wp_is_large_network()
2209       *
2210       * @param bool   $small_network Whether the network is considered small.
2211       * @param string $context       Context. Either 'users' or 'sites'.
2212       */
2213      if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'sites' ) )
2214          return;
2215  
2216      wp_update_network_site_counts();
2217  }
2218  
2219  /**
2220   * Update the network-wide users count.
2221   *
2222   * If enabled through the 'enable_live_network_counts' filter, update the users count
2223   * on a network when a user is created or its status is updated.
2224   *
2225   * @since 3.7.0
2226   *
2227   * @uses wp_update_network_user_counts()
2228   */
2229  function wp_maybe_update_network_user_counts() {
2230      $is_small_network = ! wp_is_large_network( 'users' );
2231  
2232      /** This filter is documented in wp-includes/ms-functions.php */
2233      if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) )
2234          return;
2235  
2236      wp_update_network_user_counts();
2237  }
2238  
2239  /**
2240   * Update the network-wide site count.
2241   *
2242   * @since 3.7.0
2243   */
2244  function wp_update_network_site_counts() {
2245      global $wpdb;
2246  
2247      $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $wpdb->siteid) );
2248      update_site_option( 'blog_count', $count );
2249  }
2250  
2251  /**
2252   * Update the network-wide user count.
2253   *
2254   * @since 3.7.0
2255   */
2256  function wp_update_network_user_counts() {
2257      global $wpdb;
2258  
2259      $count = $wpdb->get_var( "SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'" );
2260      update_site_option( 'user_count', $count );
2261  }
2262  
2263  /**
2264   * Returns the space used by the current blog.
2265   *
2266   * @since 3.5.0
2267   *
2268   * @return int Used space in megabytes
2269   */
2270  function get_space_used() {
2271      /**
2272       * Filter the amount of storage space used by the current site.
2273       *
2274       * @since 3.5.0
2275       *
2276       * @param int|bool $space_used The amount of used space, in megabytes. Default false.
2277       */
2278      $space_used = apply_filters( 'pre_get_space_used', false );
2279      if ( false === $space_used ) {
2280          $upload_dir = wp_upload_dir();
2281          $space_used = get_dirsize( $upload_dir['basedir'] ) / 1024 / 1024;
2282      }
2283  
2284      return $space_used;
2285  }
2286  
2287  /**
2288   * Returns the upload quota for the current blog.
2289   *
2290   * @since MU
2291   *
2292   * @return int Quota in megabytes
2293   */
2294  function get_space_allowed() {
2295      $space_allowed = get_option( 'blog_upload_space' );
2296  
2297      if ( ! is_numeric( $space_allowed ) )
2298          $space_allowed = get_site_option( 'blog_upload_space' );
2299  
2300      if ( empty( $space_allowed ) || ! is_numeric( $space_allowed ) )
2301          $space_allowed = 100;
2302  
2303      /**
2304       * Filter the upload quota for the current site.
2305       *
2306       * @since 3.7.0
2307       *
2308       * @param int $space_allowed Upload quota in megabytes for the current blog.
2309       */
2310      return apply_filters( 'get_space_allowed', $space_allowed );
2311  }
2312  
2313  /**
2314   * Determines if there is any upload space left in the current blog's quota.
2315   *
2316   * @since 3.0.0
2317   *
2318   * @return int of upload space available in bytes
2319   */
2320  function get_upload_space_available() {
2321      $space_allowed = get_space_allowed() * 1024 * 1024;
2322      if ( get_site_option( 'upload_space_check_disabled' ) )
2323          return $space_allowed;
2324  
2325      $space_used = get_space_used() * 1024 * 1024;
2326  
2327      if ( ( $space_allowed - $space_used ) <= 0 )
2328          return 0;
2329  
2330      return $space_allowed - $space_used;
2331  }
2332  
2333  /**
2334   * Determines if there is any upload space left in the current blog's quota.
2335   *
2336   * @since 3.0.0
2337   * @return bool True if space is available, false otherwise.
2338   */
2339  function is_upload_space_available() {
2340      if ( get_site_option( 'upload_space_check_disabled' ) )
2341          return true;
2342  
2343      return (bool) get_upload_space_available();
2344  }
2345  
2346  /**
2347   * @since 3.0.0
2348   *
2349   * @return int of upload size limit in bytes
2350   */
2351  function upload_size_limit_filter( $size ) {
2352      $fileupload_maxk = 1024 * get_site_option( 'fileupload_maxk', 1500 );
2353      if ( get_site_option( 'upload_space_check_disabled' ) )
2354          return min( $size, $fileupload_maxk );
2355  
2356      return min( $size, $fileupload_maxk, get_upload_space_available() );
2357  }
2358  
2359  /**
2360   * Whether or not we have a large network.
2361   *
2362   * The default criteria for a large network is either more than 10,000 users or more than 10,000 sites.
2363   * Plugins can alter this criteria using the 'wp_is_large_network' filter.
2364   *
2365   * @since 3.3.0
2366   * @param string $using 'sites or 'users'. Default is 'sites'.
2367   * @return bool True if the network meets the criteria for large. False otherwise.
2368   */
2369  function wp_is_large_network( $using = 'sites' ) {
2370      if ( 'users' == $using ) {
2371          $count = get_user_count();
2372          /**
2373           * Filter whether the network is considered large.
2374           *
2375           * @since 3.3.0
2376           *
2377           * @param bool   $is_large_network Whether the network has more than 10000 users or sites.
2378           * @param string $component        The component to count. Accepts 'users', or 'sites'.
2379           * @param int    $count            The count of items for the component.
2380           */
2381          return apply_filters( 'wp_is_large_network', $count > 10000, 'users', $count );
2382      }
2383  
2384      $count = get_blog_count();
2385      /** This filter is documented in wp-includes/ms-functions.php */
2386      return apply_filters( 'wp_is_large_network', $count > 10000, 'sites', $count );
2387  }
2388  
2389  
2390  /**
2391   * Return an array of sites for a network or networks.
2392   *
2393   * @since 3.7.0
2394   *
2395   * @param array $args {
2396   *     Array of default arguments. Optional.
2397   *
2398   *     @type int|array $network_id A network ID or array of network IDs. Set to null to retrieve sites
2399   *                                 from all networks. Defaults to current network ID.
2400   *     @type int       $public     Retrieve public or non-public sites. Default null, for any.
2401   *     @type int       $archived   Retrieve archived or non-archived sites. Default null, for any.
2402   *     @type int       $mature     Retrieve mature or non-mature sites. Default null, for any.
2403   *     @type int       $spam       Retrieve spam or non-spam sites. Default null, for any.
2404   *     @type int       $deleted    Retrieve deleted or non-deleted sites. Default null, for any.
2405   *     @type int       $limit      Number of sites to limit the query to. Default 100.
2406   *     @type int       $offset     Exclude the first x sites. Used in combination with the $limit parameter. Default 0.
2407   * }
2408   * @return array An empty array if the install is considered "large" via wp_is_large_network(). Otherwise,
2409   *               an associative array of site data arrays, each containing the site (network) ID, blog ID,
2410   *               site domain and path, dates registered and modified, and the language ID. Also, boolean
2411   *               values for whether the site is public, archived, mature, spam, and/or deleted.
2412   */
2413  function wp_get_sites( $args = array() ) {
2414      global $wpdb;
2415  
2416      if ( wp_is_large_network() )
2417          return array();
2418  
2419      $defaults = array(
2420          'network_id' => $wpdb->siteid,
2421          'public'     => null,
2422          'archived'   => null,
2423          'mature'     => null,
2424          'spam'       => null,
2425          'deleted'    => null,
2426          'limit'      => 100,
2427          'offset'     => 0,
2428      );
2429  
2430      $args = wp_parse_args( $args, $defaults );
2431  
2432      $query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
2433  
2434      if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
2435          $network_ids = implode( ',', wp_parse_id_list( $args['network_id'] ) );
2436          $query .= "AND site_id IN ($network_ids) ";
2437      }
2438  
2439      if ( isset( $args['public'] ) )
2440          $query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
2441  
2442      if ( isset( $args['archived'] ) )
2443          $query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
2444  
2445      if ( isset( $args['mature'] ) )
2446          $query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
2447  
2448      if ( isset( $args['spam'] ) )
2449          $query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
2450  
2451      if ( isset( $args['deleted'] ) )
2452          $query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
2453  
2454      if ( isset( $args['limit'] ) && $args['limit'] ) {
2455          if ( isset( $args['offset'] ) && $args['offset'] )
2456              $query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
2457          else
2458              $query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
2459      }
2460  
2461      $site_results = $wpdb->get_results( $query, ARRAY_A );
2462  
2463      return $site_results;
2464  }


Generated: Tue Mar 25 01:41:18 2014 WordPress honlapkészítés: online1.hu