[wp-trac] [WordPress Trac] #40228: Use get_sites in get_blog_details

WordPress Trac noreply at wordpress.org
Thu Mar 23 14:37:30 UTC 2017


#40228: Use get_sites in get_blog_details
--------------------------------+------------------------------
 Reporter:  spacedmonkey        |       Owner:  flixos90
     Type:  enhancement         |      Status:  assigned
 Priority:  normal              |   Milestone:  Awaiting Review
Component:  Networks and Sites  |     Version:  3.0
 Severity:  normal              |  Resolution:
 Keywords:                      |     Focuses:  multisite
--------------------------------+------------------------------

Comment (by spacedmonkey):

 Here is a much less tidy version of the function but does things a much
 more quicker way. If you pass nothing or a number, just use get_site. This
 means that we don't need to spin up a while wp_site_query, which will use
 a lot lesss memory.

 {{{#!php

 function get_blog_details( $fields = null, $get_all = true ) {
         $args = array();
         $details = null;
         if ( is_numeric( $fields ) ){
                 $details = get_site( $fields );
         } elseif ( is_array( $fields ) ) {
                 if ( isset($fields['blog_id']) ) {
                         $args['site__in'][] = $fields['blog_id'];
                 }
                 if ( isset($fields['domain']) ){
                         $domains = array( $fields['domain'] );
                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' )
 {
                                 $nowww = substr( $fields['domain'], 4 );
                                 $domains[] = $nowww;
                         }
                         $args['domain__in'] = $domains;
                 }
                 if( isset($fields['path']) ) {
                         $args['path'] = $fields['path'];
                 }

         } elseif ( $fields && is_string( $fields ) ) {
                 $current_network = get_network();
                 $slug = trim( $fields, '/' );

                 if ( is_subdomain_install() ) {
                         $domain = $slug . '.' . preg_replace( '|^www\.|',
 '', $current_network->domain );
                         $path = $current_network->path;
                 } else {
                         $domain = $current_network->domain;
                         $path = $current_network->path . $slug . '/';
                 }
                 $args['path'] = $path;
                 $args['domain'] = $domain;
         } else {
                 $details = get_site();
         }
         if( null === $details ){
               if( empty( $args ) ){
                     return false;
                }

                $args['number'] = 1;
                $sites = get_sites( $args );

                if( empty( $sites ) ){
                      return false;
                }

                $details = array_shift( $sites );
         }
         $blog_id = $details->blog_id;

         if ( $get_all ) {
                 switch_to_blog( $blog_id );
                 $details->blogname   = get_option( 'blogname' );
                 $details->siteurl    = get_option( 'siteurl' );
                 $details->post_count = get_option( 'post_count' );
                 $details->home       = get_option( 'home' );
                 restore_current_blog();

         /**
          * Filters a blog's details.
          *
          * @since MU
          * @deprecated 4.7.0 Use site_details
          *
          * @param object $details The blog details.
          */
         $details = apply_filters_deprecated( 'blog_details', array(
 $details ), '4.7.0', 'site_details' );
         }
         return $details;
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40228#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list