/bp-forums/bbpress/bb-includes/functions.bb-users.php

  1. <?php 
  2.  
  3. /** Users */ 
  4.  
  5. function bb_block_current_user() { 
  6. global $bbdb; 
  7. if ( $id = bb_get_current_user_info( 'id' ) ) 
  8. bb_update_usermeta( $id, $bbdb->prefix . 'been_blocked', 1 ); // Just for logging. 
  9. bb_logout(); 
  10. bb_die(__("You've been blocked. If you think a mistake has been made, contact this site's administrator.")); 
  11.  
  12. function bb_get_user( $user_id, $args = null ) { 
  13. global $bbdb, $wp_users_object; 
  14.  
  15. // Get user 
  16. $user = $wp_users_object->get_user( $user_id, $args ); 
  17.  
  18. // Return on no user or error object 
  19. if ( !is_object( $user ) || is_wp_error( $user ) ) 
  20. return false; 
  21.  
  22. // Re calculate the user's meta in case we're pulling from a value cached on another site 
  23. if ( $user_vars = get_object_vars( $user ) ) { 
  24. $prefix_length = strlen( $bbdb->prefix ); 
  25. foreach ( $user_vars as $k => $v ) { 
  26. if ( 0 === strpos( $k, $bbdb->prefix ) ) { 
  27. $user->{substr( $k, $prefix_length )} = $v; 
  28.  
  29. return $user; 
  30.  
  31. function bb_cache_users( $ids ) { 
  32. global $wp_users_object; 
  33. $wp_users_object->get_user( $ids ); 
  34.  
  35. function bb_get_user_by_nicename( $nicename ) { 
  36. global $wp_users_object; 
  37. $user = $wp_users_object->get_user( $nicename, array( 'by' => 'nicename' ) ); 
  38. if ( is_wp_error($user) ) 
  39. return false; 
  40. return bb_get_user( $user->ID ); 
  41.  
  42. function bb_delete_user( $user_id, $reassign = 0 ) { 
  43. global $wp_users_object, $bbdb; 
  44.  
  45. if ( !$user = bb_get_user( $user_id ) ) 
  46. return false; 
  47.  
  48. if ( $reassign ) { 
  49. if ( !$new_user = bb_get_user( $reassign ) ) 
  50. return false; 
  51. $bbdb->update( $bbdb->posts, array( 'poster_id' => $new_user->ID ), array( 'poster_id' => $user->ID ) ); 
  52. $bbdb->update( $bbdb->term_relationships, array( 'user_id' => $new_user->ID ), array( 'user_id' => $user->ID ) ); 
  53. $bbdb->update( $bbdb->topics, array( 'topic_poster' => $new_user->ID, 'topic_poster_name' => $new_user->user_login), array( 'topic_poster' => $user->ID ) ); 
  54. $bbdb->update( $bbdb->topics, array( 'topic_last_poster' => $new_user->ID, 'topic_last_poster_name' => $new_user->user_login ), array( 'topic_last_poster' => $user->ID ) ); 
  55. bb_update_topics_replied( $new_user->ID ); 
  56. wp_cache_flush( 'bb_post' ); 
  57. wp_cache_flush( 'bb_thread' ); 
  58. wp_cache_flush( 'bb_topic_tag' ); 
  59. wp_cache_flush( 'bb_topic' ); 
  60.  
  61. do_action( 'bb_delete_user', $user->ID, $reassign ); 
  62.  
  63. $wp_users_object->delete_user( $user->ID ); 
  64.  
  65. return true; 
  66.  
  67. function bb_update_topics_replied( $user_id ) { 
  68. global $bbdb; 
  69.  
  70. $user_id = (int) $user_id; 
  71.  
  72. if ( !$user = bb_get_user( $user_id ) ) 
  73. return false; 
  74.  
  75. $topics_replied = (int) $bbdb->get_var( $bbdb->prepare( "SELECT COUNT(DISTINCT topic_id) FROM $bbdb->posts WHERE post_status = '0' AND poster_id = %d", $user_id ) ); 
  76. return bb_update_usermeta( $user_id, $bbdb->prefix . 'topics_replied', $topics_replied ); 
  77.  
  78. function bb_update_user_status( $user_id, $user_status = 0 ) { 
  79. global $wp_users_object; 
  80. $user = bb_get_user( $user_id ); 
  81. $user_status = (int) $user_status; 
  82. $wp_users_object->update_user( $user->ID, compact( 'user_status' ) ); 
  83.  
  84. function bb_trusted_roles() { 
  85. return apply_filters( 'bb_trusted_roles', array('moderator', 'administrator', 'keymaster') ); 
  86.  
  87. function bb_is_trusted_user( $user ) { // ID, user_login, WP_User, DB user obj 
  88. if ( is_numeric($user) || is_string($user) ) 
  89. $user = new BP_User( $user ); 
  90. elseif ( is_object($user) && is_a($user, 'BP_User') ); // Intentional 
  91. elseif ( is_object($user) && isset($user->ID) && isset($user->user_login) ) // Make sure it's actually a user object 
  92. $user = new BP_User( $user->ID ); 
  93. else 
  94. return; 
  95.  
  96. if ( !$user->ID ) 
  97. return; 
  98.  
  99. return apply_filters( 'bb_is_trusted_user', (bool) array_intersect(bb_trusted_roles(), $user->roles), $user->ID ); 
  100.  
  101. function bb_apply_wp_role_map_to_user( $user, $reload = true ) { 
  102. // Expects only user ids 
  103. if ( !is_numeric( $user ) ) { 
  104. return; 
  105.  
  106. $user = (int) $user; 
  107.  
  108. if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) { 
  109. return; 
  110.  
  111. if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) { 
  112. $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_'; 
  113.  
  114. if ( !$wordpress_roles_map = bb_get_option( 'wp_roles_map' ) ) { 
  115. return; 
  116.  
  117. global $bbdb; 
  118. global $wp_roles; 
  119. global $bb; 
  120.  
  121. static $bbpress_roles_map = false; 
  122.  
  123. if ( !$bbpress_roles_map ) { 
  124. $bbpress_roles_map = array(); 
  125. foreach ( $wp_roles->get_names() as $_bbpress_role => $_bbpress_rolename ) { 
  126. $bbpress_roles_map[$_bbpress_role] = 'subscriber'; 
  127. unset( $_bbpress_role, $_bbpress_rolename ); 
  128. $bbpress_roles_map = array_merge( $bbpress_roles_map, array_flip( $wordpress_roles_map ) ); 
  129. unset( $bbpress_roles_map['inactive'], $bbpress_roles_map['blocked'] ); 
  130.  
  131. static $wordpress_userlevel_map = array( 
  132. 'administrator' => 10,  
  133. 'editor' => 7,  
  134. 'author' => 2,  
  135. 'contributor' => 1,  
  136. 'subscriber' => 0 
  137. ); 
  138.  
  139. $bbpress_roles = bb_get_usermeta( $user, $bbdb->prefix . 'capabilities' ); 
  140. $wordpress_roles = bb_get_usermeta( $user, $wordpress_table_prefix . 'capabilities' ); 
  141.  
  142. if ( !$bbpress_roles && is_array( $wordpress_roles ) ) { 
  143. $bbpress_roles_new = array(); 
  144.  
  145. foreach ( $wordpress_roles as $wordpress_role => $wordpress_role_value ) { 
  146. if ( $wordpress_roles_map[strtolower( $wordpress_role )] && $wordpress_role_value ) { 
  147. $bbpress_roles_new[$wordpress_roles_map[strtolower( $wordpress_role )]] = true; 
  148.  
  149. if ( count( $bbpress_roles_new ) ) { 
  150. bb_update_usermeta( $user, $bbdb->prefix . 'capabilities', $bbpress_roles_new ); 
  151. if ( $reload ) { 
  152. header( 'Location: ' . bb_get_uri( null, null, BB_URI_CONTEXT_HEADER ) ); 
  153. exit; 
  154. } elseif ( !$wordpress_roles && is_array( $bbpress_roles ) ) { 
  155. $wordpress_roles_new = array(); 
  156.  
  157. foreach ( $bbpress_roles as $bbpress_role => $bbpress_role_value ) { 
  158. if ( $bbpress_roles_map[strtolower( $bbpress_role )] && $bbpress_role_value ) { 
  159. $wordpress_roles_new[$bbpress_roles_map[strtolower( $bbpress_role )]] = true; 
  160. $wordpress_userlevels_new[] = $wordpress_userlevel_map[$bbpress_roles_map[strtolower( $bbpress_role )]]; 
  161.  
  162. if ( count( $wordpress_roles_new ) ) { 
  163. bb_update_usermeta( $user, $wordpress_table_prefix . 'capabilities', $wordpress_roles_new ); 
  164. bb_update_usermeta( $user, $wordpress_table_prefix . 'user_level', max( $wordpress_userlevels_new ) ); 
  165.  
  166. function bb_apply_wp_role_map_to_orphans() { 
  167. if ( !$wordpress_table_prefix = bb_get_option('wp_table_prefix') ) { 
  168. return; 
  169.  
  170. if ( $wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' ) ) { 
  171. $wordpress_table_prefix .= $wordpress_mu_primary_blog_id . '_'; 
  172.  
  173. $role_query = <<<EOQ 
  174. SELECT 
  175. ID 
  176. FROM 
  177. `%1\$s` 
  178. LEFT JOIN `%2\$s` AS bbrole 
  179. ON ID = bbrole.user_id 
  180. AND bbrole.meta_key = '%3\$scapabilities' 
  181. LEFT JOIN `%2\$s` AS wprole 
  182. ON ID = wprole.user_id 
  183. AND wprole.meta_key = '%4\$scapabilities' 
  184. WHERE 
  185. bbrole.meta_key IS NULL OR 
  186. bbrole.meta_value IS NULL OR 
  187. wprole.meta_key IS NULL OR 
  188. wprole.meta_value IS NULL 
  189. ORDER BY 
  190. ID 
  191. EOQ; 
  192.  
  193. global $bbdb; 
  194.  
  195. $role_query = $bbdb->prepare( $role_query, $bbdb->users, $bbdb->usermeta, $bbdb->prefix, $wordpress_table_prefix ); 
  196.  
  197. if ( $user_ids = $bbdb->get_col( $role_query ) ) { 
  198. foreach ( $user_ids as $user_id ) { 
  199. bb_apply_wp_role_map_to_user( $user_id, false ); 
  200.  
  201. /** 
  202. * Updates a user's details in the database 
  203. * 
  204. * {@internal Missing Long Description}} 
  205. * 
  206. * @since 0.7.2 
  207. * @global bbdb $bbdb 
  208. * 
  209. * @param int $user_id 
  210. * @param string $user_email 
  211. * @param string $user_url 
  212. * @return int 
  213. */ 
  214. function bb_update_user( $user_id, $user_email, $user_url, $display_name ) { 
  215. global $wp_users_object; 
  216.  
  217. $user_id = (int) $user_id; 
  218. $user_url = bb_fix_link( $user_url ); 
  219.  
  220. $wp_users_object->update_user( $user_id, compact( 'user_email', 'user_url', 'display_name' ) ); 
  221.  
  222. do_action('bb_update_user', $user_id); 
  223. return $user_id; 
  224.  
  225. /** 
  226. * Sends a reset password email 
  227. * 
  228. * Sends an email to the email address specified in the user's profile 
  229. * prompting them to change their password. 
  230. * 
  231. * @since 0.7.2 
  232. * @global bbdb $bbdb 
  233. * 
  234. * @param string $user_login 
  235. * @return bool 
  236. */ 
  237. function bb_reset_email( $user_login ) 
  238. global $bbdb; 
  239.  
  240. $user_login = sanitize_user( $user_login, true ); 
  241.  
  242. if ( !$user = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->users WHERE user_login = %s", $user_login ) ) ) { 
  243. return new WP_Error( 'user_does_not_exist', __( 'The specified user does not exist.' ) ); 
  244.  
  245. $resetkey = substr( md5( bb_generate_password() ), 0, 15 ); 
  246. bb_update_usermeta( $user->ID, 'newpwdkey', $resetkey ); 
  247.  
  248. $reseturi = bb_get_uri( 
  249. 'bb-reset-password.php',  
  250. array( 'key' => $resetkey ),  
  251. BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_BB_USER_FORMS 
  252. ); 
  253.  
  254. $message = sprintf( 
  255. __( "If you wanted to reset your password, you may do so by visiting the following address:\n\n%s\n\nIf you don't want to reset your password, just ignore this email. Thanks!" ),  
  256. $reseturi 
  257. ); 
  258. $message = apply_filters( 'bb_reset_email_message', $message, $user, $reseturi, $resetkey ); 
  259.  
  260. $subject = sprintf( 
  261. __( '%s: Password Reset' ),  
  262. bb_get_option( 'name' ) 
  263. ); 
  264. $subject = apply_filters( 'bb_reset_email_subject', $subject, $user ); 
  265.  
  266. $mail_result = bb_mail( 
  267. bb_get_user_email( $user->ID ),  
  268. $subject,  
  269. $message 
  270. ); 
  271.  
  272. if ( !$mail_result ) { 
  273. return new WP_Error( 'sending_mail_failed', __( 'The email containing the password reset link could not be sent.' ) ); 
  274.  
  275. return true; 
  276.  
  277. /** 
  278. * Handles the resetting of users' passwords 
  279. * 
  280. * Handles resetting a user's password, prompted by an email sent by 
  281. * {@see bb_reset_email()} 
  282. * 
  283. * @since 0.7.2 
  284. * @global bbdb $bbdb 
  285. * 
  286. * @param string $key 
  287. * @return unknown 
  288. */ 
  289. function bb_reset_password( $key ) 
  290. global $bbdb; 
  291.  
  292. $key = sanitize_user( $key, true ); 
  293.  
  294. if ( empty( $key ) || !is_string( $key ) ) { 
  295. return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 
  296.  
  297. if ( !$user_id = $bbdb->get_var( $bbdb->prepare( "SELECT user_id FROM $bbdb->usermeta WHERE meta_key = 'newpwdkey' AND meta_value = %s", $key ) ) ) { 
  298. return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 
  299.  
  300. $user = new BP_User( $user_id ); 
  301.  
  302. if ( !$user || is_wp_error( $user ) ) { 
  303. return new WP_Error( 'invalid_key', __( 'Invalid key' ) ); 
  304.  
  305. if ( bb_has_broken_pass( $user->ID ) ) { 
  306. bb_block_current_user(); 
  307.  
  308. if ( !$user->has_cap( 'change_user_password', $user->ID ) ) { 
  309. return new WP_Error( 'permission_denied', __( 'You are not allowed to change your password.' ) ); 
  310.  
  311. $newpass = bb_generate_password(); 
  312. bb_update_user_password( $user->ID, $newpass ); 
  313. if ( !bb_send_pass( $user->ID, $newpass ) ) { 
  314. return new WP_Error( 'sending_mail_failed', __( 'The email containing the new password could not be sent.' ) ); 
  315.  
  316. bb_update_usermeta( $user->ID, 'newpwdkey', '' ); 
  317. return true; 
  318.  
  319. /** 
  320. * Updates a user's password in the database 
  321. * 
  322. * {@internal Missing Long Description}} 
  323. * 
  324. * @since 0.7.2 
  325. * @global bbdb $bbdb 
  326. * 
  327. * @param int $user_id 
  328. * @param string $password 
  329. * @return int 
  330. */ 
  331. function bb_update_user_password( $user_id, $password ) { 
  332. global $wp_users_object; 
  333.  
  334. $user_id = (int) $user_id; 
  335.  
  336. $wp_users_object->set_password( $password, $user_id ); 
  337.  
  338. do_action('bb_update_user_password', $user_id); 
  339. return $user_id; 
  340.  
  341. /** 
  342. * Sends an email with the user's new password 
  343. * 
  344. * {@internal Missing Long Description}} 
  345. * 
  346. * @since 0.7.2 
  347. * @global bbdb $bbdb {@internal Not used}} 
  348. * 
  349. * @param int|string $user 
  350. * @param string $pass 
  351. * @return bool 
  352. */ 
  353. function bb_send_pass( $user, $pass ) 
  354. if ( !$user = bb_get_user( $user ) ) { 
  355. return false; 
  356.  
  357. $message = sprintf( 
  358. __( "Your username is: %1\$s \nYour password is: %2\$s \nYou can now log in: %3\$s \n\nEnjoy!" ),  
  359. $user->user_login,  
  360. $pass,  
  361. bb_get_uri( null, null, BB_URI_CONTEXT_TEXT ) 
  362. ); 
  363. $message = apply_filters( 'bb_send_pass_message', $message, $user, $pass ); 
  364.  
  365. $subject = sprintf( 
  366. __( '%s: Password' ),  
  367. bb_get_option( 'name' ) 
  368. ); 
  369. $subject = apply_filters( 'bb_send_pass_subject', $subject, $user ); 
  370.  
  371. return bb_mail( 
  372. bb_get_user_email( $user->ID ),  
  373. $subject,  
  374. $message 
  375. ); 
  376.  
  377.  
  378.  
  379. /** Favorites */ 
  380.  
  381. function get_user_favorites( $user_id, $topics = false ) { 
  382. $user = bb_get_user( $user_id ); 
  383. if ( !empty($user->favorites) ) { 
  384. if ( $topics ) 
  385. $query = new BB_Query( 'topic', array('favorites' => $user_id, 'index_hint' => 'USE INDEX (`forum_time`)'), 'get_user_favorites' ); 
  386. else 
  387. $query = new BB_Query( 'post', array('favorites' => $user_id), 'get_user_favorites' ); 
  388. return $query->results; 
  389.  
  390. function is_user_favorite( $user_id = 0, $topic_id = 0 ) { 
  391. if ( $user_id ) 
  392. $user = bb_get_user( $user_id ); 
  393. else 
  394. global $user; 
  395. if ( $topic_id ) 
  396. $topic = get_topic( $topic_id ); 
  397. else 
  398. global $topic; 
  399. if ( !$user || !$topic ) 
  400. return; 
  401.  
  402. if ( isset($user->favorites) ) 
  403. return in_array($topic->topic_id, explode(', ', $user->favorites)); 
  404. return false; 
  405.  
  406. function bb_add_user_favorite( $user_id, $topic_id ) { 
  407. global $bbdb; 
  408. $user_id = (int) $user_id; 
  409. $topic_id = (int) $topic_id; 
  410. $user = bb_get_user( $user_id ); 
  411. $topic = get_topic( $topic_id ); 
  412. if ( !$user || !$topic ) 
  413. return false; 
  414.  
  415. $favorites_key = $bbdb->prefix . 'favorites'; 
  416. $fav = $user->$favorites_key ? explode(', ', $user->$favorites_key) : array(); 
  417. if ( ! in_array( $topic_id, $fav ) ) { 
  418. $fav[] = $topic_id; 
  419. $fav = implode(', ', $fav); 
  420. bb_update_usermeta( $user->ID, $favorites_key, $fav ); 
  421. do_action('bb_add_user_favorite', $user_id, $topic_id); 
  422. return true; 
  423.  
  424. function bb_remove_user_favorite( $user_id, $topic_id ) { 
  425. global $bbdb; 
  426. $user_id = (int) $user_id; 
  427. $topic_id = (int) $topic_id; 
  428. $user = bb_get_user( $user_id ); 
  429. if ( !$user ) 
  430. return false; 
  431.  
  432. $favorites_key = $bbdb->prefix . 'favorites'; 
  433. $fav = explode(', ', $user->$favorites_key); 
  434. if ( is_int( $pos = array_search($topic_id, $fav) ) ) { 
  435. array_splice($fav, $pos, 1); 
  436. $fav = implode(', ', $fav); 
  437. bb_update_usermeta( $user->ID, $favorites_key, $fav); 
  438. do_action('bb_remove_user_favorite', $user_id, $topic_id); 
  439. return true; 
.