/bp-members/bp-members-functions.php

  1. <?php 
  2. /** 
  3. * BuddyPress Member Functions. 
  4. * 
  5. * Functions specific to the members component. 
  6. * 
  7. * @package BuddyPress 
  8. * @subpackage MembersFunctions 
  9. * @since 1.5.0 
  10. */ 
  11.  
  12. // Exit if accessed directly. 
  13. defined( 'ABSPATH' ) || exit; 
  14.  
  15. /** 
  16. * Check for the existence of a Members directory page. 
  17. * 
  18. * @since 1.5.0 
  19. * 
  20. * @return bool True if found, otherwise false. 
  21. */ 
  22. function bp_members_has_directory() { 
  23. $bp = buddypress(); 
  24.  
  25. return (bool) !empty( $bp->pages->members->id ); 
  26.  
  27. /** 
  28. * Define the slug constants for the Members component. 
  29. * 
  30. * Handles the three slug constants used in the Members component - 
  31. * BP_MEMBERS_SLUG, BP_REGISTER_SLUG, and BP_ACTIVATION_SLUG. If these 
  32. * constants are not overridden in wp-config.php or bp-custom.php, they are 
  33. * defined here to match the slug of the corresponding WP pages. 
  34. * 
  35. * In general, fallback values are only used during initial BP page creation,  
  36. * when no slugs have been explicitly defined. 
  37. * 
  38. * @since 1.5.0 
  39. */ 
  40. function bp_core_define_slugs() { 
  41. $bp = buddypress(); 
  42.  
  43. // No custom members slug. 
  44. if ( !defined( 'BP_MEMBERS_SLUG' ) ) { 
  45. if ( !empty( $bp->pages->members ) ) { 
  46. define( 'BP_MEMBERS_SLUG', $bp->pages->members->slug ); 
  47. } else { 
  48. define( 'BP_MEMBERS_SLUG', 'members' ); 
  49.  
  50. // No custom registration slug. 
  51. if ( !defined( 'BP_REGISTER_SLUG' ) ) { 
  52. if ( !empty( $bp->pages->register ) ) { 
  53. define( 'BP_REGISTER_SLUG', $bp->pages->register->slug ); 
  54. } else { 
  55. define( 'BP_REGISTER_SLUG', 'register' ); 
  56.  
  57. // No custom activation slug. 
  58. if ( !defined( 'BP_ACTIVATION_SLUG' ) ) { 
  59. if ( !empty( $bp->pages->activate ) ) { 
  60. define( 'BP_ACTIVATION_SLUG', $bp->pages->activate->slug ); 
  61. } else { 
  62. define( 'BP_ACTIVATION_SLUG', 'activate' ); 
  63. add_action( 'bp_setup_globals', 'bp_core_define_slugs', 11 ); 
  64.  
  65. /** 
  66. * Fetch an array of users based on the parameters passed. 
  67. * 
  68. * Since BuddyPress 1.7, bp_core_get_users() uses BP_User_Query. If you 
  69. * need backward compatibility with BP_Core_User::get_users(), filter the 
  70. * bp_use_legacy_user_query value, returning true. 
  71. * 
  72. * @since 1.2.0 
  73. * 
  74. * @param array|string $args { 
  75. * Array of arguments. All are optional. See {@link BP_User_Query} for 
  76. * a more complete description of arguments. 
  77. * @type string $type Sort order. Default: 'active'. 
  78. * @type int $user_id Limit results to friends of a user. Default: false. 
  79. * @type mixed $exclude IDs to exclude from results. Default: false. 
  80. * @type string $search_terms Limit to users matching search terms. Default: false. 
  81. * @type string $meta_key Limit to users with a meta_key. Default: false. 
  82. * @type string $meta_value Limit to users with a meta_value (with meta_key). Default: false. 
  83. * @type array|string $member_type Array or comma-separated string of member types. 
  84. * @type array|string $member_type__in Array or comma-separated string of member types. 
  85. * `$member_type` takes precedence over this parameter. 
  86. * @type array|string $member_type__not_in Array or comma-separated string of member types to be excluded. 
  87. * @type mixed $include Limit results by user IDs. Default: false. 
  88. * @type int $per_page Results per page. Default: 20. 
  89. * @type int $page Page of results. Default: 1. 
  90. * @type bool $populate_extras Fetch optional extras. Default: true. 
  91. * @type string|bool $count_total How to do total user count. Default: 'count_query'. 
  92. * } 
  93. * @return array 
  94. */ 
  95. function bp_core_get_users( $args = '' ) { 
  96.  
  97. // Parse the user query arguments. 
  98. $r = bp_parse_args( $args, array( 
  99. 'type' => 'active', // Active, newest, alphabetical, random or popular. 
  100. 'user_id' => false, // Pass a user_id to limit to only friend connections for this user. 
  101. 'exclude' => false, // Users to exclude from results. 
  102. 'search_terms' => false, // Limit to users that match these search terms. 
  103. 'meta_key' => false, // Limit to users who have this piece of usermeta. 
  104. 'meta_value' => false, // With meta_key, limit to users where usermeta matches this value. 
  105. 'member_type' => '',  
  106. 'member_type__in' => '',  
  107. 'member_type__not_in' => '',  
  108. 'include' => false, // Pass comma separated list of user_ids to limit to only these users. 
  109. 'per_page' => 20, // The number of results to return per page. 
  110. 'page' => 1, // The page to return if limiting per page. 
  111. 'populate_extras' => true, // Fetch the last active, where the user is a friend, total friend count, latest update. 
  112. 'count_total' => 'count_query' // What kind of total user count to do, if any. 'count_query', 'sql_calc_found_rows', or false. 
  113. ), 'core_get_users' ); 
  114.  
  115. // For legacy users. Use of BP_Core_User::get_users() is deprecated. 
  116. if ( apply_filters( 'bp_use_legacy_user_query', false, __FUNCTION__, $r ) ) { 
  117. $retval = BP_Core_User::get_users( 
  118. $r['type'],  
  119. $r['per_page'],  
  120. $r['page'],  
  121. $r['user_id'],  
  122. $r['include'],  
  123. $r['search_terms'],  
  124. $r['populate_extras'],  
  125. $r['exclude'],  
  126. $r['meta_key'],  
  127. $r['meta_value'] 
  128. ); 
  129.  
  130. // Default behavior as of BuddyPress 1.7.0. 
  131. } else { 
  132.  
  133. // Get users like we were asked to do... 
  134. $users = new BP_User_Query( $r ); 
  135.  
  136. // ...but reformat the results to match bp_core_get_users() behavior. 
  137. $retval = array( 
  138. 'users' => array_values( $users->results ),  
  139. 'total' => $users->total_users 
  140. ); 
  141.  
  142. /** 
  143. * Filters the results of the user query. 
  144. * 
  145. * @since 1.2.0 
  146. * 
  147. * @param array $retval Array of users for the current query. 
  148. * @param array $r Array of parsed query arguments. 
  149. */ 
  150. return apply_filters( 'bp_core_get_users', $retval, $r ); 
  151.  
  152. /** 
  153. * Return the domain for the passed user: e.g. http://example.com/members/andy/. 
  154. * 
  155. * @since 1.0.0 
  156. * 
  157. * @param int $user_id The ID of the user. 
  158. * @param string|bool $user_nicename Optional. user_nicename of the user. 
  159. * @param string|bool $user_login Optional. user_login of the user. 
  160. * @return string 
  161. */ 
  162. function bp_core_get_user_domain( $user_id = 0, $user_nicename = false, $user_login = false ) { 
  163.  
  164. if ( empty( $user_id ) ) { 
  165. return; 
  166.  
  167. $username = bp_core_get_username( $user_id, $user_nicename, $user_login ); 
  168.  
  169. if ( bp_is_username_compatibility_mode() ) { 
  170. $username = rawurlencode( $username ); 
  171.  
  172. $after_domain = bp_core_enable_root_profiles() ? $username : bp_get_members_root_slug() . '/' . $username; 
  173. $domain = trailingslashit( bp_get_root_domain() . '/' . $after_domain ); 
  174.  
  175. // Don't use this filter. Subject to removal in a future release. 
  176. // Use the 'bp_core_get_user_domain' filter instead. 
  177. $domain = apply_filters( 'bp_core_get_user_domain_pre_cache', $domain, $user_id, $user_nicename, $user_login ); 
  178.  
  179. /** 
  180. * Filters the domain for the passed user. 
  181. * 
  182. * @since 1.0.1 
  183. * 
  184. * @param string $domain Domain for the passed user. 
  185. * @param int $user_id ID of the passed user. 
  186. * @param string $user_nicename User nicename of the passed user. 
  187. * @param string $user_login User login of the passed user. 
  188. */ 
  189. return apply_filters( 'bp_core_get_user_domain', $domain, $user_id, $user_nicename, $user_login ); 
  190.  
  191. /** 
  192. * Fetch everything in the wp_users table for a user, without any usermeta. 
  193. * 
  194. * @since 1.2.0 
  195. * 
  196. * @param int $user_id The ID of the user. 
  197. * @return array 
  198. */ 
  199. function bp_core_get_core_userdata( $user_id = 0 ) { 
  200. if ( empty( $user_id ) ) { 
  201. return false; 
  202.  
  203. if ( !$userdata = wp_cache_get( 'bp_core_userdata_' . $user_id, 'bp' ) ) { 
  204. $userdata = BP_Core_User::get_core_userdata( $user_id ); 
  205. wp_cache_set( 'bp_core_userdata_' . $user_id, $userdata, 'bp' ); 
  206.  
  207. /** 
  208. * Filters the userdata for a passed user. 
  209. * 
  210. * @since 1.2.0 
  211. * 
  212. * @param array $userdata Array of user data for a passed user. 
  213. */ 
  214. return apply_filters( 'bp_core_get_core_userdata', $userdata ); 
  215.  
  216. /** 
  217. * Return the ID of a user, based on user_login. 
  218. * 
  219. * No longer used. 
  220. * 
  221. * @todo Deprecate. 
  222. * 
  223. * @param string $user_login user_login of the user being queried. 
  224. * @return int 
  225. */ 
  226. function bp_core_get_displayed_userid( $user_login ) { 
  227. return apply_filters( 'bp_core_get_displayed_userid', bp_core_get_userid( $user_login ) ); 
  228.  
  229. /** 
  230. * Return the user ID based on a user's user_login. 
  231. * 
  232. * @since 1.0.0 
  233. * 
  234. * @param string $username user_login to check. 
  235. * @return int|null The ID of the matched user on success, null on failure. 
  236. */ 
  237. function bp_core_get_userid( $username = '' ) { 
  238. if ( empty( $username ) ) { 
  239. return false; 
  240.  
  241. $user = get_user_by( 'login', $username ); 
  242.  
  243. /** 
  244. * Filters the ID of a user, based on user_login. 
  245. * 
  246. * @since 1.0.1 
  247. * 
  248. * @param int|null $value ID of the user or null. 
  249. * @param string $username User login to check. 
  250. */ 
  251. return apply_filters( 'bp_core_get_userid', ! empty( $user->ID ) ? $user->ID : NULL, $username ); 
  252.  
  253. /** 
  254. * Return the user ID based on a user's user_nicename. 
  255. * 
  256. * @since 1.2.3 
  257. * 
  258. * @param string $user_nicename user_nicename to check. 
  259. * @return int|null The ID of the matched user on success, null on failure. 
  260. */ 
  261. function bp_core_get_userid_from_nicename( $user_nicename = '' ) { 
  262. if ( empty( $user_nicename ) ) { 
  263. return false; 
  264.  
  265. $user = get_user_by( 'slug', $user_nicename ); 
  266.  
  267. /** 
  268. * Filters the user ID based on user_nicename. 
  269. * 
  270. * @since 1.2.3 
  271. * 
  272. * @param int|null $value ID of the user or null. 
  273. * @param string $user_nicename User nicename to check. 
  274. */ 
  275. return apply_filters( 'bp_core_get_userid_from_nicename', ! empty( $user->ID ) ? $user->ID : NULL, $user_nicename ); 
  276.  
  277. /** 
  278. * Return the username for a user based on their user id. 
  279. * 
  280. * This function is sensitive to the BP_ENABLE_USERNAME_COMPATIBILITY_MODE,  
  281. * so it will return the user_login or user_nicename as appropriate. 
  282. * 
  283. * @since 1.0.0 
  284. * 
  285. * @param int $user_id User ID to check. 
  286. * @param string|bool $user_nicename Optional. user_nicename of user being checked. 
  287. * @param string|bool $user_login Optional. user_login of user being checked. 
  288. * @return string|bool The username of the matched user, or false. 
  289. */ 
  290. function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login = false ) { 
  291. $bp = buddypress(); 
  292.  
  293. // Check cache for user nicename. 
  294. $username = wp_cache_get( 'bp_user_username_' . $user_id, 'bp' ); 
  295. if ( false === $username ) { 
  296.  
  297. // Cache not found so prepare to update it. 
  298. $update_cache = true; 
  299.  
  300. // Nicename and login were not passed. 
  301. if ( empty( $user_nicename ) && empty( $user_login ) ) { 
  302.  
  303. // User ID matches logged in user. 
  304. if ( bp_loggedin_user_id() == $user_id ) { 
  305. $userdata = &$bp->loggedin_user->userdata; 
  306.  
  307. // User ID matches displayed in user. 
  308. } elseif ( bp_displayed_user_id() == $user_id ) { 
  309. $userdata = &$bp->displayed_user->userdata; 
  310.  
  311. // No user ID match. 
  312. } else { 
  313. $userdata = false; 
  314.  
  315. // No match so go dig. 
  316. if ( empty( $userdata ) ) { 
  317.  
  318. // User not found so return false. 
  319. if ( !$userdata = bp_core_get_core_userdata( $user_id ) ) { 
  320. return false; 
  321.  
  322. // Update the $user_id for later. 
  323. $user_id = $userdata->ID; 
  324.  
  325. // Two possible options. 
  326. $user_nicename = $userdata->user_nicename; 
  327. $user_login = $userdata->user_login; 
  328.  
  329. // Pull an audible and maybe use the login over the nicename. 
  330. $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename; 
  331.  
  332. // Username found in cache so don't update it again. 
  333. } else { 
  334. $update_cache = false; 
  335.  
  336. // Add this to cache. 
  337. if ( ( true === $update_cache ) && !empty( $username ) ) { 
  338. wp_cache_set( 'bp_user_username_' . $user_id, $username, 'bp' ); 
  339.  
  340. // @todo bust this cache if no $username found? 
  341. // } else { 
  342. // wp_cache_delete( 'bp_user_username_' . $user_id ); 
  343.  
  344. /** 
  345. * Filters the username based on originally provided user ID. 
  346. * 
  347. * @since 1.0.1 
  348. * 
  349. * @param string $username Username determined by user ID. 
  350. */ 
  351. return apply_filters( 'bp_core_get_username', $username ); 
  352.  
  353. /** 
  354. * Return the user_nicename for a user based on their user_id. 
  355. * 
  356. * This should be used for linking to user profiles and anywhere else a 
  357. * sanitized and unique slug to a user is needed. 
  358. * 
  359. * @since 1.5.0 
  360. * 
  361. * @todo Refactor to use a WP core function, if possible. 
  362. * 
  363. * @param int $user_id User ID to check. 
  364. * @return string|bool The username of the matched user, or false. 
  365. */ 
  366. function bp_members_get_user_nicename( $user_id ) { 
  367. $bp = buddypress(); 
  368.  
  369. if ( !$user_nicename = wp_cache_get( 'bp_members_user_nicename_' . $user_id, 'bp' ) ) { 
  370. $update_cache = true; 
  371.  
  372. // User ID matches logged in user. 
  373. if ( bp_loggedin_user_id() == $user_id ) { 
  374. $userdata = &$bp->loggedin_user->userdata; 
  375.  
  376. // User ID matches displayed in user. 
  377. } elseif ( bp_displayed_user_id() == $user_id ) { 
  378. $userdata = &$bp->displayed_user->userdata; 
  379.  
  380. // No user ID match. 
  381. } else { 
  382. $userdata = false; 
  383.  
  384. // No match so go dig. 
  385. if ( empty( $userdata ) ) { 
  386.  
  387. // User not found so return false. 
  388. if ( !$userdata = bp_core_get_core_userdata( $user_id ) ) { 
  389. return false; 
  390.  
  391. // User nicename found. 
  392. $user_nicename = $userdata->user_nicename; 
  393.  
  394. // Nicename found in cache so don't update it again. 
  395. } else { 
  396. $update_cache = false; 
  397.  
  398. // Add this to cache. 
  399. if ( true == $update_cache && !empty( $user_nicename ) ) { 
  400. wp_cache_set( 'bp_members_user_nicename_' . $user_id, $user_nicename, 'bp' ); 
  401.  
  402. /** 
  403. * Filters the user_nicename based on originally provided user ID. 
  404. * 
  405. * @since 1.5.0 
  406. * 
  407. * @param string $username User nice name determined by user ID. 
  408. */ 
  409. return apply_filters( 'bp_members_get_user_nicename', $user_nicename ); 
  410.  
  411. /** 
  412. * Return the email address for the user based on user ID. 
  413. * 
  414. * @since 1.0.0 
  415. * 
  416. * @param int $uid User ID to check. 
  417. * @return string The email for the matched user. Empty string if no user 
  418. * matched the $uid. 
  419. */ 
  420. function bp_core_get_user_email( $uid ) { 
  421.  
  422. if ( !$email = wp_cache_get( 'bp_user_email_' . $uid, 'bp' ) ) { 
  423.  
  424. // User exists. 
  425. $ud = bp_core_get_core_userdata( $uid ); 
  426. if ( ! empty( $ud ) ) { 
  427. $email = $ud->user_email; 
  428.  
  429. // User was deleted. 
  430. } else { 
  431. $email = ''; 
  432.  
  433. wp_cache_set( 'bp_user_email_' . $uid, $email, 'bp' ); 
  434.  
  435. /** 
  436. * Filters the user email for user based on user ID. 
  437. * 
  438. * @since 1.0.1 
  439. * 
  440. * @param string $email Email determined for the user. 
  441. */ 
  442. return apply_filters( 'bp_core_get_user_email', $email ); 
  443.  
  444. /** 
  445. * Return a HTML formatted link for a user with the user's full name as the link text. 
  446. * 
  447. * Eg: <a href="http://andy.example.com/">Andy Peatling</a> 
  448. * 
  449. * Optional parameters will return just the name or just the URL. 
  450. * 
  451. * @since 1.0.0 
  452. * 
  453. * @param int $user_id User ID to check. 
  454. * @param bool $no_anchor Disable URL and HTML and just return full name. 
  455. * Default: false. 
  456. * @param bool $just_link Disable full name and HTML and just return the URL 
  457. * text. Default false. 
  458. * @return string|bool The link text based on passed parameters, or false on 
  459. * no match. 
  460. */ 
  461. function bp_core_get_userlink( $user_id, $no_anchor = false, $just_link = false ) { 
  462. $display_name = bp_core_get_user_displayname( $user_id ); 
  463.  
  464. if ( empty( $display_name ) ) { 
  465. return false; 
  466.  
  467. if ( ! empty( $no_anchor ) ) { 
  468. return $display_name; 
  469.  
  470. if ( !$url = bp_core_get_user_domain( $user_id ) ) { 
  471. return false; 
  472.  
  473. if ( ! empty( $just_link ) ) { 
  474. return $url; 
  475.  
  476. /** 
  477. * Filters the link text for the passed in user. 
  478. * 
  479. * @since 1.2.0 
  480. * 
  481. * @param string $value Link text based on passed parameters. 
  482. * @param int $user_id ID of the user to check. 
  483. */ 
  484. return apply_filters( 'bp_core_get_userlink', '<a href="' . $url . '" title="' . $display_name . '">' . $display_name . '</a>', $user_id ); 
  485.  
  486. /** 
  487. * Fetch the display name for a group of users. 
  488. * 
  489. * Uses the 'Name' field in xprofile if available. Falls back on WP 
  490. * display_name, and then user_nicename. 
  491. * 
  492. * @since 2.0.0 
  493. * 
  494. * @param array $user_ids Array of user IDs to get display names for. 
  495. * @return array 
  496. */ 
  497. function bp_core_get_user_displaynames( $user_ids ) { 
  498.  
  499. // Sanitize. 
  500. $user_ids = wp_parse_id_list( $user_ids ); 
  501.  
  502. // Remove dupes and empties. 
  503. $user_ids = array_unique( array_filter( $user_ids ) ); 
  504.  
  505. if ( empty( $user_ids ) ) { 
  506. return array(); 
  507.  
  508. $uncached_ids = array(); 
  509. foreach ( $user_ids as $user_id ) { 
  510. if ( false === wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp' ) ) { 
  511. $uncached_ids[] = $user_id; 
  512.  
  513. // Prime caches. 
  514. if ( ! empty( $uncached_ids ) ) { 
  515. if ( bp_is_active( 'xprofile' ) ) { 
  516. $fullname_data = BP_XProfile_ProfileData::get_value_byid( 1, $uncached_ids ); 
  517.  
  518. // Key by user_id. 
  519. $fullnames = array(); 
  520. foreach ( $fullname_data as $fd ) { 
  521. if ( ! empty( $fd->value ) ) { 
  522. $fullnames[ intval( $fd->user_id ) ] = $fd->value; 
  523.  
  524. // If xprofiledata is not found for any users, we'll look 
  525. // them up separately. 
  526. $no_xprofile_ids = array_diff( $uncached_ids, array_keys( $fullnames ) ); 
  527. } else { 
  528. $fullnames = array(); 
  529. $no_xprofile_ids = $user_ids; 
  530.  
  531. if ( ! empty( $no_xprofile_ids ) ) { 
  532. // Use WP_User_Query because we don't need BP information. 
  533. $query = new WP_User_Query( array( 
  534. 'include' => $no_xprofile_ids,  
  535. 'fields' => array( 'ID', 'user_nicename', 'display_name', ),  
  536. 'count_total' => false,  
  537. 'blog_id' => 0,  
  538. ) ); 
  539.  
  540. foreach ( $query->results as $qr ) { 
  541. $fullnames[ $qr->ID ] = ! empty( $qr->display_name ) ? $qr->display_name : $qr->user_nicename; 
  542.  
  543. // If xprofile is active, set this value as the 
  544. // xprofile display name as well. 
  545. if ( bp_is_active( 'xprofile' ) ) { 
  546. xprofile_set_field_data( 1, $qr->ID, $fullnames[ $qr->ID ] ); 
  547.  
  548. foreach ( $fullnames as $fuser_id => $fname ) { 
  549. wp_cache_set( 'bp_user_fullname_' . $fuser_id, $fname, 'bp' ); 
  550.  
  551. $retval = array(); 
  552. foreach ( $user_ids as $user_id ) { 
  553. $retval[ $user_id ] = wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp' ); 
  554.  
  555. return $retval; 
  556.  
  557. /** 
  558. * Fetch the display name for a user. 
  559. * 
  560. * @since 1.0.1 
  561. * 
  562. * @param int|string|bool $user_id_or_username User ID or username. 
  563. * @return string|bool The display name for the user in question, or false if 
  564. * user not found. 
  565. */ 
  566. function bp_core_get_user_displayname( $user_id_or_username ) { 
  567. if ( empty( $user_id_or_username ) ) { 
  568. return false; 
  569.  
  570. if ( ! is_numeric( $user_id_or_username ) ) { 
  571. $user_id = bp_core_get_userid( $user_id_or_username ); 
  572. } else { 
  573. $user_id = $user_id_or_username; 
  574.  
  575. if ( empty( $user_id ) ) { 
  576. return false; 
  577.  
  578. $display_names = bp_core_get_user_displaynames( array( $user_id ) ); 
  579.  
  580. if ( ! isset( $display_names[ $user_id ] ) ) { 
  581. $fullname = false; 
  582. } else { 
  583. $fullname = $display_names[ $user_id ]; 
  584.  
  585. /** 
  586. * Filters the display name for the passed in user. 
  587. * 
  588. * @since 1.0.1 
  589. * 
  590. * @param string $fullname Display name for the user. 
  591. * @param int $user_id ID of the user to check. 
  592. */ 
  593. return apply_filters( 'bp_core_get_user_displayname', $fullname, $user_id ); 
  594. add_filter( 'bp_core_get_user_displayname', 'strip_tags', 1 ); 
  595. add_filter( 'bp_core_get_user_displayname', 'trim' ); 
  596. add_filter( 'bp_core_get_user_displayname', 'stripslashes' ); 
  597. add_filter( 'bp_core_get_user_displayname', 'esc_html' ); 
  598.  
  599. /** 
  600. * Return the user link for the user based on user email address. 
  601. * 
  602. * @since 1.0.0 
  603. * 
  604. * @param string $email The email address for the user. 
  605. * @return string The link to the users home base. False on no match. 
  606. */ 
  607. function bp_core_get_userlink_by_email( $email ) { 
  608. $user = get_user_by( 'email', $email ); 
  609.  
  610. /** 
  611. * Filters the user link for the user based on user email address. 
  612. * 
  613. * @since 1.0.1 
  614. * 
  615. * @param string|bool $value URL for the user if found, otherwise false. 
  616. */ 
  617. return apply_filters( 'bp_core_get_userlink_by_email', bp_core_get_userlink( $user->ID, false, false, true ) ); 
  618.  
  619. /** 
  620. * Return the user link for the user based on the supplied identifier. 
  621. * 
  622. * @since 1.0.0 
  623. * 
  624. * @param string $username If BP_ENABLE_USERNAME_COMPATIBILITY_MODE is set,  
  625. * this should be user_login, otherwise it should 
  626. * be user_nicename. 
  627. * @return string|bool The link to the user's domain, false on no match. 
  628. */ 
  629. function bp_core_get_userlink_by_username( $username ) { 
  630. if ( bp_is_username_compatibility_mode() ) { 
  631. $user_id = bp_core_get_userid( $username ); 
  632. } else { 
  633. $user_id = bp_core_get_userid_from_nicename( $username ); 
  634.  
  635. /** 
  636. * Filters the user link for the user based on username. 
  637. * 
  638. * @since 1.0.1 
  639. * 
  640. * @param string|bool $value URL for the user if found, otherwise false. 
  641. */ 
  642. return apply_filters( 'bp_core_get_userlink_by_username', bp_core_get_userlink( $user_id, false, false, true ) ); 
  643.  
  644. /** 
  645. * Return the total number of members for the installation. 
  646. * 
  647. * Note that this is a raw count of non-spam, activated users. It does not 
  648. * account for users who have logged activity (last_active). See 
  649. * {@link bp_core_get_active_member_count()}. 
  650. * 
  651. * @since 1.2.0 
  652. * 
  653. * @return int The total number of members. 
  654. */ 
  655. function bp_core_get_total_member_count() { 
  656. global $wpdb; 
  657.  
  658. $count = wp_cache_get( 'bp_total_member_count', 'bp' ); 
  659.  
  660. if ( false === $count ) { 
  661. $status_sql = bp_core_get_status_sql(); 
  662. $count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users} WHERE {$status_sql}" ); 
  663. wp_cache_set( 'bp_total_member_count', $count, 'bp' ); 
  664.  
  665. /** 
  666. * Filters the total number of members for the installation. 
  667. * 
  668. * @since 1.2.0 
  669. * 
  670. * @param int $count Total number of members. 
  671. */ 
  672. return apply_filters( 'bp_core_get_total_member_count', $count ); 
  673.  
  674. /** 
  675. * Return the total number of members, limited to those members with last_activity. 
  676. * 
  677. * @since 1.6.0 
  678. * 
  679. * @return int The number of active members. 
  680. */ 
  681. function bp_core_get_active_member_count() { 
  682. global $wpdb; 
  683.  
  684. $count = get_transient( 'bp_active_member_count' ); 
  685. if ( false === $count ) { 
  686. $bp = buddypress(); 
  687.  
  688. // Avoid a costly join by splitting the lookup. 
  689. if ( is_multisite() ) { 
  690. $sql = "SELECT ID FROM {$wpdb->users} WHERE (user_status != 0 OR deleted != 0 OR user_status != 0)"; 
  691. } else { 
  692. $sql = "SELECT ID FROM {$wpdb->users} WHERE user_status != 0"; 
  693.  
  694. $exclude_users = $wpdb->get_col( $sql ); 
  695. $exclude_users_sql = !empty( $exclude_users ) ? "AND user_id NOT IN (" . implode( ', ', wp_parse_id_list( $exclude_users ) ) . ")" : ''; 
  696. $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' {$exclude_users_sql}", $bp->members->id ) ); 
  697.  
  698. set_transient( 'bp_active_member_count', $count ); 
  699.  
  700. /** 
  701. * Filters the total number of members for the installation limited to those with last_activity. 
  702. * 
  703. * @since 1.6.0 
  704. * 
  705. * @param int $count Total number of active members. 
  706. */ 
  707. return apply_filters( 'bp_core_get_active_member_count', $count ); 
  708.  
  709. /** 
  710. * Process a spammed or unspammed user. 
  711. * 
  712. * This function is called from three places: 
  713. * 
  714. * - in bp_settings_action_capabilities() (from the front-end) 
  715. * - by bp_core_mark_user_spam_admin() (from wp-admin) 
  716. * - bp_core_mark_user_ham_admin() (from wp-admin) 
  717. * 
  718. * @since 1.6.0 
  719. * 
  720. * @param int $user_id The ID of the user being spammed/hammed. 
  721. * @param string $status 'spam' if being marked as spam, 'ham' otherwise. 
  722. * @param bool $do_wp_cleanup True to force the cleanup of WordPress content 
  723. * and status, otherwise false. Generally, this should 
  724. * only be false if WordPress is expected to have 
  725. * performed this cleanup independently, as when hooked 
  726. * to 'make_spam_user'. 
  727. * @return bool True on success, false on failure. 
  728. */ 
  729. function bp_core_process_spammer_status( $user_id, $status, $do_wp_cleanup = true ) { 
  730. global $wpdb; 
  731.  
  732. // Bail if no user ID. 
  733. if ( empty( $user_id ) ) { 
  734. return; 
  735.  
  736. // Bail if user ID is super admin. 
  737. if ( is_super_admin( $user_id ) ) { 
  738. return; 
  739.  
  740. // Get the functions file. 
  741. if ( is_multisite() ) { 
  742. require_once( ABSPATH . 'wp-admin/includes/ms.php' ); 
  743.  
  744. $is_spam = ( 'spam' == $status ); 
  745.  
  746. // Only you can prevent infinite loops. 
  747. remove_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' ); 
  748. remove_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' ); 
  749.  
  750. // Force the cleanup of WordPress content and status for multisite configs. 
  751. if ( $do_wp_cleanup ) { 
  752.  
  753. // Get the blogs for the user. 
  754. $blogs = get_blogs_of_user( $user_id, true ); 
  755.  
  756. foreach ( (array) array_values( $blogs ) as $details ) { 
  757.  
  758. // Do not mark the main or current root blog as spam. 
  759. if ( 1 == $details->userblog_id || bp_get_root_blog_id() == $details->userblog_id ) { 
  760. continue; 
  761.  
  762. // Update the blog status. 
  763. update_blog_status( $details->userblog_id, 'spam', $is_spam ); 
  764.  
  765. // Finally, mark this user as a spammer. 
  766. if ( is_multisite() ) { 
  767. update_user_status( $user_id, 'spam', $is_spam ); 
  768.  
  769. // Update the user status. 
  770. $wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) ); 
  771.  
  772. // Clean user cache. 
  773. clean_user_cache( $user_id ); 
  774.  
  775. if ( ! is_multisite() ) { 
  776. // Call multisite actions in single site mode for good measure. 
  777. if ( true === $is_spam ) { 
  778.  
  779. /** 
  780. * Fires at end of processing spammer in Dashboard if not multisite and user is spam. 
  781. * 
  782. * @since 1.5.0 
  783. * 
  784. * @param int $value user ID. 
  785. */ 
  786. do_action( 'make_spam_user', $user_id ); 
  787. } else { 
  788.  
  789. /** 
  790. * Fires at end of processing spammer in Dashboard if not multisite and user is not spam. 
  791. * 
  792. * @since 1.5.0 
  793. * 
  794. * @param int $value user ID. 
  795. */ 
  796. do_action( 'make_ham_user', $user_id ); 
  797.  
  798. // Hide this user's activity. 
  799. if ( ( true === $is_spam ) && bp_is_active( 'activity' ) ) { 
  800. bp_activity_hide_user_activity( $user_id ); 
  801.  
  802. // We need a special hook for is_spam so that components can delete data at spam time. 
  803. if ( true === $is_spam ) { 
  804.  
  805. /** 
  806. * Fires at the end of the process spammer process if the user is spam. 
  807. * 
  808. * @since 1.5.0 
  809. * 
  810. * @param int $value Displayed user ID. 
  811. */ 
  812. do_action( 'bp_make_spam_user', $user_id ); 
  813. } else { 
  814.  
  815. /** 
  816. * Fires at the end of the process spammer process if the user is not spam. 
  817. * 
  818. * @since 1.5.0 
  819. * 
  820. * @param int $value Displayed user ID. 
  821. */ 
  822. do_action( 'bp_make_ham_user', $user_id ); 
  823.  
  824. /** 
  825. * Fires at the end of the process for hanlding spammer status. 
  826. * 
  827. * @since 1.5.5 
  828. * 
  829. * @param int $user_id ID of the processed user. 
  830. * @param bool $is_spam The determined spam status of processed user. 
  831. */ 
  832. do_action( 'bp_core_process_spammer_status', $user_id, $is_spam ); 
  833.  
  834. // Put things back how we found them. 
  835. add_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' ); 
  836. add_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' ); 
  837.  
  838. return true; 
  839. /** 
  840. * Hook to WP's make_spam_user and run our custom BP spam functions. 
  841. * 
  842. * @since 1.6.0 
  843. * 
  844. * @param int $user_id The user ID passed from the make_spam_user hook. 
  845. */ 
  846. function bp_core_mark_user_spam_admin( $user_id ) { 
  847. bp_core_process_spammer_status( $user_id, 'spam', false ); 
  848. add_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' ); 
  849.  
  850. /** 
  851. * Hook to WP's make_ham_user and run our custom BP spam functions. 
  852. * 
  853. * @since 1.6.0 
  854. * 
  855. * @param int $user_id The user ID passed from the make_ham_user hook. 
  856. */ 
  857. function bp_core_mark_user_ham_admin( $user_id ) { 
  858. bp_core_process_spammer_status( $user_id, 'ham', false ); 
  859. add_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' ); 
  860.  
  861. /** 
  862. * Check whether a user has been marked as a spammer. 
  863. * 
  864. * @since 1.6.0 
  865. * 
  866. * @param int $user_id The ID for the user. 
  867. * @return bool True if spammer, otherwise false. 
  868. */ 
  869. function bp_is_user_spammer( $user_id = 0 ) { 
  870.  
  871. // No user to check. 
  872. if ( empty( $user_id ) ) { 
  873. return false; 
  874.  
  875. $bp = buddypress(); 
  876.  
  877. // Assume user is not spam. 
  878. $is_spammer = false; 
  879.  
  880. // Setup our user. 
  881. $user = false; 
  882.  
  883. // Get locally-cached data if available. 
  884. switch ( $user_id ) { 
  885. case bp_loggedin_user_id() : 
  886. $user = ! empty( $bp->loggedin_user->userdata ) ? $bp->loggedin_user->userdata : false; 
  887. break; 
  888.  
  889. case bp_displayed_user_id() : 
  890. $user = ! empty( $bp->displayed_user->userdata ) ? $bp->displayed_user->userdata : false; 
  891. break; 
  892.  
  893. // Manually get userdata if still empty. 
  894. if ( empty( $user ) ) { 
  895. $user = get_userdata( $user_id ); 
  896.  
  897. // No user found. 
  898. if ( empty( $user ) ) { 
  899. $is_spammer = false; 
  900.  
  901. // User found. 
  902. } else { 
  903.  
  904. // Check if spam. 
  905. if ( !empty( $user->spam ) ) { 
  906. $is_spammer = true; 
  907.  
  908. if ( 1 == $user->user_status ) { 
  909. $is_spammer = true; 
  910.  
  911. /** 
  912. * Filters whether a user is marked as a spammer. 
  913. * 
  914. * @since 1.6.0 
  915. * 
  916. * @param bool $is_spammer Whether or not user is marked as spammer. 
  917. */ 
  918. return apply_filters( 'bp_is_user_spammer', (bool) $is_spammer ); 
  919.  
  920. /** 
  921. * Check whether a user has been marked as deleted. 
  922. * 
  923. * @since 1.6.0 
  924. * 
  925. * @param int $user_id The ID for the user. 
  926. * @return bool True if deleted, otherwise false. 
  927. */ 
  928. function bp_is_user_deleted( $user_id = 0 ) { 
  929.  
  930. // No user to check. 
  931. if ( empty( $user_id ) ) { 
  932. return false; 
  933.  
  934. $bp = buddypress(); 
  935.  
  936. // Assume user is not deleted. 
  937. $is_deleted = false; 
  938.  
  939. // Setup our user. 
  940. $user = false; 
  941.  
  942. // Get locally-cached data if available. 
  943. switch ( $user_id ) { 
  944. case bp_loggedin_user_id() : 
  945. $user = ! empty( $bp->loggedin_user->userdata ) ? $bp->loggedin_user->userdata : false; 
  946. break; 
  947.  
  948. case bp_displayed_user_id() : 
  949. $user = ! empty( $bp->displayed_user->userdata ) ? $bp->displayed_user->userdata : false; 
  950. break; 
  951.  
  952. // Manually get userdata if still empty. 
  953. if ( empty( $user ) ) { 
  954. $user = get_userdata( $user_id ); 
  955.  
  956. // No user found. 
  957. if ( empty( $user ) ) { 
  958. $is_deleted = true; 
  959.  
  960. // User found. 
  961. } else { 
  962.  
  963. // Check if deleted. 
  964. if ( !empty( $user->deleted ) ) { 
  965. $is_deleted = true; 
  966.  
  967. if ( 2 == $user->user_status ) { 
  968. $is_deleted = true; 
  969.  
  970. /** 
  971. * Filters whether a user is marked as deleted. 
  972. * 
  973. * @since 1.6.0 
  974. * 
  975. * @param bool $is_deleted Whether or not user is marked as deleted. 
  976. */ 
  977. return apply_filters( 'bp_is_user_deleted', (bool) $is_deleted ); 
  978.  
  979. /** 
  980. * Check whether a user is "active", ie neither deleted nor spammer. 
  981. * 
  982. * @since 1.6.0 
  983. * 
  984. * @param int $user_id The user ID to check. 
  985. * @return bool True if active, otherwise false. 
  986. */ 
  987. function bp_is_user_active( $user_id = 0 ) { 
  988.  
  989. // Default to current user. 
  990. if ( empty( $user_id ) && is_user_logged_in() ) { 
  991. $user_id = bp_loggedin_user_id(); 
  992.  
  993. // No user to check. 
  994. if ( empty( $user_id ) ) { 
  995. return false; 
  996.  
  997. // Check spam. 
  998. if ( bp_is_user_spammer( $user_id ) ) { 
  999. return false; 
  1000.  
  1001. // Check deleted. 
  1002. if ( bp_is_user_deleted( $user_id ) ) { 
  1003. return false; 
  1004.  
  1005. // Assume true if not spam or deleted. 
  1006. return true; 
  1007.  
  1008. /** 
  1009. * Check whether user is not active. 
  1010. * 
  1011. * @since 1.6.0 
  1012. * 
  1013. * @todo No need for the user fallback checks, since they're done in 
  1014. * bp_is_user_active(). 
  1015. * 
  1016. * @param int $user_id The user ID to check. 
  1017. * @return bool True if inactive, otherwise false. 
  1018. */ 
  1019. function bp_is_user_inactive( $user_id = 0 ) { 
  1020.  
  1021. // Default to current user. 
  1022. if ( empty( $user_id ) && is_user_logged_in() ) { 
  1023. $user_id = bp_loggedin_user_id(); 
  1024.  
  1025. // No user to check. 
  1026. if ( empty( $user_id ) ) { 
  1027. return false; 
  1028.  
  1029. // Return the inverse of active. 
  1030. return !bp_is_user_active( $user_id ); 
  1031.  
  1032. /** 
  1033. * Update a user's last activity. 
  1034. * 
  1035. * @since 1.9.0 
  1036. * 
  1037. * @param int $user_id ID of the user being updated. 
  1038. * @param string $time Time of last activity, in 'Y-m-d H:i:s' format. 
  1039. * @return bool True on success, false on failure. 
  1040. */ 
  1041. function bp_update_user_last_activity( $user_id = 0, $time = '' ) { 
  1042.  
  1043. // Fall back on current user. 
  1044. if ( empty( $user_id ) ) { 
  1045. $user_id = bp_loggedin_user_id(); 
  1046.  
  1047. // Bail if the user id is 0, as there's nothing to update. 
  1048. if ( empty( $user_id ) ) { 
  1049. return false; 
  1050.  
  1051. // Fall back on current time. 
  1052. if ( empty( $time ) ) { 
  1053. $time = bp_core_current_time(); 
  1054.  
  1055. // As of BuddyPress 2.0, last_activity is no longer stored in usermeta. 
  1056. // However, we mirror it there for backward compatibility. Do not use! 
  1057. // Remove our warning and re-add. 
  1058. remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 ); 
  1059. remove_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 3 ); 
  1060. bp_update_user_meta( $user_id, 'last_activity', $time ); 
  1061. add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 ); 
  1062. add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 3 ); 
  1063.  
  1064. return BP_Core_User::update_last_activity( $user_id, $time ); 
  1065.  
  1066. /** 
  1067. * Backward compatibility for 'last_activity' usermeta fetching. 
  1068. * 
  1069. * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For 
  1070. * backward compatibility, we continue to mirror the data there. This function 
  1071. * serves two purposes: it warns plugin authors of the change, and it returns 
  1072. * the data from the proper location. 
  1073. * 
  1074. * @since 2.0.0 
  1075. * 
  1076. * @access private For internal use only. 
  1077. * 
  1078. * @param null $retval Null retval value. 
  1079. * @param int $object_id ID of the user. 
  1080. * @param string $meta_key Meta key being fetched. 
  1081. * @return mixed 
  1082. */ 
  1083. function _bp_get_user_meta_last_activity_warning( $retval, $object_id, $meta_key ) { 
  1084. static $warned = false; 
  1085.  
  1086. if ( 'last_activity' === $meta_key ) { 
  1087. // Don't send the warning more than once per pageload. 
  1088. if ( false === $warned ) { 
  1089. _doing_it_wrong( 'get_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_get_user_last_activity() instead.', 'buddypress' ), '2.0.0' ); 
  1090. $warned = true; 
  1091.  
  1092. return bp_get_user_last_activity( $object_id ); 
  1093.  
  1094. return $retval; 
  1095. add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 3 ); 
  1096.  
  1097. /** 
  1098. * Backward compatibility for 'last_activity' usermeta setting. 
  1099. * 
  1100. * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For 
  1101. * backward compatibility, we continue to mirror the data there. This function 
  1102. * serves two purposes: it warns plugin authors of the change, and it updates 
  1103. * the data in the proper location. 
  1104. * 
  1105. * @since 2.0.0 
  1106. * 
  1107. * @access private For internal use only. 
  1108. * 
  1109. * @param int $meta_id ID of the just-set usermeta row. 
  1110. * @param int $object_id ID of the user. 
  1111. * @param string $meta_key Meta key being fetched. 
  1112. * @param string $meta_value Active time. 
  1113. */ 
  1114. function _bp_update_user_meta_last_activity_warning( $meta_id, $object_id, $meta_key, $meta_value ) { 
  1115. if ( 'last_activity' === $meta_key ) { 
  1116. _doing_it_wrong( 'update_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_update_user_last_activity() instead.', 'buddypress' ), '2.0.0' ); 
  1117. bp_update_user_last_activity( $object_id, $meta_value ); 
  1118. add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 ); 
  1119.  
  1120. /** 
  1121. * Get the last activity for a given user. 
  1122. * 
  1123. * @since 1.9.0 
  1124. * 
  1125. * @param int $user_id The ID of the user. 
  1126. * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty 
  1127. * string if none is found. 
  1128. */ 
  1129. function bp_get_user_last_activity( $user_id = 0 ) { 
  1130. $activity = ''; 
  1131.  
  1132. $last_activity = BP_Core_User::get_last_activity( $user_id ); 
  1133. if ( ! empty( $last_activity[ $user_id ] ) ) { 
  1134. $activity = $last_activity[ $user_id ]['date_recorded']; 
  1135.  
  1136. /** 
  1137. * Filters the last activity for a given user. 
  1138. * 
  1139. * @since 1.9.0 
  1140. * 
  1141. * @param string $activity Time of last activity, in 'Y-m-d H:i:s' format or 
  1142. * an empty string if none found. 
  1143. * @param int $user_id ID of the user being checked. 
  1144. */ 
  1145. return apply_filters( 'bp_get_user_last_activity', $activity, $user_id ); 
  1146.  
  1147. /** 
  1148. * Migrate last_activity data from the usermeta table to the activity table. 
  1149. * 
  1150. * Generally, this function is only run when BP is upgraded to 2.0. It can also 
  1151. * be called directly from the BuddyPress Tools panel. 
  1152. * 
  1153. * @since 2.0.0 
  1154. * 
  1155. * @return bool 
  1156. */ 
  1157. function bp_last_activity_migrate() { 
  1158. global $wpdb; 
  1159.  
  1160. $bp = buddypress(); 
  1161.  
  1162. // Wipe out existing last_activity data in the activity table - 
  1163. // this helps to prevent duplicates when pulling from the usermeta 
  1164. // table. 
  1165. $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity'", $bp->members->id ) ); 
  1166.  
  1167. $sql = "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `action`, `content`, `primary_link`, `item_id`, `date_recorded` ) ( 
  1168. SELECT user_id, '{$bp->members->id}' as component, 'last_activity' as type, '' as action, '' as content, '' as primary_link, 0 as item_id, meta_value AS date_recorded 
  1169. FROM {$wpdb->usermeta} 
  1170. WHERE 
  1171. meta_key = 'last_activity' 
  1172. );"; 
  1173.  
  1174. return $wpdb->query( $sql ); 
  1175.  
  1176. /** 
  1177. * Fetch every post that is authored by the given user for the current blog. 
  1178. * 
  1179. * No longer used in BuddyPress. 
  1180. * 
  1181. * @todo Deprecate. 
  1182. * 
  1183. * @param int $user_id ID of the user being queried. 
  1184. * @return array Post IDs. 
  1185. */ 
  1186. function bp_core_get_all_posts_for_user( $user_id = 0 ) { 
  1187. global $wpdb; 
  1188.  
  1189. if ( empty( $user_id ) ) { 
  1190. $user_id = bp_displayed_user_id(); 
  1191.  
  1192. return apply_filters( 'bp_core_get_all_posts_for_user', $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_author = %d AND post_status = 'publish' AND post_type = 'post'", $user_id ) ) ); 
  1193.  
  1194. /** 
  1195. * Process account deletion requests. 
  1196. * 
  1197. * Primarily used for self-deletions, as requested through Settings. 
  1198. * 
  1199. * @since 1.0.0 
  1200. * 
  1201. * @param int $user_id Optional. ID of the user to be deleted. Default: the 
  1202. * logged-in user. 
  1203. * @return bool True on success, false on failure. 
  1204. */ 
  1205. function bp_core_delete_account( $user_id = 0 ) { 
  1206.  
  1207. // Use logged in user ID if none is passed. 
  1208. if ( empty( $user_id ) ) { 
  1209. $user_id = bp_loggedin_user_id(); 
  1210.  
  1211. // Site admins cannot be deleted. 
  1212. if ( is_super_admin( $user_id ) ) { 
  1213. return false; 
  1214.  
  1215. // Extra checks if user is not deleting themselves. 
  1216. if ( bp_loggedin_user_id() !== absint( $user_id ) ) { 
  1217.  
  1218. // Bail if current user cannot delete any users. 
  1219. if ( ! bp_current_user_can( 'delete_users' ) ) { 
  1220. return false; 
  1221.  
  1222. // Bail if current user cannot delete this user. 
  1223. if ( ! current_user_can_for_blog( bp_get_root_blog_id(), 'delete_user', $user_id ) ) { 
  1224. return false; 
  1225.  
  1226. /** 
  1227. * Fires before the processing of an account deletion. 
  1228. * 
  1229. * @since 1.6.0 
  1230. * 
  1231. * @param int $user_id ID of the user account being deleted. 
  1232. */ 
  1233. do_action( 'bp_core_pre_delete_account', $user_id ); 
  1234.  
  1235. // Specifically handle multi-site environment. 
  1236. if ( is_multisite() ) { 
  1237. require_once( ABSPATH . '/wp-admin/includes/ms.php' ); 
  1238. require_once( ABSPATH . '/wp-admin/includes/user.php' ); 
  1239.  
  1240. $retval = wpmu_delete_user( $user_id ); 
  1241.  
  1242. // Single site user deletion. 
  1243. } else { 
  1244. require_once( ABSPATH . '/wp-admin/includes/user.php' ); 
  1245. $retval = wp_delete_user( $user_id ); 
  1246.  
  1247. /** 
  1248. * Fires after the deletion of an account. 
  1249. * 
  1250. * @since 1.6.0 
  1251. * 
  1252. * @param int $user_id ID of the user account that was deleted. 
  1253. */ 
  1254. do_action( 'bp_core_deleted_account', $user_id ); 
  1255.  
  1256. return $retval; 
  1257.  
  1258. /** 
  1259. * Delete a user's avatar when the user is deleted. 
  1260. * 
  1261. * @since 1.9.0 
  1262. * 
  1263. * @param int $user_id ID of the user who is about to be deleted. 
  1264. * @return bool True on success, false on failure. 
  1265. */ 
  1266. function bp_core_delete_avatar_on_user_delete( $user_id ) { 
  1267. return bp_core_delete_existing_avatar( array( 
  1268. 'item_id' => $user_id,  
  1269. 'object' => 'user',  
  1270. ) ); 
  1271. add_action( 'wpmu_delete_user', 'bp_core_delete_avatar_on_user_delete' ); 
  1272. add_action( 'delete_user', 'bp_core_delete_avatar_on_user_delete' ); 
  1273.  
  1274. /** 
  1275. * Multibyte-safe ucfirst() support. 
  1276. * 
  1277. * Uses multibyte functions when available on the PHP build. 
  1278. * 
  1279. * @since 1.0.0 
  1280. * 
  1281. * @param string $str String to be upper-cased. 
  1282. * @return string 
  1283. */ 
  1284. function bp_core_ucfirst( $str ) { 
  1285. if ( function_exists( 'mb_strtoupper' ) && function_exists( 'mb_substr' ) ) { 
  1286. $fc = mb_strtoupper( mb_substr( $str, 0, 1 ) ); 
  1287. return $fc.mb_substr( $str, 1 ); 
  1288. } else { 
  1289. return ucfirst( $str ); 
  1290.  
  1291. /** 
  1292. * Prevent spammers from logging in. 
  1293. * 
  1294. * When a user logs in, check if they have been marked as a spammer. If yes 
  1295. * then simply redirect them to the home page and stop them from logging in. 
  1296. * 
  1297. * @since 1.1.2 
  1298. * 
  1299. * @param WP_User|WP_Error $user Either the WP_User object or the WP_Error 
  1300. * object, as passed to the 'authenticate' filter. 
  1301. * @return WP_User|WP_Error If the user is not a spammer, return the WP_User 
  1302. * object. Otherwise a new WP_Error object. 
  1303. */ 
  1304. function bp_core_boot_spammer( $user ) { 
  1305.  
  1306. // Check to see if the $user has already failed logging in, if so return $user as-is. 
  1307. if ( is_wp_error( $user ) || empty( $user ) ) { 
  1308. return $user; 
  1309.  
  1310. // The user exists; now do a check to see if the user is a spammer 
  1311. // if the user is a spammer, stop them in their tracks! 
  1312. if ( is_a( $user, 'WP_User' ) && ( ( is_multisite() && (int) $user->spam ) || 1 == $user->user_status ) ) { 
  1313. return new WP_Error( 'invalid_username', __( '<strong>ERROR</strong>: Your account has been marked as a spammer.', 'buddypress' ) ); 
  1314.  
  1315. // User is good to go! 
  1316. return $user; 
  1317. add_filter( 'authenticate', 'bp_core_boot_spammer', 30 ); 
  1318.  
  1319. /** 
  1320. * Delete last_activity data for the user when the user is deleted. 
  1321. * 
  1322. * @since 1.0.0 
  1323. * 
  1324. * @param int $user_id The user ID for the user to delete usermeta for. 
  1325. */ 
  1326. function bp_core_remove_data( $user_id ) { 
  1327.  
  1328. // Remove last_activity data. 
  1329. BP_Core_User::delete_last_activity( $user_id ); 
  1330.  
  1331. // Flush the cache to remove the user from all cached objects. 
  1332. wp_cache_flush(); 
  1333. add_action( 'wpmu_delete_user', 'bp_core_remove_data' ); 
  1334. add_action( 'delete_user', 'bp_core_remove_data' ); 
  1335. add_action( 'bp_make_spam_user', 'bp_core_remove_data' ); 
  1336.  
  1337. /** 
  1338. * Check whether the logged-in user can edit settings for the displayed user. 
  1339. * 
  1340. * @since 1.5.0 
  1341. * 
  1342. * @return bool True if editing is allowed, otherwise false. 
  1343. */ 
  1344. function bp_core_can_edit_settings() { 
  1345. if ( bp_is_my_profile() ) { 
  1346. return true; 
  1347.  
  1348. if ( is_super_admin( bp_displayed_user_id() ) && ! is_super_admin() ) { 
  1349. return false; 
  1350.  
  1351. if ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) { 
  1352. return true; 
  1353.  
  1354. return false; 
  1355.  
  1356. /** Sign-up *******************************************************************/ 
  1357.  
  1358. /** 
  1359. * Flush illegal names by getting and setting 'illegal_names' site option. 
  1360. * 
  1361. * @since 1.2.5 
  1362. */ 
  1363. function bp_core_flush_illegal_names() { 
  1364. $illegal_names = get_site_option( 'illegal_names' ); 
  1365. update_site_option( 'illegal_names', $illegal_names ); 
  1366.  
  1367. /** 
  1368. * Add BuddyPress-specific items to the illegal_names array. 
  1369. * 
  1370. * @since 1.2.7 
  1371. * 
  1372. * @param array|string $value Illegal names as being saved defined in 
  1373. * Multisite settings. 
  1374. * @param array|string $oldvalue The old value of the option. 
  1375. * @return array Merged and unique array of illegal names. 
  1376. */ 
  1377. function bp_core_get_illegal_names( $value = '', $oldvalue = '' ) { 
  1378.  
  1379. // Make sure $value is array. 
  1380. if ( empty( $value ) ) { 
  1381. $db_illegal_names = array(); 
  1382.  
  1383. if ( is_array( $value ) ) { 
  1384. $db_illegal_names = $value; 
  1385. } elseif ( is_string( $value ) ) { 
  1386. $db_illegal_names = explode( ' ', $value ); 
  1387.  
  1388. // Add the core components' slugs to the banned list even if their components aren't active. 
  1389. $bp_component_slugs = array( 
  1390. 'groups',  
  1391. 'members',  
  1392. 'forums',  
  1393. 'blogs',  
  1394. 'activity',  
  1395. 'profile',  
  1396. 'friends',  
  1397. 'search',  
  1398. 'settings',  
  1399. 'notifications',  
  1400. 'register',  
  1401. 'activate' 
  1402. ); 
  1403.  
  1404. // Core constants. 
  1405. $slug_constants = array( 
  1406. 'BP_GROUPS_SLUG',  
  1407. 'BP_MEMBERS_SLUG',  
  1408. 'BP_FORUMS_SLUG',  
  1409. 'BP_BLOGS_SLUG',  
  1410. 'BP_ACTIVITY_SLUG',  
  1411. 'BP_XPROFILE_SLUG',  
  1412. 'BP_FRIENDS_SLUG',  
  1413. 'BP_SEARCH_SLUG',  
  1414. 'BP_SETTINGS_SLUG',  
  1415. 'BP_NOTIFICATIONS_SLUG',  
  1416. 'BP_REGISTER_SLUG',  
  1417. 'BP_ACTIVATION_SLUG',  
  1418. ); 
  1419. foreach( $slug_constants as $constant ) { 
  1420. if ( defined( $constant ) ) { 
  1421. $bp_component_slugs[] = constant( $constant ); 
  1422.  
  1423. /** 
  1424. * Filters the array of default illegal usernames. 
  1425. * 
  1426. * @since 1.2.2 
  1427. * 
  1428. * @param array $value Merged and unique array of illegal usernames. 
  1429. */ 
  1430. $filtered_illegal_names = apply_filters( 'bp_core_illegal_usernames', array_merge( array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' ), $bp_component_slugs ) ); 
  1431.  
  1432. // Merge the arrays together. 
  1433. $merged_names = array_merge( (array) $filtered_illegal_names, (array) $db_illegal_names ); 
  1434.  
  1435. // Remove duplicates. 
  1436. $illegal_names = array_unique( (array) $merged_names ); 
  1437.  
  1438. /** 
  1439. * Filters the array of default illegal names. 
  1440. * 
  1441. * @since 1.2.5 
  1442. * 
  1443. * @param array $value Merged and unique array of illegal names. 
  1444. */ 
  1445. return apply_filters( 'bp_core_illegal_names', $illegal_names ); 
  1446. add_filter( 'pre_update_site_option_illegal_names', 'bp_core_get_illegal_names', 10, 2 ); 
  1447.  
  1448. /** 
  1449. * Check that an email address is valid for use. 
  1450. * 
  1451. * Performs the following checks: 
  1452. * - Is the email address well-formed? 
  1453. * - Is the email address already used? 
  1454. * - If there's an email domain blacklist, is the current domain on it? 
  1455. * - If there's an email domain whitelest, is the current domain on it? 
  1456. * 
  1457. * @since 1.6.2 
  1458. * 
  1459. * @param string $user_email The email being checked. 
  1460. * @return bool|array True if the address passes all checks; otherwise an array 
  1461. * of error codes. 
  1462. */ 
  1463. function bp_core_validate_email_address( $user_email ) { 
  1464. $errors = array(); 
  1465.  
  1466. $user_email = sanitize_email( $user_email ); 
  1467.  
  1468. // Is the email well-formed? 
  1469. if ( ! is_email( $user_email ) ) { 
  1470. $errors['invalid'] = 1; 
  1471.  
  1472. // Is the email on the Banned Email Domains list? 
  1473. // Note: This check only works on Multisite. 
  1474. if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $user_email ) ) { 
  1475. $errors['domain_banned'] = 1; 
  1476.  
  1477. // Is the email on the Limited Email Domains list? 
  1478. // Note: This check only works on Multisite. 
  1479. $limited_email_domains = get_site_option( 'limited_email_domains' ); 
  1480. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) { 
  1481. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) ); 
  1482. if ( ! in_array( $emaildomain, $limited_email_domains ) ) { 
  1483. $errors['domain_not_allowed'] = 1; 
  1484.  
  1485. // Is the email alreday in use? 
  1486. if ( email_exists( $user_email ) ) { 
  1487. $errors['in_use'] = 1; 
  1488.  
  1489. $retval = ! empty( $errors ) ? $errors : true; 
  1490.  
  1491. return $retval; 
  1492.  
  1493. /** 
  1494. * Add the appropriate errors to a WP_Error object, given results of a validation test. 
  1495. * 
  1496. * Functions like bp_core_validate_email_address() return a structured array 
  1497. * of error codes. bp_core_add_validation_error_messages() takes this array and 
  1498. * parses, adding the appropriate error messages to the WP_Error object. 
  1499. * 
  1500. * @since 1.7.0 
  1501. * 
  1502. * @see bp_core_validate_email_address() 
  1503. * 
  1504. * @param WP_Error $errors WP_Error object. 
  1505. * @param array $validation_results The return value of a validation function 
  1506. * like bp_core_validate_email_address(). 
  1507. */ 
  1508. function bp_core_add_validation_error_messages( WP_Error $errors, $validation_results ) { 
  1509. if ( ! empty( $validation_results['invalid'] ) ) { 
  1510. $errors->add( 'user_email', __( 'Please check your email address.', 'buddypress' ) ); 
  1511.  
  1512. if ( ! empty( $validation_results['domain_banned'] ) ) { 
  1513. $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) ); 
  1514.  
  1515. if ( ! empty( $validation_results['domain_not_allowed'] ) ) { 
  1516. $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) ); 
  1517.  
  1518. if ( ! empty( $validation_results['in_use'] ) ) { 
  1519. $errors->add( 'user_email', __( 'Sorry, that email address is already used!', 'buddypress' ) ); 
  1520.  
  1521. /** 
  1522. * Validate a user name and email address when creating a new user. 
  1523. * 
  1524. * @since 1.2.2 
  1525. * 
  1526. * @param string $user_name Username to validate. 
  1527. * @param string $user_email Email address to validate. 
  1528. * @return array Results of user validation including errors, if any. 
  1529. */ 
  1530. function bp_core_validate_user_signup( $user_name, $user_email ) { 
  1531.  
  1532. // Make sure illegal names include BuddyPress slugs and values. 
  1533. bp_core_flush_illegal_names(); 
  1534.  
  1535. // WordPress Multisite has its own validation. Use it, so that we 
  1536. // properly mirror restrictions on username, etc. 
  1537. if ( function_exists( 'wpmu_validate_user_signup' ) ) { 
  1538. $result = wpmu_validate_user_signup( $user_name, $user_email ); 
  1539.  
  1540. // When not running Multisite, we perform our own validation. What 
  1541. // follows reproduces much of the logic of wpmu_validate_user_signup(),  
  1542. // minus the multisite-specific restrictions on user_login. 
  1543. } else { 
  1544. $errors = new WP_Error(); 
  1545.  
  1546. /** 
  1547. * Filters the username before being validated. 
  1548. * 
  1549. * @since 1.5.5 
  1550. * 
  1551. * @param string $user_name Username to validate. 
  1552. */ 
  1553. $user_name = apply_filters( 'pre_user_login', $user_name ); 
  1554.  
  1555. // User name can't be empty. 
  1556. if ( empty( $user_name ) ) { 
  1557. $errors->add( 'user_name', __( 'Please enter a username', 'buddypress' ) ); 
  1558.  
  1559. // User name can't be on the blacklist. 
  1560. $illegal_names = get_site_option( 'illegal_names' ); 
  1561. if ( in_array( $user_name, (array) $illegal_names ) ) { 
  1562. $errors->add( 'user_name', __( 'That username is not allowed', 'buddypress' ) ); 
  1563.  
  1564. // User name must pass WP's validity check. 
  1565. if ( ! validate_username( $user_name ) ) { 
  1566. $errors->add( 'user_name', __( 'Usernames can contain only letters, numbers, ., -, and @', 'buddypress' ) ); 
  1567.  
  1568. // Minimum of 4 characters. 
  1569. if ( strlen( $user_name ) < 4 ) { 
  1570. $errors->add( 'user_name', __( 'Username must be at least 4 characters', 'buddypress' ) ); 
  1571.  
  1572. // No underscores. @todo Why not? 
  1573. if ( false !== strpos( ' ' . $user_name, '_' ) ) { 
  1574. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character "_"!', 'buddypress' ) ); 
  1575.  
  1576. // No usernames that are all numeric. @todo Why? 
  1577. $match = array(); 
  1578. preg_match( '/[0-9]*/', $user_name, $match ); 
  1579. if ( $match[0] == $user_name ) { 
  1580. $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', 'buddypress' ) ); 
  1581.  
  1582. // Check into signups. 
  1583. $signups = BP_Signup::get( array( 
  1584. 'user_login' => $user_name,  
  1585. ) ); 
  1586.  
  1587. $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false; 
  1588.  
  1589. // Check if the username has been used already. 
  1590. if ( username_exists( $user_name ) || ! empty( $signup ) ) { 
  1591. $errors->add( 'user_name', __( 'Sorry, that username already exists!', 'buddypress' ) ); 
  1592.  
  1593. // Validate the email address and process the validation results into 
  1594. // error messages. 
  1595. $validate_email = bp_core_validate_email_address( $user_email ); 
  1596. bp_core_add_validation_error_messages( $errors, $validate_email ); 
  1597.  
  1598. // Assemble the return array. 
  1599. $result = array( 
  1600. 'user_name' => $user_name,  
  1601. 'user_email' => $user_email,  
  1602. 'errors' => $errors,  
  1603. ); 
  1604.  
  1605. // Apply WPMU legacy filter. 
  1606. $result = apply_filters( 'wpmu_validate_user_signup', $result ); 
  1607.  
  1608. /** 
  1609. * Filters the result of the user signup validation. 
  1610. * 
  1611. * @since 1.2.2 
  1612. * 
  1613. * @param array $result Results of user validation including errors, if any. 
  1614. */ 
  1615. return apply_filters( 'bp_core_validate_user_signup', $result ); 
  1616.  
  1617. /** 
  1618. * Validate blog URL and title provided at signup. 
  1619. * 
  1620. * @since 1.2.2 
  1621. * 
  1622. * @todo Why do we have this wrapper? 
  1623. * 
  1624. * @param string $blog_url Blog URL requested during registration. 
  1625. * @param string $blog_title Blog title requested during registration. 
  1626. * @return array 
  1627. */ 
  1628. function bp_core_validate_blog_signup( $blog_url, $blog_title ) { 
  1629. if ( ! is_multisite() || ! function_exists( 'wpmu_validate_blog_signup' ) ) { 
  1630. return false; 
  1631.  
  1632. /** 
  1633. * Filters the validated blog url and title provided at signup. 
  1634. * 
  1635. * @since 1.2.2 
  1636. * 
  1637. * @param array $value Array with the new site data and error messages. 
  1638. */ 
  1639. return apply_filters( 'bp_core_validate_blog_signup', wpmu_validate_blog_signup( $blog_url, $blog_title ) ); 
  1640.  
  1641. /** 
  1642. * Process data submitted at user registration and convert to a signup object. 
  1643. * 
  1644. * @since 1.2.0 
  1645. * 
  1646. * @todo There appears to be a bug in the return value on success. 
  1647. * 
  1648. * @param string $user_login Login name requested by the user. 
  1649. * @param string $user_password Password requested by the user. 
  1650. * @param string $user_email Email address entered by the user. 
  1651. * @param array $usermeta Miscellaneous metadata about the user (blog-specific 
  1652. * signup data, xprofile data, etc). 
  1653. * @return bool|WP_Error True on success, WP_Error on failure. 
  1654. */ 
  1655. function bp_core_signup_user( $user_login, $user_password, $user_email, $usermeta ) { 
  1656. $bp = buddypress(); 
  1657.  
  1658. // We need to cast $user_id to pass to the filters. 
  1659. $user_id = false; 
  1660.  
  1661. // Multisite installs have their own install procedure. 
  1662. if ( is_multisite() ) { 
  1663. wpmu_signup_user( $user_login, $user_email, $usermeta ); 
  1664.  
  1665. } else { 
  1666. // Format data. 
  1667. $user_login = preg_replace( '/\s+/', '', sanitize_user( $user_login, true ) ); 
  1668. $user_email = sanitize_email( $user_email ); 
  1669. $activation_key = wp_generate_password( 32, false ); 
  1670.  
  1671. /** 
  1672. * WordPress's default behavior is to create user accounts 
  1673. * immediately at registration time. BuddyPress uses a system 
  1674. * borrowed from WordPress Multisite, where signups are stored 
  1675. * separately and accounts are only created at the time of 
  1676. * activation. For backward compatibility with plugins that may 
  1677. * be anticipating WP's default behavior, BP silently creates 
  1678. * accounts for registrations (though it does not use them). If 
  1679. * you know that you are not running any plugins dependent on 
  1680. * these pending accounts, you may want to save a little DB 
  1681. * clutter by defining setting the BP_SIGNUPS_SKIP_USER_CREATION 
  1682. * to true in your wp-config.php file. 
  1683. */ 
  1684. if ( ! defined( 'BP_SIGNUPS_SKIP_USER_CREATION' ) || ! BP_SIGNUPS_SKIP_USER_CREATION ) { 
  1685. $user_id = BP_Signup::add_backcompat( $user_login, $user_password, $user_email, $usermeta ); 
  1686.  
  1687. if ( is_wp_error( $user_id ) ) { 
  1688. return $user_id; 
  1689.  
  1690. bp_update_user_meta( $user_id, 'activation_key', $activation_key ); 
  1691.  
  1692. $args = array( 
  1693. 'user_login' => $user_login,  
  1694. 'user_email' => $user_email,  
  1695. 'activation_key' => $activation_key,  
  1696. 'meta' => $usermeta,  
  1697. ); 
  1698.  
  1699. BP_Signup::add( $args ); 
  1700.  
  1701. /** 
  1702. * Filters if BuddyPress should send an activation key for a new signup. 
  1703. * 
  1704. * @since 1.2.3 
  1705. * 
  1706. * @param bool $value Whether or not to send the activation key. 
  1707. * @param int $user_id User ID to send activation key to. 
  1708. * @param string $user_email User email to send activation key to. 
  1709. * @param string $activation_key Activation key to be sent. 
  1710. * @param array $usermeta Miscellaneous metadata about the user (blog-specific 
  1711. * signup data, xprofile data, etc). 
  1712. */ 
  1713. if ( apply_filters( 'bp_core_signup_send_activation_key', true, $user_id, $user_email, $activation_key, $usermeta ) ) { 
  1714. bp_core_signup_send_validation_email( $user_id, $user_email, $activation_key, $user_login ); 
  1715.  
  1716. $bp->signup->username = $user_login; 
  1717.  
  1718. /** 
  1719. * Fires at the end of the process to sign up a user. 
  1720. * 
  1721. * @since 1.2.2 
  1722. * 
  1723. * @param bool|WP_Error $user_id True on success, WP_Error on failure. 
  1724. * @param string $user_login Login name requested by the user. 
  1725. * @param string $user_password Password requested by the user. 
  1726. * @param string $user_email Email address requested by the user. 
  1727. * @param array $usermeta Miscellaneous metadata about the user (blog-specific 
  1728. * signup data, xprofile data, etc). 
  1729. */ 
  1730. do_action( 'bp_core_signup_user', $user_id, $user_login, $user_password, $user_email, $usermeta ); 
  1731.  
  1732. return $user_id; 
  1733.  
  1734. /** 
  1735. * Create a blog and user based on data supplied at user registration. 
  1736. * 
  1737. * @since 1.2.2 
  1738. * 
  1739. * @param string $blog_domain Domain requested by user. 
  1740. * @param string $blog_path Path requested by user. 
  1741. * @param string $blog_title Title as entered by user. 
  1742. * @param string $user_name user_login of requesting user. 
  1743. * @param string $user_email Email address of requesting user. 
  1744. * @param string $usermeta Miscellaneous metadata for the user. 
  1745. * @return bool 
  1746. */ 
  1747. function bp_core_signup_blog( $blog_domain, $blog_path, $blog_title, $user_name, $user_email, $usermeta ) { 
  1748. if ( ! is_multisite() || ! function_exists( 'wpmu_signup_blog' ) ) { 
  1749. return false; 
  1750.  
  1751. /** 
  1752. * Filters the result of wpmu_signup_blog(). 
  1753. * 
  1754. * This filter provides no value and is retained for 
  1755. * backwards compatibility. 
  1756. * 
  1757. * @since 1.2.2 
  1758. * 
  1759. * @param void $value 
  1760. */ 
  1761. return apply_filters( 'bp_core_signup_blog', wpmu_signup_blog( $blog_domain, $blog_path, $blog_title, $user_name, $user_email, $usermeta ) ); 
  1762.  
  1763. /** 
  1764. * Activate a signup, as identified by an activation key. 
  1765. * 
  1766. * @since 1.2.2 
  1767. * 
  1768. * @param string $key Activation key. 
  1769. * @return int|bool User ID on success, false on failure. 
  1770. */ 
  1771. function bp_core_activate_signup( $key ) { 
  1772. global $wpdb; 
  1773.  
  1774. $user = false; 
  1775.  
  1776. // Multisite installs have their own activation routine. 
  1777. if ( is_multisite() ) { 
  1778. $user = wpmu_activate_signup( $key ); 
  1779.  
  1780. // If there were errors, add a message and redirect. 
  1781. if ( ! empty( $user->errors ) ) { 
  1782. return $user; 
  1783.  
  1784. $user_id = $user['user_id']; 
  1785.  
  1786. } else { 
  1787. $signups = BP_Signup::get( array( 
  1788. 'activation_key' => $key,  
  1789. ) ); 
  1790.  
  1791. if ( empty( $signups['signups'] ) ) { 
  1792. return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) ); 
  1793.  
  1794. $signup = $signups['signups'][0]; 
  1795.  
  1796. if ( $signup->active ) { 
  1797. if ( empty( $signup->domain ) ) { 
  1798. return new WP_Error( 'already_active', __( 'The user is already active.', 'buddypress' ), $signup ); 
  1799. } else { 
  1800. return new WP_Error( 'already_active', __( 'The site is already active.', 'buddypress' ), $signup ); 
  1801.  
  1802. // Password is hashed again in wp_insert_user. 
  1803. $password = wp_generate_password( 12, false ); 
  1804.  
  1805. $user_id = username_exists( $signup->user_login ); 
  1806.  
  1807. // Create the user. This should only be necessary if BP_SIGNUPS_SKIP_USER_CREATION is true. 
  1808. if ( ! $user_id ) { 
  1809. $user_id = wp_create_user( $signup->user_login, $password, $signup->user_email ); 
  1810.  
  1811. // Otherwise, update the existing user's status. 
  1812. } elseif ( $key === bp_get_user_meta( $user_id, 'activation_key', true ) || $key === wp_hash( $user_id ) ) { 
  1813.  
  1814. // Change the user's status so they become active. 
  1815. if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id ) ) ) { 
  1816. return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) ); 
  1817.  
  1818. bp_delete_user_meta( $user_id, 'activation_key' ); 
  1819.  
  1820. $member = get_userdata( $user_id ); 
  1821. $member->set_role( get_option('default_role') ); 
  1822.  
  1823. $user_already_created = true; 
  1824.  
  1825. } else { 
  1826. $user_already_exists = true; 
  1827.  
  1828. if ( ! $user_id ) { 
  1829. return new WP_Error( 'create_user', __( 'Could not create user', 'buddypress' ), $signup ); 
  1830.  
  1831. // Fetch the signup so we have the data later on. 
  1832. $signups = BP_Signup::get( array( 
  1833. 'activation_key' => $key,  
  1834. ) ); 
  1835.  
  1836. $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false; 
  1837.  
  1838. // Activate the signup. 
  1839. BP_Signup::validate( $key ); 
  1840.  
  1841. if ( isset( $user_already_exists ) ) { 
  1842. return new WP_Error( 'user_already_exists', __( 'That username is already activated.', 'buddypress' ), $signup ); 
  1843.  
  1844. // Set up data to pass to the legacy filter. 
  1845. $user = array( 
  1846. 'user_id' => $user_id,  
  1847. 'password' => $signup->meta['password'],  
  1848. 'meta' => $signup->meta,  
  1849. ); 
  1850.  
  1851. // Notify the site admin of a new user registration. 
  1852. wp_new_user_notification( $user_id ); 
  1853.  
  1854. if ( isset( $user_already_created ) ) { 
  1855.  
  1856. /** 
  1857. * Fires if the user has already been created. 
  1858. * 
  1859. * @since 1.2.2 
  1860. * 
  1861. * @param int $user_id ID of the user being checked. 
  1862. * @param string $key Activation key. 
  1863. * @param array $user Array of user data. 
  1864. */ 
  1865. do_action( 'bp_core_activated_user', $user_id, $key, $user ); 
  1866. return $user_id; 
  1867.  
  1868. // Set any profile data. 
  1869. if ( bp_is_active( 'xprofile' ) ) { 
  1870. if ( ! empty( $user['meta']['profile_field_ids'] ) ) { 
  1871. $profile_field_ids = explode( ', ', $user['meta']['profile_field_ids'] ); 
  1872.  
  1873. foreach( (array) $profile_field_ids as $field_id ) { 
  1874. $current_field = isset( $user['meta']["field_{$field_id}"] ) ? $user['meta']["field_{$field_id}"] : false; 
  1875.  
  1876. if ( !empty( $current_field ) ) { 
  1877. xprofile_set_field_data( $field_id, $user_id, $current_field ); 
  1878.  
  1879. // Save the visibility level. 
  1880. $visibility_level = ! empty( $user['meta']['field_' . $field_id . '_visibility'] ) ? $user['meta']['field_' . $field_id . '_visibility'] : 'public'; 
  1881. xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level ); 
  1882.  
  1883. // Replace the password automatically generated by WordPress by the one the user chose. 
  1884. if ( ! empty( $user['meta']['password'] ) ) { 
  1885. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id ) ); 
  1886.  
  1887. /** 
  1888. * Make sure to clean the user's cache as we've 
  1889. * directly edited the password without using 
  1890. * wp_update_user(). 
  1891. * 
  1892. * If we can't use wp_update_user() that's because 
  1893. * we already hashed the password at the signup step. 
  1894. */ 
  1895. $uc = wp_cache_get( $user_id, 'users' ); 
  1896.  
  1897. if ( ! empty( $uc->ID ) ) { 
  1898. clean_user_cache( $uc->ID ); 
  1899.  
  1900. /** 
  1901. * Fires at the end of the user activation process. 
  1902. * 
  1903. * @since 1.2.2 
  1904. * 
  1905. * @param int $user_id ID of the user being checked. 
  1906. * @param string $key Activation key. 
  1907. * @param array $user Array of user data. 
  1908. */ 
  1909. do_action( 'bp_core_activated_user', $user_id, $key, $user ); 
  1910.  
  1911. return $user_id; 
  1912.  
  1913. /** 
  1914. * Migrate signups from pre-2.0 configuration to wp_signups. 
  1915. * 
  1916. * @since 2.0.1 
  1917. */ 
  1918. function bp_members_migrate_signups() { 
  1919. global $wpdb; 
  1920.  
  1921. $status_2_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->users} WHERE user_status = '2'" ); 
  1922.  
  1923. if ( ! empty( $status_2_ids ) ) { 
  1924. $signups = get_users( array( 
  1925. 'fields' => array( 
  1926. 'ID',  
  1927. 'user_login',  
  1928. 'user_pass',  
  1929. 'user_registered',  
  1930. 'user_email',  
  1931. 'display_name',  
  1932. ),  
  1933. 'include' => $status_2_ids,  
  1934. ) ); 
  1935.  
  1936. // Fetch activation keys separately, to avoid the all_with_meta 
  1937. // overhead. 
  1938. $status_2_ids_sql = implode( ', ', $status_2_ids ); 
  1939. $ak_data = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'activation_key' AND user_id IN ({$status_2_ids_sql})" ); 
  1940.  
  1941. // Rekey. 
  1942. $activation_keys = array(); 
  1943. foreach ( $ak_data as $ak_datum ) { 
  1944. $activation_keys[ intval( $ak_datum->user_id ) ] = $ak_datum->meta_value; 
  1945.  
  1946. unset( $status_2_ids_sql, $status_2_ids, $ak_data ); 
  1947.  
  1948. // Merge. 
  1949. foreach ( $signups as &$signup ) { 
  1950. if ( isset( $activation_keys[ $signup->ID ] ) ) { 
  1951. $signup->activation_key = $activation_keys[ $signup->ID ]; 
  1952.  
  1953. // Reset the signup var as we're using it to process the migration. 
  1954. unset( $signup ); 
  1955.  
  1956. } else { 
  1957. return; 
  1958.  
  1959. foreach ( $signups as $signup ) { 
  1960. $meta = array(); 
  1961.  
  1962. // Rebuild the activation key, if missing. 
  1963. if ( empty( $signup->activation_key ) ) { 
  1964. $signup->activation_key = wp_generate_password( 32, false ); 
  1965.  
  1966. if ( bp_is_active( 'xprofile' ) ) { 
  1967. $meta['field_1'] = $signup->display_name; 
  1968.  
  1969. $meta['password'] = $signup->user_pass; 
  1970.  
  1971. $user_login = preg_replace( '/\s+/', '', sanitize_user( $signup->user_login, true ) ); 
  1972. $user_email = sanitize_email( $signup->user_email ); 
  1973.  
  1974. BP_Signup::add( array( 
  1975. 'user_login' => $user_login,  
  1976. 'user_email' => $user_email,  
  1977. 'registered' => $signup->user_registered,  
  1978. 'activation_key' => $signup->activation_key,  
  1979. 'meta' => $meta 
  1980. ) ); 
  1981.  
  1982. // Deleting these options will remove signups from users count. 
  1983. delete_user_option( $signup->ID, 'capabilities' ); 
  1984. delete_user_option( $signup->ID, 'user_level' ); 
  1985.  
  1986. /** 
  1987. * Map a user's WP display name to the XProfile fullname field, if necessary. 
  1988. * 
  1989. * This only happens when a user is registered in wp-admin by an administrator; 
  1990. * during normal registration, XProfile data is provided directly by the user. 
  1991. * 
  1992. * @since 1.2.0 
  1993. * 
  1994. * @param int $user_id ID of the user. 
  1995. * @return bool 
  1996. */ 
  1997. function bp_core_map_user_registration( $user_id ) { 
  1998.  
  1999. // Only map data when the site admin is adding users, not on registration. 
  2000. if ( ! is_admin() ) { 
  2001. return false; 
  2002.  
  2003. // Add the user's fullname to Xprofile. 
  2004. if ( bp_is_active( 'xprofile' ) ) { 
  2005. $firstname = bp_get_user_meta( $user_id, 'first_name', true ); 
  2006. $lastname = ' ' . bp_get_user_meta( $user_id, 'last_name', true ); 
  2007. $name = $firstname . $lastname; 
  2008.  
  2009. if ( empty( $name ) || ' ' == $name ) { 
  2010. $name = bp_get_user_meta( $user_id, 'nickname', true ); 
  2011.  
  2012. xprofile_set_field_data( 1, $user_id, $name ); 
  2013. add_action( 'user_register', 'bp_core_map_user_registration' ); 
  2014.  
  2015. /** 
  2016. * Get the avatar storage directory for use during registration. 
  2017. * 
  2018. * @since 1.1.0 
  2019. * 
  2020. * @return string|bool Directory path on success, false on failure. 
  2021. */ 
  2022. function bp_core_signup_avatar_upload_dir() { 
  2023. $bp = buddypress(); 
  2024.  
  2025. if ( empty( $bp->signup->avatar_dir ) ) { 
  2026. return false; 
  2027.  
  2028. $directory = 'avatars/signups'; 
  2029. $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $bp->signup->avatar_dir; 
  2030. $newbdir = $path; 
  2031. $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $bp->signup->avatar_dir; 
  2032. $newburl = $newurl; 
  2033. $newsubdir = '/' . $directory . '/' . $bp->signup->avatar_dir; 
  2034.  
  2035. /** 
  2036. * Filters the avatar storage directory for use during registration. 
  2037. * 
  2038. * @since 1.1.1 
  2039. * 
  2040. * @param array $value Array of path and URL values for created storage directory. 
  2041. */ 
  2042. return apply_filters( 'bp_core_signup_avatar_upload_dir', array( 
  2043. 'path' => $path,  
  2044. 'url' => $newurl,  
  2045. 'subdir' => $newsubdir,  
  2046. 'basedir' => $newbdir,  
  2047. 'baseurl' => $newburl,  
  2048. 'error' => false 
  2049. ) ); 
  2050.  
  2051. /** 
  2052. * Send activation email to a newly registered user. 
  2053. * 
  2054. * @since 1.2.2 
  2055. * @since 2.5.0 Add the $user_login parameter. 
  2056. * 
  2057. * @param int|bool $user_id ID of the new user, false if BP_SIGNUPS_SKIP_USER_CREATION is true. 
  2058. * @param string $user_email Email address of the new user. 
  2059. * @param string $key Activation key. 
  2060. * @param string $user_login Optional. The user login name. 
  2061. */ 
  2062. function bp_core_signup_send_validation_email( $user_id, $user_email, $key, $user_login = '' ) { 
  2063. $args = array( 
  2064. 'tokens' => array( 
  2065. 'activate.url' => esc_url( trailingslashit( bp_get_activation_page() ) . "{$key}/" ),  
  2066. 'key' => $key,  
  2067. 'user.email' => $user_email,  
  2068. 'user.id' => $user_id,  
  2069. ),  
  2070. ); 
  2071.  
  2072. if ( $user_id ) { 
  2073. $to = $user_id; 
  2074. } else { 
  2075. $to = array( array( $user_email => $user_login ) ); 
  2076.  
  2077. bp_send_email( 'core-user-registration', $to, $args ); 
  2078.  
  2079. /** 
  2080. * Display a "resend email" link when an unregistered user attempts to log in. 
  2081. * 
  2082. * @since 1.2.2 
  2083. * 
  2084. * @param WP_User|WP_Error|null $user Either the WP_User or the WP_Error object. 
  2085. * @param string $username The inputted, attempted username. 
  2086. * @param string $password The inputted, attempted password. 
  2087. * @return WP_User|WP_Error 
  2088. */ 
  2089. function bp_core_signup_disable_inactive( $user = null, $username = '', $password ='' ) { 
  2090. // Login form not used. 
  2091. if ( empty( $username ) && empty( $password ) ) { 
  2092. return $user; 
  2093.  
  2094. // An existing WP_User with a user_status of 2 is either a legacy 
  2095. // signup, or is a user created for backward compatibility. See 
  2096. // {@link bp_core_signup_user()} for more details. 
  2097. if ( is_a( $user, 'WP_User' ) && 2 == $user->user_status ) { 
  2098. $user_login = $user->user_login; 
  2099.  
  2100. // If no WP_User is found corresponding to the username, this 
  2101. // is a potential signup. 
  2102. } elseif ( is_wp_error( $user ) && 'invalid_username' == $user->get_error_code() ) { 
  2103. $user_login = $username; 
  2104.  
  2105. // This is an activated user, so bail. 
  2106. } else { 
  2107. return $user; 
  2108.  
  2109. // Look for the unactivated signup corresponding to the login name. 
  2110. $signup = BP_Signup::get( array( 'user_login' => sanitize_user( $user_login ) ) ); 
  2111.  
  2112. // No signup or more than one, something is wrong. Let's bail. 
  2113. if ( empty( $signup['signups'][0] ) || $signup['total'] > 1 ) { 
  2114. return $user; 
  2115.  
  2116. // Unactivated user account found! 
  2117. // Set up the feedback message. 
  2118. $signup_id = $signup['signups'][0]->signup_id; 
  2119.  
  2120. $resend_url_params = array( 
  2121. 'action' => 'bp-resend-activation',  
  2122. 'id' => $signup_id,  
  2123. ); 
  2124.  
  2125. $resend_url = wp_nonce_url( 
  2126. add_query_arg( $resend_url_params, wp_login_url() ),  
  2127. 'bp-resend-activation' 
  2128. ); 
  2129.  
  2130. $resend_string = '<br /><br />' . sprintf( __( 'If you have not received an email yet, <a href="%s">click here to resend it</a>.', 'buddypress' ), esc_url( $resend_url ) ); 
  2131.  
  2132. return new WP_Error( 'bp_account_not_activated', __( '<strong>ERROR</strong>: Your account has not been activated. Check your email for the activation link.', 'buddypress' ) . $resend_string ); 
  2133. add_filter( 'authenticate', 'bp_core_signup_disable_inactive', 30, 3 ); 
  2134.  
  2135. /** 
  2136. * On the login screen, resends the activation email for a user. 
  2137. * 
  2138. * @since 2.0.0 
  2139. * 
  2140. * @see bp_core_signup_disable_inactive() 
  2141. */ 
  2142. function bp_members_login_resend_activation_email() { 
  2143. global $error; 
  2144.  
  2145. if ( empty( $_GET['id'] ) || empty( $_GET['_wpnonce'] ) ) { 
  2146. return; 
  2147.  
  2148. // Verify nonce. 
  2149. if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'bp-resend-activation' ) ) { 
  2150. die( 'Security check' ); 
  2151.  
  2152. $signup_id = (int) $_GET['id']; 
  2153.  
  2154. // Resend the activation email. 
  2155. // also updates the 'last sent' and '# of emails sent' values. 
  2156. $resend = BP_Signup::resend( array( $signup_id ) ); 
  2157.  
  2158. // Add feedback message. 
  2159. if ( ! empty( $resend['errors'] ) ) { 
  2160. $error = __( '<strong>ERROR</strong>: Your account has already been activated.', 'buddypress' ); 
  2161. } else { 
  2162. $error = __( 'Activation email resent! Please check your inbox or spam folder.', 'buddypress' ); 
  2163. add_action( 'login_form_bp-resend-activation', 'bp_members_login_resend_activation_email' ); 
  2164.  
  2165. /** 
  2166. * Redirect away from wp-signup.php if BP registration templates are present. 
  2167. * 
  2168. * @since 1.1.0 
  2169. */ 
  2170. function bp_core_wpsignup_redirect() { 
  2171.  
  2172. // Bail in admin or if custom signup page is broken. 
  2173. if ( is_admin() || ! bp_has_custom_signup_page() ) { 
  2174. return; 
  2175.  
  2176. $action = !empty( $_GET['action'] ) ? $_GET['action'] : ''; 
  2177.  
  2178. // Not at the WP core signup page and action is not register. 
  2179. if ( ! empty( $_SERVER['SCRIPT_NAME'] ) && false === strpos( 'wp-signup.php', $_SERVER['SCRIPT_NAME'] ) && ( 'register' != $action ) ) { 
  2180. return; 
  2181.  
  2182. bp_core_redirect( bp_get_signup_page() ); 
  2183. add_action( 'bp_init', 'bp_core_wpsignup_redirect' ); 
  2184.  
  2185. /** 
  2186. * Stop a logged-in user who is marked as a spammer. 
  2187. * 
  2188. * When an admin marks a live user as a spammer, that user can still surf 
  2189. * around and cause havoc on the site until that person is logged out. 
  2190. * 
  2191. * This code checks to see if a logged-in user is marked as a spammer. If so,  
  2192. * we redirect the user back to wp-login.php with the 'reauth' parameter. 
  2193. * 
  2194. * This clears the logged-in spammer's cookies and will ask the spammer to 
  2195. * reauthenticate. 
  2196. * 
  2197. * Note: A spammer cannot log back in - {@see bp_core_boot_spammer()}. 
  2198. * 
  2199. * Runs on 'bp_init' at priority 5 so the members component globals are setup 
  2200. * before we do our spammer checks. 
  2201. * 
  2202. * This is important as the $bp->loggedin_user object is setup at priority 4. 
  2203. * 
  2204. * @since 1.8.0 
  2205. */ 
  2206. function bp_stop_live_spammer() { 
  2207. // If we're on the login page, stop now to prevent redirect loop. 
  2208. $is_login = false; 
  2209. if ( isset( $GLOBALS['pagenow'] ) && ( false !== strpos( $GLOBALS['pagenow'], 'wp-login.php' ) ) ) { 
  2210. $is_login = true; 
  2211. } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], 'wp-login.php' ) ) { 
  2212. $is_login = true; 
  2213.  
  2214. if ( $is_login ) { 
  2215. return; 
  2216.  
  2217. // User isn't logged in, so stop! 
  2218. if ( ! is_user_logged_in() ) { 
  2219. return; 
  2220.  
  2221. // If spammer, redirect to wp-login.php and reauthorize. 
  2222. if ( bp_is_user_spammer( bp_loggedin_user_id() ) ) { 
  2223. // Setup login args. 
  2224. $args = array( 
  2225. // Custom action used to throw an error message. 
  2226. 'action' => 'bp-spam',  
  2227.  
  2228. // Reauthorize user to login. 
  2229. 'reauth' => 1 
  2230. ); 
  2231.  
  2232. /** 
  2233. * Filters the url used for redirection for a logged in user marked as spam. 
  2234. * 
  2235. * @since 1.8.0 
  2236. * 
  2237. * @param string $value URL to redirect user to. 
  2238. */ 
  2239. $login_url = apply_filters( 'bp_live_spammer_redirect', add_query_arg( $args, wp_login_url() ) ); 
  2240.  
  2241. // Redirect user to login page. 
  2242. wp_redirect( $login_url ); 
  2243. die(); 
  2244. add_action( 'bp_init', 'bp_stop_live_spammer', 5 ); 
  2245.  
  2246. /** 
  2247. * Show a custom error message when a logged-in user is marked as a spammer. 
  2248. * 
  2249. * @since 1.8.0 
  2250. */ 
  2251. function bp_live_spammer_login_error() { 
  2252. global $error; 
  2253.  
  2254. $error = __( '<strong>ERROR</strong>: Your account has been marked as a spammer.', 'buddypress' ); 
  2255.  
  2256. // Shake shake shake! 
  2257. add_action( 'login_head', 'wp_shake_js', 12 ); 
  2258. add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' ); 
  2259.  
  2260. /** 
  2261. * Get the displayed user Object 
  2262. * 
  2263. * @since 2.6.0 
  2264. * 
  2265. * @return object The displayed user object, null otherwise. 
  2266. */ 
  2267. function bp_get_displayed_user() { 
  2268. $bp = buddypress(); 
  2269.  
  2270. $displayed_user = null; 
  2271. if ( ! empty( $bp->displayed_user->id ) ) { 
  2272. $displayed_user = $bp->displayed_user; 
  2273.  
  2274. /** 
  2275. * Filters the displayed_user object corresponding to the displayed member. 
  2276. * 
  2277. * @since 2.6.0 
  2278. * 
  2279. * @param object $displayed_user The displayed_user object. 
  2280. */ 
  2281. return apply_filters( 'bp_get_displayed_user', $displayed_user ); 
  2282.  
  2283. /** Member Types *************************************************************/ 
  2284.  
  2285. /** 
  2286. * Output the slug of the member type taxonomy. 
  2287. * 
  2288. * @since 2.7.0 
  2289. */ 
  2290. function bp_member_type_tax_name() { 
  2291. echo bp_get_member_type_tax_name(); 
  2292.  
  2293. /** 
  2294. * Return the slug of the member type taxonomy. 
  2295. * 
  2296. * @since 2.7.0 
  2297. * 
  2298. * @return string The unique member taxonomy slug. 
  2299. */ 
  2300. function bp_get_member_type_tax_name() { 
  2301. /** 
  2302. * Filters the slug of the member type taxonomy. 
  2303. * 
  2304. * @since 2.7.0 
  2305. * 
  2306. * @param string $value Member type taxonomy slug. 
  2307. */ 
  2308. return apply_filters( 'bp_get_member_type_tax_name', 'bp_member_type' ); 
  2309.  
  2310. /** 
  2311. * Register a member type. 
  2312. * 
  2313. * @since 2.2.0 
  2314. * 
  2315. * @param string $member_type Unique string identifier for the member type. 
  2316. * @param array $args { 
  2317. * Array of arguments describing the member type. 
  2318. * 
  2319. * @type array $labels { 
  2320. * Array of labels to use in various parts of the interface. 
  2321. * 
  2322. * @type string $name Default name. Should typically be plural. 
  2323. * @type string $singular_name Singular name. 
  2324. * } 
  2325. * @type bool|string $has_directory Whether the member type should have its own type-specific directory. 
  2326. * Pass `true` to use the `$member_type` string as the type's slug. 
  2327. * Pass a string to customize the slug. Pass `false` to disable. 
  2328. * Default: true. 
  2329. * } 
  2330. * @return object|WP_Error Member type object on success, WP_Error object on failure. 
  2331. */ 
  2332. function bp_register_member_type( $member_type, $args = array() ) { 
  2333. $bp = buddypress(); 
  2334.  
  2335. if ( isset( $bp->members->types[ $member_type ] ) ) { 
  2336. return new WP_Error( 'bp_member_type_exists', __( 'Member type already exists.', 'buddypress' ), $member_type ); 
  2337.  
  2338. $r = bp_parse_args( $args, array( 
  2339. 'labels' => array(),  
  2340. 'has_directory' => true,  
  2341. ), 'register_member_type' ); 
  2342.  
  2343. $member_type = sanitize_key( $member_type ); 
  2344.  
  2345. /** 
  2346. * Filters the list of illegal member type names. 
  2347. * 
  2348. * - 'any' is a special pseudo-type, representing items unassociated with any member type. 
  2349. * - 'null' is a special pseudo-type, representing users without any type. 
  2350. * - '_none' is used internally to denote an item that should not apply to any member types. 
  2351. * 
  2352. * @since 2.4.0 
  2353. * 
  2354. * @param array $illegal_names Array of illegal names. 
  2355. */ 
  2356. $illegal_names = apply_filters( 'bp_member_type_illegal_names', array( 'any', 'null', '_none' ) ); 
  2357. if ( in_array( $member_type, $illegal_names, true ) ) { 
  2358. return new WP_Error( 'bp_member_type_illegal_name', __( 'You may not register a member type with this name.', 'buddypress' ), $member_type ); 
  2359.  
  2360. // Store the post type name as data in the object (not just as the array key). 
  2361. $r['name'] = $member_type; 
  2362.  
  2363. // Make sure the relevant labels have been filled in. 
  2364. $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] ); 
  2365. $r['labels'] = array_merge( array( 
  2366. 'name' => $default_name,  
  2367. 'singular_name' => $default_name,  
  2368. ), $r['labels'] ); 
  2369.  
  2370. // Directory slug. 
  2371. if ( $r['has_directory'] ) { 
  2372. // A string value is intepreted as the directory slug. Otherwise fall back on member type. 
  2373. if ( is_string( $r['has_directory'] ) ) { 
  2374. $directory_slug = $r['has_directory']; 
  2375. } else { 
  2376. $directory_slug = $member_type; 
  2377.  
  2378. // Sanitize for use in URLs. 
  2379. $r['directory_slug'] = sanitize_title( $directory_slug ); 
  2380. $r['has_directory'] = true; 
  2381. } else { 
  2382. $r['directory_slug'] = ''; 
  2383. $r['has_directory'] = false; 
  2384.  
  2385. $bp->members->types[ $member_type ] = $type = (object) $r; 
  2386.  
  2387. /** 
  2388. * Fires after a member type is registered. 
  2389. * 
  2390. * @since 2.2.0 
  2391. * 
  2392. * @param string $member_type Member type identifier. 
  2393. * @param object $type Member type object. 
  2394. */ 
  2395. do_action( 'bp_registered_member_type', $member_type, $type ); 
  2396.  
  2397. return $type; 
  2398.  
  2399. /** 
  2400. * Retrieve a member type object by name. 
  2401. * 
  2402. * @since 2.2.0 
  2403. * 
  2404. * @param string $member_type The name of the member type. 
  2405. * @return object A member type object. 
  2406. */ 
  2407. function bp_get_member_type_object( $member_type ) { 
  2408. $types = bp_get_member_types( array(), 'objects' ); 
  2409.  
  2410. if ( empty( $types[ $member_type ] ) ) { 
  2411. return null; 
  2412.  
  2413. return $types[ $member_type ]; 
  2414.  
  2415. /** 
  2416. * Get a list of all registered member type objects. 
  2417. * 
  2418. * @since 2.2.0 
  2419. * 
  2420. * @see bp_register_member_type() for accepted arguments. 
  2421. * 
  2422. * @param array|string $args Optional. An array of key => value arguments to match against 
  2423. * the member type objects. Default empty array. 
  2424. * @param string $output Optional. The type of output to return. Accepts 'names' 
  2425. * or 'objects'. Default 'names'. 
  2426. * @param string $operator Optional. The logical operation to perform. 'or' means only one 
  2427. * element from the array needs to match; 'and' means all elements 
  2428. * must match. Accepts 'or' or 'and'. Default 'and'. 
  2429. * @return array A list of member type names or objects. 
  2430. */ 
  2431. function bp_get_member_types( $args = array(), $output = 'names', $operator = 'and' ) { 
  2432. $types = buddypress()->members->types; 
  2433.  
  2434. $types = wp_filter_object_list( $types, $args, $operator ); 
  2435.  
  2436. /** 
  2437. * Filters the array of member type objects. 
  2438. * 
  2439. * This filter is run before the $output filter has been applied, so that 
  2440. * filtering functions have access to the entire member type objects. 
  2441. * 
  2442. * @since 2.2.0 
  2443. * 
  2444. * @param array $types Member type objects, keyed by name. 
  2445. * @param array $args Array of key=>value arguments for filtering. 
  2446. * @param string $operator 'or' to match any of $args, 'and' to require all. 
  2447. */ 
  2448. $types = apply_filters( 'bp_get_member_types', $types, $args, $operator ); 
  2449.  
  2450. if ( 'names' === $output ) { 
  2451. $types = wp_list_pluck( $types, 'name' ); 
  2452.  
  2453. return $types; 
  2454.  
  2455. /** 
  2456. * Set type for a member. 
  2457. * 
  2458. * @since 2.2.0 
  2459. * 
  2460. * @param int $user_id ID of the user. 
  2461. * @param string $member_type Member type. 
  2462. * @param bool $append Optional. True to append this to existing types for user,  
  2463. * false to replace. Default: false. 
  2464. * @return array $retval See {@see bp_set_object_terms()}. 
  2465. */ 
  2466. function bp_set_member_type( $user_id, $member_type, $append = false ) { 
  2467. // Pass an empty $member_type to remove a user's type. 
  2468. if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) { 
  2469. return false; 
  2470.  
  2471. $retval = bp_set_object_terms( $user_id, $member_type, bp_get_member_type_tax_name(), $append ); 
  2472.  
  2473. // Bust the cache if the type has been updated. 
  2474. if ( ! is_wp_error( $retval ) ) { 
  2475. wp_cache_delete( $user_id, 'bp_member_member_type' ); 
  2476.  
  2477. /** 
  2478. * Fires just after a user's member type has been changed. 
  2479. * 
  2480. * @since 2.2.0 
  2481. * 
  2482. * @param int $user_id ID of the user whose member type has been updated. 
  2483. * @param string $member_type Member type. 
  2484. * @param bool $append Whether the type is being appended to existing types. 
  2485. */ 
  2486. do_action( 'bp_set_member_type', $user_id, $member_type, $append ); 
  2487.  
  2488. return $retval; 
  2489.  
  2490. /** 
  2491. * Remove type for a member. 
  2492. * 
  2493. * @since 2.3.0 
  2494. * 
  2495. * @param int $user_id ID of the user. 
  2496. * @param string $member_type Member Type. 
  2497. * @return bool|WP_Error 
  2498. */ 
  2499. function bp_remove_member_type( $user_id, $member_type ) { 
  2500. // Bail if no valid member type was passed. 
  2501. if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) { 
  2502. return false; 
  2503.  
  2504. $deleted = bp_remove_object_terms( $user_id, $member_type, bp_get_member_type_tax_name() ); 
  2505.  
  2506. // Bust the cache if the type has been removed. 
  2507. if ( ! is_wp_error( $deleted ) ) { 
  2508. wp_cache_delete( $user_id, 'bp_member_member_type' ); 
  2509.  
  2510. /** 
  2511. * Fires just after a user's member type has been removed. 
  2512. * 
  2513. * @since 2.3.0 
  2514. * 
  2515. * @param int $user_id ID of the user whose member type has been updated. 
  2516. * @param string $member_type Member type. 
  2517. */ 
  2518. do_action( 'bp_remove_member_type', $user_id, $member_type ); 
  2519.  
  2520. return $deleted; 
  2521.  
  2522. /** 
  2523. * Get type for a member. 
  2524. * 
  2525. * @since 2.2.0 
  2526. * 
  2527. * @param int $user_id ID of the user. 
  2528. * @param bool $single Optional. Whether to return a single type string. If multiple types are found 
  2529. * for the user, the oldest one will be returned. Default: true. 
  2530. * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member 
  2531. * types (if $single is false). Returns false on failure. 
  2532. */ 
  2533. function bp_get_member_type( $user_id, $single = true ) { 
  2534. $types = wp_cache_get( $user_id, 'bp_member_member_type' ); 
  2535.  
  2536. if ( false === $types ) { 
  2537. $raw_types = bp_get_object_terms( $user_id, bp_get_member_type_tax_name() ); 
  2538.  
  2539. if ( ! is_wp_error( $raw_types ) ) { 
  2540. $types = array(); 
  2541.  
  2542. // Only include currently registered group types. 
  2543. foreach ( $raw_types as $mtype ) { 
  2544. if ( bp_get_member_type_object( $mtype->name ) ) { 
  2545. $types[] = $mtype->name; 
  2546.  
  2547. wp_cache_set( $user_id, $types, 'bp_member_member_type' ); 
  2548.  
  2549. $type = false; 
  2550. if ( ! empty( $types ) ) { 
  2551. if ( $single ) { 
  2552. $type = array_pop( $types ); 
  2553. } else { 
  2554. $type = $types; 
  2555.  
  2556. /** 
  2557. * Filters a user's member type(s). 
  2558. * 
  2559. * @since 2.2.0 
  2560. * 
  2561. * @param string $type Member type. 
  2562. * @param int $user_id ID of the user. 
  2563. * @param bool $single Whether to return a single type string, or an array. 
  2564. */ 
  2565. return apply_filters( 'bp_get_member_type', $type, $user_id, $single ); 
  2566.  
  2567. /** 
  2568. * Check whether the given user has a certain member type. 
  2569. * 
  2570. * @since 2.3.0 
  2571. * 
  2572. * @param int $user_id $user_id ID of the user. 
  2573. * @param string $member_type Member Type. 
  2574. * @return bool Whether the user has the given member type. 
  2575. */ 
  2576. function bp_has_member_type( $user_id, $member_type ) { 
  2577. // Bail if no valid member type was passed. 
  2578. if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) { 
  2579. return false; 
  2580.  
  2581. // Get all user's member types. 
  2582. $types = bp_get_member_type( $user_id, false ); 
  2583.  
  2584. if ( ! is_array( $types ) ) { 
  2585. return false; 
  2586.  
  2587. return in_array( $member_type, $types ); 
  2588.  
  2589. /** 
  2590. * Delete a user's member type when the user when the user is deleted. 
  2591. * 
  2592. * @since 2.2.0 
  2593. * 
  2594. * @param int $user_id ID of the user. 
  2595. * @return array $value See {@see bp_set_member_type()}. 
  2596. */ 
  2597. function bp_remove_member_type_on_user_delete( $user_id ) { 
  2598. return bp_set_member_type( $user_id, '' ); 
  2599. add_action( 'wpmu_delete_user', 'bp_remove_member_type_on_user_delete' ); 
  2600. add_action( 'delete_user', 'bp_remove_member_type_on_user_delete' ); 
  2601.  
  2602. /** 
  2603. * Get the "current" member type, if one is provided, in member directories. 
  2604. * 
  2605. * @since 2.3.0 
  2606. * 
  2607. * @return string 
  2608. */ 
  2609. function bp_get_current_member_type() { 
  2610.  
  2611. /** 
  2612. * Filters the "current" member type, if one is provided, in member directories. 
  2613. * 
  2614. * @since 2.3.0 
  2615. * 
  2616. * @param string $value "Current" member type. 
  2617. */ 
  2618. return apply_filters( 'bp_get_current_member_type', buddypress()->current_member_type ); 
.