Edit File by line
/home/barbar84/www/wp-inclu...
File: class-wp-network.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Network API: WP_Network class
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Multisite
[5] Fix | Delete
* @since 4.4.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Core class used for interacting with a multisite network.
[10] Fix | Delete
*
[11] Fix | Delete
* This class is used during load to populate the `$current_site` global and
[12] Fix | Delete
* setup the current network.
[13] Fix | Delete
*
[14] Fix | Delete
* This class is most useful in WordPress multi-network installations where the
[15] Fix | Delete
* ability to interact with any network of sites is required.
[16] Fix | Delete
*
[17] Fix | Delete
* @since 4.4.0
[18] Fix | Delete
*
[19] Fix | Delete
* @property int $id
[20] Fix | Delete
* @property int $site_id
[21] Fix | Delete
*/
[22] Fix | Delete
class WP_Network {
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Network ID.
[26] Fix | Delete
*
[27] Fix | Delete
* @since 4.4.0
[28] Fix | Delete
* @since 4.6.0 Converted from public to private to explicitly enable more intuitive
[29] Fix | Delete
* access via magic methods. As part of the access change, the type was
[30] Fix | Delete
* also changed from `string` to `int`.
[31] Fix | Delete
* @var int
[32] Fix | Delete
*/
[33] Fix | Delete
private $id;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Domain of the network.
[37] Fix | Delete
*
[38] Fix | Delete
* @since 4.4.0
[39] Fix | Delete
* @var string
[40] Fix | Delete
*/
[41] Fix | Delete
public $domain = '';
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Path of the network.
[45] Fix | Delete
*
[46] Fix | Delete
* @since 4.4.0
[47] Fix | Delete
* @var string
[48] Fix | Delete
*/
[49] Fix | Delete
public $path = '';
[50] Fix | Delete
[51] Fix | Delete
/**
[52] Fix | Delete
* The ID of the network's main site.
[53] Fix | Delete
*
[54] Fix | Delete
* Named "blog" vs. "site" for legacy reasons. A main site is mapped to
[55] Fix | Delete
* the network when the network is created.
[56] Fix | Delete
*
[57] Fix | Delete
* A numeric string, for compatibility reasons.
[58] Fix | Delete
*
[59] Fix | Delete
* @since 4.4.0
[60] Fix | Delete
* @var string
[61] Fix | Delete
*/
[62] Fix | Delete
private $blog_id = '0';
[63] Fix | Delete
[64] Fix | Delete
/**
[65] Fix | Delete
* Domain used to set cookies for this network.
[66] Fix | Delete
*
[67] Fix | Delete
* @since 4.4.0
[68] Fix | Delete
* @var string
[69] Fix | Delete
*/
[70] Fix | Delete
public $cookie_domain = '';
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* Name of this network.
[74] Fix | Delete
*
[75] Fix | Delete
* Named "site" vs. "network" for legacy reasons.
[76] Fix | Delete
*
[77] Fix | Delete
* @since 4.4.0
[78] Fix | Delete
* @var string
[79] Fix | Delete
*/
[80] Fix | Delete
public $site_name = '';
[81] Fix | Delete
[82] Fix | Delete
/**
[83] Fix | Delete
* Retrieve a network from the database by its ID.
[84] Fix | Delete
*
[85] Fix | Delete
* @since 4.4.0
[86] Fix | Delete
*
[87] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[88] Fix | Delete
*
[89] Fix | Delete
* @param int $network_id The ID of the network to retrieve.
[90] Fix | Delete
* @return WP_Network|false The network's object if found. False if not.
[91] Fix | Delete
*/
[92] Fix | Delete
public static function get_instance( $network_id ) {
[93] Fix | Delete
global $wpdb;
[94] Fix | Delete
[95] Fix | Delete
$network_id = (int) $network_id;
[96] Fix | Delete
if ( ! $network_id ) {
[97] Fix | Delete
return false;
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
$_network = wp_cache_get( $network_id, 'networks' );
[101] Fix | Delete
[102] Fix | Delete
if ( false === $_network ) {
[103] Fix | Delete
$_network = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->site} WHERE id = %d LIMIT 1", $network_id ) );
[104] Fix | Delete
[105] Fix | Delete
if ( empty( $_network ) || is_wp_error( $_network ) ) {
[106] Fix | Delete
$_network = -1;
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
wp_cache_add( $network_id, $_network, 'networks' );
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
if ( is_numeric( $_network ) ) {
[113] Fix | Delete
return false;
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
return new WP_Network( $_network );
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Create a new WP_Network object.
[121] Fix | Delete
*
[122] Fix | Delete
* Will populate object properties from the object provided and assign other
[123] Fix | Delete
* default properties based on that information.
[124] Fix | Delete
*
[125] Fix | Delete
* @since 4.4.0
[126] Fix | Delete
*
[127] Fix | Delete
* @param WP_Network|object $network A network object.
[128] Fix | Delete
*/
[129] Fix | Delete
public function __construct( $network ) {
[130] Fix | Delete
foreach ( get_object_vars( $network ) as $key => $value ) {
[131] Fix | Delete
$this->$key = $value;
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
$this->_set_site_name();
[135] Fix | Delete
$this->_set_cookie_domain();
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Getter.
[140] Fix | Delete
*
[141] Fix | Delete
* Allows current multisite naming conventions when getting properties.
[142] Fix | Delete
*
[143] Fix | Delete
* @since 4.6.0
[144] Fix | Delete
*
[145] Fix | Delete
* @param string $key Property to get.
[146] Fix | Delete
* @return mixed Value of the property. Null if not available.
[147] Fix | Delete
*/
[148] Fix | Delete
public function __get( $key ) {
[149] Fix | Delete
switch ( $key ) {
[150] Fix | Delete
case 'id':
[151] Fix | Delete
return (int) $this->id;
[152] Fix | Delete
case 'blog_id':
[153] Fix | Delete
return (string) $this->get_main_site_id();
[154] Fix | Delete
case 'site_id':
[155] Fix | Delete
return $this->get_main_site_id();
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
return null;
[159] Fix | Delete
}
[160] Fix | Delete
[161] Fix | Delete
/**
[162] Fix | Delete
* Isset-er.
[163] Fix | Delete
*
[164] Fix | Delete
* Allows current multisite naming conventions when checking for properties.
[165] Fix | Delete
*
[166] Fix | Delete
* @since 4.6.0
[167] Fix | Delete
*
[168] Fix | Delete
* @param string $key Property to check if set.
[169] Fix | Delete
* @return bool Whether the property is set.
[170] Fix | Delete
*/
[171] Fix | Delete
public function __isset( $key ) {
[172] Fix | Delete
switch ( $key ) {
[173] Fix | Delete
case 'id':
[174] Fix | Delete
case 'blog_id':
[175] Fix | Delete
case 'site_id':
[176] Fix | Delete
return true;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
return false;
[180] Fix | Delete
}
[181] Fix | Delete
[182] Fix | Delete
/**
[183] Fix | Delete
* Setter.
[184] Fix | Delete
*
[185] Fix | Delete
* Allows current multisite naming conventions while setting properties.
[186] Fix | Delete
*
[187] Fix | Delete
* @since 4.6.0
[188] Fix | Delete
*
[189] Fix | Delete
* @param string $key Property to set.
[190] Fix | Delete
* @param mixed $value Value to assign to the property.
[191] Fix | Delete
*/
[192] Fix | Delete
public function __set( $key, $value ) {
[193] Fix | Delete
switch ( $key ) {
[194] Fix | Delete
case 'id':
[195] Fix | Delete
$this->id = (int) $value;
[196] Fix | Delete
break;
[197] Fix | Delete
case 'blog_id':
[198] Fix | Delete
case 'site_id':
[199] Fix | Delete
$this->blog_id = (string) $value;
[200] Fix | Delete
break;
[201] Fix | Delete
default:
[202] Fix | Delete
$this->$key = $value;
[203] Fix | Delete
}
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
/**
[207] Fix | Delete
* Returns the main site ID for the network.
[208] Fix | Delete
*
[209] Fix | Delete
* Internal method used by the magic getter for the 'blog_id' and 'site_id'
[210] Fix | Delete
* properties.
[211] Fix | Delete
*
[212] Fix | Delete
* @since 4.9.0
[213] Fix | Delete
*
[214] Fix | Delete
* @return int The ID of the main site.
[215] Fix | Delete
*/
[216] Fix | Delete
private function get_main_site_id() {
[217] Fix | Delete
/**
[218] Fix | Delete
* Filters the main site ID.
[219] Fix | Delete
*
[220] Fix | Delete
* Returning a positive integer will effectively short-circuit the function.
[221] Fix | Delete
*
[222] Fix | Delete
* @since 4.9.0
[223] Fix | Delete
*
[224] Fix | Delete
* @param int|null $main_site_id If a positive integer is returned, it is interpreted as the main site ID.
[225] Fix | Delete
* @param WP_Network $network The network object for which the main site was detected.
[226] Fix | Delete
*/
[227] Fix | Delete
$main_site_id = (int) apply_filters( 'pre_get_main_site_id', null, $this );
[228] Fix | Delete
if ( 0 < $main_site_id ) {
[229] Fix | Delete
return $main_site_id;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
if ( 0 < (int) $this->blog_id ) {
[233] Fix | Delete
return (int) $this->blog_id;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && DOMAIN_CURRENT_SITE === $this->domain && PATH_CURRENT_SITE === $this->path )
[237] Fix | Delete
|| ( defined( 'SITE_ID_CURRENT_SITE' ) && SITE_ID_CURRENT_SITE == $this->id ) ) {
[238] Fix | Delete
if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) {
[239] Fix | Delete
$this->blog_id = (string) BLOG_ID_CURRENT_SITE;
[240] Fix | Delete
[241] Fix | Delete
return (int) $this->blog_id;
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // Deprecated.
[245] Fix | Delete
$this->blog_id = (string) BLOGID_CURRENT_SITE;
[246] Fix | Delete
[247] Fix | Delete
return (int) $this->blog_id;
[248] Fix | Delete
}
[249] Fix | Delete
}
[250] Fix | Delete
[251] Fix | Delete
$site = get_site();
[252] Fix | Delete
if ( $site->domain === $this->domain && $site->path === $this->path ) {
[253] Fix | Delete
$main_site_id = (int) $site->id;
[254] Fix | Delete
} else {
[255] Fix | Delete
$cache_key = 'network:' . $this->id . ':main_site';
[256] Fix | Delete
[257] Fix | Delete
$main_site_id = wp_cache_get( $cache_key, 'site-options' );
[258] Fix | Delete
if ( false === $main_site_id ) {
[259] Fix | Delete
$_sites = get_sites(
[260] Fix | Delete
array(
[261] Fix | Delete
'fields' => 'ids',
[262] Fix | Delete
'number' => 1,
[263] Fix | Delete
'domain' => $this->domain,
[264] Fix | Delete
'path' => $this->path,
[265] Fix | Delete
'network_id' => $this->id,
[266] Fix | Delete
)
[267] Fix | Delete
);
[268] Fix | Delete
$main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0;
[269] Fix | Delete
[270] Fix | Delete
wp_cache_add( $cache_key, $main_site_id, 'site-options' );
[271] Fix | Delete
}
[272] Fix | Delete
}
[273] Fix | Delete
[274] Fix | Delete
$this->blog_id = (string) $main_site_id;
[275] Fix | Delete
[276] Fix | Delete
return (int) $this->blog_id;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Set the site name assigned to the network if one has not been populated.
[281] Fix | Delete
*
[282] Fix | Delete
* @since 4.4.0
[283] Fix | Delete
*/
[284] Fix | Delete
private function _set_site_name() {
[285] Fix | Delete
if ( ! empty( $this->site_name ) ) {
[286] Fix | Delete
return;
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
$default = ucfirst( $this->domain );
[290] Fix | Delete
$this->site_name = get_network_option( $this->id, 'site_name', $default );
[291] Fix | Delete
}
[292] Fix | Delete
[293] Fix | Delete
/**
[294] Fix | Delete
* Set the cookie domain based on the network domain if one has
[295] Fix | Delete
* not been populated.
[296] Fix | Delete
*
[297] Fix | Delete
* @todo What if the domain of the network doesn't match the current site?
[298] Fix | Delete
*
[299] Fix | Delete
* @since 4.4.0
[300] Fix | Delete
*/
[301] Fix | Delete
private function _set_cookie_domain() {
[302] Fix | Delete
if ( ! empty( $this->cookie_domain ) ) {
[303] Fix | Delete
return;
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
$this->cookie_domain = $this->domain;
[307] Fix | Delete
if ( 'www.' === substr( $this->cookie_domain, 0, 4 ) ) {
[308] Fix | Delete
$this->cookie_domain = substr( $this->cookie_domain, 4 );
[309] Fix | Delete
}
[310] Fix | Delete
}
[311] Fix | Delete
[312] Fix | Delete
/**
[313] Fix | Delete
* Retrieve the closest matching network for a domain and path.
[314] Fix | Delete
*
[315] Fix | Delete
* This will not necessarily return an exact match for a domain and path. Instead, it
[316] Fix | Delete
* breaks the domain and path into pieces that are then used to match the closest
[317] Fix | Delete
* possibility from a query.
[318] Fix | Delete
*
[319] Fix | Delete
* The intent of this method is to match a network during bootstrap for a
[320] Fix | Delete
* requested site address.
[321] Fix | Delete
*
[322] Fix | Delete
* @since 4.4.0
[323] Fix | Delete
*
[324] Fix | Delete
* @param string $domain Domain to check.
[325] Fix | Delete
* @param string $path Path to check.
[326] Fix | Delete
* @param int|null $segments Path segments to use. Defaults to null, or the full path.
[327] Fix | Delete
* @return WP_Network|false Network object if successful. False when no network is found.
[328] Fix | Delete
*/
[329] Fix | Delete
public static function get_by_path( $domain = '', $path = '', $segments = null ) {
[330] Fix | Delete
$domains = array( $domain );
[331] Fix | Delete
$pieces = explode( '.', $domain );
[332] Fix | Delete
[333] Fix | Delete
/*
[334] Fix | Delete
* It's possible one domain to search is 'com', but it might as well
[335] Fix | Delete
* be 'localhost' or some other locally mapped domain.
[336] Fix | Delete
*/
[337] Fix | Delete
while ( array_shift( $pieces ) ) {
[338] Fix | Delete
if ( ! empty( $pieces ) ) {
[339] Fix | Delete
$domains[] = implode( '.', $pieces );
[340] Fix | Delete
}
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/*
[344] Fix | Delete
* If we've gotten to this function during normal execution, there is
[345] Fix | Delete
* more than one network installed. At this point, who knows how many
[346] Fix | Delete
* we have. Attempt to optimize for the situation where networks are
[347] Fix | Delete
* only domains, thus meaning paths never need to be considered.
[348] Fix | Delete
*
[349] Fix | Delete
* This is a very basic optimization; anything further could have
[350] Fix | Delete
* drawbacks depending on the setup, so this is best done per-installation.
[351] Fix | Delete
*/
[352] Fix | Delete
$using_paths = true;
[353] Fix | Delete
if ( wp_using_ext_object_cache() ) {
[354] Fix | Delete
$using_paths = wp_cache_get( 'networks_have_paths', 'site-options' );
[355] Fix | Delete
if ( false === $using_paths ) {
[356] Fix | Delete
$using_paths = get_networks(
[357] Fix | Delete
array(
[358] Fix | Delete
'number' => 1,
[359] Fix | Delete
'count' => true,
[360] Fix | Delete
'path__not_in' => '/',
[361] Fix | Delete
)
[362] Fix | Delete
);
[363] Fix | Delete
wp_cache_add( 'networks_have_paths', $using_paths, 'site-options' );
[364] Fix | Delete
}
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
$paths = array();
[368] Fix | Delete
if ( $using_paths ) {
[369] Fix | Delete
$path_segments = array_filter( explode( '/', trim( $path, '/' ) ) );
[370] Fix | Delete
[371] Fix | Delete
/**
[372] Fix | Delete
* Filters the number of path segments to consider when searching for a site.
[373] Fix | Delete
*
[374] Fix | Delete
* @since 3.9.0
[375] Fix | Delete
*
[376] Fix | Delete
* @param int|null $segments The number of path segments to consider. WordPress by default looks at
[377] Fix | Delete
* one path segment. The function default of null only makes sense when you
[378] Fix | Delete
* know the requested path should match a network.
[379] Fix | Delete
* @param string $domain The requested domain.
[380] Fix | Delete
* @param string $path The requested path, in full.
[381] Fix | Delete
*/
[382] Fix | Delete
$segments = apply_filters( 'network_by_path_segments_count', $segments, $domain, $path );
[383] Fix | Delete
[384] Fix | Delete
if ( ( null !== $segments ) && count( $path_segments ) > $segments ) {
[385] Fix | Delete
$path_segments = array_slice( $path_segments, 0, $segments );
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
while ( count( $path_segments ) ) {
[389] Fix | Delete
$paths[] = '/' . implode( '/', $path_segments ) . '/';
[390] Fix | Delete
array_pop( $path_segments );
[391] Fix | Delete
}
[392] Fix | Delete
[393] Fix | Delete
$paths[] = '/';
[394] Fix | Delete
}
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Determine a network by its domain and path.
[398] Fix | Delete
*
[399] Fix | Delete
* This allows one to short-circuit the default logic, perhaps by
[400] Fix | Delete
* replacing it with a routine that is more optimal for your setup.
[401] Fix | Delete
*
[402] Fix | Delete
* Return null to avoid the short-circuit. Return false if no network
[403] Fix | Delete
* can be found at the requested domain and path. Otherwise, return
[404] Fix | Delete
* an object from wp_get_network().
[405] Fix | Delete
*
[406] Fix | Delete
* @since 3.9.0
[407] Fix | Delete
*
[408] Fix | Delete
* @param null|false|WP_Network $network Network value to return by path. Default null
[409] Fix | Delete
* to continue retrieving the network.
[410] Fix | Delete
* @param string $domain The requested domain.
[411] Fix | Delete
* @param string $path The requested path, in full.
[412] Fix | Delete
* @param int|null $segments The suggested number of paths to consult.
[413] Fix | Delete
* Default null, meaning the entire path was to be consulted.
[414] Fix | Delete
* @param string[] $paths Array of paths to search for, based on `$path` and `$segments`.
[415] Fix | Delete
*/
[416] Fix | Delete
$pre = apply_filters( 'pre_get_network_by_path', null, $domain, $path, $segments, $paths );
[417] Fix | Delete
if ( null !== $pre ) {
[418] Fix | Delete
return $pre;
[419] Fix | Delete
}
[420] Fix | Delete
[421] Fix | Delete
if ( ! $using_paths ) {
[422] Fix | Delete
$networks = get_networks(
[423] Fix | Delete
array(
[424] Fix | Delete
'number' => 1,
[425] Fix | Delete
'orderby' => array(
[426] Fix | Delete
'domain_length' => 'DESC',
[427] Fix | Delete
),
[428] Fix | Delete
'domain__in' => $domains,
[429] Fix | Delete
)
[430] Fix | Delete
);
[431] Fix | Delete
[432] Fix | Delete
if ( ! empty( $networks ) ) {
[433] Fix | Delete
return array_shift( $networks );
[434] Fix | Delete
}
[435] Fix | Delete
[436] Fix | Delete
return false;
[437] Fix | Delete
}
[438] Fix | Delete
[439] Fix | Delete
$networks = get_networks(
[440] Fix | Delete
array(
[441] Fix | Delete
'orderby' => array(
[442] Fix | Delete
'domain_length' => 'DESC',
[443] Fix | Delete
'path_length' => 'DESC',
[444] Fix | Delete
),
[445] Fix | Delete
'domain__in' => $domains,
[446] Fix | Delete
'path__in' => $paths,
[447] Fix | Delete
)
[448] Fix | Delete
);
[449] Fix | Delete
[450] Fix | Delete
/*
[451] Fix | Delete
* Domains are sorted by length of domain, then by length of path.
[452] Fix | Delete
* The domain must match for the path to be considered. Otherwise,
[453] Fix | Delete
* a network with the path of / will suffice.
[454] Fix | Delete
*/
[455] Fix | Delete
$found = false;
[456] Fix | Delete
foreach ( $networks as $network ) {
[457] Fix | Delete
if ( ( $network->domain === $domain ) || ( "www.{$network->domain}" === $domain ) ) {
[458] Fix | Delete
if ( in_array( $network->path, $paths, true ) ) {
[459] Fix | Delete
$found = true;
[460] Fix | Delete
break;
[461] Fix | Delete
}
[462] Fix | Delete
}
[463] Fix | Delete
if ( '/' === $network->path ) {
[464] Fix | Delete
$found = true;
[465] Fix | Delete
break;
[466] Fix | Delete
}
[467] Fix | Delete
}
[468] Fix | Delete
[469] Fix | Delete
if ( true === $found ) {
[470] Fix | Delete
return $network;
[471] Fix | Delete
}
[472] Fix | Delete
[473] Fix | Delete
return false;
[474] Fix | Delete
}
[475] Fix | Delete
}
[476] Fix | Delete
[477] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function