/includes/extend/buddypress/functions.php

  1. <?php 
  2.  
  3. /** 
  4. * Main bbPress BuddyPress Class 
  5. * 
  6. * @package bbPress 
  7. * @subpackage BuddyPress 
  8. * @todo maybe move to BuddyPress Forums once bbPress 1.1 can be removed 
  9. */ 
  10.  
  11. // Exit if accessed directly 
  12. if ( !defined( 'ABSPATH' ) ) exit; 
  13.  
  14. /** BuddyPress Helpers ********************************************************/ 
  15.  
  16. /** 
  17. * Return bbPress's component name/ID ('forums' by default) 
  18. * 
  19. * This is used primarily for Notifications integration. 
  20. * 
  21. * @since bbPress (r5232) 
  22. * @return string 
  23. */ 
  24. function bbp_get_component_name() { 
  25.  
  26. // Use existing ID 
  27. if ( !empty( bbpress()->extend->buddypress->id ) ) { 
  28. $retval = bbpress()->extend->buddypress->id; 
  29.  
  30. // Use default 
  31. } else { 
  32. $retval = 'forums'; 
  33.  
  34. return apply_filters( 'bbp_get_component_name', $retval ); 
  35.  
  36. /** 
  37. * Filter the current bbPress user ID with the current BuddyPress user ID 
  38. * 
  39. * @since bbPress (r3552) 
  40. * 
  41. * @param int $user_id 
  42. * @param bool $displayed_user_fallback 
  43. * @param bool $current_user_fallback 
  44. * 
  45. * @return int User ID 
  46. */ 
  47. function bbp_filter_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) { 
  48.  
  49. // Define local variable 
  50. $bbp_user_id = 0; 
  51.  
  52. // Get possible user ID's 
  53. $did = bp_displayed_user_id(); 
  54. $lid = bp_loggedin_user_id(); 
  55.  
  56. // Easy empty checking 
  57. if ( !empty( $user_id ) && is_numeric( $user_id ) ) 
  58. $bbp_user_id = $user_id; 
  59.  
  60. // Currently viewing or editing a user 
  61. elseif ( ( true === $displayed_user_fallback ) && !empty( $did ) ) 
  62. $bbp_user_id = $did; 
  63.  
  64. // Maybe fallback on the current_user ID 
  65. elseif ( ( true === $current_user_fallback ) && !empty( $lid ) ) 
  66. $bbp_user_id = $lid; 
  67.  
  68. return $bbp_user_id; 
  69. add_filter( 'bbp_get_user_id', 'bbp_filter_user_id', 10, 3 ); 
  70.  
  71. /** 
  72. * Filter the bbPress is_single_user function with BuddyPress eqivalent 
  73. * 
  74. * @since bbPress (r3552) 
  75. * 
  76. * @param bool $is Optional. Default false 
  77. * @return bool True if viewing single user, false if not 
  78. */ 
  79. function bbp_filter_is_single_user( $is = false ) { 
  80. if ( !empty( $is ) ) 
  81. return $is; 
  82.  
  83. return bp_is_user(); 
  84. add_filter( 'bbp_is_single_user', 'bbp_filter_is_single_user', 10, 1 ); 
  85.  
  86. /** 
  87. * Filter the bbPress is_user_home function with BuddyPress eqivalent 
  88. * 
  89. * @since bbPress (r3552) 
  90. * 
  91. * @param bool $is Optional. Default false 
  92. * @return bool True if viewing single user, false if not 
  93. */ 
  94. function bbp_filter_is_user_home( $is = false ) { 
  95. if ( !empty( $is ) ) 
  96. return $is; 
  97.  
  98. return bp_is_my_profile(); 
  99. add_filter( 'bbp_is_user_home', 'bbp_filter_is_user_home', 10, 1 ); 
  100.  
  101. /** 
  102. * Add the topic title to the <title> if viewing a single group forum topic 
  103. * 
  104. * @since bbPress (r5161) 
  105. * 
  106. * @param string $new_title The title to filter 
  107. * @param string $old_title (Not used) 
  108. * @param string $sep The separator to use 
  109. * @return string The possibly modified title 
  110. */ 
  111. function bbp_filter_modify_page_title( $new_title = '', $old_title = '', $sep = '' ) { 
  112.  
  113. // Only filter if group forums are active 
  114. if ( bbp_is_group_forums_active() ) { 
  115.  
  116. // Only filter for single group forum topics 
  117. if ( bp_is_group_forum_topic() || bp_is_group_forum_topic_edit() ) { 
  118.  
  119. // Get the topic 
  120. $topic = get_posts( array( 
  121. 'name' => bp_action_variable( 1 ),  
  122. 'post_status' => 'publish',  
  123. 'post_type' => bbp_get_topic_post_type(),  
  124. 'numberposts' => 1 
  125. ) ); 
  126.  
  127. // Add the topic title to the <title> 
  128. $new_title .= bbp_get_topic_title( $topic[0]->ID ) . ' ' . $sep . ' '; 
  129.  
  130. // Return the title 
  131. return $new_title; 
  132. add_action( 'bp_modify_page_title', 'bbp_filter_modify_page_title', 10, 3 ); 
  133.  
  134. /** BuddyPress Screens ********************************************************/ 
  135.  
  136. /** 
  137. * Hook bbPress topics template into plugins template 
  138. * 
  139. * @since bbPress (r3552) 
  140. * 
  141. * @uses add_action() To add the content hook 
  142. * @uses bp_core_load_template() To load the plugins template 
  143. */ 
  144. function bbp_member_forums_screen_topics() { 
  145. add_action( 'bp_template_content', 'bbp_member_forums_topics_content' ); 
  146. bp_core_load_template( apply_filters( 'bbp_member_forums_screen_topics', 'members/single/plugins' ) ); 
  147.  
  148. /** 
  149. * Hook bbPress replies template into plugins template 
  150. * 
  151. * @since bbPress (r3552) 
  152. * 
  153. * @uses add_action() To add the content hook 
  154. * @uses bp_core_load_template() To load the plugins template 
  155. */ 
  156. function bbp_member_forums_screen_replies() { 
  157. add_action( 'bp_template_content', 'bbp_member_forums_replies_content' ); 
  158. bp_core_load_template( apply_filters( 'bbp_member_forums_screen_replies', 'members/single/plugins' ) ); 
  159.  
  160. /** 
  161. * Hook bbPress favorites template into plugins template 
  162. * 
  163. * @since bbPress (r3552) 
  164. * 
  165. * @uses add_action() To add the content hook 
  166. * @uses bp_core_load_template() To load the plugins template 
  167. */ 
  168. function bbp_member_forums_screen_favorites() { 
  169. add_action( 'bp_template_content', 'bbp_member_forums_favorites_content' ); 
  170. bp_core_load_template( apply_filters( 'bbp_member_forums_screen_favorites', 'members/single/plugins' ) ); 
  171.  
  172. /** 
  173. * Hook bbPress subscriptions template into plugins template 
  174. * 
  175. * @since bbPress (r3552) 
  176. * 
  177. * @uses add_action() To add the content hook 
  178. * @uses bp_core_load_template() To load the plugins template 
  179. */ 
  180. function bbp_member_forums_screen_subscriptions() { 
  181. add_action( 'bp_template_content', 'bbp_member_forums_subscriptions_content' ); 
  182. bp_core_load_template( apply_filters( 'bbp_member_forums_screen_subscriptions', 'members/single/plugins' ) ); 
  183.  
  184. /** BuddyPress Templates ******************************************************/ 
  185.  
  186. /** 
  187. * Get the topics created template part 
  188. * 
  189. * @since bbPress (r3552) 
  190. * 
  191. * @uses bbp_get_template_part()s 
  192. */ 
  193. function bbp_member_forums_topics_content() { 
  194. ?> 
  195.  
  196. <div id="bbpress-forums"> 
  197.  
  198. <?php bbp_get_template_part( 'user', 'topics-created' ); ?> 
  199.  
  200. </div> 
  201.  
  202. <?php 
  203.  
  204. /** 
  205. * Get the topics replied to template part 
  206. * 
  207. * @since bbPress (r3552) 
  208. * 
  209. * @uses bbp_get_template_part() 
  210. */ 
  211. function bbp_member_forums_replies_content() { 
  212. ?> 
  213.  
  214. <div id="bbpress-forums"> 
  215.  
  216. <?php bbp_get_template_part( 'user', 'replies-created' ); ?> 
  217.  
  218. </div> 
  219.  
  220. <?php 
  221.  
  222. /** 
  223. * Get the topics favorited template part 
  224. * 
  225. * @since bbPress (r3552) 
  226. * 
  227. * @uses bbp_get_template_part() 
  228. */ 
  229. function bbp_member_forums_favorites_content() { 
  230. ?> 
  231.  
  232. <div id="bbpress-forums"> 
  233.  
  234. <?php bbp_get_template_part( 'user', 'favorites' ); ?> 
  235.  
  236. </div> 
  237.  
  238. <?php 
  239.  
  240. /** 
  241. * Get the topics subscribed template part 
  242. * 
  243. * @since bbPress (r3552) 
  244. * 
  245. * @uses bbp_get_template_part() 
  246. */ 
  247. function bbp_member_forums_subscriptions_content() { 
  248. ?> 
  249.  
  250. <div id="bbpress-forums"> 
  251.  
  252. <?php bbp_get_template_part( 'user', 'subscriptions' ); ?> 
  253.  
  254. </div> 
  255.  
  256. <?php 
  257.  
  258. /** Forum/Group Sync **********************************************************/ 
  259.  
  260. /** 
  261. * These functions are used to keep the many-to-many relationships between 
  262. * groups and forums synchronized. Each forum and group stores ponters to each 
  263. * other in their respective meta. This way if a group or forum is deleted 
  264. * their associattions can be updated without much effort. 
  265. */ 
  266.  
  267. /** 
  268. * Get forum ID's for a group 
  269. * 
  270. * @param type $group_id 
  271. * @since bbPress (r3653) 
  272. */ 
  273. function bbp_get_group_forum_ids( $group_id = 0 ) { 
  274.  
  275. // Assume no forums 
  276. $forum_ids = array(); 
  277.  
  278. // Use current group if none is set 
  279. if ( empty( $group_id ) ) 
  280. $group_id = bp_get_current_group_id(); 
  281.  
  282. // Get the forums 
  283. if ( !empty( $group_id ) ) 
  284. $forum_ids = groups_get_groupmeta( $group_id, 'forum_id' ); 
  285.  
  286. // Make sure result is an array 
  287. if ( !is_array( $forum_ids ) ) 
  288. $forum_ids = (array) $forum_ids; 
  289.  
  290. // Trim out any empty array items 
  291. $forum_ids = array_filter( $forum_ids ); 
  292.  
  293. return (array) apply_filters( 'bbp_get_group_forum_ids', $forum_ids, $group_id ); 
  294.  
  295. /** 
  296. * Get group ID's for a forum 
  297. * 
  298. * @param type $forum_id 
  299. * @since bbPress (r3653) 
  300. */ 
  301. function bbp_get_forum_group_ids( $forum_id = 0 ) { 
  302.  
  303. // Assume no forums 
  304. $group_ids = array(); 
  305.  
  306. // Use current group if none is set 
  307. if ( empty( $forum_id ) ) 
  308. $forum_id = bbp_get_forum_id(); 
  309.  
  310. // Get the forums 
  311. if ( !empty( $forum_id ) ) 
  312. $group_ids = get_post_meta( $forum_id, '_bbp_group_ids', true ); 
  313.  
  314. // Make sure result is an array 
  315. if ( !is_array( $group_ids ) ) 
  316. $group_ids = (array) $group_ids; 
  317.  
  318. // Trim out any empty array items 
  319. $group_ids = array_filter( $group_ids ); 
  320.  
  321. return (array) apply_filters( 'bbp_get_forum_group_ids', $group_ids, $forum_id ); 
  322.  
  323. /** 
  324. * Get forum ID's for a group 
  325. * 
  326. * @param type $group_id 
  327. * @since bbPress (r3653) 
  328. */ 
  329. function bbp_update_group_forum_ids( $group_id = 0, $forum_ids = array() ) { 
  330.  
  331. // Use current group if none is set 
  332. if ( empty( $group_id ) ) 
  333. $group_id = bp_get_current_group_id(); 
  334.  
  335. // Trim out any empties 
  336. $forum_ids = array_filter( $forum_ids ); 
  337.  
  338. // Get the forums 
  339. return groups_update_groupmeta( $group_id, 'forum_id', $forum_ids ); 
  340.  
  341. /** 
  342. * Update group ID's for a forum 
  343. * 
  344. * @param type $forum_id 
  345. * @since bbPress (r3653) 
  346. */ 
  347. function bbp_update_forum_group_ids( $forum_id = 0, $group_ids = array() ) { 
  348. $forum_id = bbp_get_forum_id( $forum_id ); 
  349.  
  350. // Trim out any empties 
  351. $group_ids = array_filter( $group_ids ); 
  352.  
  353. // Get the forums 
  354. return update_post_meta( $forum_id, '_bbp_group_ids', $group_ids ); 
  355.  
  356. /** 
  357. * Add a group to a forum 
  358. * 
  359. * @param type $group_id 
  360. * @since bbPress (r3653) 
  361. */ 
  362. function bbp_add_group_id_to_forum( $forum_id = 0, $group_id = 0 ) { 
  363.  
  364. // Validate forum_id 
  365. $forum_id = bbp_get_forum_id( $forum_id ); 
  366.  
  367. // Use current group if none is set 
  368. if ( empty( $group_id ) ) 
  369. $group_id = bp_get_current_group_id(); 
  370.  
  371. // Get current group IDs 
  372. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  373.  
  374. // Maybe update the groups forums 
  375. if ( !in_array( $group_id, $group_ids ) ) { 
  376. $group_ids[] = $group_id; 
  377. return bbp_update_forum_group_ids( $forum_id, $group_ids ); 
  378.  
  379. /** 
  380. * Remove a forum from a group 
  381. * 
  382. * @param type $group_id 
  383. * @since bbPress (r3653) 
  384. */ 
  385. function bbp_add_forum_id_to_group( $group_id = 0, $forum_id = 0 ) { 
  386.  
  387. // Validate forum_id 
  388. $forum_id = bbp_get_forum_id( $forum_id ); 
  389.  
  390. // Use current group if none is set 
  391. if ( empty( $group_id ) ) 
  392. $group_id = bp_get_current_group_id(); 
  393.  
  394. // Get current group IDs 
  395. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  396.  
  397. // Maybe update the groups forums 
  398. if ( !in_array( $forum_id, $forum_ids ) ) { 
  399. $forum_ids[] = $forum_id; 
  400. return bbp_update_group_forum_ids( $group_id, $forum_ids ); 
  401.  
  402. /** 
  403. * Remove a group from a forum 
  404. * 
  405. * @param type $group_id 
  406. * @since bbPress (r3653) 
  407. */ 
  408. function bbp_remove_group_id_from_forum( $forum_id = 0, $group_id = 0 ) { 
  409.  
  410. // Validate forum_id 
  411. $forum_id = bbp_get_forum_id( $forum_id ); 
  412.  
  413. // Use current group if none is set 
  414. if ( empty( $group_id ) ) 
  415. $group_id = bp_get_current_group_id(); 
  416.  
  417. // Get current group IDs 
  418. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  419.  
  420. // Maybe update the groups forums 
  421. if ( in_array( $group_id, $group_ids ) ) { 
  422. $group_ids = array_diff( array_values( $group_ids ), (array) $group_id ); 
  423. return bbp_update_forum_group_ids( $forum_id, $group_ids ); 
  424.  
  425. /** 
  426. * Remove a forum from a group 
  427. * 
  428. * @param type $group_id 
  429. * @since bbPress (r3653) 
  430. */ 
  431. function bbp_remove_forum_id_from_group( $group_id = 0, $forum_id = 0 ) { 
  432.  
  433. // Validate forum_id 
  434. $forum_id = bbp_get_forum_id( $forum_id ); 
  435.  
  436. // Use current group if none is set 
  437. if ( empty( $group_id ) ) 
  438. $group_id = bp_get_current_group_id(); 
  439.  
  440. // Get current group IDs 
  441. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  442.  
  443. // Maybe update the groups forums 
  444. if ( in_array( $forum_id, $forum_ids ) ) { 
  445. $forum_ids = array_diff( array_values( $forum_ids ), (array) $forum_id ); 
  446. return bbp_update_group_forum_ids( $group_id, $forum_ids ); 
  447.  
  448. /** 
  449. * Remove a group from aall forums 
  450. * 
  451. * @param type $group_id 
  452. * @since bbPress (r3653) 
  453. */ 
  454. function bbp_remove_group_id_from_all_forums( $group_id = 0 ) { 
  455.  
  456. // Use current group if none is set 
  457. if ( empty( $group_id ) ) 
  458. $group_id = bp_get_current_group_id(); 
  459.  
  460. // Get current group IDs 
  461. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  462.  
  463. // Loop through forums and remove this group from each one 
  464. foreach ( (array) $forum_ids as $forum_id ) { 
  465. bbp_remove_group_id_from_forum( $group_id, $forum_id ); 
  466.  
  467. /** 
  468. * Remove a forum from all groups 
  469. * 
  470. * @param type $forum_id 
  471. * @since bbPress (r3653) 
  472. */ 
  473. function bbp_remove_forum_id_from_all_groups( $forum_id = 0 ) { 
  474.  
  475. // Validate 
  476. $forum_id = bbp_get_forum_id( $forum_id ); 
  477. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  478.  
  479. // Loop through groups and remove this forum from each one 
  480. foreach ( (array) $group_ids as $group_id ) { 
  481. bbp_remove_forum_id_from_group( $forum_id, $group_id ); 
  482.  
  483. /** 
  484. * Return true if a forum is a group forum 
  485. * 
  486. * @since bbPress (r4571) 
  487. * 
  488. * @param int $forum_id 
  489. * @uses bbp_get_forum_id() To get the forum id 
  490. * @uses bbp_get_forum_group_ids() To get the forum's group ids 
  491. * @uses apply_filters() Calls 'bbp_forum_is_group_forum' with the forum id  
  492. * @return bool True if it is a group forum, false if not 
  493. */ 
  494. function bbp_is_forum_group_forum( $forum_id = 0 ) { 
  495.  
  496. // Validate 
  497. $forum_id = bbp_get_forum_id( $forum_id ); 
  498.  
  499. // Check for group ID's 
  500. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  501.  
  502. // Check if the forum has groups 
  503. $retval = (bool) !empty( $group_ids ); 
  504.  
  505. return (bool) apply_filters( 'bbp_is_forum_group_forum', $retval, $forum_id, $group_ids ); 
  506.  
  507. /*** Group Member Status ******************************************************/ 
  508.  
  509. /** 
  510. * Is the current user an admin of the current group 
  511. * 
  512. * @since bbPress (r4632) 
  513. * 
  514. * @uses is_user_logged_in() 
  515. * @uses bp_is_group() 
  516. * @uses bbpress() 
  517. * @uses get_current_user_id() 
  518. * @uses bp_get_current_group_id() 
  519. * @uses groups_is_user_admin() 
  520. * @return bool If current user is an admin of the current group 
  521. */ 
  522. function bbp_group_is_admin() { 
  523.  
  524. // Bail if user is not logged in or not looking at a group 
  525. if ( ! is_user_logged_in() || ! bp_is_group() ) 
  526. return false; 
  527.  
  528. $bbp = bbpress(); 
  529.  
  530. // Set the global if not set 
  531. if ( ! isset( $bbp->current_user->is_group_admin ) ) 
  532. $bbp->current_user->is_group_admin = groups_is_user_admin( get_current_user_id(), bp_get_current_group_id() ); 
  533.  
  534. // Return the value 
  535. return (bool) $bbp->current_user->is_group_admin; 
  536.  
  537. /** 
  538. * Is the current user a moderator of the current group 
  539. * 
  540. * @since bbPress (r4632) 
  541. * 
  542. * @uses is_user_logged_in() 
  543. * @uses bp_is_group() 
  544. * @uses bbpress() 
  545. * @uses get_current_user_id() 
  546. * @uses bp_get_current_group_id() 
  547. * @uses groups_is_user_admin() 
  548. * @return bool If current user is a moderator of the current group 
  549. */ 
  550. function bbp_group_is_mod() { 
  551.  
  552. // Bail if user is not logged in or not looking at a group 
  553. if ( ! is_user_logged_in() || ! bp_is_group() ) 
  554. return false; 
  555.  
  556. $bbp = bbpress(); 
  557.  
  558. // Set the global if not set 
  559. if ( ! isset( $bbp->current_user->is_group_mod ) ) 
  560. $bbp->current_user->is_group_mod = groups_is_user_mod( get_current_user_id(), bp_get_current_group_id() ); 
  561.  
  562. // Return the value 
  563. return (bool) $bbp->current_user->is_group_mod; 
  564.  
  565. /** 
  566. * Is the current user a member of the current group 
  567. * 
  568. * @since bbPress (r4632) 
  569. * 
  570. * @uses is_user_logged_in() 
  571. * @uses bp_is_group() 
  572. * @uses bbpress() 
  573. * @uses get_current_user_id() 
  574. * @uses bp_get_current_group_id() 
  575. * @uses groups_is_user_admin() 
  576. * @return bool If current user is a member of the current group 
  577. */ 
  578. function bbp_group_is_member() { 
  579.  
  580. // Bail if user is not logged in or not looking at a group 
  581. if ( ! is_user_logged_in() || ! bp_is_group() ) 
  582. return false; 
  583.  
  584. $bbp = bbpress(); 
  585.  
  586. // Set the global if not set 
  587. if ( ! isset( $bbp->current_user->is_group_member ) ) 
  588. $bbp->current_user->is_group_member = groups_is_user_member( get_current_user_id(), bp_get_current_group_id() ); 
  589.  
  590. // Return the value 
  591. return (bool) $bbp->current_user->is_group_member; 
  592.  
  593. /** 
  594. * Is the current user banned from the current group 
  595. * 
  596. * @since bbPress (r4632) 
  597. * 
  598. * @uses is_user_logged_in() 
  599. * @uses bp_is_group() 
  600. * @uses bbpress() 
  601. * @uses get_current_user_id() 
  602. * @uses bp_get_current_group_id() 
  603. * @uses groups_is_user_admin() 
  604. * @return bool If current user is banned from the current group 
  605. */ 
  606. function bbp_group_is_banned() { 
  607.  
  608. // Bail if user is not logged in or not looking at a group 
  609. if ( ! is_user_logged_in() || ! bp_is_group() ) 
  610. return false; 
  611.  
  612. $bbp = bbpress(); 
  613.  
  614. // Set the global if not set 
  615. if ( ! isset( $bbp->current_user->is_group_banned ) ) 
  616. $bbp->current_user->is_group_banned = groups_is_user_banned( get_current_user_id(), bp_get_current_group_id() ); 
  617.  
  618. // Return the value 
  619. return (bool) $bbp->current_user->is_group_banned; 
  620.  
  621. /** 
  622. * Is the current user the creator of the current group 
  623. * 
  624. * @since bbPress (r4632) 
  625. * 
  626. * @uses is_user_logged_in() 
  627. * @uses bp_is_group() 
  628. * @uses bbpress() 
  629. * @uses get_current_user_id() 
  630. * @uses bp_get_current_group_id() 
  631. * @uses groups_is_user_admin() 
  632. * @return bool If current user the creator of the current group 
  633. */ 
  634. function bbp_group_is_creator() { 
  635.  
  636. // Bail if user is not logged in or not looking at a group 
  637. if ( ! is_user_logged_in() || ! bp_is_group() ) 
  638. return false; 
  639.  
  640. $bbp = bbpress(); 
  641.  
  642. // Set the global if not set 
  643. if ( ! isset( $bbp->current_user->is_group_creator ) ) 
  644. $bbp->current_user->is_group_creator = groups_is_user_creator( get_current_user_id(), bp_get_current_group_id() ); 
  645.  
  646. // Return the value 
  647. return (bool) $bbp->current_user->is_group_creator; 
.