[ Index ]

PHP Cross Reference of WordPress

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


Generated: Mon Aug 3 01:00:04 2020 Cross-referenced by PHPXref 0.7.1