/includes/users/functions.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress User Functions 
  5. * 
  6. * @package bbPress 
  7. * @subpackage Functions 
  8. */ 
  9.  
  10. // Exit if accessed directly 
  11. if ( !defined( 'ABSPATH' ) ) exit; 
  12.  
  13. /** 
  14. * Redirect back to $url when attempting to use the login page 
  15. * 
  16. * @since bbPress (r2815) 
  17. * 
  18. * @param string $url The url 
  19. * @param string $raw_url Raw url 
  20. * @param object $user User object 
  21. * @uses is_wp_error() To check if the user param is a {@link WP_Error} 
  22. * @uses admin_url() To get the admin url 
  23. * @uses home_url() To get the home url 
  24. * @uses esc_url() To escape the url 
  25. * @uses wp_safe_redirect() To redirect 
  26. */ 
  27. function bbp_redirect_login( $url = '', $raw_url = '', $user = '' ) { 
  28.  
  29. // Raw redirect_to was passed, so use it 
  30. if ( !empty( $raw_url ) ) 
  31. $url = $raw_url; 
  32.  
  33. // $url was manually set in wp-login.php to redirect to admin 
  34. elseif ( admin_url() === $url ) 
  35. $url = home_url(); 
  36.  
  37. // $url is empty 
  38. elseif ( empty( $url ) ) 
  39. $url = home_url(); 
  40.  
  41. return apply_filters( 'bbp_redirect_login', $url, $raw_url, $user ); 
  42.  
  43. /** 
  44. * Is an anonymous topic/reply being made? 
  45. * 
  46. * @since bbPress (r2688) 
  47. * 
  48. * @uses is_user_logged_in() Is the user logged in? 
  49. * @uses bbp_allow_anonymous() Is anonymous posting allowed? 
  50. * @uses apply_filters() Calls 'bbp_is_anonymous' with the return value 
  51. * @return bool True if anonymous is allowed and user is not logged in, false if 
  52. * anonymous is not allowed or user is logged in 
  53. */ 
  54. function bbp_is_anonymous() { 
  55. if ( !is_user_logged_in() && bbp_allow_anonymous() ) 
  56. $is_anonymous = true; 
  57. else 
  58. $is_anonymous = false; 
  59.  
  60. return apply_filters( 'bbp_is_anonymous', $is_anonymous ); 
  61.  
  62. /** 
  63. * Echoes the values for current poster (uses WP comment cookies) 
  64. * 
  65. * @since bbPress (r2734) 
  66. * 
  67. * @param string $key Which value to echo? 
  68. * @uses bbp_get_current_anonymous_user_data() To get the current anonymous user 
  69. * data 
  70. */ 
  71. function bbp_current_anonymous_user_data( $key = '' ) { 
  72. echo bbp_get_current_anonymous_user_data( $key ); 
  73.  
  74. /** 
  75. * Get the cookies for current poster (uses WP comment cookies). 
  76. * 
  77. * @since bbPress (r2734) 
  78. * 
  79. * @param string $key Optional. Which value to get? If not given, then 
  80. * an array is returned. 
  81. * @uses sanitize_comment_cookies() To sanitize the current poster data 
  82. * @uses wp_get_current_commenter() To get the current poster data * 
  83. * @return string|array Cookie(s) for current poster 
  84. */ 
  85. function bbp_get_current_anonymous_user_data( $key = '' ) { 
  86. $cookie_names = array( 
  87. 'name' => 'comment_author',  
  88. 'email' => 'comment_author_email',  
  89. 'url' => 'comment_author_url',  
  90.  
  91. // Here just for the sake of them, use the above ones 
  92. 'comment_author' => 'comment_author',  
  93. 'comment_author_email' => 'comment_author_email',  
  94. 'comment_author_url' => 'comment_author_url',  
  95. ); 
  96.  
  97. sanitize_comment_cookies(); 
  98.  
  99. $bbp_current_poster = wp_get_current_commenter(); 
  100.  
  101. if ( !empty( $key ) && in_array( $key, array_keys( $cookie_names ) ) ) 
  102. return $bbp_current_poster[$cookie_names[$key]]; 
  103.  
  104. return $bbp_current_poster; 
  105.  
  106. /** 
  107. * Set the cookies for current poster (uses WP comment cookies) 
  108. * 
  109. * @since bbPress (r2734) 
  110. * 
  111. * @param array $anonymous_data With keys 'bbp_anonymous_name',  
  112. * 'bbp_anonymous_email', 'bbp_anonymous_website'. 
  113. * Should be sanitized (see 
  114. * {@link bbp_filter_anonymous_post_data()} for 
  115. * sanitization) 
  116. * @uses apply_filters() Calls 'comment_cookie_lifetime' for cookie lifetime. 
  117. * Defaults to 30000000. 
  118. */ 
  119. function bbp_set_current_anonymous_user_data( $anonymous_data = array() ) { 
  120. if ( empty( $anonymous_data ) || !is_array( $anonymous_data ) ) 
  121. return; 
  122.  
  123. $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 ); 
  124.  
  125. setcookie( 'comment_author_' . COOKIEHASH, $anonymous_data['bbp_anonymous_name'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN ); 
  126. setcookie( 'comment_author_email_' . COOKIEHASH, $anonymous_data['bbp_anonymous_email'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN ); 
  127. setcookie( 'comment_author_url_' . COOKIEHASH, $anonymous_data['bbp_anonymous_website'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN ); 
  128.  
  129. /** 
  130. * Get the poster IP address 
  131. * 
  132. * @since bbPress (r3120) 
  133. * 
  134. * @return string 
  135. */ 
  136. function bbp_current_author_ip() { 
  137. $retval = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ); 
  138.  
  139. return apply_filters( 'bbp_current_author_ip', $retval ); 
  140.  
  141. /** 
  142. * Get the poster user agent 
  143. * 
  144. * @since bbPress (r3446) 
  145. * 
  146. * @return string 
  147. */ 
  148. function bbp_current_author_ua() { 
  149. $retval = !empty( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : ''; 
  150.  
  151. return apply_filters( 'bbp_current_author_ua', $retval ); 
  152.  
  153. /** Post Counts ***************************************************************/ 
  154.  
  155. /** 
  156. * Return the raw database count of topics by a user 
  157. * 
  158. * @since bbPress (r3633) 
  159. * @global WPDB $wpdb 
  160. * @uses bbp_get_user_id() 
  161. * @uses get_posts_by_author_sql() 
  162. * @uses bbp_get_topic_post_type() 
  163. * @uses apply_filters() 
  164. * @return int Raw DB count of topics 
  165. */ 
  166. function bbp_get_user_topic_count_raw( $user_id = 0 ) { 
  167. $user_id = bbp_get_user_id( $user_id ); 
  168. if ( empty( $user_id ) ) 
  169. return false; 
  170.  
  171. global $wpdb; 
  172.  
  173. $where = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id ); 
  174. $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" ); 
  175.  
  176. return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id ); 
  177.  
  178. /** 
  179. * Return the raw database count of replies by a user 
  180. * 
  181. * @since bbPress (r3633) 
  182. * @global WPDB $wpdb 
  183. * @uses bbp_get_user_id() 
  184. * @uses get_posts_by_author_sql() 
  185. * @uses bbp_get_reply_post_type() 
  186. * @uses apply_filters() 
  187. * @return int Raw DB count of replies 
  188. */ 
  189. function bbp_get_user_reply_count_raw( $user_id = 0 ) { 
  190. $user_id = bbp_get_user_id( $user_id ); 
  191. if ( empty( $user_id ) ) 
  192. return false; 
  193.  
  194. global $wpdb; 
  195.  
  196. $where = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id ); 
  197. $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" ); 
  198.  
  199. return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id ); 
  200.  
  201. /** Favorites *****************************************************************/ 
  202.  
  203. /** 
  204. * Get the users who have made the topic favorite 
  205. * 
  206. * @since bbPress (r2658) 
  207. * 
  208. * @param int $topic_id Optional. Topic id 
  209. * @uses wpdb::get_col() To execute our query and get the column back 
  210. * @uses apply_filters() Calls 'bbp_get_topic_favoriters' with the users and 
  211. * topic id 
  212. * @return array|bool Results if the topic has any favoriters, otherwise false 
  213. */ 
  214. function bbp_get_topic_favoriters( $topic_id = 0 ) { 
  215. $topic_id = bbp_get_topic_id( $topic_id ); 
  216. if ( empty( $topic_id ) ) 
  217. return; 
  218.  
  219. global $wpdb; 
  220.  
  221. $key = $wpdb->prefix . '_bbp_favorites'; 
  222. $users = wp_cache_get( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' ); 
  223. if ( false === $users ) { 
  224. $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ); 
  225. wp_cache_set( 'bbp_get_topic_favoriters_' . $topic_id, $users, 'bbpress_users' ); 
  226.  
  227. return apply_filters( 'bbp_get_topic_favoriters', $users ); 
  228.  
  229. /** 
  230. * Get a user's favorite topics 
  231. * 
  232. * @since bbPress (r2652) 
  233. * 
  234. * @param int $user_id Optional. User id 
  235. * @uses bbp_get_user_favorites_topic_ids() To get the user's favorites 
  236. * @uses bbp_has_topics() To get the topics 
  237. * @uses apply_filters() Calls 'bbp_get_user_favorites' with the topic query and 
  238. * user id 
  239. * @return array|bool Results if user has favorites, otherwise false 
  240. */ 
  241. function bbp_get_user_favorites( $user_id = 0 ) { 
  242. $user_id = bbp_get_user_id( $user_id ); 
  243. if ( empty( $user_id ) ) 
  244. return false; 
  245.  
  246. // If user has favorites, load them 
  247. $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 
  248. if ( !empty( $favorites ) ) { 
  249. $query = bbp_has_topics( array( 'post__in' => $favorites ) ); 
  250. } else { 
  251. $query = false; 
  252.  
  253. return apply_filters( 'bbp_get_user_favorites', $query, $user_id, $favorites ); 
  254.  
  255. /** 
  256. * Get a user's favorite topics' ids 
  257. * 
  258. * @since bbPress (r2652) 
  259. * 
  260. * @param int $user_id Optional. User id 
  261. * @uses bbp_get_user_id() To get the user id 
  262. * @uses get_user_option() To get the user favorites 
  263. * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with 
  264. * the favorites and user id 
  265. * @return array|bool Results if user has favorites, otherwise false 
  266. */ 
  267. function bbp_get_user_favorites_topic_ids( $user_id = 0 ) { 
  268. $user_id = bbp_get_user_id( $user_id ); 
  269. if ( empty( $user_id ) ) 
  270. return false; 
  271.  
  272. $favorites = get_user_option( '_bbp_favorites', $user_id ); 
  273. $favorites = array_filter( wp_parse_id_list( $favorites ) ); 
  274.  
  275. return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id ); 
  276.  
  277. /** 
  278. * Check if a topic is in user's favorites or not 
  279. * 
  280. * @since bbPress (r2652) 
  281. * 
  282. * @param int $user_id Optional. User id 
  283. * @param int $topic_id Optional. Topic id 
  284. * @uses bbp_get_user_id() To get the user id 
  285. * @uses bbp_get_user_favorites_topic_ids() To get the user favorites 
  286. * @uses bbp_get_topic() To get the topic 
  287. * @uses bbp_get_topic_id() To get the topic id 
  288. * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,  
  289. * topic id and favorites 
  290. * @return bool True if the topic is in user's favorites, otherwise false 
  291. */ 
  292. function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) { 
  293.  
  294. $user_id = bbp_get_user_id( $user_id, true, true ); 
  295. if ( empty( $user_id ) ) 
  296. return false; 
  297.  
  298. $retval = false; 
  299. $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 
  300.  
  301. if ( !empty( $favorites ) ) { 
  302.  
  303. // Checking a specific topic id 
  304. if ( !empty( $topic_id ) ) { 
  305. $topic = bbp_get_topic( $topic_id ); 
  306. $topic_id = !empty( $topic ) ? $topic->ID : 0; 
  307.  
  308. // Using the global topic id 
  309. } elseif ( bbp_get_topic_id() ) { 
  310. $topic_id = bbp_get_topic_id(); 
  311.  
  312. // Use the current post id 
  313. } elseif ( !bbp_get_topic_id() ) { 
  314. $topic_id = get_the_ID(); 
  315.  
  316. // Is topic_id in the user's favorites 
  317. if ( !empty( $topic_id ) ) { 
  318. $retval = in_array( $topic_id, $favorites ); 
  319.  
  320. return (bool) apply_filters( 'bbp_is_user_favorite', (bool) $retval, $user_id, $topic_id, $favorites ); 
  321.  
  322. /** 
  323. * Add a topic to user's favorites 
  324. * 
  325. * @since bbPress (r2652) 
  326. * 
  327. * @param int $user_id Optional. User id 
  328. * @param int $topic_id Optional. Topic id 
  329. * @uses bbp_get_user_favorites_topic_ids() To get the user favorites 
  330. * @uses update_user_option() To update the user favorites 
  331. * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id 
  332. * @return bool Always true 
  333. */ 
  334. function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) { 
  335. if ( empty( $user_id ) || empty( $topic_id ) ) 
  336. return false; 
  337.  
  338. $topic = bbp_get_topic( $topic_id ); 
  339. if ( empty( $topic ) ) 
  340. return false; 
  341.  
  342. $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 
  343. if ( !in_array( $topic_id, $favorites ) ) { 
  344. $favorites[] = $topic_id; 
  345. $favorites = implode( ', ', wp_parse_id_list( array_filter( $favorites ) ) ); 
  346. update_user_option( $user_id, '_bbp_favorites', $favorites ); 
  347.  
  348. do_action( 'bbp_add_user_favorite', $user_id, $topic_id ); 
  349.  
  350. return true; 
  351.  
  352. /** 
  353. * Remove a topic from user's favorites 
  354. * 
  355. * @since bbPress (r2652) 
  356. * 
  357. * @param int $user_id Optional. User id 
  358. * @param int $topic_id Optional. Topic id 
  359. * @uses bbp_get_user_favorites_topic_ids() To get the user favorites 
  360. * @uses update_user_option() To update the user favorites 
  361. * @uses delete_user_option() To delete the user favorites meta 
  362. * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id 
  363. * @return bool True if the topic was removed from user's favorites, otherwise 
  364. * false 
  365. */ 
  366. function bbp_remove_user_favorite( $user_id, $topic_id ) { 
  367. if ( empty( $user_id ) || empty( $topic_id ) ) 
  368. return false; 
  369.  
  370. $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ); 
  371. if ( empty( $favorites ) ) 
  372. return false; 
  373.  
  374. $pos = array_search( $topic_id, $favorites ); 
  375. if ( is_numeric( $pos ) ) { 
  376. array_splice( $favorites, $pos, 1 ); 
  377. $favorites = array_filter( $favorites ); 
  378.  
  379. if ( !empty( $favorites ) ) { 
  380. $favorites = implode( ', ', wp_parse_id_list( $favorites ) ); 
  381. update_user_option( $user_id, '_bbp_favorites', $favorites ); 
  382. } else { 
  383. delete_user_option( $user_id, '_bbp_favorites' ); 
  384.  
  385. do_action( 'bbp_remove_user_favorite', $user_id, $topic_id ); 
  386.  
  387. return true; 
  388.  
  389. /** 
  390. * Handles the front end adding and removing of favorite topics 
  391. * 
  392. * @param string $action The requested action to compare this function to 
  393. * @uses bbp_get_user_id() To get the user id 
  394. * @uses bbp_verify_nonce_request() To verify the nonce and check the request 
  395. * @uses current_user_can() To check if the current user can edit the user 
  396. * @uses bbPress:errors:add() To log the error messages 
  397. * @uses bbp_is_user_favorite() To check if the topic is in user's favorites 
  398. * @uses bbp_remove_user_favorite() To remove the user favorite 
  399. * @uses bbp_add_user_favorite() To add the user favorite 
  400. * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic 
  401. * id and action 
  402. * @uses bbp_is_favorites() To check if it's the favorites page 
  403. * @uses bbp_get_favorites_link() To get the favorites page link 
  404. * @uses bbp_get_topic_permalink() To get the topic permalink 
  405. * @uses wp_safe_redirect() To redirect to the url 
  406. */ 
  407. function bbp_favorites_handler( $action = '' ) { 
  408.  
  409. if ( !bbp_is_favorites_active() ) 
  410. return false; 
  411.  
  412. // Bail if no topic ID is passed 
  413. if ( empty( $_GET['topic_id'] ) ) 
  414. return; 
  415.  
  416. // Setup possible get actions 
  417. $possible_actions = array( 
  418. 'bbp_favorite_add',  
  419. 'bbp_favorite_remove',  
  420. ); 
  421.  
  422. // Bail if actions aren't meant for this function 
  423. if ( !in_array( $action, $possible_actions ) ) 
  424. return; 
  425.  
  426. // What action is taking place? 
  427. $topic_id = intval( $_GET['topic_id'] ); 
  428. $user_id = bbp_get_user_id( 0, true, true ); 
  429.  
  430. // Check for empty topic 
  431. if ( empty( $topic_id ) ) { 
  432. bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) ); 
  433.  
  434. // Check nonce 
  435. } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) { 
  436. bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  437.  
  438. // Check current user's ability to edit the user 
  439. } elseif ( !current_user_can( 'edit_user', $user_id ) ) { 
  440. bbp_add_error( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); 
  441.  
  442. // Bail if errors 
  443. if ( bbp_has_errors() ) 
  444. return; 
  445.  
  446. /** No errors *************************************************************/ 
  447.  
  448. $is_favorite = bbp_is_user_favorite( $user_id, $topic_id ); 
  449. $success = false; 
  450.  
  451. if ( true === $is_favorite && 'bbp_favorite_remove' === $action ) 
  452. $success = bbp_remove_user_favorite( $user_id, $topic_id ); 
  453. elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) 
  454. $success = bbp_add_user_favorite( $user_id, $topic_id ); 
  455.  
  456. // Do additional favorites actions 
  457. do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action ); 
  458.  
  459. // Success! 
  460. if ( true === $success ) { 
  461.  
  462. // Redirect back from whence we came 
  463. if ( bbp_is_favorites() ) { 
  464. $redirect = bbp_get_favorites_permalink( $user_id ); 
  465. } elseif ( bbp_is_single_user() ) { 
  466. $redirect = bbp_get_user_profile_url(); 
  467. } elseif ( is_singular( bbp_get_topic_post_type() ) ) { 
  468. $redirect = bbp_get_topic_permalink( $topic_id ); 
  469. } elseif ( is_single() || is_page() ) { 
  470. $redirect = get_permalink(); 
  471. } else { 
  472. $redirect = get_permalink( $topic_id ); 
  473.  
  474. wp_safe_redirect( $redirect ); 
  475.  
  476. // For good measure 
  477. exit(); 
  478.  
  479. // Fail! Handle errors 
  480. } elseif ( true === $is_favorite && 'bbp_favorite_remove' === $action ) { 
  481. bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) ); 
  482. } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) { 
  483. bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) ); 
  484.  
  485. /** Subscriptions *************************************************************/ 
  486.  
  487. /** 
  488. * Get the users who have subscribed to the forum 
  489. * 
  490. * @since bbPress (r5156) 
  491. * 
  492. * @param int $forum_id Optional. forum id 
  493. * @uses wpdb::get_col() To execute our query and get the column back 
  494. * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers 
  495. * @return array|bool Results if the forum has any subscribers, otherwise false 
  496. */ 
  497. function bbp_get_forum_subscribers( $forum_id = 0 ) { 
  498. $forum_id = bbp_get_forum_id( $forum_id ); 
  499. if ( empty( $forum_id ) ) 
  500. return; 
  501.  
  502. global $wpdb; 
  503.  
  504. $key = $wpdb->prefix . '_bbp_forum_subscriptions'; 
  505. $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 
  506. if ( false === $users ) { 
  507. $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" ); 
  508. wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' ); 
  509.  
  510. return apply_filters( 'bbp_get_forum_subscribers', $users ); 
  511.  
  512. /** 
  513. * Get the users who have subscribed to the topic 
  514. * 
  515. * @since bbPress (r2668) 
  516. * 
  517. * @param int $topic_id Optional. Topic id 
  518. * @uses wpdb::get_col() To execute our query and get the column back 
  519. * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers 
  520. * @return array|bool Results if the topic has any subscribers, otherwise false 
  521. */ 
  522. function bbp_get_topic_subscribers( $topic_id = 0 ) { 
  523. $topic_id = bbp_get_topic_id( $topic_id ); 
  524. if ( empty( $topic_id ) ) 
  525. return; 
  526.  
  527. global $wpdb; 
  528.  
  529. $key = $wpdb->prefix . '_bbp_subscriptions'; 
  530. $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 
  531. if ( false === $users ) { 
  532. $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ); 
  533. wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress_users' ); 
  534.  
  535. return apply_filters( 'bbp_get_topic_subscribers', $users ); 
  536.  
  537. /** 
  538. * Get a user's subscribed topics 
  539. * 
  540. * @since bbPress (r2668) 
  541. * 
  542. * @deprecated since bbPress (r5156) 
  543. * 
  544. * @param int $user_id Optional. User id 
  545. * @uses bbp_get_user_topic_subscriptions() To get the user's subscriptions 
  546. * @return array|bool Results if user has subscriptions, otherwise false 
  547. */ 
  548. function bbp_get_user_subscriptions( $user_id = 0 ) { 
  549. _deprecated_function( __FUNCTION__, 2.5, 'bbp_get_user_topic_subscriptions()' ); 
  550. $query = bbp_get_user_topic_subscriptions( $user_id ); 
  551. return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id ); 
  552.  
  553. /** 
  554. * Get a user's subscribed topics 
  555. * 
  556. * @since bbPress (r2668) 
  557. * 
  558. * @param int $user_id Optional. User id 
  559. * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 
  560. * @uses bbp_has_topics() To get the topics 
  561. * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query 
  562. * and user id 
  563. * @return array|bool Results if user has subscriptions, otherwise false 
  564. */ 
  565. function bbp_get_user_topic_subscriptions( $user_id = 0 ) { 
  566.  
  567. // Default to the displayed user 
  568. $user_id = bbp_get_user_id( $user_id ); 
  569. if ( empty( $user_id ) ) { 
  570. return false; 
  571.  
  572. // If user has subscriptions, load them 
  573. $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 
  574. if ( !empty( $subscriptions ) ) { 
  575. $query = bbp_has_topics( array( 'post__in' => $subscriptions ) ); 
  576. } else { 
  577. $query = false; 
  578.  
  579. return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id ); 
  580.  
  581. /** 
  582. * Get a user's subscribed forums 
  583. * 
  584. * @since bbPress (r5156) 
  585. * 
  586. * @param int $user_id Optional. User id 
  587. * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 
  588. * @uses bbp_has_forums() To get the forums 
  589. * @uses apply_filters() Calls 'bbp_get_user_forum_subscriptions' with the forum 
  590. * query and user id 
  591. * @return array|bool Results if user has subscriptions, otherwise false 
  592. */ 
  593. function bbp_get_user_forum_subscriptions( $user_id = 0 ) { 
  594.  
  595. // Default to the displayed user 
  596. $user_id = bbp_get_user_id( $user_id ); 
  597. if ( empty( $user_id ) ) { 
  598. return false; 
  599.  
  600. // If user has subscriptions, load them 
  601. $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 
  602. if ( !empty( $subscriptions ) ) { 
  603. $query = bbp_has_forums( array( 'post__in' => $subscriptions ) ); 
  604. } else { 
  605. $query = false; 
  606.  
  607. return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id ); 
  608.  
  609. /** 
  610. * Get a user's subscribed forum ids 
  611. * 
  612. * @since bbPress (r5156) 
  613. * 
  614. * @param int $user_id Optional. User id 
  615. * @uses bbp_get_user_id() To get the user id 
  616. * @uses get_user_option() To get the user's subscriptions 
  617. * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with 
  618. * the subscriptions and user id 
  619. * @return array|bool Results if user has subscriptions, otherwise false 
  620. */ 
  621. function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) { 
  622. $user_id = bbp_get_user_id( $user_id ); 
  623. if ( empty( $user_id ) ) 
  624. return false; 
  625.  
  626. $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id ); 
  627. $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) ); 
  628.  
  629. return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id ); 
  630.  
  631. /** 
  632. * Get a user's subscribed topics' ids 
  633. * 
  634. * @since bbPress (r2668) 
  635. * 
  636. * @param int $user_id Optional. User id 
  637. * @uses bbp_get_user_id() To get the user id 
  638. * @uses get_user_option() To get the user's subscriptions 
  639. * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with 
  640. * the subscriptions and user id 
  641. * @return array|bool Results if user has subscriptions, otherwise false 
  642. */ 
  643. function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { 
  644. $user_id = bbp_get_user_id( $user_id ); 
  645. if ( empty( $user_id ) ) 
  646. return false; 
  647.  
  648. $subscriptions = get_user_option( '_bbp_subscriptions', $user_id ); 
  649. $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) ); 
  650.  
  651. return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id ); 
  652.  
  653. /** 
  654. * Check if a topic or forum is in user's subscription list or not 
  655. * 
  656. * @since bbPress (r5156) 
  657. * 
  658. * @param int $user_id Optional. User id 
  659. * @param int $forum_id Optional. Topic id 
  660. * @uses get_post() To get the post object 
  661. * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 
  662. * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 
  663. * @uses bbp_get_forum_post_type() To get the forum post type 
  664. * @uses bbp_get_topic_post_type() To get the topic post type 
  665. * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,  
  666. * forum/topic id and subsriptions 
  667. * @return bool True if the forum or topic is in user's subscriptions, otherwise false 
  668. */ 
  669. function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) { 
  670.  
  671. // Assume user is not subscribed 
  672. $retval = false; 
  673.  
  674. // Setup ID's array 
  675. $subscribed_ids = array(); 
  676.  
  677. // User and object ID's are passed 
  678. if ( ! empty( $user_id ) && ! empty( $object_id ) ) { 
  679.  
  680. // Get the post type 
  681. $post_type = get_post_type( $object_id ); 
  682.  
  683. // Post exists, so check the types 
  684. if ( ! empty( $post_type ) ) { 
  685.  
  686. switch( $post_type ) { 
  687.  
  688. // Forum 
  689. case bbp_get_forum_post_type() : 
  690. $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id ); 
  691. $retval = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids ); 
  692. break; 
  693.  
  694. // Topic (default) 
  695. case bbp_get_topic_post_type() : 
  696. default : 
  697. $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id ); 
  698. $retval = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids ); 
  699. break; 
  700.  
  701. return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids ); 
  702.  
  703. /** 
  704. * Check if a forum is in user's subscription list or not 
  705. * 
  706. * @since bbPress (r5156) 
  707. * 
  708. * @param int $user_id Optional. User id 
  709. * @param int $forum_id Optional. Topic id 
  710. * @param array $subscribed_ids Optional. Array of forum ID's to check 
  711. * @uses bbp_get_user_id() To get the user id 
  712. * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 
  713. * @uses bbp_get_forum() To get the forum 
  714. * @uses bbp_get_forum_id() To get the forum id 
  715. * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,  
  716. * forum id and subsriptions 
  717. * @return bool True if the forum is in user's subscriptions, otherwise false 
  718. */ 
  719. function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0, $subscribed_ids = array() ) { 
  720.  
  721. // Assume user is not subscribed 
  722. $retval = false; 
  723.  
  724. // Validate user 
  725. $user_id = bbp_get_user_id( $user_id, true, true ); 
  726. if ( ! empty( $user_id ) ) { 
  727.  
  728. // Get subscription ID's if none passed 
  729. if ( empty( $subscribed_ids ) ) { 
  730. $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id ); 
  731.  
  732. // User has forum subscriptions 
  733. if ( ! empty( $subscribed_ids ) ) { 
  734.  
  735. // Checking a specific forum id 
  736. if ( ! empty( $forum_id ) ) { 
  737. $forum = bbp_get_forum( $forum_id ); 
  738. $forum_id = ! empty( $forum ) ? $forum->ID : 0; 
  739.  
  740. // Using the global forum id 
  741. } elseif ( bbp_get_forum_id() ) { 
  742. $forum_id = bbp_get_forum_id(); 
  743.  
  744. // Use the current post id 
  745. } elseif ( ! bbp_get_forum_id() ) { 
  746. $forum_id = get_the_ID(); 
  747.  
  748. // Is forum_id in the user's favorites 
  749. if ( ! empty( $forum_id ) ) { 
  750. $retval = in_array( $forum_id, $subscribed_ids ); 
  751.  
  752. return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscribed_ids ); 
  753.  
  754. /** 
  755. * Check if a topic is in user's subscription list or not 
  756. * 
  757. * @since bbPress (r5156) 
  758. * 
  759. * @param int $user_id Optional. User id 
  760. * @param int $topic_id Optional. Topic id 
  761. * @param array $subscribed_ids Optional. Array of topic ID's to check 
  762. * @uses bbp_get_user_id() To get the user id 
  763. * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 
  764. * @uses bbp_get_topic() To get the topic 
  765. * @uses bbp_get_topic_id() To get the topic id 
  766. * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,  
  767. * topic id and subsriptions 
  768. * @return bool True if the topic is in user's subscriptions, otherwise false 
  769. */ 
  770. function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0, $subscribed_ids = array() ) { 
  771.  
  772. // Assume user is not subscribed 
  773. $retval = false; 
  774.  
  775. // Validate user 
  776. $user_id = bbp_get_user_id( $user_id, true, true ); 
  777. if ( !empty( $user_id ) ) { 
  778.  
  779. // Get subscription ID's if none passed 
  780. if ( empty( $subscribed_ids ) ) { 
  781. $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id ); 
  782.  
  783. // User has topic subscriptions 
  784. if ( ! empty( $subscribed_ids ) ) { 
  785.  
  786. // Checking a specific topic id 
  787. if ( ! empty( $topic_id ) ) { 
  788. $topic = bbp_get_topic( $topic_id ); 
  789. $topic_id = ! empty( $topic ) ? $topic->ID : 0; 
  790.  
  791. // Using the global topic id 
  792. } elseif ( bbp_get_topic_id() ) { 
  793. $topic_id = bbp_get_topic_id(); 
  794.  
  795. // Use the current post id 
  796. } elseif ( !bbp_get_topic_id() ) { 
  797. $topic_id = get_the_ID(); 
  798.  
  799. // Is topic_id in the user's favorites 
  800. if ( ! empty( $topic_id ) ) { 
  801. $retval = in_array( $topic_id, $subscribed_ids ); 
  802.  
  803. return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscribed_ids ); 
  804.  
  805. /** 
  806. * Add a topic to user's subscriptions 
  807. * 
  808. * @since bbPress (r5156) 
  809. * 
  810. * @param int $user_id Optional. User id 
  811. * @param int $topic_id Optional. Topic id 
  812. * @uses get_post() To get the post object 
  813. * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 
  814. * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 
  815. * @uses bbp_get_forum_post_type() To get the forum post type 
  816. * @uses bbp_get_topic_post_type() To get the topic post type 
  817. * @uses update_user_option() To update the user's subscriptions 
  818. * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 
  819. * @return bool Always true 
  820. */ 
  821. function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) { 
  822. if ( empty( $user_id ) || empty( $object_id ) ) { 
  823. return false; 
  824.  
  825. // Get the post type 
  826. $post_type = get_post_type( $object_id ); 
  827. if ( empty( $post_type ) ) { 
  828. return false; 
  829.  
  830. switch( $post_type ) { 
  831.  
  832. // Forum 
  833. case bbp_get_forum_post_type() : 
  834. bbp_add_user_forum_subscription( $user_id, $object_id ); 
  835. break; 
  836.  
  837. // Topic 
  838. case bbp_get_topic_post_type() : 
  839. default : 
  840. bbp_add_user_topic_subscription( $user_id, $object_id ); 
  841. break; 
  842.  
  843. do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type ); 
  844.  
  845. return true; 
  846.  
  847. /** 
  848. * Add a forum to user's subscriptions 
  849. * 
  850. * @since bbPress (r5156) 
  851. * 
  852. * @param int $user_id Optional. User id 
  853. * @param int $forum_id Optional. forum id 
  854. * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 
  855. * @uses bbp_get_forum() To get the forum 
  856. * @uses update_user_option() To update the user's subscriptions 
  857. * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id 
  858. * @return bool Always true 
  859. */ 
  860. function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) { 
  861. if ( empty( $user_id ) || empty( $forum_id ) ) { 
  862. return false; 
  863.  
  864. $forum = bbp_get_forum( $forum_id ); 
  865. if ( empty( $forum ) ) { 
  866. return false; 
  867.  
  868. $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 
  869. if ( !in_array( $forum_id, $subscriptions ) ) { 
  870. $subscriptions[] = $forum_id; 
  871. $subscriptions = implode( ', ', wp_parse_id_list( array_filter( $subscriptions ) ) ); 
  872. update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 
  873.  
  874. wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 
  875.  
  876. do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id ); 
  877.  
  878. return true; 
  879.  
  880. /** 
  881. * Add a topic to user's subscriptions 
  882. * 
  883. * @since bbPress (r2668) 
  884. * 
  885. * @param int $user_id Optional. User id 
  886. * @param int $topic_id Optional. Topic id 
  887. * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 
  888. * @uses bbp_get_topic() To get the topic 
  889. * @uses update_user_option() To update the user's subscriptions 
  890. * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 
  891. * @return bool Always true 
  892. */ 
  893. function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) { 
  894. if ( empty( $user_id ) || empty( $topic_id ) ) { 
  895. return false; 
  896.  
  897. $topic = bbp_get_topic( $topic_id ); 
  898. if ( empty( $topic ) ) { 
  899. return false; 
  900.  
  901. $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); 
  902. if ( !in_array( $topic_id, $subscriptions ) ) { 
  903. $subscriptions[] = $topic_id; 
  904. $subscriptions = implode( ', ', wp_parse_id_list( array_filter( $subscriptions ) ) ); 
  905. update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); 
  906.  
  907. wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 
  908.  
  909. do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id ); 
  910.  
  911. return true; 
  912.  
  913. /** 
  914. * Remove a topic from user's subscriptions 
  915. * 
  916. * @since bbPress (r2668) 
  917. * 
  918. * @param int $user_id Optional. User id 
  919. * @param int $topic_id Optional. Topic id 
  920. * @uses get_post() To get the post object 
  921. * @uses bbp_get_forum_post_type() To get the forum post type 
  922. * @uses bbp_get_topic_post_type() To get the topic post type 
  923. * @uses bbp_remove_user_forum_subscription() To remove the user's subscription 
  924. * @uses bbp_remove_user_topic_subscription() To remove the user's subscription 
  925. * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 
  926. * topic id 
  927. * @return bool True if the topic was removed from user's subscriptions,  
  928. * otherwise false 
  929. */ 
  930. function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) { 
  931. if ( empty( $user_id ) || empty( $object_id ) ) { 
  932. return false; 
  933.  
  934. $post_type = get_post_type( $object_id ); 
  935. if ( empty( $post_type ) ) { 
  936. return false; 
  937.  
  938. switch( $post_type ) { 
  939.  
  940. // Forum 
  941. case bbp_get_forum_post_type() : 
  942. bbp_remove_user_forum_subscription( $user_id, $object_id ); 
  943. break; 
  944.  
  945. // Topic 
  946. case bbp_get_topic_post_type() : 
  947. default : 
  948. bbp_remove_user_topic_subscription( $user_id, $object_id ); 
  949. break; 
  950.  
  951. do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type ); 
  952.  
  953. return true; 
  954.  
  955. /** 
  956. * Remove a forum from user's subscriptions 
  957. * 
  958. * @since bbPress (r5156) 
  959. * 
  960. * @param int $user_id Optional. User id 
  961. * @param int $forum_id Optional. forum id 
  962. * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 
  963. * @uses update_user_option() To update the user's subscriptions 
  964. * @uses delete_user_option() To delete the user's subscriptions meta 
  965. * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 
  966. * forum id 
  967. * @return bool True if the forum was removed from user's subscriptions,  
  968. * otherwise false 
  969. */ 
  970. function bbp_remove_user_forum_subscription( $user_id, $forum_id ) { 
  971. if ( empty( $user_id ) || empty( $forum_id ) ) { 
  972. return false; 
  973.  
  974. $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 
  975. if ( empty( $subscriptions ) ) { 
  976. return false; 
  977.  
  978. $pos = array_search( $forum_id, $subscriptions ); 
  979. if ( false === $pos ) { 
  980. return false; 
  981.  
  982. array_splice( $subscriptions, $pos, 1 ); 
  983. $subscriptions = array_filter( $subscriptions ); 
  984.  
  985. if ( !empty( $subscriptions ) ) { 
  986. $subscriptions = implode( ', ', wp_parse_id_list( $subscriptions ) ); 
  987. update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 
  988. } else { 
  989. delete_user_option( $user_id, '_bbp_forum_subscriptions' ); 
  990.  
  991. wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 
  992.  
  993. do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id ); 
  994.  
  995. return true; 
  996.  
  997. /** 
  998. * Remove a topic from user's subscriptions 
  999. * 
  1000. * @since bbPress (r5156) 
  1001. * 
  1002. * @param int $user_id Optional. User id 
  1003. * @param int $topic_id Optional. Topic id 
  1004. * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 
  1005. * @uses update_user_option() To update the user's subscriptions 
  1006. * @uses delete_user_option() To delete the user's subscriptions meta 
  1007. * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and 
  1008. * topic id 
  1009. * @return bool True if the topic was removed from user's subscriptions,  
  1010. * otherwise false 
  1011. */ 
  1012. function bbp_remove_user_topic_subscription( $user_id, $topic_id ) { 
  1013. if ( empty( $user_id ) || empty( $topic_id ) ) { 
  1014. return false; 
  1015.  
  1016. $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); 
  1017. if ( empty( $subscriptions ) ) { 
  1018. return false; 
  1019.  
  1020. $pos = array_search( $topic_id, $subscriptions ); 
  1021. if ( false === $pos ) { 
  1022. return false; 
  1023.  
  1024. array_splice( $subscriptions, $pos, 1 ); 
  1025. $subscriptions = array_filter( $subscriptions ); 
  1026.  
  1027. if ( !empty( $subscriptions ) ) { 
  1028. $subscriptions = implode( ', ', wp_parse_id_list( $subscriptions ) ); 
  1029. update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); 
  1030. } else { 
  1031. delete_user_option( $user_id, '_bbp_subscriptions' ); 
  1032.  
  1033. wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 
  1034.  
  1035. do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id ); 
  1036.  
  1037. return true; 
  1038.  
  1039. /** 
  1040. * Handles the front end subscribing and unsubscribing forums 
  1041. * 
  1042. * @since bbPress (r5156) 
  1043. * 
  1044. * @param string $action The requested action to compare this function to 
  1045. * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 
  1046. * @uses bbp_get_user_id() To get the user id 
  1047. * @uses bbp_verify_nonce_request() To verify the nonce and check the request 
  1048. * @uses current_user_can() To check if the current user can edit the user 
  1049. * @uses bbPress:errors:add() To log the error messages 
  1050. * @uses bbp_is_user_subscribed() To check if the forum is in user's 
  1051. * subscriptions 
  1052. * @uses bbp_remove_user_subscription() To remove the user subscription 
  1053. * @uses bbp_add_user_subscription() To add the user subscription 
  1054. * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,  
  1055. * forum id and action 
  1056. * @uses bbp_is_subscription() To check if it's the subscription page 
  1057. * @uses bbp_get_forum_permalink() To get the forum permalink 
  1058. * @uses wp_safe_redirect() To redirect to the url 
  1059. */ 
  1060. function bbp_forum_subscriptions_handler( $action = '' ) { 
  1061.  
  1062. if ( ! bbp_is_subscriptions_active() ) { 
  1063. return false; 
  1064.  
  1065. // Bail if no forum ID is passed 
  1066. if ( empty( $_GET['forum_id'] ) ) { 
  1067. return; 
  1068.  
  1069. // Setup possible get actions 
  1070. $possible_actions = array( 
  1071. 'bbp_subscribe',  
  1072. 'bbp_unsubscribe',  
  1073. ); 
  1074.  
  1075. // Bail if actions aren't meant for this function 
  1076. if ( ! in_array( $action, $possible_actions ) ) { 
  1077. return; 
  1078.  
  1079. // Get required data 
  1080. $user_id = bbp_get_user_id( 0, true, true ); 
  1081. $forum_id = intval( $_GET['forum_id'] ); 
  1082.  
  1083. // Check for empty forum 
  1084. if ( empty( $forum_id ) ) { 
  1085. bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) ); 
  1086.  
  1087. // Check nonce 
  1088. } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) { 
  1089. bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  1090.  
  1091. // Check current user's ability to edit the user 
  1092. } elseif ( !current_user_can( 'edit_user', $user_id ) ) { 
  1093. bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); 
  1094.  
  1095. // Bail if we have errors 
  1096. if ( bbp_has_errors() ) { 
  1097. return; 
  1098.  
  1099. /** No errors *************************************************************/ 
  1100.  
  1101. $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id ); 
  1102. $success = false; 
  1103.  
  1104. if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 
  1105. $success = bbp_remove_user_subscription( $user_id, $forum_id ); 
  1106. } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 
  1107. $success = bbp_add_user_subscription( $user_id, $forum_id ); 
  1108.  
  1109. // Do additional subscriptions actions 
  1110. do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action ); 
  1111.  
  1112. // Success! 
  1113. if ( true === $success ) { 
  1114.  
  1115. // Redirect back from whence we came 
  1116. if ( bbp_is_subscriptions() ) { 
  1117. $redirect = bbp_get_subscriptions_permalink( $user_id ); 
  1118. } elseif ( bbp_is_single_user() ) { 
  1119. $redirect = bbp_get_user_profile_url(); 
  1120. } elseif ( is_singular( bbp_get_forum_post_type() ) ) { 
  1121. $redirect = bbp_get_forum_permalink( $forum_id ); 
  1122. } elseif ( is_single() || is_page() ) { 
  1123. $redirect = get_permalink(); 
  1124. } else { 
  1125. $redirect = get_permalink( $forum_id ); 
  1126.  
  1127. wp_safe_redirect( $redirect ); 
  1128.  
  1129. // For good measure 
  1130. exit(); 
  1131.  
  1132. // Fail! Handle errors 
  1133. } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 
  1134. bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) ); 
  1135. } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 
  1136. bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) ); 
  1137.  
  1138. /** 
  1139. * Handles the front end subscribing and unsubscribing topics 
  1140. * 
  1141. * @param string $action The requested action to compare this function to 
  1142. * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 
  1143. * @uses bbp_get_user_id() To get the user id 
  1144. * @uses bbp_verify_nonce_request() To verify the nonce and check the request 
  1145. * @uses current_user_can() To check if the current user can edit the user 
  1146. * @uses bbPress:errors:add() To log the error messages 
  1147. * @uses bbp_is_user_subscribed() To check if the topic is in user's 
  1148. * subscriptions 
  1149. * @uses bbp_remove_user_subscription() To remove the user subscription 
  1150. * @uses bbp_add_user_subscription() To add the user subscription 
  1151. * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,  
  1152. * topic id and action 
  1153. * @uses bbp_is_subscription() To check if it's the subscription page 
  1154. * @uses bbp_get_topic_permalink() To get the topic permalink 
  1155. * @uses wp_safe_redirect() To redirect to the url 
  1156. */ 
  1157. function bbp_subscriptions_handler( $action = '' ) { 
  1158.  
  1159. if ( !bbp_is_subscriptions_active() ) { 
  1160. return false; 
  1161.  
  1162. // Bail if no topic ID is passed 
  1163. if ( empty( $_GET['topic_id'] ) ) { 
  1164. return; 
  1165.  
  1166. // Setup possible get actions 
  1167. $possible_actions = array( 
  1168. 'bbp_subscribe',  
  1169. 'bbp_unsubscribe',  
  1170. ); 
  1171.  
  1172. // Bail if actions aren't meant for this function 
  1173. if ( !in_array( $action, $possible_actions ) ) { 
  1174. return; 
  1175.  
  1176. // Get required data 
  1177. $user_id = bbp_get_user_id( 0, true, true ); 
  1178. $topic_id = intval( $_GET['topic_id'] ); 
  1179.  
  1180. // Check for empty topic 
  1181. if ( empty( $topic_id ) ) { 
  1182. bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) ); 
  1183.  
  1184. // Check nonce 
  1185. } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) { 
  1186. bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  1187.  
  1188. // Check current user's ability to edit the user 
  1189. } elseif ( !current_user_can( 'edit_user', $user_id ) ) { 
  1190. bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); 
  1191.  
  1192. // Bail if we have errors 
  1193. if ( bbp_has_errors() ) { 
  1194. return; 
  1195.  
  1196. /** No errors *************************************************************/ 
  1197.  
  1198. $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id ); 
  1199. $success = false; 
  1200.  
  1201. if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 
  1202. $success = bbp_remove_user_subscription( $user_id, $topic_id ); 
  1203. } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 
  1204. $success = bbp_add_user_subscription( $user_id, $topic_id ); 
  1205.  
  1206. // Do additional subscriptions actions 
  1207. do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action ); 
  1208.  
  1209. // Success! 
  1210. if ( true === $success ) { 
  1211.  
  1212. // Redirect back from whence we came 
  1213. if ( bbp_is_subscriptions() ) { 
  1214. $redirect = bbp_get_subscriptions_permalink( $user_id ); 
  1215. } elseif ( bbp_is_single_user() ) { 
  1216. $redirect = bbp_get_user_profile_url(); 
  1217. } elseif ( is_singular( bbp_get_topic_post_type() ) ) { 
  1218. $redirect = bbp_get_topic_permalink( $topic_id ); 
  1219. } elseif ( is_single() || is_page() ) { 
  1220. $redirect = get_permalink(); 
  1221. } else { 
  1222. $redirect = get_permalink( $topic_id ); 
  1223.  
  1224. wp_safe_redirect( $redirect ); 
  1225.  
  1226. // For good measure 
  1227. exit(); 
  1228.  
  1229. // Fail! Handle errors 
  1230. } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 
  1231. bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) ); 
  1232. } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 
  1233. bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) ); 
  1234.  
  1235. /** Edit **********************************************************************/ 
  1236.  
  1237. /** 
  1238. * Handles the front end user editing 
  1239. * 
  1240. * @param string $action The requested action to compare this function to 
  1241. * @uses is_multisite() To check if it's a multisite 
  1242. * @uses bbp_is_user_home() To check if the user is at home (the display page 
  1243. * is the one of the logged in user) 
  1244. * @uses get_option() To get the displayed user's new email id option 
  1245. * @uses wpdb::prepare() To sanitize our sql query 
  1246. * @uses wpdb::get_var() To execute our query and get back the variable 
  1247. * @uses wpdb::query() To execute our query 
  1248. * @uses wp_update_user() To update the user 
  1249. * @uses delete_option() To delete the displayed user's email id option 
  1250. * @uses bbp_get_user_profile_edit_url() To get the edit profile url 
  1251. * @uses wp_safe_redirect() To redirect to the url 
  1252. * @uses bbp_verify_nonce_request() To verify the nonce and check the request 
  1253. * @uses current_user_can() To check if the current user can edit the user 
  1254. * @uses do_action() Calls 'personal_options_update' or 
  1255. * 'edit_user_options_update' (based on if it's the user home) 
  1256. * with the displayed user id 
  1257. * @uses edit_user() To edit the user based on the post data 
  1258. * @uses get_userdata() To get the user data 
  1259. * @uses is_email() To check if the string is an email id or not 
  1260. * @uses wpdb::get_blog_prefix() To get the blog prefix 
  1261. * @uses is_network_admin() To check if the user is the network admin 
  1262. * @uses revoke_super_admin() To revoke super admin priviledges 
  1263. * @uses grant_super_admin() To grant super admin priviledges 
  1264. * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} 
  1265. */ 
  1266. function bbp_edit_user_handler( $action = '' ) { 
  1267.  
  1268. // Bail if action is not 'bbp-update-user' 
  1269. if ( 'bbp-update-user' !== $action ) 
  1270. return; 
  1271.  
  1272. // Get the displayed user ID 
  1273. $user_id = bbp_get_displayed_user_id(); 
  1274.  
  1275. // Execute confirmed email change. See send_confirmation_on_profile_email(). 
  1276. if ( is_multisite() && bbp_is_user_home_edit() && isset( $_GET['newuseremail'] ) ) { 
  1277.  
  1278. $new_email = get_option( $user_id . '_new_email' ); 
  1279.  
  1280. if ( hash_equals( $new_email['hash'], $_GET['newuseremail'] ) ) { 
  1281. $user = new WP_User(); 
  1282. $user->ID = $user_id; 
  1283. $user->user_email = esc_html( trim( $new_email['newemail'] ) ); 
  1284.  
  1285. global $wpdb; 
  1286.  
  1287. if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ) ) { 
  1288. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login', 'raw' ) ) ); 
  1289.  
  1290. wp_update_user( get_object_vars( $user ) ); 
  1291. delete_option( $user_id . '_new_email' ); 
  1292.  
  1293. wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) ); 
  1294. exit(); 
  1295.  
  1296. // Delete new email address from user options 
  1297. } elseif ( is_multisite() && bbp_is_user_home_edit() && !empty( $_GET['dismiss'] ) && ( $user_id . '_new_email' === $_GET['dismiss'] ) ) { 
  1298. delete_option( $user_id . '_new_email' ); 
  1299. wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) ); 
  1300. exit(); 
  1301.  
  1302. // Nonce check 
  1303. if ( ! bbp_verify_nonce_request( 'update-user_' . $user_id ) ) { 
  1304. bbp_add_error( 'bbp_update_user_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  1305. return; 
  1306.  
  1307. // Cap check 
  1308. if ( ! current_user_can( 'edit_user', $user_id ) ) { 
  1309. bbp_add_error( 'bbp_update_user_capability', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 
  1310. return; 
  1311.  
  1312. // Do action based on who's profile you're editing 
  1313. $edit_action = bbp_is_user_home_edit() ? 'personal_options_update' : 'edit_user_profile_update'; 
  1314. do_action( $edit_action, $user_id ); 
  1315.  
  1316. // Prevent edit_user() from wiping out the user's Toolbar on front setting 
  1317. if ( !isset( $_POST['admin_bar_front'] ) && _get_admin_bar_pref( 'front', $user_id ) ) { 
  1318. $_POST['admin_bar_front'] = 1; 
  1319.  
  1320. // Handle user edit 
  1321. $edit_user = edit_user( $user_id ); 
  1322.  
  1323. // Error(s) editng the user, so copy them into the global 
  1324. if ( is_wp_error( $edit_user ) ) { 
  1325. bbpress()->errors = $edit_user; 
  1326.  
  1327. // Successful edit to redirect 
  1328. } elseif ( is_integer( $edit_user ) ) { 
  1329.  
  1330. // Maybe update super admin ability 
  1331. if ( is_multisite() && ! bbp_is_user_home_edit() && current_user_can( 'manage_network_options' ) && is_super_admin() ) { 
  1332. empty( $_POST['super_admin'] ) ? revoke_super_admin( $edit_user ) : grant_super_admin( $edit_user ); 
  1333.  
  1334. $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $edit_user ) ); 
  1335.  
  1336. wp_safe_redirect( $redirect ); 
  1337. exit; 
  1338.  
  1339. /** 
  1340. * Conditionally hook the core WordPress output actions to the end of the 
  1341. * default user's edit profile template. 
  1342. * 
  1343. * This allows clever plugin authors to conditionally unhook the WordPress core 
  1344. * output actions if they don't want any unexpected junk to appear there, and 
  1345. * also avoids needing to pollute the templates with additional logic and actions. 
  1346. * 
  1347. * @since bbPress (r4273) 
  1348. * 
  1349. * @uses bbp_is_user_home_edit() To switch the action fired 
  1350. * @uses get_userdata() To get the current user's data 
  1351. * @uses bbp_get_displayed_user_id() To get the currently displayed user ID 
  1352. */ 
  1353. function bbp_user_edit_after() { 
  1354. $action = bbp_is_user_home_edit() ? 'show_user_profile' : 'edit_user_profile'; 
  1355.  
  1356. do_action( $action, get_userdata( bbp_get_displayed_user_id() ) ); 
  1357.  
  1358. /** User Queries **************************************************************/ 
  1359.  
  1360. /** 
  1361. * Get the topics that a user created 
  1362. * 
  1363. * @since bbPress (r2660) 
  1364. * 
  1365. * @param int $user_id Optional. User id 
  1366. * @uses bbp_get_user_id() To get the topic id 
  1367. * @uses bbp_has_topics() To get the topics created by the user 
  1368. * @return array|bool Results if the user has created topics, otherwise false 
  1369. */ 
  1370. function bbp_get_user_topics_started( $user_id = 0 ) { 
  1371.  
  1372. // Validate user 
  1373. $user_id = bbp_get_user_id( $user_id ); 
  1374. if ( empty( $user_id ) ) 
  1375. return false; 
  1376.  
  1377. // Try to get the topics 
  1378. $query = bbp_has_topics( array( 
  1379. 'author' => $user_id 
  1380. ) ); 
  1381.  
  1382. return apply_filters( 'bbp_get_user_topics_started', $query, $user_id ); 
  1383.  
  1384. /** 
  1385. * Get the replies that a user created 
  1386. * 
  1387. * @since bbPress (r4225) 
  1388. * 
  1389. * @param int $user_id Optional. User id 
  1390. * @uses bbp_get_user_id() To get the topic id 
  1391. * @uses bbp_has_replies() To get the topics created by the user 
  1392. * @return array|bool Results if the user has created topics, otherwise false 
  1393. */ 
  1394. function bbp_get_user_replies_created( $user_id = 0 ) { 
  1395.  
  1396. // Validate user 
  1397. $user_id = bbp_get_user_id( $user_id ); 
  1398. if ( empty( $user_id ) ) 
  1399. return false; 
  1400.  
  1401. // Try to get the topics 
  1402. $query = bbp_has_replies( array( 
  1403. 'post_type' => bbp_get_reply_post_type(),  
  1404. 'order' => 'DESC',  
  1405. 'author' => $user_id 
  1406. ) ); 
  1407.  
  1408. return apply_filters( 'bbp_get_user_replies_created', $query, $user_id ); 
  1409.  
  1410. /** 
  1411. * Get the total number of users on the forums 
  1412. * 
  1413. * @since bbPress (r2769) 
  1414. * @uses count_users() To execute our query and get the var back 
  1415. * @uses apply_filters() Calls 'bbp_get_total_users' with number of users 
  1416. * @return int Total number of users 
  1417. */ 
  1418. function bbp_get_total_users() { 
  1419. $user_count = count_users(); 
  1420. return apply_filters( 'bbp_get_total_users', (int) $user_count['total_users'] ); 
  1421.  
  1422. /** Premissions ***************************************************************/ 
  1423.  
  1424. /** 
  1425. * Redirect if unathorized user is attempting to edit another user 
  1426. * 
  1427. * This is hooked to 'bbp_template_redirect' and controls the conditions under 
  1428. * which a user can edit another user (or themselves.) If these conditions are 
  1429. * met. We assume a user cannot perform this task, and look for ways they can 
  1430. * earn the ability to access this template. 
  1431. * 
  1432. * @since bbPress (r3605) 
  1433. * 
  1434. * @uses bbp_is_topic_edit() 
  1435. * @uses current_user_can() 
  1436. * @uses bbp_get_topic_id() 
  1437. * @uses wp_safe_redirect() 
  1438. * @uses bbp_get_topic_permalink() 
  1439. */ 
  1440. function bbp_check_user_edit() { 
  1441.  
  1442. // Bail if not editing a topic 
  1443. if ( ! bbp_is_single_user_edit() ) 
  1444. return; 
  1445.  
  1446. // Default to false 
  1447. $redirect = true; 
  1448.  
  1449. // Allow user to edit their own profile 
  1450. if ( bbp_is_user_home_edit() ) { 
  1451. $redirect = false; 
  1452.  
  1453. // Allow if current user can edit the displayed user 
  1454. } elseif ( current_user_can( 'edit_user', bbp_get_displayed_user_id() ) ) { 
  1455. $redirect = false; 
  1456.  
  1457. // Allow if user can manage network users, or edit-any is enabled 
  1458. } elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', false ) ) { 
  1459. $redirect = false; 
  1460.  
  1461. // Maybe redirect back to profile page 
  1462. if ( true === $redirect ) { 
  1463. wp_safe_redirect( bbp_get_user_profile_url( bbp_get_displayed_user_id() ) ); 
  1464. exit(); 
  1465.  
  1466. /** 
  1467. * Check if a user is blocked, or cannot spectate the forums. 
  1468. * 
  1469. * @since bbPress (r2996) 
  1470. * 
  1471. * @uses is_user_logged_in() To check if user is logged in 
  1472. * @uses bbp_is_user_keymaster() To check if user is a keymaster 
  1473. * @uses current_user_can() To check if the current user can spectate 
  1474. * @uses is_bbpress() To check if in a bbPress section of the site 
  1475. * @uses bbp_set_404() To set a 404 status 
  1476. */ 
  1477. function bbp_forum_enforce_blocked() { 
  1478.  
  1479. // Bail if not logged in or keymaster 
  1480. if ( ! is_user_logged_in() || bbp_is_user_keymaster() ) { 
  1481. return; 
  1482.  
  1483. // Set 404 if in bbPress and user cannot spectate 
  1484. if ( is_bbpress() && ! current_user_can( 'spectate' ) ) { 
  1485. bbp_set_404(); 
  1486.  
  1487. /** Sanitization **************************************************************/ 
  1488.  
  1489. /** 
  1490. * Sanitize displayed user data, when viewing and editing any user. 
  1491. * 
  1492. * This somewhat monolithic function handles the escaping and sanitization of 
  1493. * user data for a bbPress profile. There are two reasons this all happers here: 
  1494. * 
  1495. * 1. bbPress took a similar approach to WordPress, and funnels all user profile 
  1496. * data through a central helper. This eventually calls sanitize_user_field() 
  1497. * which applies a few context based filters, which some third party plugins 
  1498. * might be relying on bbPress to play nicely with. 
  1499. * 
  1500. * 2. Early versions of bbPress 2.x templates did not escape this data meaning 
  1501. * a backwards compatible approach like this one was necessary to protect 
  1502. * existing installations that may have custom template parts. 
  1503. * 
  1504. * @since bbPress (r5368) 
  1505. * 
  1506. * @param string $value 
  1507. * @param string $field 
  1508. * @param string $context 
  1509. * @return string 
  1510. */ 
  1511. function bbp_sanitize_displayed_user_field( $value = '', $field = '', $context = 'display' ) { 
  1512.  
  1513. // Bail if not editing or displaying (maybe we'll do more here later) 
  1514. if ( ! in_array( $context, array( 'edit', 'display' ) ) ) { 
  1515. return $value; 
  1516.  
  1517. // By default, no filter set (consider making this an array later) 
  1518. $filter = false; 
  1519.  
  1520. // Big switch statement to decide which user field we're sanitizing and how 
  1521. switch ( $field ) { 
  1522.  
  1523. // Description is a paragraph 
  1524. case 'description' : 
  1525. $filter = ( 'edit' === $context ) ? '' : 'wp_kses_data'; 
  1526. break; 
  1527.  
  1528. // Email addresses are sanitized with a specific function 
  1529. case 'user_email' : 
  1530. $filter = 'sanitize_email'; 
  1531. break; 
  1532.  
  1533. // Name & login fields 
  1534. case 'user_login' : 
  1535. case 'display_name' : 
  1536. case 'first_name' : 
  1537. case 'last_name' : 
  1538. case 'nick_name' : 
  1539. $filter = ( 'edit' === $context ) ? 'esc_attr' : 'esc_html'; 
  1540. break; 
  1541.  
  1542. // wp-includes/default-filters.php escapes this for us via esc_url() 
  1543. case 'user_url' : 
  1544. break; 
  1545.  
  1546. // Run any applicable filters on the value 
  1547. if ( ! empty( $filter ) ) { 
  1548. $value = call_user_func( $filter, $value ); 
  1549.  
  1550. return $value; 
  1551.  
  1552. /** Converter *****************************************************************/ 
  1553.  
  1554. /** 
  1555. * Convert passwords from previous platfrom encryption to WordPress encryption. 
  1556. * 
  1557. * @since bbPress (r3813) 
  1558. * @global WPDB $wpdb 
  1559. */ 
  1560. function bbp_user_maybe_convert_pass() { 
  1561.  
  1562. // Bail if no username 
  1563. $username = !empty( $_POST['log'] ) ? $_POST['log'] : ''; 
  1564. if ( empty( $username ) ) 
  1565. return; 
  1566.  
  1567. global $wpdb; 
  1568.  
  1569. // Bail if no user password to convert 
  1570. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON user_id = ID WHERE meta_key = '_bbp_class' AND user_login = '%s' LIMIT 1", $username ) ); 
  1571. if ( empty( $row ) || is_wp_error( $row ) ) 
  1572. return; 
  1573.  
  1574. // Setup admin (to include converter) 
  1575. require_once( bbpress()->includes_dir . 'admin/admin.php' ); 
  1576.  
  1577. // Create the admin object 
  1578. bbp_admin(); 
  1579.  
  1580. // Convert password 
  1581. require_once( bbpress()->admin->admin_dir . 'converter.php' ); 
  1582. require_once( bbpress()->admin->admin_dir . 'converters/' . $row->meta_value . '.php' ); 
  1583.  
  1584. // Create the converter 
  1585. $converter = bbp_new_converter( $row->meta_value ); 
  1586.  
  1587. // Try to call the conversion method 
  1588. if ( is_a( $converter, 'BBP_Converter_Base' ) && method_exists( $converter, 'callback_pass' ) ) { 
  1589. $converter->callback_pass( $username, $_POST['pwd'] ); 
.