| 459 | |
| 460 | /** |
| 461 | * Retrieve the main site ID of a given network. |
| 462 | * |
| 463 | * @param int $network_id ID of the network. |
| 464 | * |
| 465 | * @return int|bool ID of the main site if found. False if the network ID is invalid. |
| 466 | */ |
| 467 | function get_main_site( $network_id = 0 ) { |
| 468 | global $wpdb, $current_site, $current_blog; |
| 469 | |
| 470 | if ( 0 === $network_id ) { |
| 471 | if ( defined( 'BLOG_ID_CURRENT_SITE' ) ) { |
| 472 | return BLOG_ID_CURRENT_SITE; |
| 473 | } elseif ( defined( 'BLOGID_CURRENT_SITE' ) ) { // deprecated. |
| 474 | return BLOGID_CURRENT_SITE; |
| 475 | } elseif ( $current_blog->domain === $current_site->domain && $current_blog->path === $current_site->path ) { |
| 476 | return $current_blog->blog_id; |
| 477 | } else { |
| 478 | $network = $current_site; |
| 479 | } |
| 480 | } else { |
| 481 | $network = wp_get_network( $network_id ); |
| 482 | } |
| 483 | |
| 484 | if ( ! $network ) { |
| 485 | return false; |
| 486 | } |
| 487 | |
| 488 | $site_id = wp_cache_get( 'network:' . $network->id . ':main_site', 'site-options' ); |
| 489 | |
| 490 | if ( ! $site_id ) { |
| 491 | $site_id = $wpdb->get_var( $wpdb->prepare( "SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s", $network->domain, $network->path ) ); |
| 492 | wp_cache_add( 'network:' . $network->id . ':main_site', $site_id, 'site-options' ); |
| 493 | } |
| 494 | |
| 495 | return $site_id; |
| 496 | } |
| 497 | No newline at end of file |