/includes/users/template.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress User Template Tags 
  5. * 
  6. * @package bbPress 
  7. * @subpackage TemplateTags 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** Users *********************************************************************/ 
  14.  
  15. /** 
  16. * Output a validated user id 
  17. * 
  18. * @since bbPress (r2729) 
  19. * 
  20. * @param int $user_id Optional. User id 
  21. * @param bool $displayed_user_fallback Fallback on displayed user? 
  22. * @param bool $current_user_fallback Fallback on current user? 
  23. * @uses bbp_get_user_id() To get the user id 
  24. */ 
  25. function bbp_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) { 
  26. echo bbp_get_user_id( $user_id, $displayed_user_fallback, $current_user_fallback ); 
  27. /** 
  28. * Return a validated user id 
  29. * 
  30. * @since bbPress (r2729) 
  31. * 
  32. * @param int $user_id Optional. User id 
  33. * @param bool $displayed_user_fallback Fallback on displayed user? 
  34. * @param bool $current_user_fallback Fallback on current user? 
  35. * @uses get_query_var() To get the 'bbp_user_id' query var 
  36. * @uses apply_filters() Calls 'bbp_get_user_id' with the user id 
  37. * @return int Validated user id 
  38. */ 
  39. function bbp_get_user_id( $user_id = 0, $displayed_user_fallback = true, $current_user_fallback = false ) { 
  40. $bbp = bbpress(); 
  41.  
  42. // Easy empty checking 
  43. if ( !empty( $user_id ) && is_numeric( $user_id ) ) { 
  44. $bbp_user_id = $user_id; 
  45.  
  46. // Currently viewing or editing a user 
  47. } elseif ( ( true === $displayed_user_fallback ) && !empty( $bbp->displayed_user->ID ) ) { 
  48. $bbp_user_id = $bbp->displayed_user->ID; 
  49.  
  50. // Maybe fallback on the current_user ID 
  51. } elseif ( ( true === $current_user_fallback ) && !empty( $bbp->current_user->ID ) ) { 
  52. $bbp_user_id = $bbp->current_user->ID; 
  53.  
  54. // Failsafe 
  55. } else { 
  56. $bbp_user_id = 0; 
  57.  
  58. return (int) apply_filters( 'bbp_get_user_id', (int) $bbp_user_id, $displayed_user_fallback, $current_user_fallback ); 
  59.  
  60. /** 
  61. * Output ID of current user 
  62. * 
  63. * @since bbPress (r2574) 
  64. * 
  65. * @uses bbp_get_current_user_id() To get the current user id 
  66. */ 
  67. function bbp_current_user_id() { 
  68. echo bbp_get_current_user_id(); 
  69. /** 
  70. * Return ID of current user 
  71. * 
  72. * @since bbPress (r2574) 
  73. * 
  74. * @uses bbp_get_user_id() To get the current user id 
  75. * @uses apply_filters() Calls 'bbp_get_current_user_id' with the id 
  76. * @return int Current user id 
  77. */ 
  78. function bbp_get_current_user_id() { 
  79. return apply_filters( 'bbp_get_current_user_id', bbp_get_user_id( 0, false, true ) ); 
  80.  
  81. /** 
  82. * Output ID of displayed user 
  83. * 
  84. * @since bbPress (r2688) 
  85. * 
  86. * @uses bbp_get_displayed_user_id() To get the displayed user id 
  87. */ 
  88. function bbp_displayed_user_id() { 
  89. echo bbp_get_displayed_user_id(); 
  90. /** 
  91. * Return ID of displayed user 
  92. * 
  93. * @since bbPress (r2688) 
  94. * 
  95. * @uses bbp_get_user_id() To get the displayed user id 
  96. * @uses apply_filters() Calls 'bbp_get_displayed_user_id' with the id 
  97. * @return int Displayed user id 
  98. */ 
  99. function bbp_get_displayed_user_id() { 
  100. return apply_filters( 'bbp_get_displayed_user_id', bbp_get_user_id( 0, true, false ) ); 
  101.  
  102. /** 
  103. * Output a sanitized user field value 
  104. * 
  105. * This function relies on the $filter parameter to decide how to sanitize 
  106. * the field value that it finds. Since it uses the WP_User object's magic 
  107. * __get() method, it can also be used to get user_meta values. 
  108. * 
  109. * @since bbPress (r2688) 
  110. * 
  111. * @param string $field Field to get 
  112. * @param string $filter How to filter the field value (null|raw|db|display|edit) 
  113. * @uses bbp_get_displayed_user_field() To get the field 
  114. */ 
  115. function bbp_displayed_user_field( $field = '', $filter = 'display' ) { 
  116. echo bbp_get_displayed_user_field( $field, $filter ); 
  117. /** 
  118. * Return a sanitized user field value 
  119. * 
  120. * This function relies on the $filter parameter to decide how to sanitize 
  121. * the field value that it finds. Since it uses the WP_User object's magic 
  122. * __get() method, it can also be used to get user_meta values. 
  123. * 
  124. * @since bbPress (r2688) 
  125. * 
  126. * @param string $field Field to get 
  127. * @param string $filter How to filter the field value (null|raw|db|display|edit) 
  128. * @see WP_User::__get() for more on how the value is retrieved 
  129. * @see sanitize_user_field() for more on how the value is sanitized 
  130. * @uses apply_filters() Calls 'bbp_get_displayed_user_field' with the value 
  131. * @return string|bool Value of the field if it exists, else false 
  132. */ 
  133. function bbp_get_displayed_user_field( $field = '', $filter = 'display' ) { 
  134.  
  135. // Get the displayed user 
  136. $user = bbpress()->displayed_user; 
  137.  
  138. // Juggle the user filter property because we don't want to muck up how 
  139. // other code might interact with this object. 
  140. $old_filter = $user->filter; 
  141. $user->filter = $filter; 
  142.  
  143. // Get the field value from the WP_User object. We don't need to perform 
  144. // an isset() because the WP_User::__get() does it for us. 
  145. $value = $user->$field; 
  146.  
  147. // Put back the user filter property that was previously juggled above. 
  148. $user->filter = $old_filter; 
  149.  
  150. // Return empty 
  151. return apply_filters( 'bbp_get_displayed_user_field', $value, $field, $filter ); 
  152.  
  153. /** 
  154. * Output name of current user 
  155. * 
  156. * @since bbPress (r2574) 
  157. * 
  158. * @uses bbp_get_current_user_name() To get the current user name 
  159. */ 
  160. function bbp_current_user_name() { 
  161. echo bbp_get_current_user_name(); 
  162. /** 
  163. * Return name of current user 
  164. * 
  165. * @since bbPress (r2574) 
  166. * 
  167. * @uses apply_filters() Calls 'bbp_get_current_user_name' with the 
  168. * current user name 
  169. * @return string 
  170. */ 
  171. function bbp_get_current_user_name() { 
  172. global $user_identity; 
  173.  
  174. $current_user_name = is_user_logged_in() ? $user_identity : __( 'Anonymous', 'bbpress' ); 
  175.  
  176. return apply_filters( 'bbp_get_current_user_name', $current_user_name ); 
  177.  
  178. /** 
  179. * Output avatar of current user 
  180. * 
  181. * @since bbPress (r2574) 
  182. * 
  183. * @param int $size Size of the avatar. Defaults to 40 
  184. * @uses bbp_get_current_user_avatar() To get the current user avatar 
  185. */ 
  186. function bbp_current_user_avatar( $size = 40 ) { 
  187. echo bbp_get_current_user_avatar( $size ); 
  188.  
  189. /** 
  190. * Return avatar of current user 
  191. * 
  192. * @since bbPress (r2574) 
  193. * 
  194. * @param int $size Size of the avatar. Defaults to 40 
  195. * @uses bbp_get_current_user_id() To get the current user id 
  196. * @uses bbp_get_current_anonymous_user_data() To get the current 
  197. * anonymous user's email 
  198. * @uses get_avatar() To get the avatar 
  199. * @uses apply_filters() Calls 'bbp_get_current_user_avatar' with the 
  200. * avatar and size 
  201. * @return string Current user avatar 
  202. */ 
  203. function bbp_get_current_user_avatar( $size = 40 ) { 
  204.  
  205. $user = bbp_get_current_user_id(); 
  206. if ( empty( $user ) ) 
  207. $user = bbp_get_current_anonymous_user_data( 'email' ); 
  208.  
  209. $avatar = get_avatar( $user, $size ); 
  210.  
  211. return apply_filters( 'bbp_get_current_user_avatar', $avatar, $size ); 
  212.  
  213. /** 
  214. * Output link to the profile page of a user 
  215. * 
  216. * @since bbPress (r2688) 
  217. * 
  218. * @param int $user_id Optional. User id 
  219. * @uses bbp_get_user_profile_link() To get user profile link 
  220. */ 
  221. function bbp_user_profile_link( $user_id = 0 ) { 
  222. echo bbp_get_user_profile_link( $user_id ); 
  223. /** 
  224. * Return link to the profile page of a user 
  225. * 
  226. * @since bbPress (r2688) 
  227. * 
  228. * @param int $user_id Optional. User id 
  229. * @uses bbp_get_user_id() To get user id 
  230. * @uses get_userdata() To get user data 
  231. * @uses bbp_get_user_profile_url() To get user profile url 
  232. * @uses apply_filters() Calls 'bbp_get_user_profile_link' with the user 
  233. * profile link and user id 
  234. * @return string User profile link 
  235. */ 
  236. function bbp_get_user_profile_link( $user_id = 0 ) { 
  237.  
  238. // Validate user id 
  239. $user_id = bbp_get_user_id( $user_id ); 
  240. if ( empty( $user_id ) ) 
  241. return false; 
  242.  
  243. $user = get_userdata( $user_id ); 
  244. $user_link = '<a href="' . esc_url( bbp_get_user_profile_url( $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>'; 
  245.  
  246. return apply_filters( 'bbp_get_user_profile_link', $user_link, $user_id ); 
  247.  
  248. /** 
  249. * Output a users nicename to the screen 
  250. * 
  251. * @since bbPress (r4671) 
  252. * 
  253. * @param int $user_id User ID whose nicename to get 
  254. * @param array $args before|after|user_id|force 
  255. */ 
  256. function bbp_user_nicename( $user_id = 0, $args = array() ) { 
  257. echo bbp_get_user_nicename( $user_id, $args ); 
  258. /** 
  259. * Return a users nicename to the screen 
  260. * 
  261. * @since bbPress (r4671) 
  262. * 
  263. * @param int $user_id User ID whose nicename to get 
  264. * @param array $args before|after|user_id|force 
  265. * @return string User nicename, maybe wrapped in before/after strings 
  266. */ 
  267. function bbp_get_user_nicename( $user_id = 0, $args = array() ) { 
  268.  
  269. // Bail if no user ID passed 
  270. $user_id = bbp_get_user_id( $user_id ); 
  271. if ( empty( $user_id ) ) 
  272. return false; 
  273.  
  274. // Parse default arguments 
  275. $r = bbp_parse_args( $args, array( 
  276. 'user_id' => $user_id,  
  277. 'before' => '',  
  278. 'after' => '',  
  279. 'force' => '' 
  280. ), 'get_user_nicename' ); 
  281.  
  282. // Get the user data and nicename 
  283. if ( empty( $r['force'] ) ) { 
  284. $user = get_userdata( $user_id ); 
  285. $nicename = $user->user_nicename; 
  286.  
  287. // Force the nicename to something else 
  288. } else { 
  289. $nicename = (string) $r['force']; 
  290.  
  291. // Maybe wrap the nicename 
  292. $retval = !empty( $nicename ) ? ( $r['before'] . $nicename . $r['after'] ) : ''; 
  293.  
  294. // Filter and return 
  295. return (string) apply_filters( 'bbp_get_user_nicename', $retval, $user_id, $r ); 
  296.  
  297. /** 
  298. * Output URL to the profile page of a user 
  299. * 
  300. * @since bbPress (r2688) 
  301. * 
  302. * @param int $user_id Optional. User id 
  303. * @param string $user_nicename Optional. User nicename 
  304. * @uses bbp_get_user_profile_url() To get user profile url 
  305. */ 
  306. function bbp_user_profile_url( $user_id = 0, $user_nicename = '' ) { 
  307. echo esc_url( bbp_get_user_profile_url( $user_id, $user_nicename ) ); 
  308. /** 
  309. * Return URL to the profile page of a user 
  310. * 
  311. * @since bbPress (r2688) 
  312. * 
  313. * @param int $user_id Optional. User id 
  314. * @param string $user_nicename Optional. User nicename 
  315. * @uses bbp_get_user_id() To get user id 
  316. * @uses WP_Rewrite::using_permalinks() To check if the blog is using 
  317. * permalinks 
  318. * @uses add_query_arg() To add custom args to the url 
  319. * @uses home_url() To get blog home url 
  320. * @uses apply_filters() Calls 'bbp_get_user_profile_url' with the user 
  321. * profile url, user id and user nicename 
  322. * @return string User profile url 
  323. */ 
  324. function bbp_get_user_profile_url( $user_id = 0, $user_nicename = '' ) { 
  325. global $wp_rewrite; 
  326.  
  327. // Use displayed user ID if there is one, and one isn't requested 
  328. $user_id = bbp_get_user_id( $user_id ); 
  329. if ( empty( $user_id ) ) 
  330. return false; 
  331.  
  332. // Allow early overriding of the profile URL to cut down on processing 
  333. $early_profile_url = apply_filters( 'bbp_pre_get_user_profile_url', (int) $user_id ); 
  334. if ( is_string( $early_profile_url ) ) 
  335. return $early_profile_url; 
  336.  
  337. // Pretty permalinks 
  338. if ( $wp_rewrite->using_permalinks() ) { 
  339. $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%'; 
  340.  
  341. // Get username if not passed 
  342. if ( empty( $user_nicename ) ) { 
  343. $user_nicename = bbp_get_user_nicename( $user_id ); 
  344.  
  345. $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url ); 
  346. $url = home_url( user_trailingslashit( $url ) ); 
  347.  
  348. // Unpretty permalinks 
  349. } else { 
  350. $url = add_query_arg( array( bbp_get_user_rewrite_id() => $user_id ), home_url( '/' ) ); 
  351.  
  352. return apply_filters( 'bbp_get_user_profile_url', $url, $user_id, $user_nicename ); 
  353.  
  354. /** 
  355. * Output link to the profile edit page of a user 
  356. * 
  357. * @since bbPress (r2688) 
  358. * 
  359. * @param int $user_id Optional. User id 
  360. * @uses bbp_get_user_profile_edit_link() To get user profile edit link 
  361. */ 
  362. function bbp_user_profile_edit_link( $user_id = 0 ) { 
  363. echo bbp_get_user_profile_edit_link( $user_id ); 
  364. /** 
  365. * Return link to the profile edit page of a user 
  366. * 
  367. * @since bbPress (r2688) 
  368. * 
  369. * @param int $user_id Optional. User id 
  370. * @uses bbp_get_user_id() To get user id 
  371. * @uses get_userdata() To get user data 
  372. * @uses bbp_get_user_profile_edit_url() To get user profile edit url 
  373. * @uses apply_filters() Calls 'bbp_get_user_profile_link' with the edit 
  374. * link and user id 
  375. * @return string User profile edit link 
  376. */ 
  377. function bbp_get_user_profile_edit_link( $user_id = 0 ) { 
  378.  
  379. // Validate user id 
  380. $user_id = bbp_get_user_id( $user_id ); 
  381. if ( empty( $user_id ) ) 
  382. return false; 
  383.  
  384. $user = get_userdata( $user_id ); 
  385. $edit_link = '<a href="' . esc_url( bbp_get_user_profile_url( $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>'; 
  386. return apply_filters( 'bbp_get_user_profile_edit_link', $edit_link, $user_id ); 
  387.  
  388. /** 
  389. * Output URL to the profile edit page of a user 
  390. * 
  391. * @since bbPress (r2688) 
  392. * 
  393. * @param int $user_id Optional. User id 
  394. * @param string $user_nicename Optional. User nicename 
  395. * @uses bbp_get_user_profile_edit_url() To get user profile edit url 
  396. */ 
  397. function bbp_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) { 
  398. echo esc_url( bbp_get_user_profile_edit_url( $user_id, $user_nicename ) ); 
  399. /** 
  400. * Return URL to the profile edit page of a user 
  401. * 
  402. * @since bbPress (r2688) 
  403. * 
  404. * @param int $user_id Optional. User id 
  405. * @param string $user_nicename Optional. User nicename 
  406. * @uses bbp_get_user_id() To get user id 
  407. * @uses WP_Rewrite::using_permalinks() To check if the blog is using 
  408. * permalinks 
  409. * @uses add_query_arg() To add custom args to the url 
  410. * @uses home_url() To get blog home url 
  411. * @uses apply_filters() Calls 'bbp_get_user_edit_profile_url' with the 
  412. * edit profile url, user id and user nicename 
  413. * @return string 
  414. */ 
  415. function bbp_get_user_profile_edit_url( $user_id = 0, $user_nicename = '' ) { 
  416. global $wp_rewrite; 
  417.  
  418. $bbp = bbpress(); 
  419. $user_id = bbp_get_user_id( $user_id ); 
  420. if ( empty( $user_id ) ) 
  421. return false; 
  422.  
  423. // Pretty permalinks 
  424. if ( $wp_rewrite->using_permalinks() ) { 
  425. $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . $bbp->user_id . '%/' . $bbp->edit_id; 
  426.  
  427. // Get username if not passed 
  428. if ( empty( $user_nicename ) ) { 
  429. $user = get_userdata( $user_id ); 
  430. if ( !empty( $user->user_nicename ) ) { 
  431. $user_nicename = $user->user_nicename; 
  432.  
  433. $url = str_replace( '%' . $bbp->user_id . '%', $user_nicename, $url ); 
  434. $url = home_url( user_trailingslashit( $url ) ); 
  435.  
  436. // Unpretty permalinks 
  437. } else { 
  438. $url = add_query_arg( array( $bbp->user_id => $user_id, $bbp->edit_id => '1' ), home_url( '/' ) ); 
  439.  
  440. return apply_filters( 'bbp_get_user_edit_profile_url', $url, $user_id, $user_nicename ); 
  441.  
  442.  
  443. /** 
  444. * Output a user's main role for display 
  445. * 
  446. * @since bbPress (r3860) 
  447. * 
  448. * @param int $user_id 
  449. * @uses bbp_get_user_display_role To get the user display role 
  450. */ 
  451. function bbp_user_display_role( $user_id = 0 ) { 
  452. echo bbp_get_user_display_role( $user_id ); 
  453. /** 
  454. * Return a user's main role for display 
  455. * 
  456. * @since bbPress (r3860) 
  457. * 
  458. * @param int $user_id 
  459. * @uses bbp_get_user_id() to verify the user ID 
  460. * @uses bbp_is_user_inactive() to check if user is inactive 
  461. * @uses user_can() to check if user has special capabilities 
  462. * @uses apply_filters() Calls 'bbp_get_user_display_role' with the 
  463. * display role, user id, and user role 
  464. * @return string 
  465. */ 
  466. function bbp_get_user_display_role( $user_id = 0 ) { 
  467.  
  468. // Validate user id 
  469. $user_id = bbp_get_user_id( $user_id ); 
  470.  
  471. // User is not registered 
  472. if ( empty( $user_id ) ) { 
  473. $role = __( 'Guest', 'bbpress' ); 
  474.  
  475. // User is not active 
  476. } elseif ( bbp_is_user_inactive( $user_id ) ) { 
  477. $role = __( 'Inactive', 'bbpress' ); 
  478.  
  479. // User have a role 
  480. } else { 
  481. $role_id = bbp_get_user_role( $user_id ); 
  482. $role = bbp_get_dynamic_role_name( $role_id ); 
  483.  
  484. // No role found so default to generic "Member" 
  485. if ( empty( $role ) ) { 
  486. $role = __( 'Member', 'bbpress' ); 
  487.  
  488. return apply_filters( 'bbp_get_user_display_role', $role, $user_id ); 
  489.  
  490. /** 
  491. * Output the link to the admin section 
  492. * 
  493. * @since bbPress (r2827) 
  494. * 
  495. * @param mixed $args Optional. See {@link bbp_get_admin_link()} 
  496. * @uses bbp_get_admin_link() To get the admin link 
  497. */ 
  498. function bbp_admin_link( $args = '' ) { 
  499. echo bbp_get_admin_link( $args ); 
  500. /** 
  501. * Return the link to the admin section 
  502. * 
  503. * @since bbPress (r2827) 
  504. * 
  505. * @param mixed $args Optional. This function supports these arguments: 
  506. * - text: The text 
  507. * - before: Before the lnk 
  508. * - after: After the link 
  509. * @uses current_user_can() To check if the current user can moderate 
  510. * @uses admin_url() To get the admin url 
  511. * @uses apply_filters() Calls 'bbp_get_admin_link' with the link & args 
  512. * @return The link 
  513. */ 
  514. function bbp_get_admin_link( $args = '' ) { 
  515. if ( !current_user_can( 'moderate' ) ) 
  516. return; 
  517.  
  518. if ( !empty( $args ) && is_string( $args ) && ( false === strpos( $args, '=' ) ) ) 
  519. $args = array( 'text' => $args ); 
  520.  
  521. // Parse arguments against default values 
  522. $r = bbp_parse_args( $args, array( 
  523. 'text' => __( 'Admin', 'bbpress' ),  
  524. 'before' => '',  
  525. 'after' => '' 
  526. ), 'get_admin_link' ); 
  527.  
  528. $retval = $r['before'] . '<a href="' . esc_url( admin_url() ) . '">' . $r['text'] . '</a>' . $r['after']; 
  529.  
  530. return apply_filters( 'bbp_get_admin_link', $retval, $r ); 
  531.  
  532. /** User IP *******************************************************************/ 
  533.  
  534. /** 
  535. * Output the author IP address of a post 
  536. * 
  537. * @since bbPress (r3120) 
  538. * 
  539. * @param mixed $args Optional. If it is an integer, it is used as post id. 
  540. * @uses bbp_get_author_ip() To get the post author link 
  541. */ 
  542. function bbp_author_ip( $args = '' ) { 
  543. echo bbp_get_author_ip( $args ); 
  544. /** 
  545. * Return the author IP address of a post 
  546. * 
  547. * @since bbPress (r3120) 
  548. * 
  549. * @param mixed $args Optional. If an integer, it is used as reply id. 
  550. * @uses get_post_meta() To check if it's a topic page 
  551. * @return string Author link of reply 
  552. */ 
  553. function bbp_get_author_ip( $args = '' ) { 
  554.  
  555. // Used as post id 
  556. $post_id = is_numeric( $args ) ? (int) $args : 0; 
  557.  
  558. // Parse arguments against default values 
  559. $r = bbp_parse_args( $args, array( 
  560. 'post_id' => $post_id,  
  561. 'before' => '<span class="bbp-author-ip">(',  
  562. 'after' => ')</span>' 
  563. ), 'get_author_ip' ); 
  564.  
  565. // Get the author IP meta value 
  566. $author_ip = get_post_meta( $r['post_id'], '_bbp_author_ip', true ); 
  567. if ( !empty( $author_ip ) ) { 
  568. $author_ip = $r['before'] . $author_ip . $r['after']; 
  569.  
  570. // No IP address 
  571. } else { 
  572. $author_ip = ''; 
  573.  
  574. return apply_filters( 'bbp_get_author_ip', $author_ip, $r ); 
  575.  
  576. /** Anonymous Fields **********************************************************/ 
  577.  
  578. /** 
  579. * Output the author disylay-name of a topic or reply. 
  580. * 
  581. * Convenience function to ensure proper template functions are called 
  582. * and correct filters are executed. Used primarily to display topic 
  583. * and reply author information in the anonymous form template-part. 
  584. * 
  585. * @since bbPress (r5119) 
  586. * 
  587. * @param int $post_id 
  588. * @uses bbp_get_author_display_name() to get the author name 
  589. */ 
  590. function bbp_author_display_name( $post_id = 0 ) { 
  591. echo bbp_get_author_display_name( $post_id ); 
  592.  
  593. /** 
  594. * Return the author name of a topic or reply. 
  595. * 
  596. * Convenience function to ensure proper template functions are called 
  597. * and correct filters are executed. Used primarily to display topic 
  598. * and reply author information in the anonymous form template-part. 
  599. * 
  600. * @since bbPress (r5119) 
  601. * 
  602. * @param int $post_id 
  603. * 
  604. * @uses bbp_is_topic_edit() 
  605. * @uses bbp_get_topic_author_display_name() 
  606. * @uses bbp_is_reply_edit() 
  607. * @uses bbp_get_reply_author_display_name() 
  608. * @uses bbp_current_anonymous_user_data() 
  609. * 
  610. * @return string The name of the author 
  611. */ 
  612. function bbp_get_author_display_name( $post_id = 0 ) { 
  613.  
  614. // Define local variable(s) 
  615. $retval = ''; 
  616.  
  617. // Topic edit 
  618. if ( bbp_is_topic_edit() ) { 
  619. $retval = bbp_get_topic_author_display_name( $post_id ); 
  620.  
  621. // Reply edit 
  622. } elseif ( bbp_is_reply_edit() ) { 
  623. $retval = bbp_get_reply_author_display_name( $post_id ); 
  624.  
  625. // Not an edit, so rely on current user cookie data 
  626. } else { 
  627. $retval = bbp_current_anonymous_user_data( 'name' ); 
  628.  
  629. return apply_filters( 'bbp_get_author_display_name', $retval, $post_id ); 
  630.  
  631. /** 
  632. * Output the author email of a topic or reply. 
  633. * 
  634. * Convenience function to ensure proper template functions are called 
  635. * and correct filters are executed. Used primarily to display topic 
  636. * and reply author information in the anonymous user form template-part. 
  637. * 
  638. * @since bbPress (r5119) 
  639. * 
  640. * @param int $post_id 
  641. * @uses bbp_get_author_email() to get the author email 
  642. */ 
  643. function bbp_author_email( $post_id = 0 ) { 
  644. echo bbp_get_author_email( $post_id ); 
  645.  
  646. /** 
  647. * Return the author email of a topic or reply. 
  648. * 
  649. * Convenience function to ensure proper template functions are called 
  650. * and correct filters are executed. Used primarily to display topic 
  651. * and reply author information in the anonymous user form template-part. 
  652. * 
  653. * @since bbPress (r5119) 
  654. * 
  655. * @param int $post_id 
  656. * 
  657. * @uses bbp_is_topic_edit() 
  658. * @uses bbp_get_topic_author_email() 
  659. * @uses bbp_is_reply_edit() 
  660. * @uses bbp_get_reply_author_email() 
  661. * @uses bbp_current_anonymous_user_data() 
  662. * 
  663. * @return string The email of the author 
  664. */ 
  665. function bbp_get_author_email( $post_id = 0 ) { 
  666.  
  667. // Define local variable(s) 
  668. $retval = ''; 
  669.  
  670. // Topic edit 
  671. if ( bbp_is_topic_edit() ) { 
  672. $retval = bbp_get_topic_author_email( $post_id ); 
  673.  
  674. // Reply edit 
  675. } elseif ( bbp_is_reply_edit() ) { 
  676. $retval = bbp_get_reply_author_email( $post_id ); 
  677.  
  678. // Not an edit, so rely on current user cookie data 
  679. } else { 
  680. $retval = bbp_current_anonymous_user_data( 'email' ); 
  681.  
  682. return apply_filters( 'bbp_get_author_email', $retval, $post_id ); 
  683.  
  684. /** 
  685. * Output the author url of a topic or reply. 
  686. * 
  687. * Convenience function to ensure proper template functions are called 
  688. * and correct filters are executed. Used primarily to display topic 
  689. * and reply author information in the anonymous user form template-part. 
  690. * 
  691. * @since bbPress (r5119) 
  692. * 
  693. * @param int $post_id 
  694. * @uses bbp_get_author_url() to get the author url 
  695. */ 
  696. function bbp_author_url( $post_id = 0 ) { 
  697. echo bbp_get_author_url( $post_id ); 
  698.  
  699. /** 
  700. * Return the author url of a topic or reply. 
  701. * 
  702. * Convenience function to ensure proper template functions are called 
  703. * and correct filters are executed. Used primarily to display topic 
  704. * and reply author information in the anonymous user form template-part. 
  705. * 
  706. * @since bbPress (r5119) 
  707. * 
  708. * @param int $post_id 
  709. * 
  710. * @uses bbp_is_topic_edit() 
  711. * @uses bbp_get_topic_author_url() 
  712. * @uses bbp_is_reply_edit() 
  713. * @uses bbp_get_reply_author_url() 
  714. * @uses bbp_current_anonymous_user_data() 
  715. * 
  716. * @return string The url of the author 
  717. */ 
  718. function bbp_get_author_url( $post_id = 0 ) { 
  719.  
  720. // Define local variable(s) 
  721. $retval = ''; 
  722.  
  723. // Topic edit 
  724. if ( bbp_is_topic_edit() ) { 
  725. $retval = bbp_get_topic_author_url( $post_id ); 
  726.  
  727. // Reply edit 
  728. } elseif ( bbp_is_reply_edit() ) { 
  729. $retval = bbp_get_reply_author_url( $post_id ); 
  730.  
  731. // Not an edit, so rely on current user cookie data 
  732. } else { 
  733. $retval = bbp_current_anonymous_user_data( 'url' ); 
  734.  
  735. return apply_filters( 'bbp_get_author_url', $retval, $post_id ); 
  736.  
  737. /** Favorites *****************************************************************/ 
  738.  
  739. /** 
  740. * Output the link to the user's favorites page (profile page) 
  741. * 
  742. * @since bbPress (r2652) 
  743. * 
  744. * @param int $user_id Optional. User id 
  745. * @uses bbp_get_favorites_permalink() To get the favorites permalink 
  746. */ 
  747. function bbp_favorites_permalink( $user_id = 0 ) { 
  748. echo esc_url( bbp_get_favorites_permalink( $user_id ) ); 
  749. /** 
  750. * Return the link to the user's favorites page (profile page) 
  751. * 
  752. * @since bbPress (r2652) 
  753. * 
  754. * @param int $user_id Optional. User id 
  755. * @uses bbp_get_user_profile_url() To get the user profile url 
  756. * @uses apply_filters() Calls 'bbp_get_favorites_permalink' with the 
  757. * user profile url and user id 
  758. * @return string Permanent link to user profile page 
  759. */ 
  760. function bbp_get_favorites_permalink( $user_id = 0 ) { 
  761. global $wp_rewrite; 
  762.  
  763. // Use displayed user ID if there is one, and one isn't requested 
  764. $user_id = bbp_get_user_id( $user_id ); 
  765. if ( empty( $user_id ) ) 
  766. return false; 
  767.  
  768. // Allow early overriding of the profile URL to cut down on processing 
  769. $early_profile_url = apply_filters( 'bbp_pre_get_favorites_permalink', (int) $user_id ); 
  770. if ( is_string( $early_profile_url ) ) 
  771. return $early_profile_url; 
  772.  
  773. // Pretty permalinks 
  774. if ( $wp_rewrite->using_permalinks() ) { 
  775. $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/%' . bbp_get_user_favorites_rewrite_id() . '%'; 
  776. $user = get_userdata( $user_id ); 
  777. if ( ! empty( $user->user_nicename ) ) { 
  778. $user_nicename = $user->user_nicename; 
  779. } else { 
  780. $user_nicename = $user->user_login; 
  781. $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url ); 
  782. $url = str_replace( '%' . bbp_get_user_favorites_rewrite_id() . '%', bbp_get_user_favorites_slug(), $url ); 
  783. $url = home_url( user_trailingslashit( $url ) ); 
  784.  
  785. // Unpretty permalinks 
  786. } else { 
  787. $url = add_query_arg( array( 
  788. bbp_get_user_rewrite_id() => $user_id,  
  789. bbp_get_user_favorites_rewrite_id() => bbp_get_user_favorites_slug(),  
  790. ), home_url( '/' ) ); 
  791.  
  792. return apply_filters( 'bbp_get_favorites_permalink', $url, $user_id ); 
  793.  
  794. /** 
  795. * Output the link to make a topic favorite/remove a topic from favorites 
  796. * 
  797. * @since bbPress (r2652) 
  798. * 
  799. * @param mixed $args See {@link bbp_get_user_favorites_link()} 
  800. * @param int $user_id Optional. User id 
  801. * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. 
  802. * @uses bbp_get_user_favorites_link() To get the user favorites link 
  803. */ 
  804. function bbp_user_favorites_link( $args = array(), $user_id = 0, $wrap = true ) { 
  805. echo bbp_get_user_favorites_link( $args, $user_id, $wrap ); 
  806. /** 
  807. * User favorites link 
  808. * 
  809. * Return the link to make a topic favorite/remove a topic from 
  810. * favorites 
  811. * 
  812. * @since bbPress (r2652) 
  813. * 
  814. * @param mixed $args This function supports these arguments: 
  815. * - subscribe: Favorite text 
  816. * - unsubscribe: Unfavorite text 
  817. * - user_id: User id 
  818. * - topic_id: Topic id 
  819. * - before: Before the link 
  820. * - after: After the link 
  821. * @param int $user_id Optional. User id 
  822. * @param int $topic_id Optional. Topic id 
  823. * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. See ajax_favorite() 
  824. * @uses bbp_get_user_id() To get the user id 
  825. * @uses current_user_can() If the current user can edit the user 
  826. * @uses bbp_get_topic_id() To get the topic id 
  827. * @uses bbp_is_user_favorite() To check if the topic is user's favorite 
  828. * @uses bbp_get_favorites_permalink() To get the favorites permalink 
  829. * @uses bbp_get_topic_permalink() To get the topic permalink 
  830. * @uses bbp_is_favorites() Is it the favorites page? 
  831. * @uses apply_filters() Calls 'bbp_get_user_favorites_link' with the 
  832. * html, add args, remove args, user & topic id 
  833. * @return string User favorites link 
  834. */ 
  835. function bbp_get_user_favorites_link( $args = '', $user_id = 0, $wrap = true ) { 
  836. if ( ! bbp_is_favorites_active() ) { 
  837. return false; 
  838.  
  839. // Parse arguments against default values 
  840. $r = bbp_parse_args( $args, array( 
  841. 'favorite' => __( 'Favorite', 'bbpress' ),  
  842. 'favorited' => __( 'Favorited', 'bbpress' ),  
  843. 'user_id' => 0,  
  844. 'topic_id' => 0,  
  845. 'before' => '',  
  846. 'after' => '' 
  847. ), 'get_user_favorites_link' ); 
  848.  
  849. // Validate user and topic ID's 
  850. $user_id = bbp_get_user_id( $r['user_id'], true, true ); 
  851. $topic_id = bbp_get_topic_id( $r['topic_id'] ); 
  852. if ( empty( $user_id ) || empty( $topic_id ) ) { 
  853. return false; 
  854.  
  855. // No link if you can't edit yourself 
  856. if ( ! current_user_can( 'edit_user', (int) $user_id ) ) { 
  857. return false; 
  858.  
  859. // Decide which link to show 
  860. $is_fav = bbp_is_user_favorite( $user_id, $topic_id ); 
  861. if ( ! empty( $is_fav ) ) { 
  862. $text = $r['favorited']; 
  863. $query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id ); 
  864. } else { 
  865. $text = $r['favorite']; 
  866. $query_args = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id ); 
  867.  
  868. // Create the link based where the user is and if the topic is 
  869. // already the user's favorite 
  870. if ( bbp_is_favorites() ) { 
  871. $permalink = bbp_get_favorites_permalink( $user_id ); 
  872. } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 
  873. $permalink = bbp_get_topic_permalink( $topic_id ); 
  874. } else { 
  875. $permalink = get_permalink(); 
  876.  
  877. $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) ); 
  878. $sub = $is_fav ? ' class="is-favorite"' : ''; 
  879. $html = sprintf( '%s<span id="favorite-%d" %s><a href="%s" class="favorite-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] ); 
  880.  
  881. // Initial output is wrapped in a span, ajax output is hooked to this 
  882. if ( ! empty( $wrap ) ) { 
  883. $html = '<span id="favorite-toggle">' . $html . '</span>'; 
  884.  
  885. // Return the link 
  886. return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $topic_id ); 
  887.  
  888. /** Subscriptions *************************************************************/ 
  889.  
  890. /** 
  891. * Output the link to the user's subscriptions page (profile page) 
  892. * 
  893. * @since bbPress (r2688) 
  894. * 
  895. * @param int $user_id Optional. User id 
  896. * @uses bbp_get_subscriptions_permalink() To get the subscriptions link 
  897. */ 
  898. function bbp_subscriptions_permalink( $user_id = 0 ) { 
  899. echo esc_url( bbp_get_subscriptions_permalink( $user_id ) ); 
  900. /** 
  901. * Return the link to the user's subscriptions page (profile page) 
  902. * 
  903. * @since bbPress (r2688) 
  904. * 
  905. * @param int $user_id Optional. User id 
  906. * @uses bbp_get_user_profile_url() To get the user profile url 
  907. * @uses apply_filters() Calls 'bbp_get_subscriptions_permalink' with 
  908. * the user profile url and user id 
  909. * @return string Permanent link to user subscriptions page 
  910. */ 
  911. function bbp_get_subscriptions_permalink( $user_id = 0 ) { 
  912. global $wp_rewrite; 
  913.  
  914. // Use displayed user ID if there is one, and one isn't requested 
  915. $user_id = bbp_get_user_id( $user_id ); 
  916. if ( empty( $user_id ) ) 
  917. return false; 
  918.  
  919. // Allow early overriding of the profile URL to cut down on processing 
  920. $early_profile_url = apply_filters( 'bbp_pre_get_subscriptions_permalink', (int) $user_id ); 
  921. if ( is_string( $early_profile_url ) ) 
  922. return $early_profile_url; 
  923.  
  924. // Pretty permalinks 
  925. if ( $wp_rewrite->using_permalinks() ) { 
  926. $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/%' . bbp_get_user_subscriptions_rewrite_id() . '%'; 
  927. $user = get_userdata( $user_id ); 
  928. if ( ! empty( $user->user_nicename ) ) { 
  929. $user_nicename = $user->user_nicename; 
  930. } else { 
  931. $user_nicename = $user->user_login; 
  932. $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url ); 
  933. $url = str_replace( '%' . bbp_get_user_subscriptions_rewrite_id() . '%', bbp_get_user_subscriptions_slug(), $url ); 
  934. $url = home_url( user_trailingslashit( $url ) ); 
  935.  
  936. // Unpretty permalinks 
  937. } else { 
  938. $url = add_query_arg( array( 
  939. bbp_get_user_rewrite_id() => $user_id,  
  940. bbp_get_user_subscriptions_rewrite_id() => bbp_get_user_subscriptions_slug(),  
  941. ), home_url( '/' ) ); 
  942.  
  943. return apply_filters( 'bbp_get_subscriptions_permalink', $url, $user_id ); 
  944.  
  945. /** 
  946. * Output the link to subscribe/unsubscribe from a topic 
  947. * 
  948. * @since bbPress (r2668) 
  949. * 
  950. * @param mixed $args See {@link bbp_get_user_subscribe_link()} 
  951. * @param int $user_id Optional. User id 
  952. * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">. 
  953. * @uses bbp_get_user_subscribe_link() To get the subscribe link 
  954. */ 
  955. function bbp_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 
  956. echo bbp_get_user_subscribe_link( $args, $user_id, $wrap ); 
  957. /** 
  958. * Return the link to subscribe/unsubscribe from a forum or topic 
  959. * 
  960. * @since bbPress (r2668) 
  961. * 
  962. * @param mixed $args This function supports these arguments: 
  963. * - subscribe: Subscribe text 
  964. * - unsubscribe: Unsubscribe text 
  965. * - user_id: User id 
  966. * - topic_id: Topic id 
  967. * - forum_id: Forum id 
  968. * - before: Before the link 
  969. * - after: After the link 
  970. * @param int $user_id Optional. User id 
  971. * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">. 
  972. * @uses bbp_is_subscriptions_active() to check if subscriptions are active 
  973. * @uses bbp_get_user_id() To get the user id 
  974. * @uses bbp_get_user_id() To get the user id 
  975. * @uses bbp_get_topic_id() To get the topic id 
  976. * @uses bbp_get_forum_id() To get the forum id 
  977. * @uses current_user_can() To check if the current user can edit user 
  978. * @uses bbp_is_user_subscribed_to_forum() To check if the user is subscribed to the forum 
  979. * @uses bbp_is_user_subscribed_to_topic() To check if the user is subscribed to the topic 
  980. * @uses bbp_is_subscriptions() To check if it's the subscriptions page 
  981. * @uses bbp_get_subscriptions_permalink() To get subscriptions link 
  982. * @uses bbp_get_topic_permalink() To get topic link 
  983. * @uses apply_filters() Calls 'bbp_get_user_subscribe_link' with the 
  984. * link, args, user id & topic id 
  985. * @return string Permanent link to topic 
  986. */ 
  987. function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 
  988. if ( ! bbp_is_subscriptions_active() ) { 
  989. return; 
  990.  
  991. // Parse arguments against default values 
  992. $r = bbp_parse_args( $args, array( 
  993. 'subscribe' => __( 'Subscribe', 'bbpress' ),  
  994. 'unsubscribe' => __( 'Unsubscribe', 'bbpress' ),  
  995. 'user_id' => 0,  
  996. 'topic_id' => 0,  
  997. 'forum_id' => 0,  
  998. 'before' => ' | ',  
  999. 'after' => '' 
  1000. ), 'get_user_subscribe_link' ); 
  1001.  
  1002. // Validate user and object ID's 
  1003. $user_id = bbp_get_user_id( $r['user_id'], true, true ); 
  1004. $topic_id = bbp_get_topic_id( $r['topic_id'] ); 
  1005. $forum_id = bbp_get_forum_id( $r['forum_id'] ); 
  1006. if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) { 
  1007. return false; 
  1008.  
  1009. // No link if you can't edit yourself 
  1010. if ( ! current_user_can( 'edit_user', (int) $user_id ) ) { 
  1011. return false; 
  1012.  
  1013. // Check if viewing a single forum 
  1014. if ( empty( $topic_id ) && ! empty( $forum_id ) ) { 
  1015.  
  1016. // Decide which link to show 
  1017. $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id ); 
  1018. if ( ! empty( $is_subscribed ) ) { 
  1019. $text = $r['unsubscribe']; 
  1020. $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id ); 
  1021. } else { 
  1022. $text = $r['subscribe']; 
  1023. $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id ); 
  1024.  
  1025. // Create the link based where the user is and if the user is 
  1026. // subscribed already 
  1027. if ( bbp_is_subscriptions() ) { 
  1028. $permalink = bbp_get_subscriptions_permalink( $user_id ); 
  1029. } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) { 
  1030. $permalink = bbp_get_forum_permalink( $forum_id ); 
  1031. } else { 
  1032. $permalink = get_permalink(); 
  1033.  
  1034. $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) ); 
  1035. $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 
  1036. $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-forum="%d">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, $text, $r['after'] ); 
  1037.  
  1038. // Initial output is wrapped in a span, ajax output is hooked to this 
  1039. if ( !empty( $wrap ) ) { 
  1040. $html = '<span id="subscription-toggle">' . $html . '</span>'; 
  1041.  
  1042. } else { 
  1043.  
  1044. // Decide which link to show 
  1045. $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id ); 
  1046. if ( ! empty( $is_subscribed ) ) { 
  1047. $text = $r['unsubscribe']; 
  1048. $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); 
  1049. } else { 
  1050. $text = $r['subscribe']; 
  1051. $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 
  1052.  
  1053. // Create the link based where the user is and if the user is 
  1054. // subscribed already 
  1055. if ( bbp_is_subscriptions() ) { 
  1056. $permalink = bbp_get_subscriptions_permalink( $user_id ); 
  1057. } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 
  1058. $permalink = bbp_get_topic_permalink( $topic_id ); 
  1059. } else { 
  1060. $permalink = get_permalink(); 
  1061.  
  1062. $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 
  1063. $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 
  1064. $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] ); 
  1065.  
  1066. // Initial output is wrapped in a span, ajax output is hooked to this 
  1067. if ( !empty( $wrap ) ) { 
  1068. $html = '<span id="subscription-toggle">' . $html . '</span>'; 
  1069.  
  1070. // Return the link 
  1071. return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id ); 
  1072.  
  1073.  
  1074. /** Edit User *****************************************************************/ 
  1075.  
  1076. /** 
  1077. * Edit profile success message 
  1078. * 
  1079. * @since bbPress (r2688) 
  1080. * 
  1081. * @uses bbp_is_single_user() To check if it's the profile page 
  1082. * @uses bbp_is_single_user_edit() To check if it's the profile edit page 
  1083. */ 
  1084. function bbp_notice_edit_user_success() { 
  1085. if ( isset( $_GET['updated'] ) && ( bbp_is_single_user() || bbp_is_single_user_edit() ) ) : ?> 
  1086.  
  1087. <div class="bbp-template-notice updated"> 
  1088. <p><?php esc_html_e( 'User updated.', 'bbpress' ); ?></p> 
  1089. </div> 
  1090.  
  1091. <?php endif; 
  1092.  
  1093. /** 
  1094. * Super admin privileges notice 
  1095. * 
  1096. * @since bbPress (r2688) 
  1097. * 
  1098. * @uses is_multisite() To check if the blog is multisite 
  1099. * @uses bbp_is_single_user() To check if it's the profile page 
  1100. * @uses bbp_is_single_user_edit() To check if it's the profile edit page 
  1101. * @uses current_user_can() To check if the current user can manage network 
  1102. * options 
  1103. * @uses bbp_get_displayed_user_id() To get the displayed user id 
  1104. * @uses is_super_admin() To check if the user is super admin 
  1105. * @uses bbp_is_user_home() To check if it's the user home 
  1106. * @uses bbp_is_user_home_edit() To check if it's the user home edit 
  1107. */ 
  1108. function bbp_notice_edit_user_is_super_admin() { 
  1109. if ( is_multisite() && ( bbp_is_single_user() || bbp_is_single_user_edit() ) && current_user_can( 'manage_network_options' ) && is_super_admin( bbp_get_displayed_user_id() ) ) : ?> 
  1110.  
  1111. <div class="bbp-template-notice important"> 
  1112. <p><?php bbp_is_user_home() || bbp_is_user_home_edit() ? esc_html_e( 'You have super admin privileges.', 'bbpress' ) : esc_html_e( 'This user has super admin privileges.', 'bbpress' ); ?></p> 
  1113. </div> 
  1114.  
  1115. <?php endif; 
  1116.  
  1117. /** 
  1118. * Drop down for selecting the user's display name 
  1119. * 
  1120. * @since bbPress (r2688) 
  1121. */ 
  1122. function bbp_edit_user_display_name() { 
  1123. $bbp = bbpress(); 
  1124. $public_display = array(); 
  1125. $public_display['display_username'] = $bbp->displayed_user->user_login; 
  1126.  
  1127. if ( !empty( $bbp->displayed_user->nickname ) ) 
  1128. $public_display['display_nickname'] = $bbp->displayed_user->nickname; 
  1129.  
  1130. if ( !empty( $bbp->displayed_user->first_name ) ) 
  1131. $public_display['display_firstname'] = $bbp->displayed_user->first_name; 
  1132.  
  1133. if ( !empty( $bbp->displayed_user->last_name ) ) 
  1134. $public_display['display_lastname'] = $bbp->displayed_user->last_name; 
  1135.  
  1136. if ( !empty( $bbp->displayed_user->first_name ) && !empty( $bbp->displayed_user->last_name ) ) { 
  1137. $public_display['display_firstlast'] = $bbp->displayed_user->first_name . ' ' . $bbp->displayed_user->last_name; 
  1138. $public_display['display_lastfirst'] = $bbp->displayed_user->last_name . ' ' . $bbp->displayed_user->first_name; 
  1139.  
  1140. if ( !in_array( $bbp->displayed_user->display_name, $public_display ) ) // Only add this if it isn't duplicated elsewhere 
  1141. $public_display = array( 'display_displayname' => $bbp->displayed_user->display_name ) + $public_display; 
  1142.  
  1143. $public_display = array_map( 'trim', $public_display ); 
  1144. $public_display = array_unique( $public_display ); ?> 
  1145.  
  1146. <select name="display_name" id="display_name"> 
  1147.  
  1148. <?php foreach ( $public_display as $id => $item ) : ?> 
  1149.  
  1150. <option id="<?php echo $id; ?>" value="<?php echo esc_attr( $item ); ?>"<?php selected( $bbp->displayed_user->display_name, $item ); ?>><?php echo $item; ?></option> 
  1151.  
  1152. <?php endforeach; ?> 
  1153.  
  1154. </select> 
  1155.  
  1156. <?php 
  1157.  
  1158. /** 
  1159. * Output blog role selector (for user edit) 
  1160. * 
  1161. * @since bbPress (r2688) 
  1162. */ 
  1163. function bbp_edit_user_blog_role() { 
  1164.  
  1165. // Return if no user is being edited 
  1166. if ( ! bbp_is_single_user_edit() ) 
  1167. return; 
  1168.  
  1169. // Get users current blog role 
  1170. $user_role = bbp_get_user_blog_role( bbp_get_displayed_user_id() ); 
  1171.  
  1172. // Get the blog roles 
  1173. $blog_roles = bbp_get_blog_roles(); ?> 
  1174.  
  1175. <select name="role" id="role"> 
  1176. <option value=""><?php esc_html_e( '— No role for this site —', 'bbpress' ); ?></option> 
  1177.  
  1178. <?php foreach ( $blog_roles as $role => $details ) : ?> 
  1179.  
  1180. <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option> 
  1181.  
  1182. <?php endforeach; ?> 
  1183.  
  1184. </select> 
  1185.  
  1186. <?php 
  1187.  
  1188. /** 
  1189. * Output forum role selector (for user edit) 
  1190. * 
  1191. * @since bbPress (r4284) 
  1192. */ 
  1193. function bbp_edit_user_forums_role() { 
  1194.  
  1195. // Return if no user is being edited 
  1196. if ( ! bbp_is_single_user_edit() ) 
  1197. return; 
  1198.  
  1199. // Get the user's current forum role 
  1200. $user_role = bbp_get_user_role( bbp_get_displayed_user_id() ); 
  1201.  
  1202. // Get the folum roles 
  1203. $dynamic_roles = bbp_get_dynamic_roles(); 
  1204.  
  1205. // Only keymasters can set other keymasters 
  1206. if ( ! bbp_is_user_keymaster() ) 
  1207. unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); ?> 
  1208.  
  1209. <select name="bbp-forums-role" id="bbp-forums-role"> 
  1210. <option value=""><?php esc_html_e( '— No role for these forums —', 'bbpress' ); ?></option> 
  1211.  
  1212. <?php foreach ( $dynamic_roles as $role => $details ) : ?> 
  1213.  
  1214. <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option> 
  1215.  
  1216. <?php endforeach; ?> 
  1217.  
  1218. </select> 
  1219.  
  1220. <?php 
  1221.  
  1222. /** 
  1223. * Return user contact methods Selectbox 
  1224. * 
  1225. * @since bbPress (r2688) 
  1226. * 
  1227. * @uses _wp_get_user_contactmethods() To get the contact methods 
  1228. * @uses apply_filters() Calls 'bbp_edit_user_contact_methods' with the methods 
  1229. * @return string User contact methods 
  1230. */ 
  1231. function bbp_edit_user_contact_methods() { 
  1232.  
  1233. // Get the core WordPress contact methods 
  1234. $contact_methods = _wp_get_user_contactmethods( bbpress()->displayed_user ); 
  1235.  
  1236. return apply_filters( 'bbp_edit_user_contact_methods', $contact_methods ); 
  1237.  
  1238. /** Topics Created ************************************************************/ 
  1239.  
  1240. /** 
  1241. * Output the link to the user's topics 
  1242. * 
  1243. * @since bbPress (r4225) 
  1244. * 
  1245. * @param int $user_id Optional. User id 
  1246. * @uses bbp_get_favorites_permalink() To get the favorites permalink 
  1247. */ 
  1248. function bbp_user_topics_created_url( $user_id = 0 ) { 
  1249. echo esc_url( bbp_get_user_topics_created_url( $user_id ) ); 
  1250. /** 
  1251. * Return the link to the user's topics 
  1252. * 
  1253. * @since bbPress (r4225) 
  1254. * 
  1255. * @param int $user_id Optional. User id 
  1256. * @uses bbp_get_user_profile_url() To get the user profile url 
  1257. * @uses apply_filters() Calls 'bbp_get_favorites_permalink' with the 
  1258. * user profile url and user id 
  1259. * @return string Permanent link to user profile page 
  1260. */ 
  1261. function bbp_get_user_topics_created_url( $user_id = 0 ) { 
  1262. global $wp_rewrite; 
  1263.  
  1264. // Use displayed user ID if there is one, and one isn't requested 
  1265. $user_id = bbp_get_user_id( $user_id ); 
  1266. if ( empty( $user_id ) ) 
  1267. return false; 
  1268.  
  1269. // Allow early overriding of the profile URL to cut down on processing 
  1270. $early_url = apply_filters( 'bbp_pre_get_user_topics_created_url', (int) $user_id ); 
  1271. if ( is_string( $early_url ) ) 
  1272. return $early_url; 
  1273.  
  1274. // Pretty permalinks 
  1275. if ( $wp_rewrite->using_permalinks() ) { 
  1276. $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/' . bbp_get_topic_archive_slug(); 
  1277. $user = get_userdata( $user_id ); 
  1278. if ( ! empty( $user->user_nicename ) ) { 
  1279. $user_nicename = $user->user_nicename; 
  1280. } else { 
  1281. $user_nicename = $user->user_login; 
  1282. $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url ); 
  1283. $url = home_url( user_trailingslashit( $url ) ); 
  1284.  
  1285. // Unpretty permalinks 
  1286. } else { 
  1287. $url = add_query_arg( array( 
  1288. bbp_get_user_rewrite_id() => $user_id,  
  1289. bbp_get_user_topics_rewrite_id() => '1',  
  1290. ), home_url( '/' ) ); 
  1291.  
  1292. return apply_filters( 'bbp_get_user_topics_created_url', $url, $user_id ); 
  1293.  
  1294. /** Topics Created ************************************************************/ 
  1295.  
  1296. /** 
  1297. * Output the link to the user's replies 
  1298. * 
  1299. * @since bbPress (r4225) 
  1300. * 
  1301. * @param int $user_id Optional. User id 
  1302. * @uses bbp_get_favorites_permalink() To get the favorites permalink 
  1303. */ 
  1304. function bbp_user_replies_created_url( $user_id = 0 ) { 
  1305. echo esc_url( bbp_get_user_replies_created_url( $user_id ) ); 
  1306. /** 
  1307. * Return the link to the user's replies 
  1308. * 
  1309. * @since bbPress (r4225) 
  1310. * 
  1311. * @param int $user_id Optional. User id 
  1312. * @uses bbp_get_user_profile_url() To get the user profile url 
  1313. * @uses apply_filters() Calls 'bbp_get_favorites_permalink' with the 
  1314. * user profile url and user id 
  1315. * @return string Permanent link to user profile page 
  1316. */ 
  1317. function bbp_get_user_replies_created_url( $user_id = 0 ) { 
  1318. global $wp_rewrite; 
  1319.  
  1320. // Use displayed user ID if there is one, and one isn't requested 
  1321. $user_id = bbp_get_user_id( $user_id ); 
  1322. if ( empty( $user_id ) ) 
  1323. return false; 
  1324.  
  1325. // Allow early overriding of the profile URL to cut down on processing 
  1326. $early_url = apply_filters( 'bbp_pre_get_user_replies_created_url', (int) $user_id ); 
  1327. if ( is_string( $early_url ) ) 
  1328. return $early_url; 
  1329.  
  1330. // Pretty permalinks 
  1331. if ( $wp_rewrite->using_permalinks() ) { 
  1332. $url = $wp_rewrite->root . bbp_get_user_slug() . '/%' . bbp_get_user_rewrite_id() . '%/' . bbp_get_reply_archive_slug(); 
  1333. $user = get_userdata( $user_id ); 
  1334. if ( ! empty( $user->user_nicename ) ) { 
  1335. $user_nicename = $user->user_nicename; 
  1336. } else { 
  1337. $user_nicename = $user->user_login; 
  1338. $url = str_replace( '%' . bbp_get_user_rewrite_id() . '%', $user_nicename, $url ); 
  1339. $url = home_url( user_trailingslashit( $url ) ); 
  1340.  
  1341. // Unpretty permalinks 
  1342. } else { 
  1343. $url = add_query_arg( array( 
  1344. bbp_get_user_rewrite_id() => $user_id,  
  1345. bbp_get_user_replies_rewrite_id() => '1',  
  1346. ), home_url( '/' ) ); 
  1347.  
  1348. return apply_filters( 'bbp_get_user_replies_created_url', $url, $user_id ); 
  1349.  
  1350. /** Login *********************************************************************/ 
  1351.  
  1352. /** 
  1353. * Handle the login and registration template notices 
  1354. * 
  1355. * @since bbPress (r2970) 
  1356. * 
  1357. * @uses WP_Error bbPress::errors::add() To add an error or message 
  1358. */ 
  1359. function bbp_login_notices() { 
  1360.  
  1361. // loggedout was passed 
  1362. if ( !empty( $_GET['loggedout'] ) && ( true === $_GET['loggedout'] ) ) { 
  1363. bbp_add_error( 'loggedout', __( 'You are now logged out.', 'bbpress' ), 'message' ); 
  1364.  
  1365. // registration is disabled 
  1366. } elseif ( !empty( $_GET['registration'] ) && ( 'disabled' === $_GET['registration'] ) ) { 
  1367. bbp_add_error( 'registerdisabled', __( 'New user registration is currently not allowed.', 'bbpress' ) ); 
  1368.  
  1369. // Prompt user to check their email 
  1370. } elseif ( !empty( $_GET['checkemail'] ) && in_array( $_GET['checkemail'], array( 'confirm', 'newpass', 'registered' ) ) ) { 
  1371.  
  1372. switch ( $_GET['checkemail'] ) { 
  1373.  
  1374. // Email needs confirmation 
  1375. case 'confirm' : 
  1376. bbp_add_error( 'confirm', __( 'Check your e-mail for the confirmation link.', 'bbpress' ), 'message' ); 
  1377. break; 
  1378.  
  1379. // User requested a new password 
  1380. case 'newpass' : 
  1381. bbp_add_error( 'newpass', __( 'Check your e-mail for your new password.', 'bbpress' ), 'message' ); 
  1382. break; 
  1383.  
  1384. // User is newly registered 
  1385. case 'registered' : 
  1386. bbp_add_error( 'registered', __( 'Registration complete. Please check your e-mail.', 'bbpress' ), 'message' ); 
  1387. break; 
  1388.  
  1389. /** 
  1390. * Redirect a user back to their profile if they are already logged in. 
  1391. * 
  1392. * This should be used before {@link get_header()} is called in template files 
  1393. * where the user should never have access to the contents of that file. 
  1394. * 
  1395. * @since bbPress (r2815) 
  1396. * 
  1397. * @param string $url The URL to redirect to 
  1398. * @uses is_user_logged_in() Check if user is logged in 
  1399. * @uses wp_safe_redirect() To safely redirect 
  1400. * @uses bbp_get_user_profile_url() To get the profile url of the user 
  1401. * @uses bbp_get_current_user_id() To get the current user id 
  1402. */ 
  1403. function bbp_logged_in_redirect( $url = '' ) { 
  1404.  
  1405. // Bail if user is not logged in 
  1406. if ( !is_user_logged_in() ) 
  1407. return; 
  1408.  
  1409. // Setup the profile page to redirect to 
  1410. $redirect_to = !empty( $url ) ? $url : bbp_get_user_profile_url( bbp_get_current_user_id() ); 
  1411.  
  1412. // Do a safe redirect and exit 
  1413. wp_safe_redirect( $redirect_to ); 
  1414. exit; 
  1415.  
  1416. /** 
  1417. * Output the required hidden fields when logging in 
  1418. * 
  1419. * @since bbPress (r2815) 
  1420. * 
  1421. * @uses apply_filters() To allow custom redirection 
  1422. * @uses bbp_redirect_to_field() To output the hidden request url field 
  1423. * @uses wp_nonce_field() To generate hidden nonce fields 
  1424. */ 
  1425. function bbp_user_login_fields() { 
  1426. ?> 
  1427.  
  1428. <input type="hidden" name="user-cookie" value="1" /> 
  1429.  
  1430. <?php 
  1431.  
  1432. // Allow custom login redirection 
  1433. $redirect_to = apply_filters( 'bbp_user_login_redirect_to', '' ); 
  1434. bbp_redirect_to_field( $redirect_to ); 
  1435.  
  1436. // Prevent intention hi-jacking of log-in form 
  1437. wp_nonce_field( 'bbp-user-login' ); 
  1438.  
  1439. /** Register ******************************************************************/ 
  1440.  
  1441. /** 
  1442. * Output the required hidden fields when registering 
  1443. * 
  1444. * @since bbPress (r2815) 
  1445. * 
  1446. * @uses add_query_arg() To add query args 
  1447. * @uses bbp_login_url() To get the login url 
  1448. * @uses apply_filters() To allow custom redirection 
  1449. * @uses bbp_redirect_to_field() To output the redirect to field 
  1450. * @uses wp_nonce_field() To generate hidden nonce fields 
  1451. */ 
  1452. function bbp_user_register_fields() { 
  1453. ?> 
  1454.  
  1455. <input type="hidden" name="action" value="register" /> 
  1456. <input type="hidden" name="user-cookie" value="1" /> 
  1457.  
  1458. <?php 
  1459.  
  1460. // Allow custom registration redirection 
  1461. $redirect_to = apply_filters( 'bbp_user_register_redirect_to', '' ); 
  1462. bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'registered' ), $redirect_to ) ); 
  1463.  
  1464. // Prevent intention hi-jacking of sign-up form 
  1465. wp_nonce_field( 'bbp-user-register' ); 
  1466.  
  1467. /** Lost Password *************************************************************/ 
  1468.  
  1469. /** 
  1470. * Output the required hidden fields when user lost password 
  1471. * 
  1472. * @since bbPress (r2815) 
  1473. * 
  1474. * @uses apply_filters() To allow custom redirection 
  1475. * @uses bbp_redirect_to_field() Set referer 
  1476. * @uses wp_nonce_field() To generate hidden nonce fields 
  1477. */ 
  1478. function bbp_user_lost_pass_fields() { 
  1479. ?> 
  1480.  
  1481. <input type="hidden" name="user-cookie" value="1" /> 
  1482.  
  1483. <?php 
  1484.  
  1485. // Allow custom lost pass redirection 
  1486. $redirect_to = apply_filters( 'bbp_user_lost_pass_redirect_to', get_permalink() ); 
  1487. bbp_redirect_to_field( add_query_arg( array( 'checkemail' => 'confirm' ), $redirect_to ) ); 
  1488.  
  1489. // Prevent intention hi-jacking of lost pass form 
  1490. wp_nonce_field( 'bbp-user-lost-pass' ); 
  1491.  
  1492. /** Author Avatar *************************************************************/ 
  1493.  
  1494. /** 
  1495. * Output the author link of a post 
  1496. * 
  1497. * @since bbPress (r2875) 
  1498. * 
  1499. * @param mixed $args Optional. If it is an integer, it is used as post id. 
  1500. * @uses bbp_get_author_link() To get the post author link 
  1501. */ 
  1502. function bbp_author_link( $args = '' ) { 
  1503. echo bbp_get_author_link( $args ); 
  1504. /** 
  1505. * Return the author link of the post 
  1506. * 
  1507. * @since bbPress (r2875) 
  1508. * 
  1509. * @param mixed $args Optional. If an integer, it is used as reply id. 
  1510. * @uses bbp_is_topic() To check if it's a topic page 
  1511. * @uses bbp_get_topic_author_link() To get the topic author link 
  1512. * @uses bbp_is_reply() To check if it's a reply page 
  1513. * @uses bbp_get_reply_author_link() To get the reply author link 
  1514. * @uses get_post_field() To get the post author 
  1515. * @uses bbp_is_reply_anonymous() To check if the reply is by an 
  1516. * anonymous user 
  1517. * @uses get_the_author_meta() To get the author name 
  1518. * @uses bbp_get_user_profile_url() To get the author profile url 
  1519. * @uses get_avatar() To get the author avatar 
  1520. * @uses apply_filters() Calls 'bbp_get_reply_author_link' with the 
  1521. * author link and args 
  1522. * @return string Author link of reply 
  1523. */ 
  1524. function bbp_get_author_link( $args = '' ) { 
  1525.  
  1526. $post_id = is_numeric( $args ) ? (int) $args : 0; 
  1527.  
  1528. // Parse arguments against default values 
  1529. $r = bbp_parse_args( $args, array( 
  1530. 'post_id' => $post_id,  
  1531. 'link_title' => '',  
  1532. 'type' => 'both',  
  1533. 'size' => 80 
  1534. ), 'get_author_link' ); 
  1535.  
  1536. // Confirmed topic 
  1537. if ( bbp_is_topic( $r['post_id'] ) ) { 
  1538. return bbp_get_topic_author_link( $r ); 
  1539.  
  1540. // Confirmed reply 
  1541. } elseif ( bbp_is_reply( $r['post_id'] ) ) { 
  1542. return bbp_get_reply_author_link( $r ); 
  1543.  
  1544. // Get the post author and proceed 
  1545. $user_id = get_post_field( 'post_author', $r['post_id'] ); 
  1546.  
  1547. // Neither a reply nor a topic, so could be a revision 
  1548. if ( !empty( $r['post_id'] ) ) { 
  1549.  
  1550. // Generate title with the display name of the author 
  1551. if ( empty( $r['link_title'] ) ) { 
  1552. $r['link_title'] = sprintf( !bbp_is_reply_anonymous( $r['post_id'] ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), get_the_author_meta( 'display_name', $user_id ) ); 
  1553.  
  1554. // Assemble some link bits 
  1555. $link_title = !empty( $r['link_title'] ) ? ' title="' . $r['link_title'] . '"' : ''; 
  1556. $anonymous = bbp_is_reply_anonymous( $r['post_id'] ); 
  1557.  
  1558. // Get avatar 
  1559. if ( 'avatar' === $r['type'] || 'both' === $r['type'] ) { 
  1560. $author_links[] = get_avatar( $user_id, $r['size'] ); 
  1561.  
  1562. // Get display name 
  1563. if ( 'name' === $r['type'] || 'both' === $r['type'] ) { 
  1564. $author_links[] = get_the_author_meta( 'display_name', $user_id ); 
  1565.  
  1566. // Add links if not anonymous 
  1567. if ( empty( $anonymous ) && bbp_user_has_profile( $user_id ) ) { 
  1568. $author_url = bbp_get_user_profile_url( $user_id ); 
  1569. foreach ( $author_links as $link_text ) { 
  1570. $author_link[] = sprintf( '<a href="%1$s"%2$s>%3$s</a>', $author_url, $link_title, $link_text ); 
  1571. $author_link = implode( ' ', $author_link ); 
  1572.  
  1573. // No links if anonymous 
  1574. } else { 
  1575. $author_link = implode( ' ', $author_links ); 
  1576.  
  1577. // No post so link is empty 
  1578. } else { 
  1579. $author_link = ''; 
  1580.  
  1581. return apply_filters( 'bbp_get_author_link', $author_link, $r ); 
  1582.  
  1583. /** Capabilities **************************************************************/ 
  1584.  
  1585. /** 
  1586. * Check if the user can access a specific forum 
  1587. * 
  1588. * @since bbPress (r3127) 
  1589. * 
  1590. * @uses bbp_get_current_user_id() 
  1591. * @uses bbp_get_forum_id() 
  1592. * @uses bbp_allow_anonymous() 
  1593. * @uses bbp_parse_args() 
  1594. * @uses bbp_get_user_id() 
  1595. * @uses current_user_can() 
  1596. * @uses bbp_is_user_keymaster() 
  1597. * @uses bbp_is_forum_public() 
  1598. * @uses bbp_is_forum_private() 
  1599. * @uses bbp_is_forum_hidden() 
  1600. * @uses current_user_can() 
  1601. * @uses apply_filters() 
  1602. * 
  1603. * @return bool 
  1604. */ 
  1605. function bbp_user_can_view_forum( $args = '' ) { 
  1606.  
  1607. // Parse arguments against default values 
  1608. $r = bbp_parse_args( $args, array( 
  1609. 'user_id' => bbp_get_current_user_id(),  
  1610. 'forum_id' => bbp_get_forum_id(),  
  1611. 'check_ancestors' => false 
  1612. ), 'user_can_view_forum' ); 
  1613.  
  1614. // Validate parsed values 
  1615. $user_id = bbp_get_user_id( $r['user_id'], false, false ); 
  1616. $forum_id = bbp_get_forum_id( $r['forum_id'] ); 
  1617. $retval = false; 
  1618.  
  1619. // User is a keymaster 
  1620. if ( !empty( $user_id ) && bbp_is_user_keymaster( $user_id ) ) { 
  1621. $retval = true; 
  1622.  
  1623. // Forum is public, and user can read forums or is not logged in 
  1624. } elseif ( bbp_is_forum_public( $forum_id, $r['check_ancestors'] ) ) { 
  1625. $retval = true; 
  1626.  
  1627. // Forum is private, and user can see it 
  1628. } elseif ( bbp_is_forum_private( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_private_forums' ) ) { 
  1629. $retval = true; 
  1630.  
  1631. // Forum is hidden, and user can see it 
  1632. } elseif ( bbp_is_forum_hidden ( $forum_id, $r['check_ancestors'] ) && user_can( $user_id, 'read_hidden_forums' ) ) { 
  1633. $retval = true; 
  1634.  
  1635. return apply_filters( 'bbp_user_can_view_forum', $retval, $forum_id, $user_id ); 
  1636.  
  1637. /** 
  1638. * Check if the current user can publish topics 
  1639. * 
  1640. * @since bbPress (r3127) 
  1641. * 
  1642. * @uses bbp_is_user_keymaster() 
  1643. * @uses is_user_logged_in() 
  1644. * @uses bbp_allow_anonymous() 
  1645. * @uses bbp_is_user_active() 
  1646. * @uses current_user_can() 
  1647. * @uses apply_filters() 
  1648. * 
  1649. * @return bool 
  1650. */ 
  1651. function bbp_current_user_can_publish_topics() { 
  1652.  
  1653. // Users need to earn access 
  1654. $retval = false; 
  1655.  
  1656. // Always allow keymasters 
  1657. if ( bbp_is_user_keymaster() ) { 
  1658. $retval = true; 
  1659.  
  1660. // Do not allow anonymous if not enabled 
  1661. } elseif ( !is_user_logged_in() && bbp_allow_anonymous() ) { 
  1662. $retval = true; 
  1663.  
  1664. // User is logged in 
  1665. } elseif ( current_user_can( 'publish_topics' ) ) { 
  1666. $retval = true; 
  1667.  
  1668. // Allow access to be filtered 
  1669. return (bool) apply_filters( 'bbp_current_user_can_publish_topics', $retval ); 
  1670.  
  1671. /** 
  1672. * Check if the current user can publish forums 
  1673. * 
  1674. * @since bbPress (r3549) 
  1675. * 
  1676. * @uses bbp_is_user_keymaster() 
  1677. * @uses bbp_is_user_active() 
  1678. * @uses current_user_can() 
  1679. * @uses apply_filters() 
  1680. * 
  1681. * @return bool 
  1682. */ 
  1683. function bbp_current_user_can_publish_forums() { 
  1684.  
  1685. // Users need to earn access 
  1686. $retval = false; 
  1687.  
  1688. // Always allow keymasters 
  1689. if ( bbp_is_user_keymaster() ) { 
  1690. $retval = true; 
  1691.  
  1692. // User is logged in 
  1693. } elseif ( current_user_can( 'publish_forums' ) ) { 
  1694. $retval = true; 
  1695.  
  1696. // Allow access to be filtered 
  1697. return (bool) apply_filters( 'bbp_current_user_can_publish_forums', $retval ); 
  1698.  
  1699. /** 
  1700. * Check if the current user can publish replies 
  1701. * 
  1702. * @since bbPress (r3127) 
  1703. * 
  1704. * @uses bbp_is_user_keymaster() 
  1705. * @uses is_user_logged_in() 
  1706. * @uses bbp_allow_anonymous() 
  1707. * @uses bbp_is_user_active() 
  1708. * @uses current_user_can() 
  1709. * @uses apply_filters() 
  1710. * 
  1711. * @return bool 
  1712. */ 
  1713. function bbp_current_user_can_publish_replies() { 
  1714.  
  1715. // Users need to earn access 
  1716. $retval = false; 
  1717.  
  1718. // Always allow keymasters 
  1719. if ( bbp_is_user_keymaster() ) { 
  1720. $retval = true; 
  1721.  
  1722. // Do not allow anonymous if not enabled 
  1723. } elseif ( !is_user_logged_in() && bbp_allow_anonymous() ) { 
  1724. $retval = true; 
  1725.  
  1726. // User is logged in 
  1727. } elseif ( current_user_can( 'publish_replies' ) ) { 
  1728. $retval = true; 
  1729.  
  1730. // Allow access to be filtered 
  1731. return (bool) apply_filters( 'bbp_current_user_can_publish_replies', $retval ); 
  1732.  
  1733. /** Forms *********************************************************************/ 
  1734.  
  1735. /** 
  1736. * The following functions should be turned into mapped meta capabilities in a 
  1737. * future version. They exist only to remove complex logistical capability 
  1738. * checks from within template parts. 
  1739. */ 
  1740.  
  1741. /** 
  1742. * Get the forums the current user has the ability to see and post to 
  1743. * 
  1744. * @since bbPress (r3127) 
  1745. * 
  1746. * @uses bbp_get_forum_post_type() 
  1747. * @uses get_posts() 
  1748. * 
  1749. * @param type $args 
  1750. * @return type 
  1751. */ 
  1752. function bbp_get_forums_for_current_user( $args = array() ) { 
  1753.  
  1754. // Setup arrays 
  1755. $private = $hidden = $post__not_in = array(); 
  1756.  
  1757. // Private forums 
  1758. if ( !current_user_can( 'read_private_forums' ) ) 
  1759. $private = bbp_get_private_forum_ids(); 
  1760.  
  1761. // Hidden forums 
  1762. if ( !current_user_can( 'read_hidden_forums' ) ) 
  1763. $hidden = bbp_get_hidden_forum_ids(); 
  1764.  
  1765. // Merge private and hidden forums together and remove any empties 
  1766. $forum_ids = (array) array_filter( wp_parse_id_list( array_merge( $private, $hidden ) ) ); 
  1767.  
  1768. // There are forums that need to be ex 
  1769. if ( !empty( $forum_ids ) ) 
  1770. $post__not_in = implode( ', ', $forum_ids ); 
  1771.  
  1772. // Parse arguments against default values 
  1773. $r = bbp_parse_args( $args, array( 
  1774. 'post_type' => bbp_get_forum_post_type(),  
  1775. 'post_status' => bbp_get_public_status_id(),  
  1776. 'numberposts' => -1,  
  1777. 'exclude' => $post__not_in 
  1778. ), 'get_forums_for_current_user' ); 
  1779.  
  1780. // Get the forums 
  1781. $forums = get_posts( $r ); 
  1782.  
  1783. // No availabe forums 
  1784. if ( empty( $forums ) ) 
  1785. $forums = false; 
  1786.  
  1787. return apply_filters( 'bbp_get_forums_for_current_user', $forums ); 
  1788.  
  1789. /** 
  1790. * Performs a series of checks to ensure the current user can create forums. 
  1791. * 
  1792. * @since bbPress (r3549) 
  1793. * 
  1794. * @uses bbp_is_user_keymaster() 
  1795. * @uses bbp_is_forum_edit() 
  1796. * @uses current_user_can() 
  1797. * @uses bbp_get_forum_id() 
  1798. * 
  1799. * @return bool 
  1800. */ 
  1801. function bbp_current_user_can_access_create_forum_form() { 
  1802.  
  1803. // Users need to earn access 
  1804. $retval = false; 
  1805.  
  1806. // Always allow keymasters 
  1807. if ( bbp_is_user_keymaster() ) { 
  1808. $retval = true; 
  1809.  
  1810. // Looking at a single forum & forum is open 
  1811. } elseif ( ( is_page() || is_single() ) && bbp_is_forum_open() ) { 
  1812. $retval = bbp_current_user_can_publish_forums(); 
  1813.  
  1814. // User can edit this topic 
  1815. } elseif ( bbp_is_forum_edit() ) { 
  1816. $retval = current_user_can( 'edit_forum', bbp_get_forum_id() ); 
  1817.  
  1818. // Allow access to be filtered 
  1819. return (bool) apply_filters( 'bbp_current_user_can_access_create_forum_form', (bool) $retval ); 
  1820.  
  1821. /** 
  1822. * Performs a series of checks to ensure the current user can create topics. 
  1823. * 
  1824. * @since bbPress (r3127) 
  1825. * 
  1826. * @uses bbp_is_user_keymaster() 
  1827. * @uses bbp_is_topic_edit() 
  1828. * @uses current_user_can() 
  1829. * @uses bbp_get_topic_id() 
  1830. * @uses bbp_allow_anonymous() 
  1831. * @uses is_user_logged_in() 
  1832. * 
  1833. * @return bool 
  1834. */ 
  1835. function bbp_current_user_can_access_create_topic_form() { 
  1836.  
  1837. // Users need to earn access 
  1838. $retval = false; 
  1839.  
  1840. // Always allow keymasters 
  1841. if ( bbp_is_user_keymaster() ) { 
  1842. $retval = true; 
  1843.  
  1844. // Looking at a single forum & forum is open 
  1845. } elseif ( ( bbp_is_single_forum() || is_page() || is_single() ) && bbp_is_forum_open() ) { 
  1846. $retval = bbp_current_user_can_publish_topics(); 
  1847.  
  1848. // User can edit this topic 
  1849. } elseif ( bbp_is_topic_edit() ) { 
  1850. $retval = current_user_can( 'edit_topic', bbp_get_topic_id() ); 
  1851.  
  1852. // Allow access to be filtered 
  1853. return (bool) apply_filters( 'bbp_current_user_can_access_create_topic_form', (bool) $retval ); 
  1854.  
  1855. /** 
  1856. * Performs a series of checks to ensure the current user can create replies. 
  1857. * 
  1858. * @since bbPress (r3127) 
  1859. * 
  1860. * @uses bbp_is_user_keymaster() 
  1861. * @uses bbp_is_topic_edit() 
  1862. * @uses current_user_can() 
  1863. * @uses bbp_get_topic_id() 
  1864. * @uses bbp_allow_anonymous() 
  1865. * @uses is_user_logged_in() 
  1866. * 
  1867. * @return bool 
  1868. */ 
  1869. function bbp_current_user_can_access_create_reply_form() { 
  1870.  
  1871. // Users need to earn access 
  1872. $retval = false; 
  1873.  
  1874. // Always allow keymasters 
  1875. if ( bbp_is_user_keymaster() ) { 
  1876. $retval = true; 
  1877.  
  1878. // Looking at a single topic, topic is open, and forum is open 
  1879. } elseif ( ( bbp_is_single_topic() || is_page() || is_single() ) && bbp_is_topic_open() && bbp_is_forum_open() ) { 
  1880. $retval = bbp_current_user_can_publish_replies(); 
  1881.  
  1882. // User can edit this topic 
  1883. } elseif ( bbp_is_reply_edit() ) { 
  1884. $retval = current_user_can( 'edit_reply', bbp_get_reply_id() ); 
  1885.  
  1886. // Allow access to be filtered 
  1887. return (bool) apply_filters( 'bbp_current_user_can_access_create_reply_form', (bool) $retval ); 
  1888.  
  1889. /** 
  1890. * Performs a series of checks to ensure the current user should see the 
  1891. * anonymous user form fields. 
  1892. * 
  1893. * @since bbPress (r5119) 
  1894. * 
  1895. * @uses bbp_is_anonymous() 
  1896. * @uses bbp_is_topic_edit() 
  1897. * @uses bbp_is_topic_anonymous() 
  1898. * @uses bbp_is_reply_edit() 
  1899. * @uses bbp_is_reply_anonymous() 
  1900. * 
  1901. * @return bool 
  1902. */ 
  1903. function bbp_current_user_can_access_anonymous_user_form() { 
  1904.  
  1905. // Users need to earn access 
  1906. $retval = false; 
  1907.  
  1908. // User is not logged in, and anonymous posting is allowed 
  1909. if ( bbp_is_anonymous() ) { 
  1910. $retval = true; 
  1911.  
  1912. // User is editing a topic, and topic is authored by anonymous user 
  1913. } elseif ( bbp_is_topic_edit() && bbp_is_topic_anonymous() ) { 
  1914. $retval = true; 
  1915.  
  1916. // User is editing a reply, and reply is authored by anonymous user 
  1917. } elseif ( bbp_is_reply_edit() && bbp_is_reply_anonymous() ) { 
  1918. $retval = true; 
  1919.  
  1920. // Allow access to be filtered 
  1921. return (bool) apply_filters( 'bbp_current_user_can_access_anonymous_user_form', (bool) $retval ); 
.