[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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


Generated: Tue Oct 15 08:20:01 2019 Cross-referenced by PHPXref 0.7