Changeset 41884 for trunk/src/wp-includes/ms-blogs.php
- Timestamp:
- 10/16/2017 10:34:07 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ms-blogs.php
r41883 r41884 71 71 72 72 /** 73 * Retrieves a site 's ID given its (subdomain or directory) slug.73 * Retrieves a sites ID given its (subdomain or directory) slug. 74 74 * 75 75 * @since MU (3.0.0) 76 76 * @since 4.7.0 Converted to use get_sites(). 77 * @since 4.9.0 Converted to use get_site_by().78 77 * 79 78 * @param string $slug A site's slug. … … 81 80 */ 82 81 function get_id_from_blogname( $slug ) { 83 $result = get_site_by( 'slug', $slug ); 84 85 if ( ! $result ) { 82 $current_network = get_network(); 83 $slug = trim( $slug, '/' ); 84 85 if ( is_subdomain_install() ) { 86 $domain = $slug . '.' . preg_replace( '|^www\.|', '', $current_network->domain ); 87 $path = $current_network->path; 88 } else { 89 $domain = $current_network->domain; 90 $path = $current_network->path . $slug . '/'; 91 } 92 93 $site_ids = get_sites( array( 94 'number' => 1, 95 'fields' => 'ids', 96 'domain' => $domain, 97 'path' => $path, 98 ) ); 99 100 if ( empty( $site_ids ) ) { 86 101 return null; 87 102 } 88 103 89 return $result->id;104 return array_shift( $site_ids ); 90 105 } 91 106 … … 527 542 528 543 return $_site; 529 }530 531 /**532 * Retrieves a site by a given field and value.533 *534 * @since 4.9.0535 *536 * @param string $field Name of a field to query against. Accepts 'id', 'slug', 'url',537 * 'domain' (if a subdomain install) or 'path' (if a subdirectory install).538 * @param string|int $value The search value for $field.539 * @param int|null $network_id Optional. ID of the network. Default is the current network.540 * @return WP_Site|null The site object or null if not found.541 */542 function get_site_by( $field, $value, $network_id = null ) {543 $args = array();544 545 // `get_sites()` will return unexpected results if empty strings are passed as arguments.546 if ( 'slug' === $field || 'url' === $field || 'domain' === $field || 'path' === $field ) {547 if ( ! is_string( $value ) && ! is_numeric( $value ) ) {548 return null;549 }550 551 $value = trim( $value );552 553 if ( 0 === strlen( $value ) ) {554 return null;555 }556 }557 558 switch ( $field ) {559 case 'id':560 if ( ! is_numeric( $value ) ) {561 return null;562 }563 $args['site__in'][] = intval( $value );564 break;565 case 'slug':566 $network = get_network( $network_id );567 if ( ! $network ) {568 return null;569 }570 571 if ( is_subdomain_install() ) {572 $args['domain'] = trim( $value, '/' ) . '.' . preg_replace( '|^www\.|', '', $network->domain );573 $args['path'] = $network->path;574 } else {575 $args['domain'] = $network->domain;576 $args['path'] = $network->path . trim( $value, '/' ) . '/';577 }578 break;579 case 'url':580 if ( 0 !== strpos( $value, 'http://' ) && 0 !== strpos( $value, 'https://' ) ) {581 $value = 'http://' . $value;582 }583 584 $parts = wp_parse_url( $value );585 if ( ! $parts ) {586 return null;587 }588 589 $args['domain'] = $parts['host'];590 if ( ! empty( $parts['path'] ) ) {591 $args['path'] = '/' . trim( $parts['path'], '/' ) . '/';592 } else {593 $args['path'] = '/';594 }595 break;596 case 'domain':597 if ( ! is_subdomain_install() ) {598 return null;599 }600 601 $args['domain'] = $value;602 break;603 case 'path':604 if ( is_subdomain_install() ) {605 return null;606 }607 608 $args['path'] = '/' . trim( $value, '/' ) . '/';609 break;610 default:611 return null;612 }613 614 if ( isset( $args['domain'] ) && substr( $args['domain'], 0, 4 ) === 'www.' ) {615 $nowww = substr( $args['domain'], 4 );616 617 $args['domain__in'] = array( $nowww, $args['domain'] );618 unset( $args['domain'] );619 620 $args['orderby'] = 'domain_length';621 $args['order'] = 'DESC';622 }623 624 if ( isset( $args['path'] ) ) {625 $args['path'] = str_replace( '//', '/', $args['path'] );626 }627 628 if ( ! empty( $network_id ) ) {629 $args['network_id'] = (int) $network_id;630 }631 632 $args['number'] = 1;633 634 $sites = get_sites( $args );635 636 if ( empty( $sites ) ) {637 return null;638 }639 640 return array_shift( $sites );641 544 } 642 545
Note: See TracChangeset
for help on using the changeset viewer.