/bp-blogs/bp-blogs-functions.php

  1. <?php 
  2. /** 
  3. * Blogs component functions. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage BlogsFunctions 
  7. * @since 1.5.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * Check whether the $bp global lists an activity directory page. 
  15. * 
  16. * @since 1.5.0 
  17. * 
  18. * @return bool True if set, false if empty. 
  19. */ 
  20. function bp_blogs_has_directory() { 
  21. $bp = buddypress(); 
  22.  
  23. return (bool) !empty( $bp->pages->blogs->id ); 
  24.  
  25. /** 
  26. * Retrieve a set of blogs. 
  27. * 
  28. * @see BP_Blogs_Blog::get() for a description of arguments and return value. 
  29. * 
  30. * @param array|string $args { 
  31. * Arguments are listed here with their default values. For more 
  32. * information about the arguments, see {@link BP_Blogs_Blog::get()}. 
  33. * @type string $type Default: 'active'. 
  34. * @type int|bool $user_id Default: false. 
  35. * @type array $include_blog_ids Default: false. 
  36. * @type string|bool $search_terms Default: false. 
  37. * @type int $per_page Default: 20. 
  38. * @type int $page Default: 1. 
  39. * @type bool $update_meta_cache Whether to pre-fetch blogmeta. Default: true. 
  40. * } 
  41. * @return array See {@link BP_Blogs_Blog::get()}. 
  42. */ 
  43. function bp_blogs_get_blogs( $args = '' ) { 
  44.  
  45. // Parse query arguments. 
  46. $r = bp_parse_args( $args, array( 
  47. 'type' => 'active', // 'active', 'alphabetical', 'newest', or 'random' 
  48. 'include_blog_ids' => false, // Array of blog IDs to include 
  49. 'user_id' => false, // Limit to blogs this user can post to 
  50. 'search_terms' => false, // Limit to blogs matching these search terms 
  51. 'per_page' => 20, // The number of results to return per page 
  52. 'page' => 1, // The page to return if limiting per page 
  53. 'update_meta_cache' => true // Whether to pre-fetch blogmeta 
  54. ), 'blogs_get_blogs' ); 
  55.  
  56. // Get the blogs. 
  57. $blogs = BP_Blogs_Blog::get( 
  58. $r['type'],  
  59. $r['per_page'],  
  60. $r['page'],  
  61. $r['user_id'],  
  62. $r['search_terms'],  
  63. $r['update_meta_cache'],  
  64. $r['include_blog_ids'] 
  65. ); 
  66.  
  67. /** 
  68. * Filters a set of blogs. 
  69. * 
  70. * @since 1.2.0 
  71. * 
  72. * @param array $blogs Array of blog data. 
  73. * @param array $r Parsed query arguments. 
  74. */ 
  75. return apply_filters( 'bp_blogs_get_blogs', $blogs, $r ); 
  76.  
  77. /** 
  78. * Populate the BP blogs table with existing blogs. 
  79. * 
  80. * Warning: By default, this will remove all existing records from the BP 
  81. * blogs and blogmeta tables before re-populating the tables. 
  82. * 
  83. * @since 1.0.0 
  84. * @since 2.6.0 Accepts $args as a parameter. 
  85. * 
  86. * @param array $args { 
  87. * Array of arguments. 
  88. * @type int $offset The offset to use. 
  89. * @type int $limit The number of blogs to record at one time. 
  90. * @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded. 
  91. * @type array $site_id The network site ID to use. 
  92. * } 
  93. * 
  94. * @return bool 
  95. */ 
  96. function bp_blogs_record_existing_blogs( $args = array() ) { 
  97. global $wpdb; 
  98.  
  99. // Query for all sites in network. 
  100. $r = bp_parse_args( $args, array( 
  101. 'offset' => false === bp_get_option( '_bp_record_blogs_offset' ) ? 0 : bp_get_option( '_bp_record_blogs_offset' ),  
  102. 'limit' => 50,  
  103. 'blog_ids' => array(),  
  104. 'site_id' => $wpdb->siteid 
  105. ), 'record_existing_blogs' ); 
  106.  
  107. // Truncate all BP blogs tables if starting fresh 
  108. if ( empty( $r['offset'] ) && empty( $r['blog_ids'] ) ) { 
  109. $bp = buddypress(); 
  110.  
  111. // Truncate user blogs table 
  112. $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" ); 
  113. if ( is_wp_error( $truncate ) ) { 
  114. return false; 
  115.  
  116. // Truncate user blogmeta table 
  117. $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" ); 
  118. if ( is_wp_error( $truncate ) ) { 
  119. return false; 
  120.  
  121. // Multisite 
  122. if ( is_multisite() ) { 
  123. $sql = array(); 
  124. $sql['select'] = $wpdb->prepare( "SELECT blog_id, last_updated FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $r['site_id'] ); 
  125.  
  126. // Omit root blog if large network 
  127. if ( wp_is_large_network( 'users' ) ) { 
  128. $sql['omit_root_blog'] = $wpdb->prepare( "AND blog_id != %d", bp_get_root_blog_id() ); 
  129.  
  130. // Filter by selected blog IDs 
  131. if ( ! empty( $r['blog_ids'] ) ) { 
  132. $in = implode( ', ', wp_parse_id_list( $r['blog_ids'] ) ); 
  133. $sql['in'] = "AND blog_id IN ({$in})"; 
  134.  
  135. $sql['orderby'] = 'ORDER BY blog_id ASC'; 
  136.  
  137. $sql['limit'] = $wpdb->prepare( "LIMIT %d", $r['limit'] ); 
  138.  
  139. if ( ! empty( $r['offset'] ) ) { 
  140. $sql['offset'] = $wpdb->prepare( "OFFSET %d", $r['offset'] ); 
  141.  
  142. $blogs = $wpdb->get_results( implode( ' ', $sql ) ); 
  143.  
  144. // Record a single site. 
  145. } else { 
  146. // Just record blog for the current user only. 
  147. $record = bp_blogs_record_blog( $wpdb->blogid, get_current_user_id(), true ); 
  148.  
  149. if ( false === $record ) { 
  150. return false; 
  151. } else { 
  152. return true; 
  153.  
  154. // Bail if there are no blogs 
  155. if ( empty( $blogs ) ) { 
  156. // Make sure we remove our offset marker 
  157. if ( is_multisite() ) { 
  158. bp_delete_option( '_bp_record_blogs_offset' ); 
  159.  
  160. return false; 
  161.  
  162. // Loop through users of blogs and record the relationship. 
  163. foreach ( (array) $blogs as $blog ) { 
  164.  
  165. // Ensure that the cache is clear after the table TRUNCATE above. 
  166. wp_cache_delete( $blog->blog_id, 'blog_meta' ); 
  167.  
  168. // Get all users. 
  169. $users = get_users( array( 
  170. 'blog_id' => $blog->blog_id,  
  171. 'fields' => 'ID' 
  172. ) ); 
  173.  
  174. // Continue on if no users exist for this site (how did this happen?). 
  175. if ( empty( $users ) ) { 
  176. continue; 
  177.  
  178. // Loop through users and record their relationship to this blog. 
  179. foreach ( (array) $users as $user_id ) { 
  180. bp_blogs_add_user_to_blog( $user_id, false, $blog->blog_id ); 
  181.  
  182. // Clear cache 
  183. bp_blogs_clear_blog_object_cache( $blog->blog_id, $user_id ); 
  184.  
  185. // Update blog last activity timestamp 
  186. if ( ! empty( $blog->last_updated ) && false !== strtotime( $blog->last_updated ) ) { 
  187. bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated ); 
  188.  
  189. // See if we need to do this again 
  190. if ( is_multisite() && empty( $r['blog_ids'] ) ) { 
  191. $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] ); 
  192.  
  193. // Check if there are more blogs to record 
  194. $blog_ids = $wpdb->get_results( implode( ' ', $sql ) ); 
  195.  
  196. // We have more blogs; record offset and re-run function 
  197. if ( ! empty( $blog_ids ) ) { 
  198. bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] ); 
  199. bp_blogs_record_existing_blogs( array( 
  200. 'offset' => $r['limit'] + $r['offset'],  
  201. 'limit' => $r['limit'],  
  202. 'blog_ids' => $r['blog_ids'],  
  203. 'site_id' => $r['site_id'] 
  204. ) ); 
  205.  
  206. // Bail since we have more blogs to record. 
  207. return; 
  208.  
  209. // No more blogs; delete offset marker 
  210. } else { 
  211. bp_delete_option( '_bp_record_blogs_offset' ); 
  212.  
  213. /** 
  214. * Fires after the BP blogs tables have been populated with existing blogs. 
  215. * 
  216. * @since 2.4.0 
  217. */ 
  218. do_action( 'bp_blogs_recorded_existing_blogs' ); 
  219.  
  220. // No errors. 
  221. return true; 
  222.  
  223. /** 
  224. * Check whether a given blog should be recorded in activity streams. 
  225. * 
  226. * If $user_id is provided, you can restrict site from being recordable 
  227. * only to particular users. 
  228. * 
  229. * @since 1.7.0 
  230. * 
  231. * @param int $blog_id ID of the blog being checked. 
  232. * @param int $user_id Optional. ID of the user for whom access is being checked. 
  233. * @return bool True if blog is recordable, otherwise false. 
  234. */ 
  235. function bp_blogs_is_blog_recordable( $blog_id, $user_id = 0 ) { 
  236.  
  237. /** 
  238. * Filters whether or not a blog is globally activity stream recordable. 
  239. * 
  240. * @since 1.7.0 
  241. * 
  242. * @param bool $value Whether or not recordable. Default true. 
  243. * @param int $blog_id Current blog ID. 
  244. */ 
  245. $recordable_globally = apply_filters( 'bp_blogs_is_blog_recordable', true, $blog_id ); 
  246.  
  247. if ( !empty( $user_id ) ) { 
  248. /** 
  249. * Filters whether or not a blog is globally activity stream recordable for user. 
  250. * 
  251. * @since 1.7.0 
  252. * 
  253. * @param bool $recordable_globally Whether or not recordable. 
  254. * @param int $blog_id Current blog ID. 
  255. * @param int $user_id Current user ID. 
  256. */ 
  257. $recordable_for_user = apply_filters( 'bp_blogs_is_blog_recordable_for_user', $recordable_globally, $blog_id, $user_id ); 
  258. } else { 
  259. $recordable_for_user = $recordable_globally; 
  260.  
  261. if ( !empty( $recordable_for_user ) ) { 
  262. return true; 
  263.  
  264. return $recordable_globally; 
  265.  
  266. /** 
  267. * Check whether a given blog should be tracked by the Blogs component. 
  268. * 
  269. * If $user_id is provided, the developer can restrict site from 
  270. * being trackable only to particular users. 
  271. * 
  272. * @since 1.7.0 
  273. * 
  274. * @param int $blog_id ID of the blog being checked. 
  275. * @param int $user_id Optional. ID of the user for whom access is being checked. 
  276. * @return bool True if blog is trackable, otherwise false. 
  277. */ 
  278. function bp_blogs_is_blog_trackable( $blog_id, $user_id = 0 ) { 
  279.  
  280. /** 
  281. * Filters whether or not a blog is globally trackable. 
  282. * 
  283. * @since 1.7.0 
  284. * 
  285. * @param bool $value Whether or not trackable. 
  286. * @param int $blog_id Current blog ID. 
  287. */ 
  288. $trackable_globally = apply_filters( 'bp_blogs_is_blog_trackable', bp_blogs_is_blog_recordable( $blog_id, $user_id ), $blog_id ); 
  289.  
  290. if ( !empty( $user_id ) ) { 
  291.  
  292. /** 
  293. * Filters whether or not a blog is globally trackable for user. 
  294. * 
  295. * @since 1.7.0 
  296. * 
  297. * @param bool $value Whether or not trackable. 
  298. * @param int $blog_id Current blog ID. 
  299. * @param int $user_id Current user ID. 
  300. */ 
  301. $trackable_for_user = apply_filters( 'bp_blogs_is_blog_trackable_for_user', $trackable_globally, $blog_id, $user_id ); 
  302. } else { 
  303. $trackable_for_user = $trackable_globally; 
  304.  
  305. if ( !empty( $trackable_for_user ) ) { 
  306. return $trackable_for_user; 
  307.  
  308. return $trackable_globally; 
  309.  
  310. /** 
  311. * Make BuddyPress aware of a new site so that it can track its activity. 
  312. * 
  313. * @since 1.0.0 
  314. * 
  315. * @param int $blog_id ID of the blog being recorded. 
  316. * @param int $user_id ID of the user for whom the blog is being recorded. 
  317. * @param bool $no_activity Optional. Whether to skip recording an activity 
  318. * item about this blog creation. Default: false. 
  319. * @return bool|null Returns false on failure. 
  320. */ 
  321. function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) { 
  322.  
  323. if ( empty( $user_id ) ) 
  324. $user_id = bp_loggedin_user_id(); 
  325.  
  326. // If blog is not recordable, do not record the activity. 
  327. if ( !bp_blogs_is_blog_recordable( $blog_id, $user_id ) ) 
  328. return false; 
  329.  
  330. $name = get_blog_option( $blog_id, 'blogname' ); 
  331. $url = get_home_url( $blog_id ); 
  332.  
  333. if ( empty( $name ) ) { 
  334. $name = $url; 
  335.  
  336. $description = get_blog_option( $blog_id, 'blogdescription' ); 
  337. $close_old_posts = get_blog_option( $blog_id, 'close_comments_for_old_posts' ); 
  338. $close_days_old = get_blog_option( $blog_id, 'close_comments_days_old' ); 
  339.  
  340. $thread_depth = get_blog_option( $blog_id, 'thread_comments' ); 
  341. if ( ! empty( $thread_depth ) ) { 
  342. $thread_depth = get_blog_option( $blog_id, 'thread_comments_depth' ); 
  343. } else { 
  344. // Perhaps filter this? 
  345. $thread_depth = 1; 
  346.  
  347. $recorded_blog = new BP_Blogs_Blog; 
  348. $recorded_blog->user_id = $user_id; 
  349. $recorded_blog->blog_id = $blog_id; 
  350. $recorded_blog_id = $recorded_blog->save(); 
  351. $is_recorded = !empty( $recorded_blog_id ) ? true : false; 
  352.  
  353. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'url', $url ); 
  354. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'name', $name ); 
  355. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'description', $description ); 
  356. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', bp_core_current_time() ); 
  357. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_for_old_posts', $close_old_posts ); 
  358. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_days_old', $close_days_old ); 
  359. bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'thread_comments_depth', $thread_depth ); 
  360.  
  361. $is_private = !empty( $_POST['blog_public'] ) && (int) $_POST['blog_public'] ? false : true; 
  362.  
  363. /** 
  364. * Filters whether or not a new blog is public. 
  365. * 
  366. * @since 1.5.0 
  367. * 
  368. * @param bool $is_private Whether or not blog is public. 
  369. */ 
  370. $is_private = !apply_filters( 'bp_is_new_blog_public', !$is_private ); 
  371.  
  372. /** 
  373. * Fires after BuddyPress has been made aware of a new site for activity tracking. 
  374. * 
  375. * @since 1.0.0 
  376. * @since 2.6.0 Added $no_activity as a parameter. 
  377. * 
  378. * @param BP_Blogs_Blog $recorded_blog Current blog being recorded. Passed by reference. 
  379. * @param bool $is_private Whether or not the current blog being recorded is private. 
  380. * @param bool $is_recorded Whether or not the current blog was recorded. 
  381. * @param bool $no_activity Whether to skip recording an activity item for this blog creation. 
  382. */ 
  383. do_action_ref_array( 'bp_blogs_new_blog', array( &$recorded_blog, $is_private, $is_recorded, $no_activity ) ); 
  384. add_action( 'wpmu_new_blog', 'bp_blogs_record_blog', 10, 2 ); 
  385.  
  386. /** 
  387. * Update blog name in BuddyPress blogmeta table. 
  388. * 
  389. * @global object $wpdb DB Layer. 
  390. * 
  391. * @param string $oldvalue Value before save. Passed by do_action() but 
  392. * unused here. 
  393. * @param string $newvalue Value to change meta to. 
  394. */ 
  395. function bp_blogs_update_option_blogname( $oldvalue, $newvalue ) { 
  396. global $wpdb; 
  397.  
  398. bp_blogs_update_blogmeta( $wpdb->blogid, 'name', $newvalue ); 
  399. add_action( 'update_option_blogname', 'bp_blogs_update_option_blogname', 10, 2 ); 
  400.  
  401. /** 
  402. * Update blog description in BuddyPress blogmeta table. 
  403. * 
  404. * @global object $wpdb DB Layer. 
  405. * 
  406. * @param string $oldvalue Value before save. Passed by do_action() but 
  407. * unused here. 
  408. * @param string $newvalue Value to change meta to. 
  409. */ 
  410. function bp_blogs_update_option_blogdescription( $oldvalue, $newvalue ) { 
  411. global $wpdb; 
  412.  
  413. bp_blogs_update_blogmeta( $wpdb->blogid, 'description', $newvalue ); 
  414. add_action( 'update_option_blogdescription', 'bp_blogs_update_option_blogdescription', 10, 2 ); 
  415.  
  416. /** 
  417. * Update "Close comments for old posts" option in BuddyPress blogmeta table. 
  418. * 
  419. * @since 2.0.0 
  420. * 
  421. * @global object $wpdb DB Layer. 
  422. * 
  423. * @param string $oldvalue Value before save. Passed by do_action() but 
  424. * unused here. 
  425. * @param string $newvalue Value to change meta to. 
  426. */ 
  427. function bp_blogs_update_option_close_comments_for_old_posts( $oldvalue, $newvalue ) { 
  428. global $wpdb; 
  429.  
  430. bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_for_old_posts', $newvalue ); 
  431. add_action( 'update_option_close_comments_for_old_posts', 'bp_blogs_update_option_close_comments_for_old_posts', 10, 2 ); 
  432.  
  433. /** 
  434. * Update "Close comments after days old" option in BuddyPress blogmeta table. 
  435. * 
  436. * @since 2.0.0 
  437. * 
  438. * @global object $wpdb DB Layer. 
  439. * 
  440. * @param string $oldvalue Value before save. Passed by do_action() but 
  441. * unused here. 
  442. * @param string $newvalue Value to change meta to. 
  443. */ 
  444. function bp_blogs_update_option_close_comments_days_old( $oldvalue, $newvalue ) { 
  445. global $wpdb; 
  446.  
  447. bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_days_old', $newvalue ); 
  448. add_action( 'update_option_close_comments_days_old', 'bp_blogs_update_option_close_comments_days_old', 10, 2 ); 
  449.  
  450. /** 
  451. * When toggling threaded comments, update thread depth in blogmeta table. 
  452. * 
  453. * @since 2.0.0 
  454. * 
  455. * @global object $wpdb DB Layer. 
  456. * 
  457. * @param string $oldvalue Value before save. Passed by do_action() but 
  458. * unused here. 
  459. * @param string $newvalue Value to change meta to. 
  460. */ 
  461. function bp_blogs_update_option_thread_comments( $oldvalue, $newvalue ) { 
  462. global $wpdb; 
  463.  
  464. if ( empty( $newvalue ) ) { 
  465. $thread_depth = 1; 
  466. } else { 
  467. $thread_depth = get_option( 'thread_comments_depth' ); 
  468.  
  469. bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $thread_depth ); 
  470. add_action( 'update_option_thread_comments', 'bp_blogs_update_option_thread_comments', 10, 2 ); 
  471.  
  472. /** 
  473. * When updating comment depth, update thread depth in blogmeta table. 
  474. * 
  475. * @since 2.0.0 
  476. * 
  477. * @global object $wpdb DB Layer. 
  478. * 
  479. * @param string $oldvalue Value before save. Passed by do_action() but 
  480. * unused here. 
  481. * @param string $newvalue Value to change meta to. 
  482. */ 
  483. function bp_blogs_update_option_thread_comments_depth( $oldvalue, $newvalue ) { 
  484. global $wpdb; 
  485.  
  486. $comments_enabled = get_option( 'thread_comments' ); 
  487.  
  488. if ( $comments_enabled ) { 
  489. bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $newvalue ); 
  490. add_action( 'update_option_thread_comments_depth', 'bp_blogs_update_option_thread_comments_depth', 10, 2 ); 
  491.  
  492. /** 
  493. * Syncs site icon URLs to blogmeta. 
  494. * 
  495. * @since 2.7.0 
  496. * 
  497. * @param int|string $old_value Old value 
  498. * @param int|string $new_value New value 
  499. */ 
  500. function bp_blogs_update_option_site_icon( $old_value, $new_value ) { 
  501. if ( 0 === $new_value ) { 
  502. bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', 0 ); 
  503. bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full', 0 ); 
  504. } else { 
  505. // Save site icon URL as blogmeta. 
  506. bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', get_site_icon_url( bp_core_avatar_thumb_width() ) ); 
  507. bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full', get_site_icon_url( bp_core_avatar_full_width() ) ); 
  508. add_action( 'update_option_site_icon', 'bp_blogs_update_option_site_icon', 10, 2 ); 
  509.  
  510. /** 
  511. * Deletes the 'url' blogmeta for a site. 
  512. * 
  513. * Hooked to 'refresh_blog_details', which is notably used when editing a site 
  514. * under "Network Admin > Sites". 
  515. * 
  516. * @since 2.3.0 
  517. * 
  518. * @param int $site_id The site ID. 
  519. */ 
  520. function bp_blogs_delete_url_blogmeta( $site_id = 0 ) { 
  521. bp_blogs_delete_blogmeta( (int) $site_id, 'url' ); 
  522. add_action( 'refresh_blog_details', 'bp_blogs_delete_url_blogmeta' ); 
  523.  
  524. /** 
  525. * Record activity metadata about a published blog post. 
  526. * 
  527. * @since 2.2.0 
  528. * 
  529. * @param int $activity_id ID of the activity item. 
  530. * @param WP_Post $post Post object. 
  531. * @param array $args Array of arguments. 
  532. */ 
  533. function bp_blogs_publish_post_activity_meta( $activity_id, $post, $args ) { 
  534. if ( empty( $activity_id ) || 'post' != $post->post_type ) { 
  535. return; 
  536.  
  537. bp_activity_update_meta( $activity_id, 'post_title', $post->post_title ); 
  538.  
  539. if ( ! empty( $args['post_url'] ) ) { 
  540. $post_permalink = $args['post_url']; 
  541. } else { 
  542. $post_permalink = $post->guid; 
  543.  
  544. bp_activity_update_meta( $activity_id, 'post_url', $post_permalink ); 
  545.  
  546. // Update the blog's last activity. 
  547. bp_blogs_update_blogmeta( $args['item_id'], 'last_activity', bp_core_current_time() ); 
  548.  
  549. /** 
  550. * Fires after BuddyPress has recorded metadata about a published blog post. 
  551. * 
  552. * @since 1.0.0 
  553. * 
  554. * @param int $ID ID of the blog post being recorded. 
  555. * @param WP_Post $post WP_Post object for the current blog post. 
  556. * @param string $value ID of the user associated with the current blog post. 
  557. */ 
  558. do_action( 'bp_blogs_new_blog_post', $post->ID, $post, $args['user_id'] ); 
  559. add_action( 'bp_activity_post_type_published', 'bp_blogs_publish_post_activity_meta', 10, 3 ); 
  560.  
  561. /** 
  562. * Updates a blog post's activity meta entry during a post edit. 
  563. * 
  564. * @since 2.2.0 
  565. * @since 2.5.0 Add the post type tracking args object parameter 
  566. * 
  567. * @param WP_Post $post Post object. 
  568. * @param BP_Activity_Activity $activity Activity object. 
  569. * @param object $activity_post_object The post type tracking args object. 
  570. */ 
  571. function bp_blogs_update_post_activity_meta( $post, $activity, $activity_post_object ) { 
  572. if ( empty( $activity->id ) || empty( $activity_post_object->action_id ) ) { 
  573. return; 
  574.  
  575. // Update post title in activity meta. 
  576. $existing_title = bp_activity_get_meta( $activity->id, 'post_title' ); 
  577. if ( $post->post_title !== $existing_title ) { 
  578. bp_activity_update_meta( $activity->id, 'post_title', $post->post_title ); 
  579.  
  580. if ( ! empty( $activity_post_object->comments_tracking->action_id ) ) { 
  581. // Now update activity meta for post comments... sigh. 
  582. add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 
  583. $comments = get_comments( array( 'post_id' => $post->ID ) ); 
  584. remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 
  585.  
  586. if ( ! empty( $comments ) ) { 
  587. $activity_ids = array(); 
  588. $comment_ids = wp_list_pluck( $comments, 'comment_ID' ); 
  589.  
  590. // Set up activity args. 
  591. $args = array( 
  592. 'update_meta_cache' => false,  
  593. 'show_hidden' => true,  
  594. 'per_page' => 99999,  
  595. ); 
  596.  
  597. // Query for old-style "new_blog_comment" activity items. 
  598. $args['filter'] = array( 
  599. 'object' => $activity_post_object->comments_tracking->component_id,  
  600. 'action' => $activity_post_object->comments_tracking->action_id,  
  601. 'secondary_id' => implode( ', ', $comment_ids ),  
  602. ); 
  603.  
  604. $activities = bp_activity_get( $args ); 
  605. if ( ! empty( $activities['activities'] ) ) { 
  606. $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' ); 
  607.  
  608. // Query for activity comments connected to a blog post. 
  609. unset( $args['filter'] ); 
  610. $args['meta_query'] = array( array( 
  611. 'key' => 'bp_blogs_' . $post->post_type . '_comment_id',  
  612. 'value' => $comment_ids,  
  613. 'compare' => 'IN',  
  614. ) ); 
  615. $args['type'] = 'activity_comment'; 
  616. $args['display_comments'] = 'stream'; 
  617.  
  618. $activities = bp_activity_get( $args ); 
  619. if ( ! empty( $activities['activities'] ) ) { 
  620. $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) ); 
  621.  
  622. // Update activity meta for all found activity items. 
  623. if ( ! empty( $activity_ids ) ) { 
  624. foreach ( $activity_ids as $aid ) { 
  625. bp_activity_update_meta( $aid, 'post_title', $post->post_title ); 
  626.  
  627. unset( $activities, $activity_ids, $comment_ids, $comments ); 
  628.  
  629. // Add post comment status to activity meta if closed. 
  630. if( 'closed' == $post->comment_status ) { 
  631. bp_activity_update_meta( $activity->id, 'post_comment_status', $post->comment_status ); 
  632. } else { 
  633. bp_activity_delete_meta( $activity->id, 'post_comment_status' ); 
  634. add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 3 ); 
  635.  
  636. /** 
  637. * Update Activity and blogs meta and eventually sync comment with activity comment 
  638. * 
  639. * @since 2.5.0 
  640. * 
  641. * @param int|bool $activity_id ID of recorded activity, or false if sync is active. 
  642. * @param WP_Comment|null $comment The comment object. 
  643. * @param array $activity_args Array of activity arguments. 
  644. * @param object|null $activity_post_object The post type tracking args object. 
  645. * @return int|bool Returns false if no activity, the activity id otherwise. 
  646. */ 
  647. function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) { 
  648. if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) { 
  649. return false; 
  650.  
  651. // Set the current blog id. 
  652. $blog_id = get_current_blog_id(); 
  653.  
  654. // These activity metadatas are used to build the new_blog_comment action string 
  655. if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) { 
  656. // add some post info in activity meta 
  657. bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 
  658. bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 
  659.  
  660. // Sync comment - activity comment 
  661. if ( ! bp_disable_blogforum_comments() ) { 
  662.  
  663. if ( ! empty( $_REQUEST['action'] ) ) { 
  664. $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ); 
  665.  
  666. if ( ! empty( $existing_activity_id ) ) { 
  667. $activity_args['id'] = $existing_activity_id; 
  668.  
  669. if ( empty( $activity_post_object ) ) { 
  670. $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type ); 
  671.  
  672. if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) { 
  673. // find the parent 'new_post_type' activity entry 
  674. $parent_activity_id = bp_activity_get_activity_id( array( 
  675. 'component' => $activity_post_object->component_id,  
  676. 'type' => $activity_post_object->action_id,  
  677. 'item_id' => $blog_id,  
  678. 'secondary_item_id' => $comment->comment_post_ID 
  679. ) ); 
  680.  
  681. // Try to create a new activity item for the parent blog post. 
  682. if ( empty( $parent_activity_id ) ) { 
  683. $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post ); 
  684.  
  685. // we found the parent activity entry 
  686. // so let's go ahead and reconfigure some activity args 
  687. if ( ! empty( $parent_activity_id ) ) { 
  688. // set the parent activity entry ID 
  689. $activity_args['activity_id'] = $parent_activity_id; 
  690.  
  691. // now see if the WP parent comment has a BP activity ID 
  692. $comment_parent = 0; 
  693. if ( ! empty( $comment->comment_parent ) ) { 
  694. $comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true ); 
  695.  
  696. // WP parent comment does not have a BP activity ID 
  697. // so set to 'new_' . post_type activity ID 
  698. if ( empty( $comment_parent ) ) { 
  699. $comment_parent = $parent_activity_id; 
  700.  
  701. $activity_args['parent_id'] = $comment_parent; 
  702. $activity_args['skip_notification'] = true; 
  703.  
  704. // could not find corresponding parent activity entry 
  705. // so wipe out $args array 
  706. } else { 
  707. $activity_args = array(); 
  708.  
  709. // Record in activity streams 
  710. if ( ! empty( $activity_args ) ) { 
  711. $activity_id = bp_activity_new_comment( $activity_args ); 
  712.  
  713. if ( empty( $activity_args['id'] ) ) { 
  714. // The activity metadata to inform about the corresponding comment ID 
  715. bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID ); 
  716.  
  717. // The comment metadata to inform about the corresponding activity ID 
  718. add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id ); 
  719.  
  720. // These activity metadatas are used to build the new_blog_comment action string 
  721. if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 
  722. bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 
  723. bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 
  724.  
  725. /** 
  726. * Fires after an activity comment is added from a WP post comment. 
  727. * 
  728. * @since 2.6.0 
  729. * 
  730. * @param int $activity_id The activity comment ID. 
  731. * @param WP_Comment $post_type_comment WP Comment object. 
  732. * @param array $activity_args Activity comment arguments. 
  733. * @param object $activity_post_object The post type tracking args object. 
  734. */ 
  735. do_action( 'bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object ); 
  736.  
  737. // Update the blogs last active date 
  738. bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() ); 
  739.  
  740. if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 
  741. /** 
  742. * Fires after BuddyPress has recorded metadata about a published blog post comment. 
  743. * 
  744. * @since 2.5.0 
  745. * 
  746. * @param int $value Comment ID of the blog post comment being recorded. 
  747. * @param WP_Post $post WP_Comment object for the current blog post. 
  748. * @param string $value ID of the user associated with the current blog post comment. 
  749. */ 
  750. do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() ); 
  751.  
  752. return $activity_id; 
  753. add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 ); 
  754.  
  755. /** 
  756. * Record a user's association with a blog. 
  757. * 
  758. * This function is hooked to several WordPress actions where blog roles are 
  759. * set/changed ('add_user_to_blog', 'profile_update', 'user_register'). It 
  760. * parses the changes, and records them as necessary in the BP blog tracker. 
  761. * 
  762. * BuddyPress does not track blogs for users with the 'subscriber' role by 
  763. * default, though as of 2.1.0 you can filter 'bp_blogs_get_allowed_roles' to 
  764. * modify this behavior. 
  765. * 
  766. * @param int $user_id The ID of the user. 
  767. * @param string|bool $role User's WordPress role for this blog ID. 
  768. * @param int $blog_id Blog ID user is being added to. 
  769. * @return bool|null False on failure. 
  770. */ 
  771. function bp_blogs_add_user_to_blog( $user_id, $role = false, $blog_id = 0 ) { 
  772. global $wpdb; 
  773.  
  774. // If no blog ID was passed, use the root blog ID. 
  775. if ( empty( $blog_id ) ) { 
  776. $blog_id = isset( $wpdb->blogid ) ? $wpdb->blogid : bp_get_root_blog_id(); 
  777.  
  778. // If no role was passed, try to find the blog role. 
  779. if ( empty( $role ) ) { 
  780.  
  781. // Get user capabilities. 
  782. $key = $wpdb->get_blog_prefix( $blog_id ). 'capabilities'; 
  783. $user_roles = array_keys( (array) bp_get_user_meta( $user_id, $key, true ) ); 
  784.  
  785. // User has roles so lets. 
  786. if ( ! empty( $user_roles ) ) { 
  787.  
  788. // Get blog roles. 
  789. $blog_roles = array_keys( bp_get_current_blog_roles() ); 
  790.  
  791. // Look for blog only roles of the user. 
  792. $intersect_roles = array_intersect( $user_roles, $blog_roles ); 
  793.  
  794. // If there's a role in the array, use the first one. This isn't 
  795. // very smart, but since roles aren't exactly hierarchical, and 
  796. // WordPress does not yet have a UI for multiple user roles, it's 
  797. // fine for now. 
  798. if ( ! empty( $intersect_roles ) ) { 
  799. $role = array_shift( $intersect_roles ); 
  800.  
  801. // Bail if no role was found or role is not in the allowed roles array. 
  802. if ( empty( $role ) || ! in_array( $role, bp_blogs_get_allowed_roles() ) ) { 
  803. return false; 
  804.  
  805. // Record the blog activity for this user being added to this blog. 
  806. bp_blogs_record_blog( $blog_id, $user_id, true ); 
  807. add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 ); 
  808. add_action( 'profile_update', 'bp_blogs_add_user_to_blog' ); 
  809. add_action( 'user_register', 'bp_blogs_add_user_to_blog' ); 
  810.  
  811. /** 
  812. * The allowed blog roles a member must have to be recorded into the 
  813. * `bp_user_blogs` pointer table. 
  814. * 
  815. * This added and was made filterable in BuddyPress 2.1.0 to make it easier 
  816. * to extend the functionality of the Blogs component. 
  817. * 
  818. * @since 2.1.0 
  819. * 
  820. * @return string 
  821. */ 
  822. function bp_blogs_get_allowed_roles() { 
  823.  
  824. /** 
  825. * Filters the allowed roles a member must have to be recorded into bp_user_blogs pointer table. 
  826. * 
  827. * @since 2.1.0 
  828. * 
  829. * @param array $value Array of potential roles user needs. 
  830. */ 
  831. return apply_filters( 'bp_blogs_get_allowed_roles', array( 'contributor', 'author', 'editor', 'administrator' ) ); 
  832.  
  833. /** 
  834. * Remove a blog-user pair from BP's blog tracker. 
  835. * 
  836. * @param int $user_id ID of the user whose blog is being removed. 
  837. * @param int $blog_id Optional. ID of the blog being removed. Default: current blog ID. 
  838. */ 
  839. function bp_blogs_remove_user_from_blog( $user_id, $blog_id = 0 ) { 
  840. global $wpdb; 
  841.  
  842. if ( empty( $blog_id ) ) { 
  843. $blog_id = $wpdb->blogid; 
  844.  
  845. bp_blogs_remove_blog_for_user( $user_id, $blog_id ); 
  846. add_action( 'remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2 ); 
  847.  
  848. /** 
  849. * Rehook WP's maybe_add_existing_user_to_blog with a later priority. 
  850. * 
  851. * WordPress catches add-user-to-blog requests at init:10. In some cases, this 
  852. * can precede BP's Blogs component. This function bumps the priority of the 
  853. * core function, so that we can be sure that the Blogs component is loaded 
  854. * first. See https://buddypress.trac.wordpress.org/ticket/3916. 
  855. * 
  856. * @since 1.6.0 
  857. */ 
  858. function bp_blogs_maybe_add_user_to_blog() { 
  859. if ( ! is_multisite() ) 
  860. return; 
  861.  
  862. remove_action( 'init', 'maybe_add_existing_user_to_blog' ); 
  863. add_action( 'init', 'maybe_add_existing_user_to_blog', 20 ); 
  864. add_action( 'init', 'bp_blogs_maybe_add_user_to_blog', 1 ); 
  865.  
  866. /** 
  867. * Remove the "blog created" item from the BP blogs tracker and activity stream. 
  868. * 
  869. * @param int $blog_id ID of the blog being removed. 
  870. */ 
  871. function bp_blogs_remove_blog( $blog_id ) { 
  872.  
  873. $blog_id = (int) $blog_id; 
  874.  
  875. /** 
  876. * Fires before a "blog created" item is removed from blogs 
  877. * tracker and activity stream. 
  878. * 
  879. * @since 1.5.0 
  880. * 
  881. * @param int $blog_id ID of the blog having its item removed. 
  882. */ 
  883. do_action( 'bp_blogs_before_remove_blog', $blog_id ); 
  884.  
  885. BP_Blogs_Blog::delete_blog_for_all( $blog_id ); 
  886.  
  887. /** 
  888. * Fires after a "blog created" item has been removed from blogs 
  889. * tracker and activity stream. 
  890. * 
  891. * @since 1.0.0 
  892. * 
  893. * @param int $blog_id ID of the blog who had its item removed. 
  894. */ 
  895. do_action( 'bp_blogs_remove_blog', $blog_id ); 
  896. add_action( 'delete_blog', 'bp_blogs_remove_blog' ); 
  897.  
  898. /** 
  899. * Remove a blog from the tracker for a specific user. 
  900. * 
  901. * @param int $user_id ID of the user for whom the blog is being removed. 
  902. * @param int $blog_id ID of the blog being removed. 
  903. */ 
  904. function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) { 
  905.  
  906. $blog_id = (int) $blog_id; 
  907. $user_id = (int) $user_id; 
  908.  
  909. /** 
  910. * Fires before a blog is removed from the tracker for a specific user. 
  911. * 
  912. * @since 1.5.0 
  913. * 
  914. * @param int $blog_id ID of the blog being removed. 
  915. * @param int $user_id ID of the user having the blog removed for. 
  916. */ 
  917. do_action( 'bp_blogs_before_remove_blog_for_user', $blog_id, $user_id ); 
  918.  
  919. BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id ); 
  920.  
  921. /** 
  922. * Fires after a blog has been removed from the tracker for a specific user. 
  923. * 
  924. * @since 1.0.0 
  925. * 
  926. * @param int $blog_id ID of the blog that was removed. 
  927. * @param int $user_id ID of the user having the blog removed for. 
  928. */ 
  929. do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id ); 
  930. add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 ); 
  931.  
  932. /** 
  933. * Remove a synced activity comment from the activity stream. 
  934. * 
  935. * @since 2.5.0 
  936. * 
  937. * @param bool $deleted True when a comment post type activity was successfully removed. 
  938. * @param int $comment_id ID of the comment to be removed. 
  939. * @param object $activity_post_object The post type tracking args object. 
  940. * @param string $activity_type The post type comment activity type. 
  941. * 
  942. * @return bool True on success. False on error. 
  943. */ 
  944. function bp_blogs_post_type_remove_comment( $deleted, $comment_id, $activity_post_object, $activity_type = '' ) { 
  945. // Remove synced activity comments, if needed. 
  946. if ( ! bp_disable_blogforum_comments() ) { 
  947. // Get associated activity ID from comment meta 
  948. $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true ); 
  949.  
  950. /** 
  951. * Delete the associated activity comment & also remove 
  952. * child post comments and associated activity comments. 
  953. */ 
  954. if ( ! empty( $activity_id ) ) { 
  955. // fetch the activity comments for the activity item 
  956. $activity = bp_activity_get( array( 
  957. 'in' => $activity_id,  
  958. 'display_comments' => 'stream',  
  959. 'spam' => 'all',  
  960. ) ); 
  961.  
  962. // get all activity comment IDs for the pending deleted item 
  963. if ( ! empty( $activity['activities'] ) ) { 
  964. $activity_ids = bp_activity_recurse_comments_activity_ids( $activity ); 
  965. $activity_ids[] = $activity_id; 
  966.  
  967. // delete activity items 
  968. foreach ( $activity_ids as $activity_id ) { 
  969. bp_activity_delete( array( 
  970. 'id' => $activity_id 
  971. ) ); 
  972.  
  973. // remove associated blog comments 
  974. bp_blogs_remove_associated_blog_comments( $activity_ids ); 
  975.  
  976. // rebuild activity comment tree 
  977. BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id ); 
  978.  
  979. // Set the result 
  980. $deleted = true; 
  981.  
  982. // Backcompat for comments about the 'post' post type. 
  983. if ( 'new_blog_comment' === $activity_type ) { 
  984. /** 
  985. * Fires after a blog comment activity item was removed from activity stream. 
  986. * 
  987. * @since 1.0.0 
  988. * 
  989. * @param int $value ID for the blog associated with the removed comment. 
  990. * @param int $comment_id ID of the comment being removed. 
  991. * @param int $value ID of the current logged in user. 
  992. */ 
  993. do_action( 'bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id() ); 
  994.  
  995. return $deleted; 
  996. add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 ); 
  997.  
  998. /** 
  999. * Removes blog comments that are associated with activity comments. 
  1000. * 
  1001. * @since 2.0.0 
  1002. * 
  1003. * @see bp_blogs_remove_synced_comment() 
  1004. * @see bp_blogs_sync_delete_from_activity_comment() 
  1005. * 
  1006. * @param array $activity_ids The activity IDs to check association with blog 
  1007. * comments. 
  1008. * @param bool $force_delete Whether to force delete the comments. If false,  
  1009. * comments are trashed instead. 
  1010. */ 
  1011. function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $force_delete = true ) { 
  1012. // Query args. 
  1013. $query_args = array( 
  1014. 'meta_query' => array( 
  1015. array( 
  1016. 'key' => 'bp_activity_comment_id',  
  1017. 'value' => implode( ', ', (array) $activity_ids ),  
  1018. 'compare' => 'IN',  
  1019. ); 
  1020.  
  1021. // Get comment. 
  1022. $comment_query = new WP_Comment_Query; 
  1023. $comments = $comment_query->query( $query_args ); 
  1024.  
  1025. // Found the corresponding comments 
  1026. // let's delete them! 
  1027. foreach ( $comments as $comment ) { 
  1028. wp_delete_comment( $comment->comment_ID, $force_delete ); 
  1029.  
  1030. // If we're trashing the comment, remove the meta key as well. 
  1031. if ( empty( $force_delete ) ) { 
  1032. delete_comment_meta( $comment->comment_ID, 'bp_activity_comment_id' ); 
  1033.  
  1034. /** 
  1035. * Get the total number of blogs being tracked by BuddyPress. 
  1036. * 
  1037. * @return int $count Total blog count. 
  1038. */ 
  1039. function bp_blogs_total_blogs() { 
  1040. $count = wp_cache_get( 'bp_total_blogs', 'bp' ); 
  1041.  
  1042. if ( false === $count ) { 
  1043. $blogs = BP_Blogs_Blog::get_all(); 
  1044. $count = $blogs['total']; 
  1045. wp_cache_set( 'bp_total_blogs', $count, 'bp' ); 
  1046. return $count; 
  1047.  
  1048. /** 
  1049. * Get the total number of blogs being tracked by BP for a specific user. 
  1050. * 
  1051. * @since 1.2.0 
  1052. * 
  1053. * @param int $user_id ID of the user being queried. Default: on a user page,  
  1054. * the displayed user. Otherwise, the logged-in user. 
  1055. * @return int $count Total blog count for the user. 
  1056. */ 
  1057. function bp_blogs_total_blogs_for_user( $user_id = 0 ) { 
  1058. if ( empty( $user_id ) ) { 
  1059. $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 
  1060.  
  1061. // No user ID? do not attempt to look at cache. 
  1062. if ( empty( $user_id ) ) { 
  1063. return 0; 
  1064.  
  1065. $count = wp_cache_get( 'bp_total_blogs_for_user_' . $user_id, 'bp' ); 
  1066. if ( false === $count ) { 
  1067. $count = BP_Blogs_Blog::total_blog_count_for_user( $user_id ); 
  1068. wp_cache_set( 'bp_total_blogs_for_user_' . $user_id, $count, 'bp' ); 
  1069.  
  1070. return $count; 
  1071.  
  1072. /** 
  1073. * Remove the all data related to a given blog from the BP blogs tracker and activity stream. 
  1074. * 
  1075. * @param int $blog_id The ID of the blog to expunge. 
  1076. */ 
  1077. function bp_blogs_remove_data_for_blog( $blog_id ) { 
  1078.  
  1079. /** 
  1080. * Fires before all data related to a given blog is removed from blogs tracker 
  1081. * and activity stream. 
  1082. * 
  1083. * @since 1.5.0 
  1084. * 
  1085. * @param int $blog_id ID of the blog whose data is being removed. 
  1086. */ 
  1087. do_action( 'bp_blogs_before_remove_data_for_blog', $blog_id ); 
  1088.  
  1089. // If this is regular blog, delete all data for that blog. 
  1090. BP_Blogs_Blog::delete_blog_for_all( $blog_id ); 
  1091.  
  1092. /** 
  1093. * Fires after all data related to a given blog has been removed from blogs tracker 
  1094. * and activity stream. 
  1095. * 
  1096. * @since 1.0.0 
  1097. * 
  1098. * @param int $blog_id ID of the blog whose data is being removed. 
  1099. */ 
  1100. do_action( 'bp_blogs_remove_data_for_blog', $blog_id ); 
  1101. add_action( 'delete_blog', 'bp_blogs_remove_data_for_blog', 1 ); 
  1102.  
  1103. /** 
  1104. * Get all of a user's blogs, as tracked by BuddyPress. 
  1105. * 
  1106. * @see BP_Blogs_Blog::get_blogs_for_user() for a description of parameters 
  1107. * and return values. 
  1108. * 
  1109. * @param int $user_id See {@BP_Blogs_Blog::get_blogs_for_user()}. 
  1110. * @param bool $show_hidden See {@BP_Blogs_Blog::get_blogs_for_user()}. 
  1111. * @return array See {@BP_Blogs_Blog::get_blogs_for_user()}. 
  1112. */ 
  1113. function bp_blogs_get_blogs_for_user( $user_id, $show_hidden = false ) { 
  1114. return BP_Blogs_Blog::get_blogs_for_user( $user_id, $show_hidden ); 
  1115.  
  1116. /** 
  1117. * Retrieve a list of all blogs. 
  1118. * 
  1119. * @see BP_Blogs_Blog::get_all() for a description of parameters and return values. 
  1120. * 
  1121. * @param int|null $limit See {@BP_Blogs_Blog::get_all()}. 
  1122. * @param int|null $page See {@BP_Blogs_Blog::get_all()}. 
  1123. * @return array See {@BP_Blogs_Blog::get_all()}. 
  1124. */ 
  1125. function bp_blogs_get_all_blogs( $limit = null, $page = null ) { 
  1126. return BP_Blogs_Blog::get_all( $limit, $page ); 
  1127.  
  1128. /** 
  1129. * Retrieve a random list of blogs. 
  1130. * 
  1131. * @see BP_Blogs_Blog::get() for a description of parameters and return values. 
  1132. * 
  1133. * @param int|null $limit See {@BP_Blogs_Blog::get()}. 
  1134. * @param int|null $page See {@BP_Blogs_Blog::get()}. 
  1135. * @return array See {@BP_Blogs_Blog::get()}. 
  1136. */ 
  1137. function bp_blogs_get_random_blogs( $limit = null, $page = null ) { 
  1138. return BP_Blogs_Blog::get( 'random', $limit, $page ); 
  1139.  
  1140. /** 
  1141. * Check whether a given blog is hidden. 
  1142. * 
  1143. * @see BP_Blogs_Blog::is_hidden() for a description of parameters and return values. 
  1144. * 
  1145. * @param int $blog_id See {@BP_Blogs_Blog::is_hidden()}. 
  1146. * @return bool See {@BP_Blogs_Blog::is_hidden()}. 
  1147. */ 
  1148. function bp_blogs_is_blog_hidden( $blog_id ) { 
  1149. return BP_Blogs_Blog::is_hidden( $blog_id ); 
  1150.  
  1151. /** 
  1152. * Blog meta functions 
  1153. * 
  1154. * These functions are used to store specific blogmeta in one global table,  
  1155. * rather than in each blog's options table. Significantly speeds up global blog 
  1156. * queries. By default each blog's name, description and last updated time are 
  1157. * stored and synced here. 
  1158. */ 
  1159.  
  1160. /** 
  1161. * Delete a metadata from the DB for a blog. 
  1162. * 
  1163. * @global object $wpdb WordPress database access object. 
  1164. * 
  1165. * @param int $blog_id ID of the blog whose metadata is being deleted. 
  1166. * @param string|bool $meta_key Optional. The key of the metadata being deleted. If 
  1167. * omitted, all BP metadata associated with the blog will 
  1168. * be deleted. 
  1169. * @param string|bool $meta_value Optional. If present, the metadata will only be 
  1170. * deleted if the meta_value matches this parameter. 
  1171. * @param bool $delete_all Optional. If true, delete matching metadata entries for 
  1172. * all objects, ignoring the specified blog_id. Otherwise, only 
  1173. * delete matching metadata entries for the specified blog. 
  1174. * Default: false. 
  1175. * @return bool True on success, false on failure. 
  1176. */ 
  1177. function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false, $delete_all = false ) { 
  1178. global $wpdb; 
  1179.  
  1180. // Legacy - if no meta_key is passed, delete all for the blog_id. 
  1181. if ( empty( $meta_key ) ) { 
  1182. $keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->blogmeta} WHERE blog_id = %d", $blog_id ) ); 
  1183. $delete_all = false; 
  1184. } else { 
  1185. $keys = array( $meta_key ); 
  1186.  
  1187. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1188.  
  1189. $retval = false; 
  1190. foreach ( $keys as $key ) { 
  1191. $retval = delete_metadata( 'blog', $blog_id, $key, $meta_value, $delete_all ); 
  1192.  
  1193. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1194.  
  1195. return $retval; 
  1196.  
  1197. /** 
  1198. * Get metadata for a given blog. 
  1199. * 
  1200. * @since 1.2.0 
  1201. * 
  1202. * @global object $wpdb WordPress database access object. 
  1203. * 
  1204. * @param int $blog_id ID of the blog whose metadata is being requested. 
  1205. * @param string $meta_key Optional. If present, only the metadata matching 
  1206. * that meta key will be returned. Otherwise, all 
  1207. * metadata for the blog will be fetched. 
  1208. * @param bool $single Optional. If true, return only the first value of the 
  1209. * specified meta_key. This parameter has no effect if 
  1210. * meta_key is not specified. Default: true. 
  1211. * @return mixed The meta value(s) being requested. 
  1212. */ 
  1213. function bp_blogs_get_blogmeta( $blog_id, $meta_key = '', $single = true ) { 
  1214. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1215. $retval = get_metadata( 'blog', $blog_id, $meta_key, $single ); 
  1216. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1217.  
  1218. return $retval; 
  1219.  
  1220. /** 
  1221. * Update a piece of blog meta. 
  1222. * 
  1223. * @global object $wpdb WordPress database access object. 
  1224. * 
  1225. * @param int $blog_id ID of the blog whose metadata is being updated. 
  1226. * @param string $meta_key Key of the metadata being updated. 
  1227. * @param mixed $meta_value Value to be set. 
  1228. * @param mixed $prev_value Optional. If specified, only update existing 
  1229. * metadata entries with the specified value. 
  1230. * Otherwise, update all entries. 
  1231. * @return bool|int Returns false on failure. On successful update of existing 
  1232. * metadata, returns true. On successful creation of new metadata,  
  1233. * returns the integer ID of the new metadata row. 
  1234. */ 
  1235. function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value, $prev_value = '' ) { 
  1236. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1237. $retval = update_metadata( 'blog', $blog_id, $meta_key, $meta_value, $prev_value ); 
  1238. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1239.  
  1240. return $retval; 
  1241.  
  1242. /** 
  1243. * Add a piece of blog metadata. 
  1244. * 
  1245. * @since 2.0.0 
  1246. * 
  1247. * @param int $blog_id ID of the blog. 
  1248. * @param string $meta_key Metadata key. 
  1249. * @param mixed $meta_value Metadata value. 
  1250. * @param bool $unique Optional. Whether to enforce a single metadata value 
  1251. * for the given key. If true, and the object already has a value for 
  1252. * the key, no change will be made. Default: false. 
  1253. * @return int|bool The meta ID on successful update, false on failure. 
  1254. */ 
  1255. function bp_blogs_add_blogmeta( $blog_id, $meta_key, $meta_value, $unique = false ) { 
  1256. add_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1257. $retval = add_metadata( 'blog', $blog_id, $meta_key, $meta_value, $unique ); 
  1258. remove_filter( 'query', 'bp_filter_metaid_column_name' ); 
  1259.  
  1260. return $retval; 
  1261. /** 
  1262. * Remove all blog associations for a given user. 
  1263. * 
  1264. * @param int $user_id ID whose blog data should be removed. 
  1265. * @return bool Returns false on failure. 
  1266. */ 
  1267. function bp_blogs_remove_data( $user_id ) { 
  1268. if ( !is_multisite() ) 
  1269. return false; 
  1270.  
  1271. /** 
  1272. * Fires before all blog associations are removed for a given user. 
  1273. * 
  1274. * @since 1.5.0 
  1275. * 
  1276. * @param int $user_id ID of the user whose blog associations are being removed. 
  1277. */ 
  1278. do_action( 'bp_blogs_before_remove_data', $user_id ); 
  1279.  
  1280. // If this is regular blog, delete all data for that blog. 
  1281. BP_Blogs_Blog::delete_blogs_for_user( $user_id ); 
  1282.  
  1283. /** 
  1284. * Fires after all blog associations are removed for a given user. 
  1285. * 
  1286. * @since 1.0.0 
  1287. * 
  1288. * @param int $user_id ID of the user whose blog associations were removed. 
  1289. */ 
  1290. do_action( 'bp_blogs_remove_data', $user_id ); 
  1291. add_action( 'wpmu_delete_user', 'bp_blogs_remove_data' ); 
  1292. add_action( 'delete_user', 'bp_blogs_remove_data' ); 
  1293. add_action( 'bp_make_spam_user', 'bp_blogs_remove_data' ); 
  1294.  
  1295. /** 
  1296. * Restore all blog associations for a given user. 
  1297. * 
  1298. * @since 2.2.0 
  1299. * 
  1300. * @param int $user_id ID whose blog data should be restored. 
  1301. */ 
  1302. function bp_blogs_restore_data( $user_id = 0 ) { 
  1303. if ( ! is_multisite() ) { 
  1304. return; 
  1305.  
  1306. // Get the user's blogs. 
  1307. $user_blogs = get_blogs_of_user( $user_id ); 
  1308. if ( empty( $user_blogs ) ) { 
  1309. return; 
  1310.  
  1311. $blogs = array_keys( $user_blogs ); 
  1312.  
  1313. foreach ( $blogs as $blog_id ) { 
  1314. bp_blogs_add_user_to_blog( $user_id, false, $blog_id ); 
  1315. add_action( 'bp_make_ham_user', 'bp_blogs_restore_data', 10, 1 ); 
.