get_blog_details

Retrieve the details for a blog from the blogs table and blog options.

Description

(WP_Site|false) get_blog_details( (constant) $fields = null, (bool) $get_all = true ); 

Returns (WP_Site|false)

Blog details on success. False on failure.

Parameters (2)

0. $fields — Optional. (constant) => null
A blog ID, a blog slug, or an array of fields to query against. If not specified the current blog ID is used.
1. $get_all — Optional. (bool) => true
Whether to retrieve all details or only the details in the blogs table. Default is true.

Usage

  1. if ( !function_exists( 'get_blog_details' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-blogs.php'; 
  3.  
  4. // Optional. A blog ID, a blog slug, or an array of fields to query against. 
  5. // If not specified the current blog ID is used. 
  6. $fields = null; 
  7.  
  8. // Whether to retrieve all details or only the details in the blogs table. 
  9. // Default is true. 
  10. $get_all = true; 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = get_blog_details($fields, $get_all); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/ms-blogs.php  
  1. function get_blog_details( $fields = null, $get_all = true ) { 
  2. global $wpdb; 
  3.  
  4. if ( is_array($fields ) ) { 
  5. if ( isset($fields['blog_id']) ) { 
  6. $blog_id = $fields['blog_id']; 
  7. } elseif ( isset($fields['domain']) && isset($fields['path']) ) { 
  8. $key = md5( $fields['domain'] . $fields['path'] ); 
  9. $blog = wp_cache_get($key, 'blog-lookup'); 
  10. if ( false !== $blog ) 
  11. return $blog; 
  12. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { 
  13. $nowww = substr( $fields['domain'], 4 ); 
  14. $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'] ) ); 
  15. } else { 
  16. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) ); 
  17. if ( $blog ) { 
  18. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); 
  19. $blog_id = $blog->blog_id; 
  20. } else { 
  21. return false; 
  22. } elseif ( isset($fields['domain']) && is_subdomain_install() ) { 
  23. $key = md5( $fields['domain'] ); 
  24. $blog = wp_cache_get($key, 'blog-lookup'); 
  25. if ( false !== $blog ) 
  26. return $blog; 
  27. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) { 
  28. $nowww = substr( $fields['domain'], 4 ); 
  29. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s, %s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) ); 
  30. } else { 
  31. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) ); 
  32. if ( $blog ) { 
  33. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details'); 
  34. $blog_id = $blog->blog_id; 
  35. } else { 
  36. return false; 
  37. } else { 
  38. return false; 
  39. } else { 
  40. if ( ! $fields ) 
  41. $blog_id = get_current_blog_id(); 
  42. elseif ( ! is_numeric( $fields ) ) 
  43. $blog_id = get_id_from_blogname( $fields ); 
  44. else 
  45. $blog_id = $fields; 
  46.  
  47. $blog_id = (int) $blog_id; 
  48.  
  49. $all = $get_all == true ? '' : 'short'; 
  50. $details = wp_cache_get( $blog_id . $all, 'blog-details' ); 
  51.  
  52. if ( $details ) { 
  53. if ( ! is_object( $details ) ) { 
  54. if ( $details == -1 ) { 
  55. return false; 
  56. } else { 
  57. // Clear old pre-serialized objects. Cache clients do better with that. 
  58. wp_cache_delete( $blog_id . $all, 'blog-details' ); 
  59. unset($details); 
  60. } else { 
  61. return $details; 
  62.  
  63. // Try the other cache. 
  64. if ( $get_all ) { 
  65. $details = wp_cache_get( $blog_id . 'short', 'blog-details' ); 
  66. } else { 
  67. $details = wp_cache_get( $blog_id, 'blog-details' ); 
  68. // If short was requested and full cache is set, we can return. 
  69. if ( $details ) { 
  70. if ( ! is_object( $details ) ) { 
  71. if ( $details == -1 ) { 
  72. return false; 
  73. } else { 
  74. // Clear old pre-serialized objects. Cache clients do better with that. 
  75. wp_cache_delete( $blog_id, 'blog-details' ); 
  76. unset($details); 
  77. } else { 
  78. return $details; 
  79.  
  80. if ( empty($details) ) { 
  81. $details = WP_Site::get_instance( $blog_id ); 
  82. if ( ! $details ) { 
  83. // Set the full cache. 
  84. wp_cache_set( $blog_id, -1, 'blog-details' ); 
  85. return false; 
  86.  
  87. if ( ! $details instanceof WP_Site ) { 
  88. $details = new WP_Site( $details ); 
  89.  
  90. if ( ! $get_all ) { 
  91. wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 
  92. return $details; 
  93.  
  94. switch_to_blog( $blog_id ); 
  95. $details->blogname = get_option( 'blogname' ); 
  96. $details->siteurl = get_option( 'siteurl' ); 
  97. $details->post_count = get_option( 'post_count' ); 
  98. $details->home = get_option( 'home' ); 
  99.  
  100. /** 
  101. * Filters a blog's details. 
  102. * @since MU 
  103. * @deprecated 4.7.0 Use site_details 
  104. * @param object $details The blog details. 
  105. */ 
  106. $details = apply_filters_deprecated( 'blog_details', array( $details ), '4.7.0', 'site_details' ); 
  107.  
  108. wp_cache_set( $blog_id . $all, $details, 'blog-details' ); 
  109.  
  110. $key = md5( $details->domain . $details->path ); 
  111. wp_cache_set( $key, $details, 'blog-lookup' ); 
  112.  
  113. return $details;