/bp-blogs/classes/class-bp-blogs-blog.php

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