Changeset 41698
- Timestamp:
- 10/03/2017 04:09:50 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ms-blogs.php
r41666 r41698 534 534 535 535 return $_site; 536 } 537 538 /** 539 * Retrieves a site by a given field and value. 540 * 541 * @since 4.9.0 542 * 543 * @param string $field Name of a field to query against. Accepts 'id', 'slug', 'url', 544 * 'domain' (if a subdomain install) or 'path' (if a subdirectory install). 545 * @param string|int $value The search value for $field. 546 * @param int|null $network_id Optional. ID of the network. Default is the current network. 547 * @return WP_Site|null The site object or null if not found. 548 */ 549 function get_site_by( $field, $value, $network_id = null ) { 550 $args = array(); 551 552 // `get_sites()` will return unexpected results if empty strings are passed as arguments. 553 if ( 'slug' === $field || 'url' === $field || 'domain' === $field || 'path' === $field ) { 554 if ( ! is_string( $value ) && ! is_numeric( $value ) ) { 555 return null; 556 } 557 558 $value = trim( $value ); 559 560 if ( 0 === strlen( $value ) ) { 561 return null; 562 } 563 } 564 565 switch ( $field ) { 566 case 'id': 567 if ( ! is_numeric( $value ) ) { 568 return null; 569 } 570 $args['site__in'][] = intval( $value ); 571 break; 572 case 'slug': 573 $network = get_network( $network_id ); 574 if ( ! $network ) { 575 return null; 576 } 577 578 if ( is_subdomain_install() ) { 579 $args['domain'] = trim( $value, '/' ) . '.' . preg_replace( '|^www\.|', '', $network->domain ); 580 $args['path'] = $network->path; 581 } else { 582 $args['domain'] = $network->domain; 583 $args['path'] = $network->path . trim( $value, '/' ) . '/'; 584 } 585 break; 586 case 'url': 587 if ( 0 !== strpos( $value, 'http://' ) && 0 !== strpos( $value, 'https://' ) ) { 588 $value = 'http://' . $value; 589 } 590 591 $parts = wp_parse_url( $value ); 592 if ( ! $parts ) { 593 return null; 594 } 595 596 $args['domain'] = $parts['host']; 597 if ( ! empty( $parts['path'] ) ) { 598 $args['path'] = '/' . trim( $parts['path'], '/' ) . '/'; 599 } else { 600 $args['path'] = '/'; 601 } 602 break; 603 case 'domain': 604 if ( ! is_subdomain_install() ) { 605 return null; 606 } 607 608 $args['domain'] = $value; 609 break; 610 case 'path': 611 if ( is_subdomain_install() ) { 612 return null; 613 } 614 615 $args['path'] = '/' . trim( $value, '/' ) . '/'; 616 break; 617 default: 618 return null; 619 } 620 621 if ( isset( $args['domain'] ) && substr( $args['domain'], 0, 4 ) === 'www.' ) { 622 $nowww = substr( $args['domain'], 4 ); 623 624 $args['domain__in'] = array( $nowww, $args['domain'] ); 625 unset( $args['domain'] ); 626 627 $args['orderby'] = 'domain_length'; 628 $args['order'] = 'DESC'; 629 } 630 631 if ( isset( $args['path'] ) ) { 632 $args['path'] = str_replace( '//', '/', $args['path'] ); 633 } 634 635 if ( ! empty( $network_id ) ) { 636 $args['network_id'] = (int) $network_id; 637 } 638 639 $args['number'] = 1; 640 641 $sites = get_sites( $args ); 642 643 if ( empty( $sites ) ) { 644 return null; 645 } 646 647 return array_shift( $sites ); 536 648 } 537 649
Note: See TracChangeset
for help on using the changeset viewer.