wp/wp-includes/ms-functions.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   143  *
   143  *
   144  * @since MU (3.0.0)
   144  * @since MU (3.0.0)
   145  *
   145  *
   146  * @param int    $blog_id ID of the blog the user is being added to.
   146  * @param int    $blog_id ID of the blog the user is being added to.
   147  * @param int    $user_id ID of the user being added.
   147  * @param int    $user_id ID of the user being added.
   148  * @param string $role    The role you want the user to have.
   148  * @param string $role    User role.
   149  * @return true|WP_Error True on success or a WP_Error object if the user doesn't exist
   149  * @return true|WP_Error True on success or a WP_Error object if the user doesn't exist
   150  *                       or could not be added.
   150  *                       or could not be added.
   151  */
   151  */
   152 function add_user_to_blog( $blog_id, $user_id, $role ) {
   152 function add_user_to_blog( $blog_id, $user_id, $role ) {
   153 	switch_to_blog( $blog_id );
   153 	switch_to_blog( $blog_id );
   243 	 * @param int $blog_id  ID of the blog the user is being removed from.
   243 	 * @param int $blog_id  ID of the blog the user is being removed from.
   244 	 * @param int $reassign ID of the user to whom to reassign posts.
   244 	 * @param int $reassign ID of the user to whom to reassign posts.
   245 	 */
   245 	 */
   246 	do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign );
   246 	do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign );
   247 
   247 
   248 	// If being removed from the primary blog, set a new primary
   248 	/*
   249 	// if the user is assigned to multiple blogs.
   249 	 * If being removed from the primary blog, set a new primary
       
   250 	 * if the user is assigned to multiple blogs.
       
   251 	 */
   250 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
   252 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
   251 	if ( $primary_blog == $blog_id ) {
   253 	if ( $primary_blog == $blog_id ) {
   252 		$new_id     = '';
   254 		$new_id     = '';
   253 		$new_domain = '';
   255 		$new_domain = '';
   254 		$blogs      = get_blogs_of_user( $user_id );
   256 		$blogs      = get_blogs_of_user( $user_id );
   272 	}
   274 	}
   273 
   275 
   274 	$user->remove_all_caps();
   276 	$user->remove_all_caps();
   275 
   277 
   276 	$blogs = get_blogs_of_user( $user_id );
   278 	$blogs = get_blogs_of_user( $user_id );
   277 	if ( count( $blogs ) == 0 ) {
   279 	if ( count( $blogs ) === 0 ) {
   278 		update_user_meta( $user_id, 'primary_blog', '' );
   280 		update_user_meta( $user_id, 'primary_blog', '' );
   279 		update_user_meta( $user_id, 'source_domain', '' );
   281 		update_user_meta( $user_id, 'source_domain', '' );
   280 	}
   282 	}
   281 
   283 
   282 	if ( $reassign ) {
   284 	if ( $reassign ) {
   293 			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
   295 			$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id ) );
   294 			array_walk( $link_ids, 'clean_bookmark_cache' );
   296 			array_walk( $link_ids, 'clean_bookmark_cache' );
   295 		}
   297 		}
   296 	}
   298 	}
   297 
   299 
       
   300 	clean_user_cache( $user_id );
   298 	restore_current_blog();
   301 	restore_current_blog();
   299 
   302 
   300 	return true;
   303 	return true;
   301 }
   304 }
   302 
   305 
   305  *
   308  *
   306  * @since MU (3.0.0) 1.0
   309  * @since MU (3.0.0) 1.0
   307  *
   310  *
   308  * @param int $blog_id ID of the source blog.
   311  * @param int $blog_id ID of the source blog.
   309  * @param int $post_id ID of the desired post.
   312  * @param int $post_id ID of the desired post.
   310  * @return string The post's permalink
   313  * @return string The post's permalink.
   311  */
   314  */
   312 function get_blog_permalink( $blog_id, $post_id ) {
   315 function get_blog_permalink( $blog_id, $post_id ) {
   313 	switch_to_blog( $blog_id );
   316 	switch_to_blog( $blog_id );
   314 	$link = get_permalink( $post_id );
   317 	$link = get_permalink( $post_id );
   315 	restore_current_blog();
   318 	restore_current_blog();
   327  *
   330  *
   328  * @since MU (3.0.0)
   331  * @since MU (3.0.0)
   329  *
   332  *
   330  * @global wpdb $wpdb WordPress database abstraction object.
   333  * @global wpdb $wpdb WordPress database abstraction object.
   331  *
   334  *
   332  * @param string $domain
   335  * @param string $domain Website domain.
   333  * @param string $path   Optional. Not required for subdomain installations.
   336  * @param string $path   Optional. Not required for subdomain installations. Default '/'.
   334  * @return int 0 if no blog found, otherwise the ID of the matching blog
   337  * @return int 0 if no blog found, otherwise the ID of the matching blog.
   335  */
   338  */
   336 function get_blog_id_from_url( $domain, $path = '/' ) {
   339 function get_blog_id_from_url( $domain, $path = '/' ) {
   337 	$domain = strtolower( $domain );
   340 	$domain = strtolower( $domain );
   338 	$path   = strtolower( $path );
   341 	$path   = strtolower( $path );
   339 	$id     = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
   342 	$id     = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
   398 		foreach ( $banned_names as $banned_domain ) {
   401 		foreach ( $banned_names as $banned_domain ) {
   399 			if ( ! $banned_domain ) {
   402 			if ( ! $banned_domain ) {
   400 				continue;
   403 				continue;
   401 			}
   404 			}
   402 
   405 
   403 			if ( $email_domain == $banned_domain ) {
   406 			if ( $email_domain === $banned_domain ) {
   404 				$is_email_address_unsafe = true;
   407 				$is_email_address_unsafe = true;
   405 				break;
   408 				break;
   406 			}
   409 			}
   407 
   410 
   408 			$dotted_domain = ".$banned_domain";
   411 			if ( str_ends_with( $normalized_email, ".$banned_domain" ) ) {
   409 			if ( substr( $normalized_email, -strlen( $dotted_domain ) ) === $dotted_domain ) {
       
   410 				$is_email_address_unsafe = true;
   412 				$is_email_address_unsafe = true;
   411 				break;
   413 				break;
   412 			}
   414 			}
   413 		}
   415 		}
   414 	}
   416 	}
   601  * @since MU (3.0.0)
   603  * @since MU (3.0.0)
   602  *
   604  *
   603  * @global wpdb   $wpdb   WordPress database abstraction object.
   605  * @global wpdb   $wpdb   WordPress database abstraction object.
   604  * @global string $domain
   606  * @global string $domain
   605  *
   607  *
   606  * @param string         $blogname   The blog name provided by the user. Must be unique.
   608  * @param string         $blogname   The site name provided by the user. Must be unique.
   607  * @param string         $blog_title The blog title provided by the user.
   609  * @param string         $blog_title The site title provided by the user.
   608  * @param WP_User|string $user       Optional. The user object to check against the new site name.
   610  * @param WP_User|string $user       Optional. The user object to check against the new site name.
       
   611  *                                   Default empty string.
   609  * @return array {
   612  * @return array {
   610  *     Array of domain, path, blog name, blog title, user and error messages.
   613  *     Array of domain, path, site name, site title, user and error messages.
   611  *
   614  *
   612  *     @type string         $domain     Domain for the site.
   615  *     @type string         $domain     Domain for the site.
   613  *     @type string         $path       Path for the site. Used in subdirectory installations.
   616  *     @type string         $path       Path for the site. Used in subdirectory installations.
   614  *     @type string         $blogname   The unique site name (slug).
   617  *     @type string         $blogname   The unique site name (slug).
   615  *     @type string         $blog_title Blog title.
   618  *     @type string         $blog_title Blog title.
   714 		if ( ! is_object( $user ) || ( is_object( $user ) && ( $user->user_login != $blogname ) ) ) {
   717 		if ( ! is_object( $user ) || ( is_object( $user ) && ( $user->user_login != $blogname ) ) ) {
   715 			$errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
   718 			$errors->add( 'blogname', __( 'Sorry, that site is reserved!' ) );
   716 		}
   719 		}
   717 	}
   720 	}
   718 
   721 
   719 	// Has someone already signed up for this domain?
   722 	/*
   720 	// TODO: Check email too?
   723 	 * Has someone already signed up for this domain?
       
   724 	 * TODO: Check email too?
       
   725 	 */
   721 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) );
   726 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) );
   722 	if ( $signup instanceof stdClass ) {
   727 	if ( $signup instanceof stdClass ) {
   723 		$diff = time() - mysql2date( 'U', $signup->registered );
   728 		$diff = time() - mysql2date( 'U', $signup->registered );
   724 		// If registered more than two days ago, cancel registration and let this signup go through.
   729 		// If registered more than two days ago, cancel registration and let this signup go through.
   725 		if ( $diff > 2 * DAY_IN_SECONDS ) {
   730 		if ( $diff > 2 * DAY_IN_SECONDS ) {
   748 	 * Filters site details and error messages following registration.
   753 	 * Filters site details and error messages following registration.
   749 	 *
   754 	 *
   750 	 * @since MU (3.0.0)
   755 	 * @since MU (3.0.0)
   751 	 *
   756 	 *
   752 	 * @param array $result {
   757 	 * @param array $result {
   753 	 *     Array of domain, path, blog name, blog title, user and error messages.
   758 	 *     Array of domain, path, site name, site title, user and error messages.
   754 	 *
   759 	 *
   755 	 *     @type string         $domain     Domain for the site.
   760 	 *     @type string         $domain     Domain for the site.
   756 	 *     @type string         $path       Path for the site. Used in subdirectory installations.
   761 	 *     @type string         $path       Path for the site. Used in subdirectory installations.
   757 	 *     @type string         $blogname   The unique site name (slug).
   762 	 *     @type string         $blogname   The unique site name (slug).
   758 	 *     @type string         $blog_title Blog title.
   763 	 *     @type string         $blog_title Site title.
   759 	 *     @type string|WP_User $user       By default, an empty string. A user object if provided.
   764 	 *     @type string|WP_User $user       By default, an empty string. A user object if provided.
   760 	 *     @type WP_Error       $errors     WP_Error containing any errors found.
   765 	 *     @type WP_Error       $errors     WP_Error containing any errors found.
   761 	 * }
   766 	 * }
   762 	 */
   767 	 */
   763 	return apply_filters( 'wpmu_validate_blog_signup', $result );
   768 	return apply_filters( 'wpmu_validate_blog_signup', $result );
   911  * @param string $domain     The new blog domain.
   916  * @param string $domain     The new blog domain.
   912  * @param string $path       The new blog path.
   917  * @param string $path       The new blog path.
   913  * @param string $title      The site title.
   918  * @param string $title      The site title.
   914  * @param string $user_login The user's login name.
   919  * @param string $user_login The user's login name.
   915  * @param string $user_email The user's email address.
   920  * @param string $user_email The user's email address.
   916  * @param string $key        The activation key created in wpmu_signup_blog()
   921  * @param string $key        The activation key created in wpmu_signup_blog().
   917  * @param array  $meta       Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
   922  * @param array  $meta       Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
   918  * @return bool
   923  * @return bool
   919  */
   924  */
   920 function wpmu_signup_blog_notification( $domain, $path, $title, $user_login, $user_email, $key, $meta = array() ) {
   925 function wpmu_signup_blog_notification( $domain, $path, $title, $user_login, $user_email, $key, $meta = array() ) {
   921 	/**
   926 	/**
   952 
   957 
   953 	$from_name       = ( '' !== get_site_option( 'site_name' ) ) ? esc_html( get_site_option( 'site_name' ) ) : 'WordPress';
   958 	$from_name       = ( '' !== get_site_option( 'site_name' ) ) ? esc_html( get_site_option( 'site_name' ) ) : 'WordPress';
   954 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
   959 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
   955 
   960 
   956 	$user            = get_user_by( 'login', $user_login );
   961 	$user            = get_user_by( 'login', $user_login );
   957 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
   962 	$switched_locale = $user && switch_to_user_locale( $user->ID );
   958 
   963 
   959 	$message = sprintf(
   964 	$message = sprintf(
   960 		/**
   965 		/**
   961 		 * Filters the message content of the new blog notification email.
   966 		 * Filters the message content of the new blog notification email.
   962 		 *
   967 		 *
  1066 	if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) {
  1071 	if ( ! apply_filters( 'wpmu_signup_user_notification', $user_login, $user_email, $key, $meta ) ) {
  1067 		return false;
  1072 		return false;
  1068 	}
  1073 	}
  1069 
  1074 
  1070 	$user            = get_user_by( 'login', $user_login );
  1075 	$user            = get_user_by( 'login', $user_login );
  1071 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
  1076 	$switched_locale = $user && switch_to_user_locale( $user->ID );
  1072 
  1077 
  1073 	// Send email with activation link.
  1078 	// Send email with activation link.
  1074 	$admin_email = get_site_option( 'admin_email' );
  1079 	$admin_email = get_site_option( 'admin_email' );
  1075 
  1080 
  1076 	if ( '' === $admin_email ) {
  1081 	if ( '' === $admin_email ) {
  1150  * @since MU (3.0.0)
  1155  * @since MU (3.0.0)
  1151  *
  1156  *
  1152  * @global wpdb $wpdb WordPress database abstraction object.
  1157  * @global wpdb $wpdb WordPress database abstraction object.
  1153  *
  1158  *
  1154  * @param string $key The activation key provided to the user.
  1159  * @param string $key The activation key provided to the user.
  1155  * @return array|WP_Error An array containing information about the activated user and/or blog
  1160  * @return array|WP_Error An array containing information about the activated user and/or blog.
  1156  */
  1161  */
  1157 function wpmu_activate_signup( $key ) {
  1162 function wpmu_activate_signup( $key ) {
  1158 	global $wpdb;
  1163 	global $wpdb;
  1159 
  1164 
  1160 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );
  1165 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );
  1277 /**
  1282 /**
  1278  * Deletes an associated signup entry when a user is deleted from the database.
  1283  * Deletes an associated signup entry when a user is deleted from the database.
  1279  *
  1284  *
  1280  * @since 5.5.0
  1285  * @since 5.5.0
  1281  *
  1286  *
       
  1287  * @global wpdb $wpdb WordPress database abstraction object.
       
  1288  *
  1282  * @param int      $id       ID of the user to delete.
  1289  * @param int      $id       ID of the user to delete.
  1283  * @param int|null $reassign ID of the user to reassign posts and links to.
  1290  * @param int|null $reassign ID of the user to reassign posts and links to.
  1284  * @param WP_User  $user     User object.
  1291  * @param WP_User  $user     User object.
  1285  */
  1292  */
  1286 function wp_delete_signup_on_user_delete( $id, $reassign, $user ) {
  1293 function wp_delete_signup_on_user_delete( $id, $reassign, $user ) {
  1300  * @since MU (3.0.0)
  1307  * @since MU (3.0.0)
  1301  *
  1308  *
  1302  * @param string $user_name The new user's login name.
  1309  * @param string $user_name The new user's login name.
  1303  * @param string $password  The new user's password.
  1310  * @param string $password  The new user's password.
  1304  * @param string $email     The new user's email address.
  1311  * @param string $email     The new user's email address.
  1305  * @return int|false Returns false on failure, or int $user_id on success
  1312  * @return int|false Returns false on failure, or int $user_id on success.
  1306  */
  1313  */
  1307 function wpmu_create_user( $user_name, $password, $email ) {
  1314 function wpmu_create_user( $user_name, $password, $email ) {
  1308 	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  1315 	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
  1309 
  1316 
  1310 	$user_id = wp_create_user( $user_name, $password, $email );
  1317 	$user_id = wp_create_user( $user_name, $password, $email );
  1350  *                           If valid status keys are included ('public', 'archived', 'mature',
  1357  *                           If valid status keys are included ('public', 'archived', 'mature',
  1351  *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
  1358  *                           'spam', 'deleted', or 'lang_id') the given site status(es) will be
  1352  *                           updated. Otherwise, keys and values will be used to set options for
  1359  *                           updated. Otherwise, keys and values will be used to set options for
  1353  *                           the new site. Default empty array.
  1360  *                           the new site. Default empty array.
  1354  * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
  1361  * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
       
  1362  *                           Default 1.
  1355  * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
  1363  * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
  1356  */
  1364  */
  1357 function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
  1365 function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
  1358 	$defaults = array(
  1366 	$defaults = array(
  1359 		'public' => 0,
  1367 		'public' => 0,
  1394 
  1402 
  1395 	if ( is_wp_error( $blog_id ) ) {
  1403 	if ( is_wp_error( $blog_id ) ) {
  1396 		return $blog_id;
  1404 		return $blog_id;
  1397 	}
  1405 	}
  1398 
  1406 
  1399 	wp_cache_set( 'last_changed', microtime(), 'sites' );
  1407 	wp_cache_set_sites_last_changed();
  1400 
  1408 
  1401 	return $blog_id;
  1409 	return $blog_id;
  1402 }
  1410 }
  1403 
  1411 
  1404 /**
  1412 /**
  1535  *
  1543  *
  1536  * @since MU (3.0.0)
  1544  * @since MU (3.0.0)
  1537  *
  1545  *
  1538  * @param string $domain     The domain to be checked.
  1546  * @param string $domain     The domain to be checked.
  1539  * @param string $path       The path to be checked.
  1547  * @param string $path       The path to be checked.
  1540  * @param int    $network_id Optional. Network ID. Relevant only on multi-network installations.
  1548  * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
       
  1549  *                           Default 1.
  1541  * @return int|null The site ID if the site name exists, null otherwise.
  1550  * @return int|null The site ID if the site name exists, null otherwise.
  1542  */
  1551  */
  1543 function domain_exists( $domain, $path, $network_id = 1 ) {
  1552 function domain_exists( $domain, $path, $network_id = 1 ) {
  1544 	$path   = trailingslashit( $path );
  1553 	$path   = trailingslashit( $path );
  1545 	$args   = array(
  1554 	$args   = array(
  1562 	 * @since 3.5.0
  1571 	 * @since 3.5.0
  1563 	 *
  1572 	 *
  1564 	 * @param int|null $result     The site ID if the site name exists, null otherwise.
  1573 	 * @param int|null $result     The site ID if the site name exists, null otherwise.
  1565 	 * @param string   $domain     Domain to be checked.
  1574 	 * @param string   $domain     Domain to be checked.
  1566 	 * @param string   $path       Path to be checked.
  1575 	 * @param string   $path       Path to be checked.
  1567 	 * @param int      $network_id Network ID. Relevant only on multi-network installations.
  1576 	 * @param int      $network_id Network ID. Only relevant on multi-network installations.
  1568 	 */
  1577 	 */
  1569 	return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
  1578 	return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
  1570 }
  1579 }
  1571 
  1580 
  1572 /**
  1581 /**
  1606 		return false;
  1615 		return false;
  1607 	}
  1616 	}
  1608 
  1617 
  1609 	$user = get_userdata( $user_id );
  1618 	$user = get_userdata( $user_id );
  1610 
  1619 
  1611 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
  1620 	$switched_locale = switch_to_user_locale( $user_id );
  1612 
  1621 
  1613 	$welcome_email = get_site_option( 'welcome_email' );
  1622 	$welcome_email = get_site_option( 'welcome_email' );
  1614 	if ( false == $welcome_email ) {
  1623 	if ( false == $welcome_email ) {
  1615 		/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
  1624 		/* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
  1616 		$welcome_email = __(
  1625 		$welcome_email = __(
  1730 	$switched_locale = false;
  1739 	$switched_locale = false;
  1731 	$network_admin   = get_user_by( 'email', $email );
  1740 	$network_admin   = get_user_by( 'email', $email );
  1732 
  1741 
  1733 	if ( $network_admin ) {
  1742 	if ( $network_admin ) {
  1734 		// If the network admin email address corresponds to a user, switch to their locale.
  1743 		// If the network admin email address corresponds to a user, switch to their locale.
  1735 		$switched_locale = switch_to_locale( get_user_locale( $network_admin ) );
  1744 		$switched_locale = switch_to_user_locale( $network_admin->ID );
  1736 	} else {
  1745 	} else {
  1737 		// Otherwise switch to the locale of the current site.
  1746 		// Otherwise switch to the locale of the current site.
  1738 		$switched_locale = switch_to_locale( get_locale() );
  1747 		$switched_locale = switch_to_locale( get_locale() );
  1739 	}
  1748 	}
  1740 
  1749 
  1839 
  1848 
  1840 	$welcome_email = get_site_option( 'welcome_user_email' );
  1849 	$welcome_email = get_site_option( 'welcome_user_email' );
  1841 
  1850 
  1842 	$user = get_userdata( $user_id );
  1851 	$user = get_userdata( $user_id );
  1843 
  1852 
  1844 	$switched_locale = switch_to_locale( get_user_locale( $user ) );
  1853 	$switched_locale = switch_to_user_locale( $user_id );
  1845 
  1854 
  1846 	/**
  1855 	/**
  1847 	 * Filters the content of the welcome email after user activation.
  1856 	 * Filters the content of the welcome email after user activation.
  1848 	 *
  1857 	 *
  1849 	 * Content should be formatted for transmission via wp_mail().
  1858 	 * Content should be formatted for transmission via wp_mail().
  1904  *
  1913  *
  1905  * @see wpmu_current_site()
  1914  * @see wpmu_current_site()
  1906  *
  1915  *
  1907  * @since MU (3.0.0)
  1916  * @since MU (3.0.0)
  1908  *
  1917  *
  1909  * @global WP_Network $current_site
  1918  * @global WP_Network $current_site The current network.
  1910  *
  1919  *
  1911  * @return WP_Network
  1920  * @return WP_Network The current network.
  1912  */
  1921  */
  1913 function get_current_site() {
  1922 function get_current_site() {
  1914 	global $current_site;
  1923 	global $current_site;
  1915 	return $current_site;
  1924 	return $current_site;
  1916 }
  1925 }
  1923  *
  1932  *
  1924  * @since MU (3.0.0)
  1933  * @since MU (3.0.0)
  1925  *
  1934  *
  1926  * @global wpdb $wpdb WordPress database abstraction object.
  1935  * @global wpdb $wpdb WordPress database abstraction object.
  1927  *
  1936  *
  1928  * @param int $user_id
  1937  * @param int $user_id User ID.
  1929  * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
  1938  * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts.
  1930  */
  1939  */
  1931 function get_most_recent_post_of_user( $user_id ) {
  1940 function get_most_recent_post_of_user( $user_id ) {
  1932 	global $wpdb;
  1941 	global $wpdb;
  1933 
  1942 
  1934 	$user_blogs       = get_blogs_of_user( (int) $user_id );
  1943 	$user_blogs       = get_blogs_of_user( (int) $user_id );
  1935 	$most_recent_post = array();
  1944 	$most_recent_post = array();
  1936 
  1945 
  1937 	// Walk through each blog and get the most recent post
  1946 	/*
  1938 	// published by $user_id.
  1947 	 * Walk through each blog and get the most recent post
       
  1948 	 * published by $user_id.
       
  1949 	 */
  1939 	foreach ( (array) $user_blogs as $blog ) {
  1950 	foreach ( (array) $user_blogs as $blog ) {
  1940 		$prefix      = $wpdb->get_blog_prefix( $blog->userblog_id );
  1951 		$prefix      = $wpdb->get_blog_prefix( $blog->userblog_id );
  1941 		$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 );
  1952 		$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 );
  1942 
  1953 
  1943 		// Make sure we found a post.
  1954 		// Make sure we found a post.
  1984 function check_upload_mimes( $mimes ) {
  1995 function check_upload_mimes( $mimes ) {
  1985 	$site_exts  = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
  1996 	$site_exts  = explode( ' ', get_site_option( 'upload_filetypes', 'jpg jpeg png gif' ) );
  1986 	$site_mimes = array();
  1997 	$site_mimes = array();
  1987 	foreach ( $site_exts as $ext ) {
  1998 	foreach ( $site_exts as $ext ) {
  1988 		foreach ( $mimes as $ext_pattern => $mime ) {
  1999 		foreach ( $mimes as $ext_pattern => $mime ) {
  1989 			if ( '' !== $ext && false !== strpos( $ext_pattern, $ext ) ) {
  2000 			if ( '' !== $ext && str_contains( $ext_pattern, $ext ) ) {
  1990 				$site_mimes[ $ext_pattern ] = $mime;
  2001 				$site_mimes[ $ext_pattern ] = $mime;
  1991 			}
  2002 			}
  1992 		}
  2003 		}
  1993 	}
  2004 	}
  1994 	return $site_mimes;
  2005 	return $site_mimes;
  2048 		);
  2059 		);
  2049 	}
  2060 	}
  2050 }
  2061 }
  2051 
  2062 
  2052 /**
  2063 /**
  2053  * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
       
  2054  *
       
  2055  * @since 3.0.0
       
  2056  *
       
  2057  * @see term_id_filter
       
  2058  *
       
  2059  * @global wpdb $wpdb WordPress database abstraction object.
       
  2060  *
       
  2061  * @param int    $term_id    An ID for a term on the current blog.
       
  2062  * @param string $deprecated Not used.
       
  2063  * @return int An ID from the global terms table mapped from $term_id.
       
  2064  */
       
  2065 function global_terms( $term_id, $deprecated = '' ) {
       
  2066 	global $wpdb;
       
  2067 	static $global_terms_recurse = null;
       
  2068 
       
  2069 	if ( ! global_terms_enabled() ) {
       
  2070 		return $term_id;
       
  2071 	}
       
  2072 
       
  2073 	// Prevent a race condition.
       
  2074 	$recurse_start = false;
       
  2075 	if ( null === $global_terms_recurse ) {
       
  2076 		$recurse_start        = true;
       
  2077 		$global_terms_recurse = 1;
       
  2078 	} elseif ( 10 < $global_terms_recurse++ ) {
       
  2079 		return $term_id;
       
  2080 	}
       
  2081 
       
  2082 	$term_id = (int) $term_id;
       
  2083 	$c       = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
       
  2084 
       
  2085 	$global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
       
  2086 	if ( null == $global_id ) {
       
  2087 		$used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
       
  2088 		if ( null == $used_global_id ) {
       
  2089 			$wpdb->insert(
       
  2090 				$wpdb->sitecategories,
       
  2091 				array(
       
  2092 					'cat_ID'            => $term_id,
       
  2093 					'cat_name'          => $c->name,
       
  2094 					'category_nicename' => $c->slug,
       
  2095 				)
       
  2096 			);
       
  2097 			$global_id = $wpdb->insert_id;
       
  2098 			if ( empty( $global_id ) ) {
       
  2099 				return $term_id;
       
  2100 			}
       
  2101 		} else {
       
  2102 			$max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
       
  2103 			$max_local_id  = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
       
  2104 			$new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
       
  2105 			$wpdb->insert(
       
  2106 				$wpdb->sitecategories,
       
  2107 				array(
       
  2108 					'cat_ID'            => $new_global_id,
       
  2109 					'cat_name'          => $c->name,
       
  2110 					'category_nicename' => $c->slug,
       
  2111 				)
       
  2112 			);
       
  2113 			$global_id = $wpdb->insert_id;
       
  2114 		}
       
  2115 	} elseif ( $global_id != $term_id ) {
       
  2116 		$local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
       
  2117 		if ( null != $local_id ) {
       
  2118 			global_terms( $local_id );
       
  2119 			if ( 10 < $global_terms_recurse ) {
       
  2120 				$global_id = $term_id;
       
  2121 			}
       
  2122 		}
       
  2123 	}
       
  2124 
       
  2125 	if ( $global_id != $term_id ) {
       
  2126 		if ( get_option( 'default_category' ) == $term_id ) {
       
  2127 			update_option( 'default_category', $global_id );
       
  2128 		}
       
  2129 
       
  2130 		$wpdb->update( $wpdb->terms, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
       
  2131 		$wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
       
  2132 		$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $global_id ), array( 'parent' => $term_id ) );
       
  2133 
       
  2134 		clean_term_cache( $term_id );
       
  2135 	}
       
  2136 	if ( $recurse_start ) {
       
  2137 		$global_terms_recurse = null;
       
  2138 	}
       
  2139 
       
  2140 	return $global_id;
       
  2141 }
       
  2142 
       
  2143 /**
       
  2144  * Ensures that the current site's domain is listed in the allowed redirect host list.
  2064  * Ensures that the current site's domain is listed in the allowed redirect host list.
  2145  *
  2065  *
  2146  * @see wp_validate_redirect()
  2066  * @see wp_validate_redirect()
  2147  * @since MU (3.0.0)
  2067  * @since MU (3.0.0)
  2148  *
  2068  *
  2160 /**
  2080 /**
  2161  * Checks whether an upload is too big.
  2081  * Checks whether an upload is too big.
  2162  *
  2082  *
  2163  * @since MU (3.0.0)
  2083  * @since MU (3.0.0)
  2164  *
  2084  *
  2165  * @blessed
  2085  * @param array $upload An array of information about the newly-uploaded file.
  2166  *
       
  2167  * @param array $upload
       
  2168  * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  2086  * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  2169  */
  2087  */
  2170 function upload_is_file_too_big( $upload ) {
  2088 function upload_is_file_too_big( $upload ) {
  2171 	if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) ) {
  2089 	if ( ! is_array( $upload ) || defined( 'WP_IMPORTING' ) || get_site_option( 'upload_space_check_disabled' ) ) {
  2172 		return $upload;
  2090 		return $upload;
  2248  * added, as when a user is invited through the regular WP Add User interface.
  2166  * added, as when a user is invited through the regular WP Add User interface.
  2249  *
  2167  *
  2250  * @since MU (3.0.0)
  2168  * @since MU (3.0.0)
  2251  */
  2169  */
  2252 function maybe_add_existing_user_to_blog() {
  2170 function maybe_add_existing_user_to_blog() {
  2253 	if ( false === strpos( $_SERVER['REQUEST_URI'], '/newbloguser/' ) ) {
  2171 	if ( ! str_contains( $_SERVER['REQUEST_URI'], '/newbloguser/' ) ) {
  2254 		return;
  2172 		return;
  2255 	}
  2173 	}
  2256 
  2174 
  2257 	$parts = explode( '/', $_SERVER['REQUEST_URI'] );
  2175 	$parts = explode( '/', $_SERVER['REQUEST_URI'] );
  2258 	$key   = array_pop( $parts );
  2176 	$key   = array_pop( $parts );
  2349 		}
  2267 		}
  2350 	}
  2268 	}
  2351 }
  2269 }
  2352 
  2270 
  2353 /**
  2271 /**
  2354  * Corrects From host on outgoing mail to match the site domain
  2272  * Corrects From host on outgoing mail to match the site domain.
  2355  *
  2273  *
  2356  * @since MU (3.0.0)
  2274  * @since MU (3.0.0)
  2357  *
  2275  *
  2358  * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
  2276  * @param PHPMailer $phpmailer The PHPMailer instance (passed by reference).
  2359  */
  2277  */
  2387  *
  2305  *
  2388  * Public blogs have a setting of 1, private blogs are 0.
  2306  * Public blogs have a setting of 1, private blogs are 0.
  2389  *
  2307  *
  2390  * @since MU (3.0.0)
  2308  * @since MU (3.0.0)
  2391  *
  2309  *
  2392  * @param int $old_value
  2310  * @param int $old_value The old public value.
  2393  * @param int $value     The new public value
  2311  * @param int $value     The new public value.
  2394  */
  2312  */
  2395 function update_blog_public( $old_value, $value ) {
  2313 function update_blog_public( $old_value, $value ) {
  2396 	update_blog_status( get_current_blog_id(), 'public', (int) $value );
  2314 	update_blog_status( get_current_blog_id(), 'public', (int) $value );
  2397 }
  2315 }
  2398 
  2316 
  2465  *
  2383  *
  2466  * Useful as a filter.
  2384  * Useful as a filter.
  2467  *
  2385  *
  2468  * @since 2.8.5
  2386  * @since 2.8.5
  2469  *
  2387  *
  2470  * @param string $url URL
  2388  * @param string $url URL.
  2471  * @return string URL with https as the scheme
  2389  * @return string URL with https as the scheme.
  2472  */
  2390  */
  2473 function filter_SSL( $url ) {  // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  2391 function filter_SSL( $url ) {  // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
  2474 	if ( ! is_string( $url ) ) {
  2392 	if ( ! is_string( $url ) ) {
  2475 		return get_bloginfo( 'url' ); // Return home blog URL with proper scheme.
  2393 		return get_bloginfo( 'url' ); // Return home site URL with proper scheme.
  2476 	}
  2394 	}
  2477 
  2395 
  2478 	if ( force_ssl_content() && is_ssl() ) {
  2396 	if ( force_ssl_content() && is_ssl() ) {
  2479 		$url = set_url_scheme( $url, 'https' );
  2397 		$url = set_url_scheme( $url, 'https' );
  2480 	}
  2398 	}
  2632 /**
  2550 /**
  2633  * Returns the upload quota for the current blog.
  2551  * Returns the upload quota for the current blog.
  2634  *
  2552  *
  2635  * @since MU (3.0.0)
  2553  * @since MU (3.0.0)
  2636  *
  2554  *
  2637  * @return int Quota in megabytes
  2555  * @return int Quota in megabytes.
  2638  */
  2556  */
  2639 function get_space_allowed() {
  2557 function get_space_allowed() {
  2640 	$space_allowed = get_option( 'blog_upload_space' );
  2558 	$space_allowed = get_option( 'blog_upload_space' );
  2641 
  2559 
  2642 	if ( ! is_numeric( $space_allowed ) ) {
  2560 	if ( ! is_numeric( $space_allowed ) ) {
  2660 /**
  2578 /**
  2661  * Determines if there is any upload space left in the current blog's quota.
  2579  * Determines if there is any upload space left in the current blog's quota.
  2662  *
  2580  *
  2663  * @since 3.0.0
  2581  * @since 3.0.0
  2664  *
  2582  *
  2665  * @return int of upload space available in bytes
  2583  * @return int of upload space available in bytes.
  2666  */
  2584  */
  2667 function get_upload_space_available() {
  2585 function get_upload_space_available() {
  2668 	$allowed = get_space_allowed();
  2586 	$allowed = get_space_allowed();
  2669 	if ( $allowed < 0 ) {
  2587 	if ( $allowed < 0 ) {
  2670 		$allowed = 0;
  2588 		$allowed = 0;
  2704  *
  2622  *
  2705  * @param int $size Upload size limit in bytes.
  2623  * @param int $size Upload size limit in bytes.
  2706  * @return int Upload size limit in bytes.
  2624  * @return int Upload size limit in bytes.
  2707  */
  2625  */
  2708 function upload_size_limit_filter( $size ) {
  2626 function upload_size_limit_filter( $size ) {
  2709 	$fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
  2627 	$fileupload_maxk         = (int) get_site_option( 'fileupload_maxk', 1500 );
       
  2628 	$max_fileupload_in_bytes = KB_IN_BYTES * $fileupload_maxk;
       
  2629 
  2710 	if ( get_site_option( 'upload_space_check_disabled' ) ) {
  2630 	if ( get_site_option( 'upload_space_check_disabled' ) ) {
  2711 		return min( $size, $fileupload_maxk );
  2631 		return min( $size, $max_fileupload_in_bytes );
  2712 	}
  2632 	}
  2713 
  2633 
  2714 	return min( $size, $fileupload_maxk, get_upload_space_available() );
  2634 	return min( $size, $max_fileupload_in_bytes, get_upload_space_available() );
  2715 }
  2635 }
  2716 
  2636 
  2717 /**
  2637 /**
  2718  * Determines whether or not we have a large network.
  2638  * Determines whether or not we have a large network.
  2719  *
  2639  *
  2721  * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter.
  2641  * Plugins can alter this criteria using the {@see 'wp_is_large_network'} filter.
  2722  *
  2642  *
  2723  * @since 3.3.0
  2643  * @since 3.3.0
  2724  * @since 4.8.0 The `$network_id` parameter has been added.
  2644  * @since 4.8.0 The `$network_id` parameter has been added.
  2725  *
  2645  *
  2726  * @param string   $using      'sites or 'users'. Default is 'sites'.
  2646  * @param string   $using      'sites' or 'users'. Default is 'sites'.
  2727  * @param int|null $network_id ID of the network. Default is the current network.
  2647  * @param int|null $network_id ID of the network. Default is the current network.
  2728  * @return bool True if the network meets the criteria for large. False otherwise.
  2648  * @return bool True if the network meets the criteria for large. False otherwise.
  2729  */
  2649  */
  2730 function wp_is_large_network( $using = 'sites', $network_id = null ) {
  2650 function wp_is_large_network( $using = 'sites', $network_id = null ) {
  2731 	$network_id = (int) $network_id;
  2651 	$network_id = (int) $network_id;
  2811 		'hash'     => $hash,
  2731 		'hash'     => $hash,
  2812 		'newemail' => $value,
  2732 		'newemail' => $value,
  2813 	);
  2733 	);
  2814 	update_site_option( 'network_admin_hash', $new_admin_email );
  2734 	update_site_option( 'network_admin_hash', $new_admin_email );
  2815 
  2735 
  2816 	$switched_locale = switch_to_locale( get_user_locale() );
  2736 	$switched_locale = switch_to_user_locale( get_current_user_id() );
  2817 
  2737 
  2818 	/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  2738 	/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
  2819 	$email_text = __(
  2739 	$email_text = __(
  2820 		'Howdy ###USERNAME###,
  2740 		'Howdy ###USERNAME###,
  2821 
  2741