BP_Blogs_Blog

The main BuddyPress blog class.

Defined (1)

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

/bp-blogs/classes/class-bp-blogs-blog.php  
  1. class BP_Blogs_Blog { 
  2.  
  3. /** 
  4. * Site ID. 
  5. * @var int|null 
  6. */ 
  7. public $id; 
  8.  
  9. /** 
  10. * User ID. 
  11. * @var int 
  12. */ 
  13. public $user_id; 
  14.  
  15. /** 
  16. * Blog ID. 
  17. * @var int 
  18. */ 
  19. public $blog_id; 
  20.  
  21. /** 
  22. * Constructor method. 
  23. * @param int|null $id Optional. The ID of the blog. 
  24. */ 
  25. public function __construct( $id = null ) { 
  26. if ( !empty( $id ) ) { 
  27. $this->id = (int) $id; 
  28. $this->populate(); 
  29.  
  30. /** 
  31. * Populate the object with data about the specific activity item. 
  32. */ 
  33. public function populate() { 
  34. global $wpdb; 
  35.  
  36. $bp = buddypress(); 
  37.  
  38. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name} WHERE id = %d", $this->id ) ); 
  39.  
  40. $this->user_id = (int) $blog->user_id; 
  41. $this->blog_id = (int) $blog->blog_id; 
  42.  
  43. /** 
  44. * Save the BP blog data to the database. 
  45. * @return bool True on success, false on failure. 
  46. */ 
  47. public function save() { 
  48. global $wpdb; 
  49.  
  50. $this->user_id = apply_filters( 'bp_blogs_blog_user_id_before_save', $this->user_id, $this->id ); 
  51. $this->blog_id = apply_filters( 'bp_blogs_blog_id_before_save', $this->blog_id, $this->id ); 
  52.  
  53. /** 
  54. * Fires before the current blog item gets saved. 
  55. * Please use this hook to filter the properties above. Each part will be passed in. 
  56. * @since 1.0.0 
  57. * @param BP_Blogs_Blog $this Current instance of the blog item being saved. Passed by reference. 
  58. */ 
  59. do_action_ref_array( 'bp_blogs_blog_before_save', array( &$this ) ); 
  60.  
  61. // Don't try and save if there is no user ID or blog ID set. 
  62. if ( !$this->user_id || !$this->blog_id ) 
  63. return false; 
  64.  
  65. // Don't save if this blog has already been recorded for the user. 
  66. if ( !$this->id && $this->exists() ) 
  67. return false; 
  68.  
  69. $bp = buddypress(); 
  70.  
  71. if ( $this->id ) { 
  72. // Update. 
  73. $sql = $wpdb->prepare( "UPDATE {$bp->blogs->table_name} SET user_id = %d, blog_id = %d WHERE id = %d", $this->user_id, $this->blog_id, $this->id ); 
  74. } else { 
  75. // Save. 
  76. $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name} ( user_id, blog_id ) VALUES ( %d, %d )", $this->user_id, $this->blog_id ); 
  77.  
  78. if ( !$wpdb->query($sql) ) 
  79. return false; 
  80.  
  81. /** 
  82. * Fires after the current blog item gets saved. 
  83. * Please use this hook to filter the properties above. Each part will be passed in. 
  84. * @since 1.0.0 
  85. * @param BP_Blogs_Blog $this Current instance of the blog item being saved. Passed by reference. 
  86. */ 
  87. do_action_ref_array( 'bp_blogs_blog_after_save', array( &$this ) ); 
  88.  
  89. if ( $this->id ) 
  90. return $this->id; 
  91. else 
  92. return $wpdb->insert_id; 
  93.  
  94. /** 
  95. * Check whether an association between this user and this blog exists. 
  96. * @return int $value The number of associations between the user and blog 
  97. * saved in the blog component tables. 
  98. */ 
  99. public function exists() { 
  100. global $wpdb; 
  101.  
  102. $bp = buddypress(); 
  103.  
  104. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $this->user_id, $this->blog_id ) ); 
  105.  
  106. /** Static Methods ***************************************************/ 
  107.  
  108. /** 
  109. * Retrieve a set of blog-user associations. 
  110. * @param string $type The order in which results should be returned. 
  111. * 'active', 'alphabetical', 'newest', or 'random'. 
  112. * @param int|bool $limit Optional. The maximum records to return. 
  113. * Default: false. 
  114. * @param int|bool $page Optional. The page of records to return. 
  115. * Default: false (unlimited results). 
  116. * @param int $user_id Optional. ID of the user whose blogs are being 
  117. * retrieved. Default: 0. 
  118. * @param string|bool $search_terms Optional. Search by text stored in 
  119. * blogmeta (such as the blog name). Default: false. 
  120. * @param bool $update_meta_cache Whether to pre-fetch metadata for 
  121. * blogs. Default: true. 
  122. * @param array|bool $include_blog_ids Array of blog IDs to include. 
  123. * @return array Multidimensional results array, structured as follows: 
  124. * 'blogs' - Array of located blog objects 
  125. * 'total' - A count of the total blogs matching the filter params 
  126. */ 
  127. public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false, $update_meta_cache = true, $include_blog_ids = false ) { 
  128. global $wpdb; 
  129.  
  130. $bp = buddypress(); 
  131.  
  132. if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) 
  133. $hidden_sql = "AND wb.public = 1"; 
  134. else 
  135. $hidden_sql = ''; 
  136.  
  137. $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 
  138.  
  139. $user_sql = !empty( $user_id ) ? $wpdb->prepare( " AND b.user_id = %d", $user_id ) : ''; 
  140.  
  141. switch ( $type ) { 
  142. case 'active': default: 
  143. $order_sql = "ORDER BY bm.meta_value DESC"; 
  144. break; 
  145. case 'alphabetical': 
  146. $order_sql = "ORDER BY bm_name.meta_value ASC"; 
  147. break; 
  148. case 'newest': 
  149. $order_sql = "ORDER BY wb.registered DESC"; 
  150. break; 
  151. case 'random': 
  152. $order_sql = "ORDER BY RAND()"; 
  153. break; 
  154.  
  155. $include_sql = ''; 
  156. $include_blog_ids = array_filter( wp_parse_id_list( $include_blog_ids ) ); 
  157. if ( ! empty( $include_blog_ids ) ) { 
  158. $blog_ids_sql = implode( ', ', $include_blog_ids ); 
  159. $include_sql = " AND b.blog_id IN ({$blog_ids_sql})"; 
  160.  
  161. if ( ! empty( $search_terms ) ) { 
  162. $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 
  163. $search_terms_sql = $wpdb->prepare( 'AND (bm_name.meta_value LIKE %s OR bm_description.meta_value LIKE %s)', $search_terms_like, $search_terms_like ); 
  164. } else { 
  165. $search_terms_sql = ''; 
  166.  
  167. $paged_blogs = $wpdb->get_results( " 
  168. SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm_name.meta_value as name 
  169. FROM 
  170. {$bp->blogs->table_name} b 
  171. LEFT JOIN {$bp->blogs->table_name_blogmeta} bm ON (b.blog_id = bm.blog_id) 
  172. LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) 
  173. LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) 
  174. LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) 
  175. LEFT JOIN {$wpdb->users} u ON (b.user_id = u.ID) 
  176. WHERE 
  177. wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} 
  178. AND bm.meta_key = 'last_activity' AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' 
  179. {$search_terms_sql} {$user_sql} {$include_sql} 
  180. GROUP BY b.blog_id {$order_sql} {$pag_sql} 
  181. " ); 
  182.  
  183. $total_blogs = $wpdb->get_var( " 
  184. SELECT COUNT(DISTINCT b.blog_id) 
  185. FROM 
  186. {$bp->blogs->table_name} b 
  187. LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) 
  188. LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) 
  189. LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) 
  190. WHERE 
  191. wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} 
  192. AND 
  193. bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' 
  194. {$search_terms_sql} {$user_sql} {$include_sql} 
  195. " ); 
  196.  
  197. $blog_ids = array(); 
  198. foreach ( (array) $paged_blogs as $blog ) { 
  199. $blog_ids[] = (int) $blog->blog_id; 
  200.  
  201. $paged_blogs = BP_Blogs_Blog::get_blog_extras( $paged_blogs, $blog_ids, $type ); 
  202.  
  203. // Integer casting. 
  204. foreach ( (array) $paged_blogs as $key => $data ) { 
  205. $paged_blogs[ $key ]->blog_id = (int) $paged_blogs[ $key ]->blog_id; 
  206. $paged_blogs[ $key ]->admin_user_id = (int) $paged_blogs[ $key ]->admin_user_id; 
  207.  
  208. if ( $update_meta_cache ) { 
  209. bp_blogs_update_meta_cache( $blog_ids ); 
  210.  
  211. return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); 
  212.  
  213. /** 
  214. * Delete the record of a given blog for all users. 
  215. * @param int $blog_id The blog being removed from all users. 
  216. * @return int|bool Number of rows deleted on success, false on failure. 
  217. */ 
  218. public static function delete_blog_for_all( $blog_id ) { 
  219. global $wpdb; 
  220.  
  221. bp_blogs_delete_blogmeta( $blog_id ); 
  222.  
  223. $bp = buddypress(); 
  224.  
  225. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id ) ); 
  226.  
  227. /** 
  228. * Delete the record of a given blog for a specific user. 
  229. * @param int $blog_id The blog being removed. 
  230. * @param int|null $user_id Optional. The ID of the user from whom the blog is 
  231. * being removed. If absent, defaults to the logged-in user ID. 
  232. * @return int|bool Number of rows deleted on success, false on failure. 
  233. */ 
  234. public static function delete_blog_for_user( $blog_id, $user_id = null ) { 
  235. global $wpdb; 
  236.  
  237. if ( !$user_id ) 
  238. $user_id = bp_loggedin_user_id(); 
  239.  
  240. $bp = buddypress(); 
  241.  
  242. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id ) ); 
  243.  
  244. /** 
  245. * Delete all of a user's blog associations in the BP tables. 
  246. * @param int|null $user_id Optional. The ID of the user whose blog associations 
  247. * are being deleted. If absent, defaults to logged-in user ID. 
  248. * @return int|bool Number of rows deleted on success, false on failure. 
  249. */ 
  250. public static function delete_blogs_for_user( $user_id = null ) { 
  251. global $wpdb; 
  252.  
  253. if ( !$user_id ) 
  254. $user_id = bp_loggedin_user_id(); 
  255.  
  256. $bp = buddypress(); 
  257.  
  258. return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id ) ); 
  259.  
  260. /** 
  261. * Get all of a user's blogs, as tracked by BuddyPress. 
  262. * Note that this is different from the WordPress function 
  263. * {@link get_blogs_of_user()}; the current method returns only those 
  264. * blogs that have been recorded by BuddyPress, while the WP function 
  265. * does a true query of a user's blog capabilities. 
  266. * @param int $user_id Optional. ID of the user whose blogs are being 
  267. * queried. Defaults to logged-in user. 
  268. * @param bool $show_hidden Optional. Whether to include blogs that are not marked 
  269. * public. Defaults to true when viewing one's own profile. 
  270. * @return array Multidimensional results array, structured as follows: 
  271. * 'blogs' - Array of located blog objects. 
  272. * 'total' - A count of the total blogs for the user. 
  273. */ 
  274. public static function get_blogs_for_user( $user_id = 0, $show_hidden = false ) { 
  275. global $wpdb; 
  276.  
  277. $bp = buddypress(); 
  278.  
  279. if ( !$user_id ) 
  280. $user_id = bp_displayed_user_id(); 
  281.  
  282. // Show logged in users their hidden blogs. 
  283. if ( !bp_is_my_profile() && !$show_hidden ) 
  284. $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) ); 
  285. else 
  286. $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) ); 
  287.  
  288. $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user( $user_id ); 
  289.  
  290. $user_blogs = array(); 
  291. foreach ( (array) $blogs as $blog ) { 
  292. $user_blogs[$blog->blog_id] = new stdClass; 
  293. $user_blogs[$blog->blog_id]->id = (int) $blog->id; 
  294. $user_blogs[$blog->blog_id]->blog_id = (int) $blog->blog_id; 
  295. $user_blogs[$blog->blog_id]->siteurl = ( is_ssl() ) ? 'https://' . $blog->domain . $blog->path : 'http://' . $blog->domain . $blog->path; 
  296. $user_blogs[$blog->blog_id]->name = $blog->name; 
  297.  
  298. return array( 'blogs' => $user_blogs, 'count' => $total_blog_count ); 
  299.  
  300. /** 
  301. * Get IDs of all of a user's blogs, as tracked by BuddyPress. 
  302. * This method always includes hidden blogs. 
  303. * @param int $user_id Optional. ID of the user whose blogs are being 
  304. * queried. Defaults to logged-in user. 
  305. * @return int The number of blogs associated with the user. 
  306. */ 
  307. public static function get_blog_ids_for_user( $user_id = 0 ) { 
  308. global $wpdb; 
  309.  
  310. $bp = buddypress(); 
  311.  
  312. if ( !$user_id ) 
  313. $user_id = bp_displayed_user_id(); 
  314.  
  315. return array_map( 'intval', $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id ) ) ); 
  316.  
  317. /** 
  318. * Check whether a blog has been recorded by BuddyPress. 
  319. * @param int $blog_id ID of the blog being queried. 
  320. * @return int|null The ID of the first located entry in the BP table 
  321. * on success, otherwise null. 
  322. */ 
  323. public static function is_recorded( $blog_id ) { 
  324. global $wpdb; 
  325.  
  326. $bp = buddypress(); 
  327.  
  328. $query = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id ) ); 
  329.  
  330. return is_numeric( $query ) ? (int) $query : $query; 
  331.  
  332. /** 
  333. * Return a count of associated blogs for a given user. 
  334. * Includes hidden blogs when the logged-in user is the same as the 
  335. * $user_id parameter, or when the logged-in user has the bp_moderate 
  336. * cap. 
  337. * @param int|null $user_id Optional. ID of the user whose blogs are being 
  338. * queried. Defaults to logged-in user. 
  339. * @return int Blog count for the user. 
  340. */ 
  341. public static function total_blog_count_for_user( $user_id = null ) { 
  342. global $wpdb; 
  343.  
  344. $bp = buddypress(); 
  345.  
  346. if ( !$user_id ) 
  347. $user_id = bp_displayed_user_id(); 
  348.  
  349. // If the user is logged in return the blog count including their hidden blogs. 
  350. if ( ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) || bp_current_user_can( 'bp_moderate' ) ) { 
  351. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id ) ); 
  352. } else { 
  353. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d", $user_id ) ); 
  354.  
  355. /** 
  356. * Return a list of blogs matching a search term. 
  357. * Matches against blog names and descriptions, as stored in the BP 
  358. * blogmeta table. 
  359. * @param string $filter The search term. 
  360. * @param int|null $limit Optional. The maximum number of items to return. 
  361. * Default: null (no limit). 
  362. * @param int|null $page Optional. The page of results to return. Default: 
  363. * null (no limit). 
  364. * @return array Multidimensional results array, structured as follows: 
  365. * 'blogs' - Array of located blog objects. 
  366. * 'total' - A count of the total blogs matching the query. 
  367. */ 
  368. public static function search_blogs( $filter, $limit = null, $page = null ) { 
  369. global $wpdb; 
  370.  
  371. $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 
  372. $search_terms_sql = $wpdb->prepare( 'bm.meta_value LIKE %s', $search_terms_like ); 
  373.  
  374. $hidden_sql = ''; 
  375. if ( !bp_current_user_can( 'bp_moderate' ) ) 
  376. $hidden_sql = "AND wb.public = 1"; 
  377.  
  378. $pag_sql = ''; 
  379. if ( $limit && $page ) { 
  380. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  381.  
  382. $bp = buddypress(); 
  383.  
  384. $paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND {$search_terms_sql} ) {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC{$pag_sql}" ); 
  385. $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND {$search_terms_sql} ) {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC" ); 
  386.  
  387. // Integer casting. 
  388. foreach ( (array) $paged_blogs as $key => $data ) { 
  389. $paged_blogs[ $key ]->blog_id = (int) $paged_blogs[ $key ]->blog_id; 
  390.  
  391. return array( 'blogs' => $paged_blogs, 'total' => (int) $total_blogs ); 
  392.  
  393. /** 
  394. * Retrieve a list of all blogs. 
  395. * Query will include hidden blogs if the logged-in user has the 
  396. * 'bp_moderate' cap. 
  397. * @param int|null $limit Optional. The maximum number of items to return. 
  398. * Default: null (no limit). 
  399. * @param int|null $page Optional. The page of results to return. Default: 
  400. * null (no limit). 
  401. * @return array Multidimensional results array, structured as follows: 
  402. * 'blogs' - Array of located blog objects. 
  403. * 'total' - A count of the total blogs. 
  404. */ 
  405. public static function get_all( $limit = null, $page = null ) { 
  406. global $wpdb; 
  407.  
  408. $bp = buddypress(); 
  409.  
  410. $hidden_sql = !bp_current_user_can( 'bp_moderate' ) ? "AND wb.public = 1" : ''; 
  411. $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 
  412.  
  413. $paged_blogs = $wpdb->get_results( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$pag_sql}" ); 
  414. $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql}" ); 
  415.  
  416. // Integer casting. 
  417. foreach ( (array) $paged_blogs as $key => $data ) { 
  418. $paged_blogs[ $key ]->blog_id = (int) $paged_blogs[ $key ]->blog_id; 
  419.  
  420. return array( 'blogs' => $paged_blogs, 'total' => (int) $total_blogs ); 
  421.  
  422. /** 
  423. * Retrieve a list of blogs whose names start with a given letter. 
  424. * Query will include hidden blogs if the logged-in user has the 
  425. * 'bp_moderate' cap. 
  426. * @param string $letter The letter you're looking for. 
  427. * @param int|null $limit Optional. The maximum number of items to return. 
  428. * Default: null (no limit). 
  429. * @param int|null $page Optional. The page of results to return. Default: 
  430. * null (no limit). 
  431. * @return array Multidimensional results array, structured as follows: 
  432. * 'blogs' - Array of located blog objects. 
  433. * 'total' - A count of the total blogs matching the query. 
  434. */ 
  435. public static function get_by_letter( $letter, $limit = null, $page = null ) { 
  436. global $wpdb; 
  437.  
  438. $bp = buddypress(); 
  439.  
  440. $letter_like = '%' . bp_esc_like( $letter ) . '%'; 
  441. $letter_sql = $wpdb->prepare( 'bm.meta_value LIKE %s', $letter_like ); 
  442.  
  443. $hidden_sql = ''; 
  444. if ( !bp_current_user_can( 'bp_moderate' ) ) 
  445. $hidden_sql = "AND wb.public = 1"; 
  446.  
  447. $pag_sql = ''; 
  448. if ( $limit && $page ) 
  449. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  450.  
  451. $paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND {$letter_sql} {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" ); 
  452. $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND {$letter_sql} {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" ); 
  453.  
  454. // Integer casting. 
  455. foreach ( (array) $paged_blogs as $key => $data ) { 
  456. $paged_blogs[ $key ]->blog_id = (int) $paged_blogs[ $key ]->blog_id; 
  457.  
  458. return array( 'blogs' => $paged_blogs, 'total' => (int) $total_blogs ); 
  459.  
  460. /** 
  461. * Fetch blog data not caught in the main query and append it to results array. 
  462. * Gets the following information, which is either unavailable at the 
  463. * time of the original query, or is more efficient to look up in one 
  464. * fell swoop: 
  465. * - The latest post for each blog, include Featured Image data 
  466. * - The blog description 
  467. * @param array $paged_blogs Array of results from the original query. 
  468. * @param array $blog_ids Array of IDs returned from the original query. 
  469. * @param string|bool $type Not currently used. Default: false. 
  470. * @return array $paged_blogs The located blogs array, with the extras added. 
  471. */ 
  472. public static function get_blog_extras( &$paged_blogs, &$blog_ids, $type = false ) { 
  473. global $wpdb; 
  474.  
  475. $bp = buddypress(); 
  476.  
  477. if ( empty( $blog_ids ) ) 
  478. return $paged_blogs; 
  479.  
  480. $blog_ids = implode( ', ', wp_parse_id_list( $blog_ids ) ); 
  481.  
  482. for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) { 
  483. $blog_prefix = $wpdb->get_blog_prefix( $paged_blogs[$i]->blog_id ); 
  484. $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT ID, post_content, post_title, post_excerpt, guid FROM {$blog_prefix}posts WHERE post_status = 'publish' AND post_type = 'post' AND id != 1 ORDER BY id DESC LIMIT 1" ); 
  485. $images = array(); 
  486.  
  487. // Add URLs to any Featured Image this post might have. 
  488. if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) { 
  489.  
  490. // Grab 4 sizes of the image. Thumbnail. 
  491. $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false ); 
  492. if ( ! empty( $image ) ) 
  493. $images['thumbnail'] = $image[0]; 
  494.  
  495. // Medium. 
  496. $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false ); 
  497. if ( ! empty( $image ) ) 
  498. $images['medium'] = $image[0]; 
  499.  
  500. // Large. 
  501. $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false ); 
  502. if ( ! empty( $image ) ) 
  503. $images['large'] = $image[0]; 
  504.  
  505. // Post thumbnail. 
  506. $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false ); 
  507. if ( ! empty( $image ) ) 
  508. $images['post-thumbnail'] = $image[0]; 
  509.  
  510. // Add the images to the latest_post object. 
  511. $paged_blogs[$i]->latest_post->images = $images; 
  512.  
  513. /** Fetch the blog description for each blog (as it may be empty we can't fetch it in the main query). */ 
  514. $blog_descs = $wpdb->get_results( "SELECT blog_id, meta_value as description FROM {$bp->blogs->table_name_blogmeta} WHERE meta_key = 'description' AND blog_id IN ( {$blog_ids} )" ); 
  515.  
  516. for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) { 
  517. foreach ( (array) $blog_descs as $desc ) { 
  518. if ( $desc->blog_id == $paged_blogs[$i]->blog_id ) 
  519. $paged_blogs[$i]->description = $desc->description; 
  520.  
  521. return $paged_blogs; 
  522.  
  523. /** 
  524. * Check whether a given blog is hidden. 
  525. * Checks the 'public' column in the wp_blogs table. 
  526. * @param int $blog_id The ID of the blog being checked. 
  527. * @return bool True if hidden (public = 0), false otherwise. 
  528. */ 
  529. public static function is_hidden( $blog_id ) { 
  530. global $wpdb; 
  531.  
  532. if ( !(int) $wpdb->get_var( $wpdb->prepare( "SELECT public FROM {$wpdb->base_prefix}blogs WHERE blog_id = %d", $blog_id ) ) ) { 
  533. return true; 
  534.  
  535. return false; 
  536.  
  537. /** 
  538. * Get ID of user-blog link. 
  539. * @param int $user_id ID of user. 
  540. * @param int $blog_id ID of blog. 
  541. * @return int|bool ID of user-blog link, or false if not found. 
  542. */ 
  543. public static function get_user_blog( $user_id, $blog_id ) { 
  544. global $wpdb; 
  545.  
  546. $bp = buddypress(); 
  547.  
  548. $user_blog = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id ) ); 
  549.  
  550. if ( empty( $user_blog ) ) { 
  551. $user_blog = false; 
  552. } else { 
  553. $user_blog = intval( $user_blog ); 
  554.  
  555. return $user_blog;