| | 471 | * Retrieves site data given a site ID or site object. |
| | 472 | * |
| | 473 | * If an object is passed then the site data will be cached and then returned |
| | 474 | * after being passed through a filter. If the site is empty, then the global |
| | 475 | * site variable will be used, if it is set. |
| | 476 | * |
| | 477 | * @since 4.6.0 |
| | 478 | * |
| | 479 | * @global WP_Site $site |
| | 480 | * |
| | 481 | * @param WP_Site|string|int $site Site to retrieve. |
| | 482 | * @param string $output Optional. OBJECT or ARRAY_A or ARRAY_N constants. |
| | 483 | * |
| | 484 | * @return WP_Site|array|null Depends on $output value. |
| | 485 | */ |
| | 486 | function get_site( &$site = null, $output = OBJECT ) { |
| | 487 | global $current_blog; |
| | 488 | if ( empty( $site ) && isset( $current_blog ) ) { |
| | 489 | $site = $current_blog; |
| | 490 | } |
| | 491 | |
| | 492 | if ( $site instanceof WP_Site ) { |
| | 493 | $_site = $site; |
| | 494 | } elseif ( is_object( $site ) ) { |
| | 495 | $_site = new WP_Site( $site ); |
| | 496 | } else { |
| | 497 | $_site = WP_Site::get_instance( $site ); |
| | 498 | } |
| | 499 | |
| | 500 | if ( ! $_site ) { |
| | 501 | return null; |
| | 502 | } |
| | 503 | |
| | 504 | /** |
| | 505 | * Fires after a site is retrieved. |
| | 506 | * |
| | 507 | * @since 4.6.0 |
| | 508 | * |
| | 509 | * @param mixed $_site Site data. |
| | 510 | */ |
| | 511 | $_site = apply_filters( 'get_site', $_site ); |
| | 512 | |
| | 513 | if ( $output == OBJECT ) { |
| | 514 | return $_site; |
| | 515 | } elseif ( $output == ARRAY_A ) { |
| | 516 | return $_site->to_array(); |
| | 517 | } elseif ( $output == ARRAY_N ) { |
| | 518 | return array_values( $_site->to_array() ); |
| | 519 | } |
| | 520 | |
| | 521 | return $_site; |
| | 522 | } |
| | 523 | |
| | 524 | /** |
| | 525 | * Adds any sites from the given ids to the cache that do not already exist in cache |
| | 526 | * |
| | 527 | * @since 4.6.0 |
| | 528 | * @access private |
| | 529 | * |
| | 530 | * @see update_site_cache() |
| | 531 | * |
| | 532 | * @global wpdb $wpdb WordPress database abstraction object. |
| | 533 | * |
| | 534 | * @param array $ids ID list. |
| | 535 | */ |
| | 536 | function _prime_site_caches( $ids ) { |
| | 537 | global $wpdb; |
| | 538 | |
| | 539 | $non_cached_ids = _get_non_cached_ids( $ids, 'sites' ); |
| | 540 | if ( ! empty( $non_cached_ids ) ) { |
| | 541 | $fresh_sites = $wpdb->get_results( sprintf( "SELECT * FROM $wpdb->blogs WHERE blog_id IN (%s)", join( ",", $non_cached_ids ) ) ); |
| | 542 | |
| | 543 | update_site_cache( $fresh_sites ); |
| | 544 | } |
| | 545 | } |
| | 546 | |
| | 547 | /** |
| | 548 | * Updates sites in cache. |
| | 549 | * |
| | 550 | * @since 4.6.0 |
| | 551 | * |
| | 552 | * @param array $sites Array of site objects, passed by reference. |
| | 553 | */ |
| | 554 | function update_site_cache( &$sites ) { |
| | 555 | if ( ! $sites ) { |
| | 556 | return; |
| | 557 | } |
| | 558 | |
| | 559 | foreach ( $sites as $site ) { |
| | 560 | wp_cache_add( $site->blog_id, $site, 'sites' ); |
| | 561 | wp_cache_add( $site->blog_id . 'short', $site, 'blog-details' ); |
| | 562 | } |
| | 563 | } |
| | 564 | |
| | 565 | /** |