Ticket #41936: 41936.5.diff
| File 41936.5.diff, 5.8 KB (added by , 9 years ago) |
|---|
-
src/wp-includes/class-wp-network.php
148 148 case 'id': 149 149 return (int) $this->id; 150 150 case 'blog_id': 151 return (string) $this->get_main_site_id(); 152 case 'site_id': 151 153 return $this->get_main_site_id(); 152 case 'site_id':153 return (int) $this->get_main_site_id();154 154 } 155 155 156 156 return null; … … 208 208 * properties. 209 209 * 210 210 * @since 4.9.0 211 * @see get_main_site_id()212 211 * 213 * @return string Main site ID as numeric string, for compatibility reasons.212 * @return int The ID of the main site. 214 213 */ 215 214 private function get_main_site_id() { 216 if ( empty( $this->blog_id ) ) { 217 $this->blog_id = (string) get_main_site_id( $this->id ); 215 /** 216 * Filters the main site ID. 217 * 218 * Returning anything other than null will effectively short-circuit the function, returning 219 * the result parsed as an integer immediately. 220 * 221 * @since 4.9.0 222 * 223 * @param int|null $main_site_id If anything other than null is returned, it is interpreted as the main site ID. 224 * @param int $network_id The ID of the network for which the main site was detected. 225 */ 226 $main_site_id = apply_filters( 'pre_get_main_site_id', null, $this->id ); 227 if ( null !== $main_site_id ) { 228 return (int) $main_site_id; 218 229 } 219 230 220 return $this->blog_id; 231 if ( ! empty( $this->blog_id ) ) { 232 return (int) $this->blog_id; 233 } 234 235 if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE ) 236 || ( defined( 'SITE_ID_CURRENT_SITE' ) && $this->id == SITE_ID_CURRENT_SITE ) ) { 237 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 238 $this->blog_id = (string) BLOG_ID_CURRENT_SITE; 239 240 return (int) $this->blog_id; 241 } 242 243 if ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 244 $this->blog_id = (string) BLOGID_CURRENT_SITE; 245 246 return (int) $this->blog_id; 247 } 248 } 249 250 $site = get_site(); 251 if ( $site->domain === $this->domain && $site->path === $this->path ) { 252 $main_site_id = (int) $site->id; 253 } else { 254 $cache_key = 'network:' . $this->id . ':main_site'; 255 256 $main_site_id = wp_cache_get( $cache_key, 'site-options' ); 257 if ( false === $main_site_id ) { 258 $_sites = get_sites( array( 259 'fields' => 'ids', 260 'number' => 1, 261 'domain' => $this->domain, 262 'path' => $this->path, 263 'network_id' => $this->id, 264 ) ); 265 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; 266 267 wp_cache_add( $cache_key, $main_site_id, 'site-options' ); 268 } 269 } 270 271 $this->blog_id = (string) $main_site_id; 272 273 return (int) $this->blog_id; 221 274 } 222 275 223 276 /** -
src/wp-includes/functions.php
4431 4431 return 0; 4432 4432 } 4433 4433 4434 /** 4435 * Filters the main site ID. 4436 * 4437 * Returning anything other than null will effectively short-circuit the function, returning 4438 * the result parsed as an integer immediately. 4439 * 4440 * @since 4.9.0 4441 * 4442 * @param int|null $main_site_id If anything other than null is returned, it is interpreted as the main site ID. 4443 * @param int $network_id The ID of the network for which the main site was detected. 4444 */ 4445 $main_site_id = apply_filters( 'pre_get_main_site_id', null, $network->id ); 4446 if ( null !== $main_site_id ) { 4447 return (int) $main_site_id; 4448 } 4449 4450 if ( ( defined( 'DOMAIN_CURRENT_SITE' ) && defined( 'PATH_CURRENT_SITE' ) && $network->domain === DOMAIN_CURRENT_SITE && $network->path === PATH_CURRENT_SITE ) 4451 || ( defined( 'SITE_ID_CURRENT_SITE' ) && $network->id == SITE_ID_CURRENT_SITE ) ) { 4452 if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { 4453 return BLOG_ID_CURRENT_SITE; 4454 } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. 4455 return BLOGID_CURRENT_SITE; 4456 } 4457 } 4458 4459 $site = get_site(); 4460 if ( $site->domain === $network->domain && $site->path === $network->path ) { 4461 $main_site_id = (int) $site->id; 4462 } else { 4463 $main_site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' ); 4464 if ( false === $main_site_id ) { 4465 $_sites = get_sites( array( 4466 'fields' => 'ids', 4467 'number' => 1, 4468 'domain' => $network->domain, 4469 'path' => $network->path, 4470 'network_id' => $network->id, 4471 ) ); 4472 $main_site_id = ! empty( $_sites ) ? array_shift( $_sites ) : 0; 4473 4474 wp_cache_add( 'network:' . $network->id . ':main_site', $main_site_id, 'site-options' ); 4475 } 4476 } 4477 4478 return (int) $main_site_id; 4434 return $network->site_id; 4479 4435 } 4480 4436 4481 4437 /** -
tests/phpunit/tests/multisite/getMainSiteId.php
105 105 public function filter_get_main_site_id() { 106 106 return 333; 107 107 } 108 109 /** 110 * @ticket 41936 111 */ 112 public function test_get_main_site_id_with_property_value() { 113 global $current_site; 114 115 $original_main_site_id = $current_site->blog_id; 116 $current_site->blog_id = '123'; 117 118 $result = get_main_site_id(); 119 120 $current_site->blog_id = $original_main_site_id; 121 122 $this->assertSame( 123, $result ); 123 } 124 125 /** 126 * @ticket 41936 127 */ 128 public function test_get_main_site_id_filtered_with_property_value() { 129 global $current_site; 130 131 $original_main_site_id = $current_site->blog_id; 132 $current_site->blog_id = '123'; 133 134 add_filter( 'pre_get_main_site_id', array( $this, 'filter_get_main_site_id' ) ); 135 $result = get_main_site_id(); 136 137 $current_site->blog_id = $original_main_site_id; 138 139 $this->assertSame( 333, $result ); 140 } 108 141 } 109 142 110 143 endif;
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)