wp/wp-includes/class-wp-network.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    19  * @since 4.4.0
    19  * @since 4.4.0
    20  *
    20  *
    21  * @property int $id
    21  * @property int $id
    22  * @property int $site_id
    22  * @property int $site_id
    23  */
    23  */
       
    24 #[AllowDynamicProperties]
    24 class WP_Network {
    25 class WP_Network {
    25 
    26 
    26 	/**
    27 	/**
    27 	 * Network ID.
    28 	 * Network ID.
    28 	 *
    29 	 *
    80 	 * @var string
    81 	 * @var string
    81 	 */
    82 	 */
    82 	public $site_name = '';
    83 	public $site_name = '';
    83 
    84 
    84 	/**
    85 	/**
    85 	 * Retrieve a network from the database by its ID.
    86 	 * Retrieves a network from the database by its ID.
    86 	 *
    87 	 *
    87 	 * @since 4.4.0
    88 	 * @since 4.4.0
    88 	 *
    89 	 *
    89 	 * @global wpdb $wpdb WordPress database abstraction object.
    90 	 * @global wpdb $wpdb WordPress database abstraction object.
    90 	 *
    91 	 *
   117 
   118 
   118 		return new WP_Network( $_network );
   119 		return new WP_Network( $_network );
   119 	}
   120 	}
   120 
   121 
   121 	/**
   122 	/**
   122 	 * Create a new WP_Network object.
   123 	 * Creates a new WP_Network object.
   123 	 *
   124 	 *
   124 	 * Will populate object properties from the object provided and assign other
   125 	 * Will populate object properties from the object provided and assign other
   125 	 * default properties based on that information.
   126 	 * default properties based on that information.
   126 	 *
   127 	 *
   127 	 * @since 4.4.0
   128 	 * @since 4.4.0
   225 		 *
   226 		 *
   226 		 * @param int|null   $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
   227 		 * @param int|null   $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
   227 		 * @param WP_Network $network      The network object for which the main site was detected.
   228 		 * @param WP_Network $network      The network object for which the main site was detected.
   228 		 */
   229 		 */
   229 		$main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
   230 		$main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
       
   231 
   230 		if ( 0 < $main_site_id ) {
   232 		if ( 0 < $main_site_id ) {
   231 			return $main_site_id;
   233 			return $main_site_id;
   232 		}
   234 		}
   233 
   235 
   234 		if ( 0 < (int) $this->blog_id ) {
   236 		if ( 0 < (int) $this->blog_id ) {
   235 			return (int) $this->blog_id;
   237 			return (int) $this->blog_id;
   236 		}
   238 		}
   237 
   239 
   238 		if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
   240 		if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' )
   239 			|| ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
   241 			&& DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
       
   242 			|| ( defined( 'SITE_ID_CURRENT_SITE' ) && (int) SITE_ID_CURRENT_SITE === $this->id )
       
   243 		) {
   240 			if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
   244 			if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
   241 				$this->blog_id = (string) BLOG_ID_CURRENT_SITE;
   245 				$this->blog_id = (string) BLOG_ID_CURRENT_SITE;
   242 
   246 
   243 				return (int) $this->blog_id;
   247 				return (int) $this->blog_id;
   244 			}
   248 			}
   252 
   256 
   253 		$site = get_site();
   257 		$site = get_site();
   254 		if ( $site->domain === $this->domain && $site->path === $this->path ) {
   258 		if ( $site->domain === $this->domain && $site->path === $this->path ) {
   255 			$main_site_id = (int) $site->id;
   259 			$main_site_id = (int) $site->id;
   256 		} else {
   260 		} else {
   257 			$cache_key = 'network:' . $this->id . ':main_site';
   261 
   258 
   262 			$main_site_id = get_network_option( $this->id, 'main_site' );
   259 			$main_site_id = wp_cache_get( $cache_key, 'site-options' );
       
   260 			if ( false === $main_site_id ) {
   263 			if ( false === $main_site_id ) {
   261 				$_sites       = get_sites(
   264 				$_sites       = get_sites(
   262 					array(
   265 					array(
   263 						'fields'     => 'ids',
   266 						'fields'     => 'ids',
   264 						'number'     => 1,
   267 						'number'     => 1,
   267 						'network_id' => $this->id,
   270 						'network_id' => $this->id,
   268 					)
   271 					)
   269 				);
   272 				);
   270 				$main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
   273 				$main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
   271 
   274 
   272 				wp_cache_add( $cache_key, $main_site_id, 'site-options' );
   275 				update_network_option( $this->id, 'main_site', $main_site_id );
   273 			}
   276 			}
   274 		}
   277 		}
   275 
   278 
   276 		$this->blog_id = (string) $main_site_id;
   279 		$this->blog_id = (string) $main_site_id;
   277 
   280 
   278 		return (int) $this->blog_id;
   281 		return (int) $this->blog_id;
   279 	}
   282 	}
   280 
   283 
   281 	/**
   284 	/**
   282 	 * Set the site name assigned to the network if one has not been populated.
   285 	 * Sets the site name assigned to the network if one has not been populated.
   283 	 *
   286 	 *
   284 	 * @since 4.4.0
   287 	 * @since 4.4.0
   285 	 */
   288 	 */
   286 	private function _set_site_name() {
   289 	private function _set_site_name() {
   287 		if ( ! empty( $this->site_name ) ) {
   290 		if ( ! empty( $this->site_name ) ) {
   291 		$default         = ucfirst( $this->domain );
   294 		$default         = ucfirst( $this->domain );
   292 		$this->site_name = get_network_option( $this->id, 'site_name', $default );
   295 		$this->site_name = get_network_option( $this->id, 'site_name', $default );
   293 	}
   296 	}
   294 
   297 
   295 	/**
   298 	/**
   296 	 * Set the cookie domain based on the network domain if one has
   299 	 * Sets the cookie domain based on the network domain if one has
   297 	 * not been populated.
   300 	 * not been populated.
   298 	 *
   301 	 *
   299 	 * @todo What if the domain of the network doesn't match the current site?
   302 	 * @todo What if the domain of the network doesn't match the current site?
   300 	 *
   303 	 *
   301 	 * @since 4.4.0
   304 	 * @since 4.4.0
   302 	 */
   305 	 */
   303 	private function _set_cookie_domain() {
   306 	private function _set_cookie_domain() {
   304 		if ( ! empty( $this->cookie_domain ) ) {
   307 		if ( ! empty( $this->cookie_domain ) ) {
   305 			return;
   308 			return;
   306 		}
   309 		}
   307 
   310 		$domain              = parse_url( $this->domain, PHP_URL_HOST );
   308 		$this->cookie_domain = $this->domain;
   311 		$this->cookie_domain = is_string( $domain ) ? $domain : $this->domain;
   309 		if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
   312 		if ( str_starts_with( $this->cookie_domain, 'www.' ) ) {
   310 			$this->cookie_domain = substr( $this->cookie_domain, 4 );
   313 			$this->cookie_domain = substr( $this->cookie_domain, 4 );
   311 		}
   314 		}
   312 	}
   315 	}
   313 
   316 
   314 	/**
   317 	/**
   315 	 * Retrieve the closest matching network for a domain and path.
   318 	 * Retrieves the closest matching network for a domain and path.
   316 	 *
   319 	 *
   317 	 * This will not necessarily return an exact match for a domain and path. Instead, it
   320 	 * This will not necessarily return an exact match for a domain and path. Instead, it
   318 	 * breaks the domain and path into pieces that are then used to match the closest
   321 	 * breaks the domain and path into pieces that are then used to match the closest
   319 	 * possibility from a query.
   322 	 * possibility from a query.
   320 	 *
   323 	 *
   390 
   393 
   391 			$paths[] = '/';
   394 			$paths[] = '/';
   392 		}
   395 		}
   393 
   396 
   394 		/**
   397 		/**
   395 		 * Determine a network by its domain and path.
   398 		 * Determines a network by its domain and path.
   396 		 *
   399 		 *
   397 		 * This allows one to short-circuit the default logic, perhaps by
   400 		 * This allows one to short-circuit the default logic, perhaps by
   398 		 * replacing it with a routine that is more optimal for your setup.
   401 		 * replacing it with a routine that is more optimal for your setup.
   399 		 *
   402 		 *
   400 		 * Return null to avoid the short-circuit. Return false if no network
   403 		 * Return null to avoid the short-circuit. Return false if no network
   402 		 * an object from wp_get_network().
   405 		 * an object from wp_get_network().
   403 		 *
   406 		 *
   404 		 * @since 3.9.0
   407 		 * @since 3.9.0
   405 		 *
   408 		 *
   406 		 * @param null|false|WP_Network $network  Network value to return by path. Default null
   409 		 * @param null|false|WP_Network $network  Network value to return by path. Default null
   407 		 *                                       to continue retrieving the network.
   410 		 *                                        to continue retrieving the network.
   408 		 * @param string               $domain   The requested domain.
   411 		 * @param string                $domain   The requested domain.
   409 		 * @param string               $path     The requested path, in full.
   412 		 * @param string                $path     The requested path, in full.
   410 		 * @param int|null             $segments The suggested number of paths to consult.
   413 		 * @param int|null              $segments The suggested number of paths to consult.
   411 		 *                                       Default null, meaning the entire path was to be consulted.
   414 		 *                                        Default null, meaning the entire path was to be consulted.
   412 		 * @param string[]             $paths    Array of paths to search for, based on `$path` and `$segments`.
   415 		 * @param string[]              $paths    Array of paths to search for, based on `$path` and `$segments`.
   413 		 */
   416 		 */
   414 		$pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
   417 		$pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
   415 		if ( null !== $pre ) {
   418 		if ( null !== $pre ) {
   416 			return $pre;
   419 			return $pre;
   417 		}
   420 		}