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