wp/wp-includes/ms-functions.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/ms-functions.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/ms-functions.php	Fri Sep 05 18:40:08 2025 +0200
@@ -145,7 +145,7 @@
  *
  * @param int    $blog_id ID of the blog the user is being added to.
  * @param int    $user_id ID of the user being added.
- * @param string $role    The role you want the user to have.
+ * @param string $role    User role.
  * @return true|WP_Error True on success or a WP_Error object if the user doesn't exist
  *                       or could not be added.
  */
@@ -245,8 +245,10 @@
 	 */
 	do_action( 'remove_user_from_blog', $user_id, $blog_id, $reassign );
 
-	// If being removed from the primary blog, set a new primary
-	// if the user is assigned to multiple blogs.
+	/*
+	 * If being removed from the primary blog, set a new primary
+	 * if the user is assigned to multiple blogs.
+	 */
 	$primary_blog = get_user_meta( $user_id, 'primary_blog', true );
 	if ( $primary_blog == $blog_id ) {
 		$new_id     = '';
@@ -274,7 +276,7 @@
 	$user->remove_all_caps();
 
 	$blogs = get_blogs_of_user( $user_id );
-	if ( count( $blogs ) == 0 ) {
+	if ( count( $blogs ) === 0 ) {
 		update_user_meta( $user_id, 'primary_blog', '' );
 		update_user_meta( $user_id, 'source_domain', '' );
 	}
@@ -295,6 +297,7 @@
 		}
 	}
 
+	clean_user_cache( $user_id );
 	restore_current_blog();
 
 	return true;
@@ -307,7 +310,7 @@
  *
  * @param int $blog_id ID of the source blog.
  * @param int $post_id ID of the desired post.
- * @return string The post's permalink
+ * @return string The post's permalink.
  */
 function get_blog_permalink( $blog_id, $post_id ) {
 	switch_to_blog( $blog_id );
@@ -329,9 +332,9 @@
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
- * @param string $domain
- * @param string $path   Optional. Not required for subdomain installations.
- * @return int 0 if no blog found, otherwise the ID of the matching blog
+ * @param string $domain Website domain.
+ * @param string $path   Optional. Not required for subdomain installations. Default '/'.
+ * @return int 0 if no blog found, otherwise the ID of the matching blog.
  */
 function get_blog_id_from_url( $domain, $path = '/' ) {
 	$domain = strtolower( $domain );
@@ -400,13 +403,12 @@
 				continue;
 			}
 
-			if ( $email_domain == $banned_domain ) {
+			if ( $email_domain === $banned_domain ) {
 				$is_email_address_unsafe = true;
 				break;
 			}
 
-			$dotted_domain = ".$banned_domain";
-			if ( substr( $normalized_email, -strlen( $dotted_domain ) ) === $dotted_domain ) {
+			if ( str_ends_with( $normalized_email, ".$banned_domain" ) ) {
 				$is_email_address_unsafe = true;
 				break;
 			}
@@ -603,11 +605,12 @@
  * @global wpdb   $wpdb   WordPress database abstraction object.
  * @global string $domain
  *
- * @param string         $blogname   The blog name provided by the user. Must be unique.
- * @param string         $blog_title The blog title provided by the user.
+ * @param string         $blogname   The site name provided by the user. Must be unique.
+ * @param string         $blog_title The site title provided by the user.
  * @param WP_User|string $user       Optional. The user object to check against the new site name.
+ *                                   Default empty string.
  * @return array {
- *     Array of domain, path, blog name, blog title, user and error messages.
+ *     Array of domain, path, site name, site title, user and error messages.
  *
  *     @type string         $domain     Domain for the site.
  *     @type string         $path       Path for the site. Used in subdirectory installations.
@@ -716,8 +719,10 @@
 		}
 	}
 
-	// Has someone already signed up for this domain?
-	// TODO: Check email too?
+	/*
+	 * Has someone already signed up for this domain?
+	 * TODO: Check email too?
+	 */
 	$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path ) );
 	if ( $signup instanceof stdClass ) {
 		$diff = time() - mysql2date( 'U', $signup->registered );
@@ -750,12 +755,12 @@
 	 * @since MU (3.0.0)
 	 *
 	 * @param array $result {
-	 *     Array of domain, path, blog name, blog title, user and error messages.
+	 *     Array of domain, path, site name, site title, user and error messages.
 	 *
 	 *     @type string         $domain     Domain for the site.
 	 *     @type string         $path       Path for the site. Used in subdirectory installations.
 	 *     @type string         $blogname   The unique site name (slug).
-	 *     @type string         $blog_title Blog title.
+	 *     @type string         $blog_title Site title.
 	 *     @type string|WP_User $user       By default, an empty string. A user object if provided.
 	 *     @type WP_Error       $errors     WP_Error containing any errors found.
 	 * }
@@ -913,7 +918,7 @@
  * @param string $title      The site title.
  * @param string $user_login The user's login name.
  * @param string $user_email The user's email address.
- * @param string $key        The activation key created in wpmu_signup_blog()
+ * @param string $key        The activation key created in wpmu_signup_blog().
  * @param array  $meta       Optional. Signup meta data. By default, contains the requested privacy setting and lang_id.
  * @return bool
  */
@@ -954,7 +959,7 @@
 	$message_headers = "From: \"{$from_name}\" <{$admin_email}>\n" . 'Content-Type: text/plain; charset="' . get_option( 'blog_charset' ) . "\"\n";
 
 	$user            = get_user_by( 'login', $user_login );
-	$switched_locale = switch_to_locale( get_user_locale( $user ) );
+	$switched_locale = $user && switch_to_user_locale( $user->ID );
 
 	$message = sprintf(
 		/**
@@ -1068,7 +1073,7 @@
 	}
 
 	$user            = get_user_by( 'login', $user_login );
-	$switched_locale = switch_to_locale( get_user_locale( $user ) );
+	$switched_locale = $user && switch_to_user_locale( $user->ID );
 
 	// Send email with activation link.
 	$admin_email = get_site_option( 'admin_email' );
@@ -1152,7 +1157,7 @@
  * @global wpdb $wpdb WordPress database abstraction object.
  *
  * @param string $key The activation key provided to the user.
- * @return array|WP_Error An array containing information about the activated user and/or blog
+ * @return array|WP_Error An array containing information about the activated user and/or blog.
  */
 function wpmu_activate_signup( $key ) {
 	global $wpdb;
@@ -1279,6 +1284,8 @@
  *
  * @since 5.5.0
  *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
  * @param int      $id       ID of the user to delete.
  * @param int|null $reassign ID of the user to reassign posts and links to.
  * @param WP_User  $user     User object.
@@ -1302,7 +1309,7 @@
  * @param string $user_name The new user's login name.
  * @param string $password  The new user's password.
  * @param string $email     The new user's email address.
- * @return int|false Returns false on failure, or int $user_id on success
+ * @return int|false Returns false on failure, or int $user_id on success.
  */
 function wpmu_create_user( $user_name, $password, $email ) {
 	$user_name = preg_replace( '/\s+/', '', sanitize_user( $user_name, true ) );
@@ -1352,6 +1359,7 @@
  *                           updated. Otherwise, keys and values will be used to set options for
  *                           the new site. Default empty array.
  * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
+ *                           Default 1.
  * @return int|WP_Error Returns WP_Error object on failure, the new site ID on success.
  */
 function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
@@ -1396,7 +1404,7 @@
 		return $blog_id;
 	}
 
-	wp_cache_set( 'last_changed', microtime(), 'sites' );
+	wp_cache_set_sites_last_changed();
 
 	return $blog_id;
 }
@@ -1537,7 +1545,8 @@
  *
  * @param string $domain     The domain to be checked.
  * @param string $path       The path to be checked.
- * @param int    $network_id Optional. Network ID. Relevant only on multi-network installations.
+ * @param int    $network_id Optional. Network ID. Only relevant on multi-network installations.
+ *                           Default 1.
  * @return int|null The site ID if the site name exists, null otherwise.
  */
 function domain_exists( $domain, $path, $network_id = 1 ) {
@@ -1564,7 +1573,7 @@
 	 * @param int|null $result     The site ID if the site name exists, null otherwise.
 	 * @param string   $domain     Domain to be checked.
 	 * @param string   $path       Path to be checked.
-	 * @param int      $network_id Network ID. Relevant only on multi-network installations.
+	 * @param int      $network_id Network ID. Only relevant on multi-network installations.
 	 */
 	return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
 }
@@ -1608,7 +1617,7 @@
 
 	$user = get_userdata( $user_id );
 
-	$switched_locale = switch_to_locale( get_user_locale( $user ) );
+	$switched_locale = switch_to_user_locale( $user_id );
 
 	$welcome_email = get_site_option( 'welcome_email' );
 	if ( false == $welcome_email ) {
@@ -1732,7 +1741,7 @@
 
 	if ( $network_admin ) {
 		// If the network admin email address corresponds to a user, switch to their locale.
-		$switched_locale = switch_to_locale( get_user_locale( $network_admin ) );
+		$switched_locale = switch_to_user_locale( $network_admin->ID );
 	} else {
 		// Otherwise switch to the locale of the current site.
 		$switched_locale = switch_to_locale( get_locale() );
@@ -1841,7 +1850,7 @@
 
 	$user = get_userdata( $user_id );
 
-	$switched_locale = switch_to_locale( get_user_locale( $user ) );
+	$switched_locale = switch_to_user_locale( $user_id );
 
 	/**
 	 * Filters the content of the welcome email after user activation.
@@ -1906,9 +1915,9 @@
  *
  * @since MU (3.0.0)
  *
- * @global WP_Network $current_site
+ * @global WP_Network $current_site The current network.
  *
- * @return WP_Network
+ * @return WP_Network The current network.
  */
 function get_current_site() {
 	global $current_site;
@@ -1925,8 +1934,8 @@
  *
  * @global wpdb $wpdb WordPress database abstraction object.
  *
- * @param int $user_id
- * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
+ * @param int $user_id User ID.
+ * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts.
  */
 function get_most_recent_post_of_user( $user_id ) {
 	global $wpdb;
@@ -1934,8 +1943,10 @@
 	$user_blogs       = get_blogs_of_user( (int) $user_id );
 	$most_recent_post = array();
 
-	// Walk through each blog and get the most recent post
-	// published by $user_id.
+	/*
+	 * Walk through each blog and get the most recent post
+	 * published by $user_id.
+	 */
 	foreach ( (array) $user_blogs as $blog ) {
 		$prefix      = $wpdb->get_blog_prefix( $blog->userblog_id );
 		$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 );
@@ -1986,7 +1997,7 @@
 	$site_mimes = array();
 	foreach ( $site_exts as $ext ) {
 		foreach ( $mimes as $ext_pattern => $mime ) {
-			if ( '' !== $ext && false !== strpos( $ext_pattern, $ext ) ) {
+			if ( '' !== $ext && str_contains( $ext_pattern, $ext ) ) {
 				$site_mimes[ $ext_pattern ] = $mime;
 			}
 		}
@@ -2050,97 +2061,6 @@
 }
 
 /**
- * Maintains a canonical list of terms by syncing terms created for each blog with the global terms table.
- *
- * @since 3.0.0
- *
- * @see term_id_filter
- *
- * @global wpdb $wpdb WordPress database abstraction object.
- *
- * @param int    $term_id    An ID for a term on the current blog.
- * @param string $deprecated Not used.
- * @return int An ID from the global terms table mapped from $term_id.
- */
-function global_terms( $term_id, $deprecated = '' ) {
-	global $wpdb;
-	static $global_terms_recurse = null;
-
-	if ( ! global_terms_enabled() ) {
-		return $term_id;
-	}
-
-	// Prevent a race condition.
-	$recurse_start = false;
-	if ( null === $global_terms_recurse ) {
-		$recurse_start        = true;
-		$global_terms_recurse = 1;
-	} elseif ( 10 < $global_terms_recurse++ ) {
-		return $term_id;
-	}
-
-	$term_id = (int) $term_id;
-	$c       = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
-
-	$global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
-	if ( null == $global_id ) {
-		$used_global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE cat_ID = %d", $c->term_id ) );
-		if ( null == $used_global_id ) {
-			$wpdb->insert(
-				$wpdb->sitecategories,
-				array(
-					'cat_ID'            => $term_id,
-					'cat_name'          => $c->name,
-					'category_nicename' => $c->slug,
-				)
-			);
-			$global_id = $wpdb->insert_id;
-			if ( empty( $global_id ) ) {
-				return $term_id;
-			}
-		} else {
-			$max_global_id = $wpdb->get_var( "SELECT MAX(cat_ID) FROM $wpdb->sitecategories" );
-			$max_local_id  = $wpdb->get_var( "SELECT MAX(term_id) FROM $wpdb->terms" );
-			$new_global_id = max( $max_global_id, $max_local_id ) + mt_rand( 100, 400 );
-			$wpdb->insert(
-				$wpdb->sitecategories,
-				array(
-					'cat_ID'            => $new_global_id,
-					'cat_name'          => $c->name,
-					'category_nicename' => $c->slug,
-				)
-			);
-			$global_id = $wpdb->insert_id;
-		}
-	} elseif ( $global_id != $term_id ) {
-		$local_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE term_id = %d", $global_id ) );
-		if ( null != $local_id ) {
-			global_terms( $local_id );
-			if ( 10 < $global_terms_recurse ) {
-				$global_id = $term_id;
-			}
-		}
-	}
-
-	if ( $global_id != $term_id ) {
-		if ( get_option( 'default_category' ) == $term_id ) {
-			update_option( 'default_category', $global_id );
-		}
-
-		$wpdb->update( $wpdb->terms, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
-		$wpdb->update( $wpdb->term_taxonomy, array( 'term_id' => $global_id ), array( 'term_id' => $term_id ) );
-		$wpdb->update( $wpdb->term_taxonomy, array( 'parent' => $global_id ), array( 'parent' => $term_id ) );
-
-		clean_term_cache( $term_id );
-	}
-	if ( $recurse_start ) {
-		$global_terms_recurse = null;
-	}
-
-	return $global_id;
-}
-
-/**
  * Ensures that the current site's domain is listed in the allowed redirect host list.
  *
  * @see wp_validate_redirect()
@@ -2162,9 +2082,7 @@
  *
  * @since MU (3.0.0)
  *
- * @blessed
- *
- * @param array $upload
+ * @param array $upload An array of information about the newly-uploaded file.
  * @return string|array If the upload is under the size limit, $upload is returned. Otherwise returns an error message.
  */
 function upload_is_file_too_big( $upload ) {
@@ -2250,7 +2168,7 @@
  * @since MU (3.0.0)
  */
 function maybe_add_existing_user_to_blog() {
-	if ( false === strpos( $_SERVER['REQUEST_URI'], '/newbloguser/' ) ) {
+	if ( ! str_contains( $_SERVER['REQUEST_URI'], '/newbloguser/' ) ) {
 		return;
 	}
 
@@ -2351,7 +2269,7 @@
 }
 
 /**
- * Corrects From host on outgoing mail to match the site domain
+ * Corrects From host on outgoing mail to match the site domain.
  *
  * @since MU (3.0.0)
  *
@@ -2389,8 +2307,8 @@
  *
  * @since MU (3.0.0)
  *
- * @param int $old_value
- * @param int $value     The new public value
+ * @param int $old_value The old public value.
+ * @param int $value     The new public value.
  */
 function update_blog_public( $old_value, $value ) {
 	update_blog_status( get_current_blog_id(), 'public', (int) $value );
@@ -2467,12 +2385,12 @@
  *
  * @since 2.8.5
  *
- * @param string $url URL
- * @return string URL with https as the scheme
+ * @param string $url URL.
+ * @return string URL with https as the scheme.
  */
 function filter_SSL( $url ) {  // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
 	if ( ! is_string( $url ) ) {
-		return get_bloginfo( 'url' ); // Return home blog URL with proper scheme.
+		return get_bloginfo( 'url' ); // Return home site URL with proper scheme.
 	}
 
 	if ( force_ssl_content() && is_ssl() ) {
@@ -2634,7 +2552,7 @@
  *
  * @since MU (3.0.0)
  *
- * @return int Quota in megabytes
+ * @return int Quota in megabytes.
  */
 function get_space_allowed() {
 	$space_allowed = get_option( 'blog_upload_space' );
@@ -2662,7 +2580,7 @@
  *
  * @since 3.0.0
  *
- * @return int of upload space available in bytes
+ * @return int of upload space available in bytes.
  */
 function get_upload_space_available() {
 	$allowed = get_space_allowed();
@@ -2706,12 +2624,14 @@
  * @return int Upload size limit in bytes.
  */
 function upload_size_limit_filter( $size ) {
-	$fileupload_maxk = KB_IN_BYTES * get_site_option( 'fileupload_maxk', 1500 );
+	$fileupload_maxk         = (int) get_site_option( 'fileupload_maxk', 1500 );
+	$max_fileupload_in_bytes = KB_IN_BYTES * $fileupload_maxk;
+
 	if ( get_site_option( 'upload_space_check_disabled' ) ) {
-		return min( $size, $fileupload_maxk );
+		return min( $size, $max_fileupload_in_bytes );
 	}
 
-	return min( $size, $fileupload_maxk, get_upload_space_available() );
+	return min( $size, $max_fileupload_in_bytes, get_upload_space_available() );
 }
 
 /**
@@ -2723,7 +2643,7 @@
  * @since 3.3.0
  * @since 4.8.0 The `$network_id` parameter has been added.
  *
- * @param string   $using      'sites or 'users'. Default is 'sites'.
+ * @param string   $using      'sites' or 'users'. Default is 'sites'.
  * @param int|null $network_id ID of the network. Default is the current network.
  * @return bool True if the network meets the criteria for large. False otherwise.
  */
@@ -2813,7 +2733,7 @@
 	);
 	update_site_option( 'network_admin_hash', $new_admin_email );
 
-	$switched_locale = switch_to_locale( get_user_locale() );
+	$switched_locale = switch_to_user_locale( get_current_user_id() );
 
 	/* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */
 	$email_text = __(