wp/wp-includes/class-wp-network.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/class-wp-network.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/class-wp-network.php	Fri Sep 05 18:40:08 2025 +0200
@@ -21,6 +21,7 @@
  * @property int $id
  * @property int $site_id
  */
+#[AllowDynamicProperties]
 class WP_Network {
 
 	/**
@@ -82,7 +83,7 @@
 	public $site_name = '';
 
 	/**
-	 * Retrieve a network from the database by its ID.
+	 * Retrieves a network from the database by its ID.
 	 *
 	 * @since 4.4.0
 	 *
@@ -119,7 +120,7 @@
 	}
 
 	/**
-	 * Create a new WP_Network object.
+	 * Creates a new WP_Network object.
 	 *
 	 * Will populate object properties from the object provided and assign other
 	 * default properties based on that information.
@@ -227,6 +228,7 @@
 		 * @param WP_Network $network      The network object for which the main site was detected.
 		 */
 		$main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
+
 		if ( 0 < $main_site_id ) {
 			return $main_site_id;
 		}
@@ -235,8 +237,10 @@
 			return (int) $this->blog_id;
 		}
 
-		if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
-			|| ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
+		if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' )
+			&& DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
+			|| ( defined( 'SITE_ID_CURRENT_SITE' ) && (int) SITE_ID_CURRENT_SITE === $this->id )
+		) {
 			if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
 				$this->blog_id = (string) BLOG_ID_CURRENT_SITE;
 
@@ -254,9 +258,8 @@
 		if ( $site->domain === $this->domain && $site->path === $this->path ) {
 			$main_site_id = (int) $site->id;
 		} else {
-			$cache_key = 'network:' . $this->id . ':main_site';
 
-			$main_site_id = wp_cache_get( $cache_key, 'site-options' );
+			$main_site_id = get_network_option( $this->id, 'main_site' );
 			if ( false === $main_site_id ) {
 				$_sites       = get_sites(
 					array(
@@ -269,7 +272,7 @@
 				);
 				$main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
 
-				wp_cache_add( $cache_key, $main_site_id, 'site-options' );
+				update_network_option( $this->id, 'main_site', $main_site_id );
 			}
 		}
 
@@ -279,7 +282,7 @@
 	}
 
 	/**
-	 * Set the site name assigned to the network if one has not been populated.
+	 * Sets the site name assigned to the network if one has not been populated.
 	 *
 	 * @since 4.4.0
 	 */
@@ -293,7 +296,7 @@
 	}
 
 	/**
-	 * Set the cookie domain based on the network domain if one has
+	 * Sets the cookie domain based on the network domain if one has
 	 * not been populated.
 	 *
 	 * @todo What if the domain of the network doesn't match the current site?
@@ -304,15 +307,15 @@
 		if ( ! empty( $this->cookie_domain ) ) {
 			return;
 		}
-
-		$this->cookie_domain = $this->domain;
-		if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
+		$domain              = parse_url( $this->domain, PHP_URL_HOST );
+		$this->cookie_domain = is_string( $domain ) ? $domain : $this->domain;
+		if ( str_starts_with( $this->cookie_domain, 'www.' ) ) {
 			$this->cookie_domain = substr( $this->cookie_domain, 4 );
 		}
 	}
 
 	/**
-	 * Retrieve the closest matching network for a domain and path.
+	 * Retrieves the closest matching network for a domain and path.
 	 *
 	 * This will not necessarily return an exact match for a domain and path. Instead, it
 	 * breaks the domain and path into pieces that are then used to match the closest
@@ -392,7 +395,7 @@
 		}
 
 		/**
-		 * Determine a network by its domain and path.
+		 * Determines a network by its domain and path.
 		 *
 		 * This allows one to short-circuit the default logic, perhaps by
 		 * replacing it with a routine that is more optimal for your setup.
@@ -404,12 +407,12 @@
 		 * @since 3.9.0
 		 *
 		 * @param null|false|WP_Network $network  Network value to return by path. Default null
-		 *                                       to continue retrieving the network.
-		 * @param string               $domain   The requested domain.
-		 * @param string               $path     The requested path, in full.
-		 * @param int|null             $segments The suggested number of paths to consult.
-		 *                                       Default null, meaning the entire path was to be consulted.
-		 * @param string[]             $paths    Array of paths to search for, based on `$path` and `$segments`.
+		 *                                        to continue retrieving the network.
+		 * @param string                $domain   The requested domain.
+		 * @param string                $path     The requested path, in full.
+		 * @param int|null              $segments The suggested number of paths to consult.
+		 *                                        Default null, meaning the entire path was to be consulted.
+		 * @param string[]              $paths    Array of paths to search for, based on `$path` and `$segments`.
 		 */
 		$pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
 		if ( null !== $pre ) {