Make WordPress Core


Ignore:
Timestamp:
10/03/2017 07:40:01 PM (9 years ago)
Author:
flixos90
Message:

Multisite: Improve get_blog_details() by using get_site_by().

get_site_by() is now the preferred way to retrieve a site object by lookup for identifying data. By using a coherent structure and get_sites() internally, it has several advantages over the direct database queries and complex code in get_blog_details(). Therefore get_blog_details() is now a wrapper for get_site_by(), providing backward compatibility fixes where necessary.

Unit tests have been adjusted to account for the blog-details and blog-lookup cache groups, which are no longer needed.

Props spacedmonkey, jeremyfelt, flixos90.
Fixes #40228.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/ms-blogs.php

    r41717 r41719  
    109109 *
    110110 * @since MU (3.0.0)
    111  *
    112  * @global wpdb $wpdb WordPress database abstraction object.
     111 * @since 4.9.0 Use get_site_by() internally.
    113112 *
    114113 * @param int|string|array $fields  Optional. A blog ID, a blog slug, or an array of fields to query against.
    … …  
    119118 */
    120119function get_blog_details( $fields = null, $get_all = true ) {
    121         global $wpdb;
    122 
    123         if ( is_array($fields ) ) {
    124                 if ( isset($fields['blog_id']) ) {
    125                         $blog_id = $fields['blog_id'];
    126                 } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
    127                         $key = md5( $fields['domain'] . $fields['path'] );
    128                         $blog = wp_cache_get($key, 'blog-lookup');
    129                         if ( false !== $blog )
    130                                 return $blog;
    131                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
    132                                 $nowww = substr( $fields['domain'], 4 );
    133                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
    134                         } else {
    135                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
    136                         }
    137                         if ( $blog ) {
    138                                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
    139                                 $blog_id = $blog->blog_id;
    140                         } else {
    141                                 return false;
    142                         }
    143                 } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
    144                         $key = md5( $fields['domain'] );
    145                         $blog = wp_cache_get($key, 'blog-lookup');
    146                         if ( false !== $blog )
    147                                 return $blog;
    148                         if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
    149                                 $nowww = substr( $fields['domain'], 4 );
    150                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
    151                         } else {
    152                                 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
    153                         }
    154                         if ( $blog ) {
    155                                 wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
    156                                 $blog_id = $blog->blog_id;
    157                         } else {
    158                                 return false;
    159                         }
     120        if ( is_array( $fields ) ) {
     121                if ( isset( $fields['blog_id'] ) ) {
     122                        $field = 'id';
     123                        $value = (int) $fields['blog_id'];
     124                } elseif ( isset( $fields['domain'] ) && isset( $fields['path'] ) ) {
     125                        $field = 'url';
     126                        $value = $fields['domain'] . '/' . ltrim( $fields['path'], '/' );
     127                } elseif ( isset( $fields['domain'] ) && is_subdomain_install() ) {
     128                        $field = 'domain';
     129                        $value = $fields['domain'];
    160130                } else {
    161131                        return false;
    162132                }
    163133        } else {
    164                 if ( ! $fields )
    165                         $blog_id = get_current_blog_id();
    166                 elseif ( ! is_numeric( $fields ) )
    167                         $blog_id = get_id_from_blogname( $fields );
    168                 else
    169                         $blog_id = $fields;
    170         }
    171 
    172         $blog_id = (int) $blog_id;
    173 
    174         $all = $get_all == true ? '' : 'short';
    175         $details = wp_cache_get( $blog_id . $all, 'blog-details' );
    176 
    177         if ( $details ) {
    178                 if ( ! is_object( $details ) ) {
    179                         if ( $details == -1 ) {
    180                                 return false;
    181                         } else {
    182                                 // Clear old pre-serialized objects. Cache clients do better with that.
    183                                 wp_cache_delete( $blog_id . $all, 'blog-details' );
    184                                 unset($details);
    185                         }
     134                if ( ! $fields ) {
     135                        $field = 'id';
     136                        $value = get_current_blog_id();
     137                } elseif ( ! is_numeric( $fields ) ) {
     138                        $field = 'slug';
     139                        $value = $fields;
    186140                } else {
    187                         return $details;
    188                 }
    189         }
    190 
    191         // Try the other cache.
     141                        $field = 'id';
     142                        $value = (int) $fields;
     143                }
     144        }
     145
     146        $site = get_site_by( $field, $value );
     147
     148        if ( ! $site ) {
     149                return false;
     150        }
     151
    192152        if ( $get_all ) {
    193                 $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
    194         } else {
    195                 $details = wp_cache_get( $blog_id, 'blog-details' );
    196                 // If short was requested and full cache is set, we can return.
    197                 if ( $details ) {
    198                         if ( ! is_object( $details ) ) {
    199                                 if ( $details == -1 ) {
    200                                         return false;
    201                                 } else {
    202                                         // Clear old pre-serialized objects. Cache clients do better with that.
    203                                         wp_cache_delete( $blog_id, 'blog-details' );
    204                                         unset($details);
    205                                 }
    206                         } else {
    207                                 return $details;
    208                         }
    209                 }
    210         }
    211 
    212         if ( empty($details) ) {
    213                 $details = WP_Site::get_instance( $blog_id );
    214                 if ( ! $details ) {
    215                         // Set the full cache.
    216                         wp_cache_set( $blog_id, -1, 'blog-details' );
    217                         return false;
    218                 }
    219         }
    220 
    221         if ( ! $details instanceof WP_Site ) {
    222                 $details = new WP_Site( $details );
    223         }
    224 
    225         if ( ! $get_all ) {
    226                 wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    227                 return $details;
    228         }
    229 
    230         switch_to_blog( $blog_id );
    231         $details->blogname   = get_option( 'blogname' );
    232         $details->siteurl    = get_option( 'siteurl' );
    233         $details->post_count = get_option( 'post_count' );
    234         $details->home       = get_option( 'home' );
    235         restore_current_blog();
    236 
    237         /**
    238          * Filters a blog's details.
    239          *
    240          * @since MU (3.0.0)
    241          * @deprecated 4.7.0 Use site_details
    242          *
    243          * @param object $details The blog details.
    244          */
    245         $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' );
    246 
    247         wp_cache_set( $blog_id . $all, $details, 'blog-details' );
    248 
    249         $key = md5( $details->domain . $details->path );
    250         wp_cache_set( $key, $details, 'blog-lookup' );
    251 
    252         return $details;
     153                // Prepopulate magic properties for backward compatibility.
     154                foreach ( array( 'blogname', 'siteurl', 'post_count', 'home' ) as $detail ) {
     155                        $site->$detail = $site->$detail;
     156                }
     157        }
     158
     159        return $site;
    253160}
    254161
Note: See TracChangeset for help on using the changeset viewer.