| | 541 | * Retrieves a site by given site data. |
| | 542 | * |
| | 543 | * @since 4.8.0 |
| | 544 | * |
| | 545 | * @param int|string|array $field Name of a single field or an array of multiple fields to query against. |
| | 546 | * The supported fields are 'id', 'slug', 'domain', 'path' and 'network_id'. |
| | 547 | * @param mixed $value Optional. If $field is the name of a field, this parameter must be |
| | 548 | * provided for that field's value to query for. Otherwise it can be omitted. |
| | 549 | * Default null. |
| | 550 | * @return WP_Site|null The site object or null if not found. |
| | 551 | */ |
| | 552 | function get_site_by( $field, $value = null ) { |
| | 553 | // If only an ID is provided, take a shortcut. |
| | 554 | if ( 'id' === $field ) { |
| | 555 | return get_site( $value ); |
| | 556 | } |
| | 557 | |
| | 558 | $args = $field; |
| | 559 | if ( ! is_array( $args ) ) { |
| | 560 | $args = array( $field => $value ); |
| | 561 | } |
| | 562 | |
| | 563 | // If only an ID is provided, take a shortcut. |
| | 564 | if ( isset( $args['id'] ) && count( $args ) === 1 ) { |
| | 565 | return get_site( $args['id'] ); |
| | 566 | } |
| | 567 | |
| | 568 | if ( isset( $args['id'] ) ) { |
| | 569 | if ( ! empty( $args['site__in'] ) ) { |
| | 570 | $args['site__in'][] = $args['id']; |
| | 571 | } else { |
| | 572 | $args['site__in'] = array( $args['id'] ); |
| | 573 | } |
| | 574 | |
| | 575 | unset( $args['id'] ); |
| | 576 | } |
| | 577 | |
| | 578 | if ( isset( $args['slug'] ) ) { |
| | 579 | $network = isset( $args['network_id'] ) ? get_network( $args['network_id'] ) : get_network(); |
| | 580 | |
| | 581 | // Create domain and path from the slug. |
| | 582 | if ( is_subdomain_install() ) { |
| | 583 | $args['domain'] = trim( $args['slug'], '/' ) . '.' . preg_replace( '|^www\.|', '', $network->domain ); |
| | 584 | $args['path'] = $network->path; |
| | 585 | } else { |
| | 586 | $args['domain'] = $network->domain; |
| | 587 | $args['path'] = $network->path . trim( $args['slug'], '/' ) . '/'; |
| | 588 | } |
| | 589 | |
| | 590 | unset( $args['slug'] ); |
| | 591 | } elseif ( isset( $args['url'] ) ) { |
| | 592 | $parts = wp_parse_url( $args['url'] ); |
| | 593 | if ( ! $parts ) { |
| | 594 | return null; |
| | 595 | } |
| | 596 | |
| | 597 | // Create domain and path from the URL. |
| | 598 | $args['domain'] = $parts['host']; |
| | 599 | if ( ! empty( $parts['path'] ) ) { |
| | 600 | $args['path'] = '/' . trim( $parts['path'], '/' ) . '/'; |
| | 601 | } else { |
| | 602 | $args['path'] = '/'; |
| | 603 | } |
| | 604 | |
| | 605 | unset( $args['url'] ); |
| | 606 | } elseif ( isset( $args['domain'] ) && ! isset( $args['path'] ) ) { |
| | 607 | |
| | 608 | // Do not allow domain-only lookup if not a subdomain install. |
| | 609 | if ( ! is_subdomain_install() && empty( $args['site__in'] ) ) { |
| | 610 | return null; |
| | 611 | } |
| | 612 | } elseif ( isset( $args['path'] ) && ! isset( $args['domain'] ) ) { |
| | 613 | |
| | 614 | // Do not allow path-only lookup if not a subdirectory install. |
| | 615 | if ( is_subdomain_install() && empty( $args['site__in'] ) ) { |
| | 616 | return null; |
| | 617 | } |
| | 618 | } |
| | 619 | |
| | 620 | // If none of these critical arguments are provided, bail. |
| | 621 | if ( ! isset( $args['site__in'] ) && ! isset( $args['domain'] ) && ! isset( $args['path'] ) ) { |
| | 622 | return null; |
| | 623 | } |
| | 624 | |
| | 625 | // If the domain is prefixed with www., account for the unprefixed variant as well. |
| | 626 | if ( isset( $args['domain'] ) && substr( $args['domain'], 0, 4 ) == 'www.' ) { |
| | 627 | $nowww = substr( $args['domain'], 4 ); |
| | 628 | |
| | 629 | $args['domain__in'] = array( $nowww, $args['domain'] ); |
| | 630 | unset( $args['domain'] ); |
| | 631 | } |
| | 632 | |
| | 633 | $args['number'] = 1; |
| | 634 | $args['fields'] = ''; |
| | 635 | $args['count'] = false; |
| | 636 | |
| | 637 | $sites = get_sites( $args ); |
| | 638 | |
| | 639 | if ( empty( $sites ) ) { |
| | 640 | return null; |
| | 641 | } |
| | 642 | |
| | 643 | return array_shift( $sites ); |
| | 644 | } |
| | 645 | |
| | 646 | /** |