/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 . '">' . $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 ); 
  1059. remove_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10 ); 
  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. $status = false; 
  1346.  
  1347. if ( bp_is_my_profile() ) { 
  1348. $status = true; 
  1349. } elseif ( is_super_admin( bp_displayed_user_id() ) && ! is_super_admin() ) { 
  1350. $status = false; 
  1351. } elseif ( bp_current_user_can( 'bp_moderate' ) || current_user_can( 'edit_users' ) ) { 
  1352. $status = true; 
  1353.  
  1354. /** 
  1355. * Filters the status of whether the logged-in user can edit settings for the displayed user or not. 
  1356. * 
  1357. * @since 2.8.0 
  1358. * 
  1359. * @param bool True if editing is allowed, otherwise false. 
  1360. */ 
  1361. return apply_filters( 'bp_core_can_edit_settings', $status ); 
  1362.  
  1363. /** Sign-up *******************************************************************/ 
  1364.  
  1365. /** 
  1366. * Flush illegal names by getting and setting 'illegal_names' site option. 
  1367. * 
  1368. * @since 1.2.5 
  1369. */ 
  1370. function bp_core_flush_illegal_names() { 
  1371. $illegal_names = get_site_option( 'illegal_names' ); 
  1372. update_site_option( 'illegal_names', $illegal_names ); 
  1373.  
  1374. /** 
  1375. * Add BuddyPress-specific items to the illegal_names array. 
  1376. * 
  1377. * @since 1.2.7 
  1378. * 
  1379. * @param array|string $value Illegal names as being saved defined in 
  1380. * Multisite settings. 
  1381. * @param array|string $oldvalue The old value of the option. 
  1382. * @return array Merged and unique array of illegal names. 
  1383. */ 
  1384. function bp_core_get_illegal_names( $value = '', $oldvalue = '' ) { 
  1385.  
  1386. // Make sure $value is array. 
  1387. if ( empty( $value ) ) { 
  1388. $db_illegal_names = array(); 
  1389.  
  1390. if ( is_array( $value ) ) { 
  1391. $db_illegal_names = $value; 
  1392. } elseif ( is_string( $value ) ) { 
  1393. $db_illegal_names = explode( ' ', $value ); 
  1394.  
  1395. // Add the core components' slugs to the banned list even if their components aren't active. 
  1396. $bp_component_slugs = array( 
  1397. 'groups',  
  1398. 'members',  
  1399. 'forums',  
  1400. 'blogs',  
  1401. 'activity',  
  1402. 'profile',  
  1403. 'friends',  
  1404. 'search',  
  1405. 'settings',  
  1406. 'notifications',  
  1407. 'register',  
  1408. 'activate' 
  1409. ); 
  1410.  
  1411. // Core constants. 
  1412. $slug_constants = array( 
  1413. 'BP_GROUPS_SLUG',  
  1414. 'BP_MEMBERS_SLUG',  
  1415. 'BP_FORUMS_SLUG',  
  1416. 'BP_BLOGS_SLUG',  
  1417. 'BP_ACTIVITY_SLUG',  
  1418. 'BP_XPROFILE_SLUG',  
  1419. 'BP_FRIENDS_SLUG',  
  1420. 'BP_SEARCH_SLUG',  
  1421. 'BP_SETTINGS_SLUG',  
  1422. 'BP_NOTIFICATIONS_SLUG',  
  1423. 'BP_REGISTER_SLUG',  
  1424. 'BP_ACTIVATION_SLUG',  
  1425. ); 
  1426. foreach( $slug_constants as $constant ) { 
  1427. if ( defined( $constant ) ) { 
  1428. $bp_component_slugs[] = constant( $constant ); 
  1429.  
  1430. /** 
  1431. * Filters the array of default illegal usernames. 
  1432. * 
  1433. * @since 1.2.2 
  1434. * 
  1435. * @param array $value Merged and unique array of illegal usernames. 
  1436. */ 
  1437. $filtered_illegal_names = apply_filters( 'bp_core_illegal_usernames', array_merge( array( 'www', 'web', 'root', 'admin', 'main', 'invite', 'administrator' ), $bp_component_slugs ) ); 
  1438.  
  1439. // Merge the arrays together. 
  1440. $merged_names = array_merge( (array) $filtered_illegal_names, (array) $db_illegal_names ); 
  1441.  
  1442. // Remove duplicates. 
  1443. $illegal_names = array_unique( (array) $merged_names ); 
  1444.  
  1445. /** 
  1446. * Filters the array of default illegal names. 
  1447. * 
  1448. * @since 1.2.5 
  1449. * 
  1450. * @param array $value Merged and unique array of illegal names. 
  1451. */ 
  1452. return apply_filters( 'bp_core_illegal_names', $illegal_names ); 
  1453. add_filter( 'pre_update_site_option_illegal_names', 'bp_core_get_illegal_names', 10, 2 ); 
  1454.  
  1455. /** 
  1456. * Check that an email address is valid for use. 
  1457. * 
  1458. * Performs the following checks: 
  1459. * - Is the email address well-formed? 
  1460. * - Is the email address already used? 
  1461. * - If there's an email domain blacklist, is the current domain on it? 
  1462. * - If there's an email domain whitelest, is the current domain on it? 
  1463. * 
  1464. * @since 1.6.2 
  1465. * 
  1466. * @param string $user_email The email being checked. 
  1467. * @return bool|array True if the address passes all checks; otherwise an array 
  1468. * of error codes. 
  1469. */ 
  1470. function bp_core_validate_email_address( $user_email ) { 
  1471. $errors = array(); 
  1472.  
  1473. $user_email = sanitize_email( $user_email ); 
  1474.  
  1475. // Is the email well-formed? 
  1476. if ( ! is_email( $user_email ) ) { 
  1477. $errors['invalid'] = 1; 
  1478.  
  1479. // Is the email on the Banned Email Domains list? 
  1480. // Note: This check only works on Multisite. 
  1481. if ( function_exists( 'is_email_address_unsafe' ) && is_email_address_unsafe( $user_email ) ) { 
  1482. $errors['domain_banned'] = 1; 
  1483.  
  1484. // Is the email on the Limited Email Domains list? 
  1485. // Note: This check only works on Multisite. 
  1486. $limited_email_domains = get_site_option( 'limited_email_domains' ); 
  1487. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) { 
  1488. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) ); 
  1489. if ( ! in_array( $emaildomain, $limited_email_domains ) ) { 
  1490. $errors['domain_not_allowed'] = 1; 
  1491.  
  1492. // Is the email alreday in use? 
  1493. if ( email_exists( $user_email ) ) { 
  1494. $errors['in_use'] = 1; 
  1495.  
  1496. $retval = ! empty( $errors ) ? $errors : true; 
  1497.  
  1498. return $retval; 
  1499.  
  1500. /** 
  1501. * Add the appropriate errors to a WP_Error object, given results of a validation test. 
  1502. * 
  1503. * Functions like bp_core_validate_email_address() return a structured array 
  1504. * of error codes. bp_core_add_validation_error_messages() takes this array and 
  1505. * parses, adding the appropriate error messages to the WP_Error object. 
  1506. * 
  1507. * @since 1.7.0 
  1508. * 
  1509. * @see bp_core_validate_email_address() 
  1510. * 
  1511. * @param WP_Error $errors WP_Error object. 
  1512. * @param array $validation_results The return value of a validation function 
  1513. * like bp_core_validate_email_address(). 
  1514. */ 
  1515. function bp_core_add_validation_error_messages( WP_Error $errors, $validation_results ) { 
  1516. if ( ! empty( $validation_results['invalid'] ) ) { 
  1517. $errors->add( 'user_email', __( 'Please check your email address.', 'buddypress' ) ); 
  1518.  
  1519. if ( ! empty( $validation_results['domain_banned'] ) ) { 
  1520. $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) ); 
  1521.  
  1522. if ( ! empty( $validation_results['domain_not_allowed'] ) ) { 
  1523. $errors->add( 'user_email', __( 'Sorry, that email address is not allowed!', 'buddypress' ) ); 
  1524.  
  1525. if ( ! empty( $validation_results['in_use'] ) ) { 
  1526. $errors->add( 'user_email', __( 'Sorry, that email address is already used!', 'buddypress' ) ); 
  1527.  
  1528. /** 
  1529. * Validate a user name and email address when creating a new user. 
  1530. * 
  1531. * @since 1.2.2 
  1532. * 
  1533. * @param string $user_name Username to validate. 
  1534. * @param string $user_email Email address to validate. 
  1535. * @return array Results of user validation including errors, if any. 
  1536. */ 
  1537. function bp_core_validate_user_signup( $user_name, $user_email ) { 
  1538.  
  1539. // Make sure illegal names include BuddyPress slugs and values. 
  1540. bp_core_flush_illegal_names(); 
  1541.  
  1542. // WordPress Multisite has its own validation. Use it, so that we 
  1543. // properly mirror restrictions on username, etc. 
  1544. if ( function_exists( 'wpmu_validate_user_signup' ) ) { 
  1545. $result = wpmu_validate_user_signup( $user_name, $user_email ); 
  1546.  
  1547. // When not running Multisite, we perform our own validation. What 
  1548. // follows reproduces much of the logic of wpmu_validate_user_signup(),  
  1549. // minus the multisite-specific restrictions on user_login. 
  1550. } else { 
  1551. $errors = new WP_Error(); 
  1552.  
  1553. /** 
  1554. * Filters the username before being validated. 
  1555. * 
  1556. * @since 1.5.5 
  1557. * 
  1558. * @param string $user_name Username to validate. 
  1559. */ 
  1560. $user_name = apply_filters( 'pre_user_login', $user_name ); 
  1561.  
  1562. // User name can't be empty. 
  1563. if ( empty( $user_name ) ) { 
  1564. $errors->add( 'user_name', __( 'Please enter a username', 'buddypress' ) ); 
  1565.  
  1566. // User name can't be on the blacklist. 
  1567. $illegal_names = get_site_option( 'illegal_names' ); 
  1568. if ( in_array( $user_name, (array) $illegal_names ) ) { 
  1569. $errors->add( 'user_name', __( 'That username is not allowed', 'buddypress' ) ); 
  1570.  
  1571. // User name must pass WP's validity check. 
  1572. if ( ! validate_username( $user_name ) ) { 
  1573. $errors->add( 'user_name', __( 'Usernames can contain only letters, numbers, ., -, and @', 'buddypress' ) ); 
  1574.  
  1575. // Minimum of 4 characters. 
  1576. if ( strlen( $user_name ) < 4 ) { 
  1577. $errors->add( 'user_name', __( 'Username must be at least 4 characters', 'buddypress' ) ); 
  1578.  
  1579. // No underscores. @todo Why not? 
  1580. if ( false !== strpos( ' ' . $user_name, '_' ) ) { 
  1581. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character "_"!', 'buddypress' ) ); 
  1582.  
  1583. // No usernames that are all numeric. @todo Why? 
  1584. $match = array(); 
  1585. preg_match( '/[0-9]*/', $user_name, $match ); 
  1586. if ( $match[0] == $user_name ) { 
  1587. $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', 'buddypress' ) ); 
  1588.  
  1589. // Check into signups. 
  1590. $signups = BP_Signup::get( array( 
  1591. 'user_login' => $user_name,  
  1592. ) ); 
  1593.  
  1594. $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false; 
  1595.  
  1596. // Check if the username has been used already. 
  1597. if ( username_exists( $user_name ) || ! empty( $signup ) ) { 
  1598. $errors->add( 'user_name', __( 'Sorry, that username already exists!', 'buddypress' ) ); 
  1599.  
  1600. // Validate the email address and process the validation results into 
  1601. // error messages. 
  1602. $validate_email = bp_core_validate_email_address( $user_email ); 
  1603. bp_core_add_validation_error_messages( $errors, $validate_email ); 
  1604.  
  1605. // Assemble the return array. 
  1606. $result = array( 
  1607. 'user_name' => $user_name,  
  1608. 'user_email' => $user_email,  
  1609. 'errors' => $errors,  
  1610. ); 
  1611.  
  1612. // Apply WPMU legacy filter. 
  1613. $result = apply_filters( 'wpmu_validate_user_signup', $result ); 
  1614.  
  1615. /** 
  1616. * Filters the result of the user signup validation. 
  1617. * 
  1618. * @since 1.2.2 
  1619. * 
  1620. * @param array $result Results of user validation including errors, if any. 
  1621. */ 
  1622. return apply_filters( 'bp_core_validate_user_signup', $result ); 
  1623.  
  1624. /** 
  1625. * Validate blog URL and title provided at signup. 
  1626. * 
  1627. * @since 1.2.2 
  1628. * 
  1629. * @todo Why do we have this wrapper? 
  1630. * 
  1631. * @param string $blog_url Blog URL requested during registration. 
  1632. * @param string $blog_title Blog title requested during registration. 
  1633. * @return array 
  1634. */ 
  1635. function bp_core_validate_blog_signup( $blog_url, $blog_title ) { 
  1636. if ( ! is_multisite() || ! function_exists( 'wpmu_validate_blog_signup' ) ) { 
  1637. return false; 
  1638.  
  1639. /** 
  1640. * Filters the validated blog url and title provided at signup. 
  1641. * 
  1642. * @since 1.2.2 
  1643. * 
  1644. * @param array $value Array with the new site data and error messages. 
  1645. */ 
  1646. return apply_filters( 'bp_core_validate_blog_signup', wpmu_validate_blog_signup( $blog_url, $blog_title ) ); 
  1647.  
  1648. /** 
  1649. * Process data submitted at user registration and convert to a signup object. 
  1650. * 
  1651. * @since 1.2.0 
  1652. * 
  1653. * @todo There appears to be a bug in the return value on success. 
  1654. * 
  1655. * @param string $user_login Login name requested by the user. 
  1656. * @param string $user_password Password requested by the user. 
  1657. * @param string $user_email Email address entered by the user. 
  1658. * @param array $usermeta Miscellaneous metadata about the user (blog-specific 
  1659. * signup data, xprofile data, etc). 
  1660. * @return bool|WP_Error True on success, WP_Error on failure. 
  1661. */ 
  1662. function bp_core_signup_user( $user_login, $user_password, $user_email, $usermeta ) { 
  1663. $bp = buddypress(); 
  1664.  
  1665. // We need to cast $user_id to pass to the filters. 
  1666. $user_id = false; 
  1667.  
  1668. // Multisite installs have their own install procedure. 
  1669. if ( is_multisite() ) { 
  1670. wpmu_signup_user( $user_login, $user_email, $usermeta ); 
  1671.  
  1672. } else { 
  1673. // Format data. 
  1674. $user_login = preg_replace( '/\s+/', '', sanitize_user( $user_login, true ) ); 
  1675. $user_email = sanitize_email( $user_email ); 
  1676. $activation_key = wp_generate_password( 32, false ); 
  1677.  
  1678. /** 
  1679. * WordPress's default behavior is to create user accounts 
  1680. * immediately at registration time. BuddyPress uses a system 
  1681. * borrowed from WordPress Multisite, where signups are stored 
  1682. * separately and accounts are only created at the time of 
  1683. * activation. For backward compatibility with plugins that may 
  1684. * be anticipating WP's default behavior, BP silently creates 
  1685. * accounts for registrations (though it does not use them). If 
  1686. * you know that you are not running any plugins dependent on 
  1687. * these pending accounts, you may want to save a little DB 
  1688. * clutter by defining setting the BP_SIGNUPS_SKIP_USER_CREATION 
  1689. * to true in your wp-config.php file. 
  1690. */ 
  1691. if ( ! defined( 'BP_SIGNUPS_SKIP_USER_CREATION' ) || ! BP_SIGNUPS_SKIP_USER_CREATION ) { 
  1692. $user_id = BP_Signup::add_backcompat( $user_login, $user_password, $user_email, $usermeta ); 
  1693.  
  1694. if ( is_wp_error( $user_id ) ) { 
  1695. return $user_id; 
  1696.  
  1697. bp_update_user_meta( $user_id, 'activation_key', $activation_key ); 
  1698.  
  1699. $args = array( 
  1700. 'user_login' => $user_login,  
  1701. 'user_email' => $user_email,  
  1702. 'activation_key' => $activation_key,  
  1703. 'meta' => $usermeta,  
  1704. ); 
  1705.  
  1706. BP_Signup::add( $args ); 
  1707.  
  1708. /** 
  1709. * Filters if BuddyPress should send an activation key for a new signup. 
  1710. * 
  1711. * @since 1.2.3 
  1712. * 
  1713. * @param bool $value Whether or not to send the activation key. 
  1714. * @param int $user_id User ID to send activation key to. 
  1715. * @param string $user_email User email to send activation key to. 
  1716. * @param string $activation_key Activation key to be sent. 
  1717. * @param array $usermeta Miscellaneous metadata about the user (blog-specific 
  1718. * signup data, xprofile data, etc). 
  1719. */ 
  1720. if ( apply_filters( 'bp_core_signup_send_activation_key', true, $user_id, $user_email, $activation_key, $usermeta ) ) { 
  1721. bp_core_signup_send_validation_email( $user_id, $user_email, $activation_key, $user_login ); 
  1722.  
  1723. $bp->signup->username = $user_login; 
  1724.  
  1725. /** 
  1726. * Fires at the end of the process to sign up a user. 
  1727. * 
  1728. * @since 1.2.2 
  1729. * 
  1730. * @param bool|WP_Error $user_id True on success, WP_Error on failure. 
  1731. * @param string $user_login Login name requested by the user. 
  1732. * @param string $user_password Password requested by the user. 
  1733. * @param string $user_email Email address requested by the user. 
  1734. * @param array $usermeta Miscellaneous metadata about the user (blog-specific 
  1735. * signup data, xprofile data, etc). 
  1736. */ 
  1737. do_action( 'bp_core_signup_user', $user_id, $user_login, $user_password, $user_email, $usermeta ); 
  1738.  
  1739. return $user_id; 
  1740.  
  1741. /** 
  1742. * Create a blog and user based on data supplied at user registration. 
  1743. * 
  1744. * @since 1.2.2 
  1745. * 
  1746. * @param string $blog_domain Domain requested by user. 
  1747. * @param string $blog_path Path requested by user. 
  1748. * @param string $blog_title Title as entered by user. 
  1749. * @param string $user_name user_login of requesting user. 
  1750. * @param string $user_email Email address of requesting user. 
  1751. * @param string $usermeta Miscellaneous metadata for the user. 
  1752. * @return bool 
  1753. */ 
  1754. function bp_core_signup_blog( $blog_domain, $blog_path, $blog_title, $user_name, $user_email, $usermeta ) { 
  1755. if ( ! is_multisite() || ! function_exists( 'wpmu_signup_blog' ) ) { 
  1756. return false; 
  1757.  
  1758. /** 
  1759. * Filters the result of wpmu_signup_blog(). 
  1760. * 
  1761. * This filter provides no value and is retained for 
  1762. * backwards compatibility. 
  1763. * 
  1764. * @since 1.2.2 
  1765. * 
  1766. * @param void $value 
  1767. */ 
  1768. return apply_filters( 'bp_core_signup_blog', wpmu_signup_blog( $blog_domain, $blog_path, $blog_title, $user_name, $user_email, $usermeta ) ); 
  1769.  
  1770. /** 
  1771. * Activate a signup, as identified by an activation key. 
  1772. * 
  1773. * @since 1.2.2 
  1774. * 
  1775. * @param string $key Activation key. 
  1776. * @return int|bool User ID on success, false on failure. 
  1777. */ 
  1778. function bp_core_activate_signup( $key ) { 
  1779. global $wpdb; 
  1780.  
  1781. $user = false; 
  1782.  
  1783. // Multisite installs have their own activation routine. 
  1784. if ( is_multisite() ) { 
  1785. $user = wpmu_activate_signup( $key ); 
  1786.  
  1787. // If there were errors, add a message and redirect. 
  1788. if ( ! empty( $user->errors ) ) { 
  1789. return $user; 
  1790.  
  1791. $user_id = $user['user_id']; 
  1792.  
  1793. } else { 
  1794. $signups = BP_Signup::get( array( 
  1795. 'activation_key' => $key,  
  1796. ) ); 
  1797.  
  1798. if ( empty( $signups['signups'] ) ) { 
  1799. return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) ); 
  1800.  
  1801. $signup = $signups['signups'][0]; 
  1802.  
  1803. if ( $signup->active ) { 
  1804. if ( empty( $signup->domain ) ) { 
  1805. return new WP_Error( 'already_active', __( 'The user is already active.', 'buddypress' ), $signup ); 
  1806. } else { 
  1807. return new WP_Error( 'already_active', __( 'The site is already active.', 'buddypress' ), $signup ); 
  1808.  
  1809. // Password is hashed again in wp_insert_user. 
  1810. $password = wp_generate_password( 12, false ); 
  1811.  
  1812. $user_id = username_exists( $signup->user_login ); 
  1813.  
  1814. // Create the user. This should only be necessary if BP_SIGNUPS_SKIP_USER_CREATION is true. 
  1815. if ( ! $user_id ) { 
  1816. $user_id = wp_create_user( $signup->user_login, $password, $signup->user_email ); 
  1817.  
  1818. // Otherwise, update the existing user's status. 
  1819. } elseif ( $key === bp_get_user_meta( $user_id, 'activation_key', true ) || $key === wp_hash( $user_id ) ) { 
  1820.  
  1821. // Change the user's status so they become active. 
  1822. if ( ! $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_status = 0 WHERE ID = %d", $user_id ) ) ) { 
  1823. return new WP_Error( 'invalid_key', __( 'Invalid activation key.', 'buddypress' ) ); 
  1824.  
  1825. bp_delete_user_meta( $user_id, 'activation_key' ); 
  1826.  
  1827. $member = get_userdata( $user_id ); 
  1828. $member->set_role( get_option('default_role') ); 
  1829.  
  1830. $user_already_created = true; 
  1831.  
  1832. } else { 
  1833. $user_already_exists = true; 
  1834.  
  1835. if ( ! $user_id ) { 
  1836. return new WP_Error( 'create_user', __( 'Could not create user', 'buddypress' ), $signup ); 
  1837.  
  1838. // Fetch the signup so we have the data later on. 
  1839. $signups = BP_Signup::get( array( 
  1840. 'activation_key' => $key,  
  1841. ) ); 
  1842.  
  1843. $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false; 
  1844.  
  1845. // Activate the signup. 
  1846. BP_Signup::validate( $key ); 
  1847.  
  1848. if ( isset( $user_already_exists ) ) { 
  1849. return new WP_Error( 'user_already_exists', __( 'That username is already activated.', 'buddypress' ), $signup ); 
  1850.  
  1851. // Set up data to pass to the legacy filter. 
  1852. $user = array( 
  1853. 'user_id' => $user_id,  
  1854. 'password' => $signup->meta['password'],  
  1855. 'meta' => $signup->meta,  
  1856. ); 
  1857.  
  1858. // Notify the site admin of a new user registration. 
  1859. wp_new_user_notification( $user_id ); 
  1860.  
  1861. if ( isset( $user_already_created ) ) { 
  1862.  
  1863. /** 
  1864. * Fires if the user has already been created. 
  1865. * 
  1866. * @since 1.2.2 
  1867. * 
  1868. * @param int $user_id ID of the user being checked. 
  1869. * @param string $key Activation key. 
  1870. * @param array $user Array of user data. 
  1871. */ 
  1872. do_action( 'bp_core_activated_user', $user_id, $key, $user ); 
  1873. return $user_id; 
  1874.  
  1875. // Set any profile data. 
  1876. if ( bp_is_active( 'xprofile' ) ) { 
  1877. if ( ! empty( $user['meta']['profile_field_ids'] ) ) { 
  1878. $profile_field_ids = explode( ', ', $user['meta']['profile_field_ids'] ); 
  1879.  
  1880. foreach( (array) $profile_field_ids as $field_id ) { 
  1881. $current_field = isset( $user['meta']["field_{$field_id}"] ) ? $user['meta']["field_{$field_id}"] : false; 
  1882.  
  1883. if ( !empty( $current_field ) ) { 
  1884. xprofile_set_field_data( $field_id, $user_id, $current_field ); 
  1885.  
  1886. // Save the visibility level. 
  1887. $visibility_level = ! empty( $user['meta']['field_' . $field_id . '_visibility'] ) ? $user['meta']['field_' . $field_id . '_visibility'] : 'public'; 
  1888. xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level ); 
  1889.  
  1890. // Replace the password automatically generated by WordPress by the one the user chose. 
  1891. if ( ! empty( $user['meta']['password'] ) ) { 
  1892. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->users} SET user_pass = %s WHERE ID = %d", $user['meta']['password'], $user_id ) ); 
  1893.  
  1894. /** 
  1895. * Make sure to clean the user's cache as we've 
  1896. * directly edited the password without using 
  1897. * wp_update_user(). 
  1898. * 
  1899. * If we can't use wp_update_user() that's because 
  1900. * we already hashed the password at the signup step. 
  1901. */ 
  1902. $uc = wp_cache_get( $user_id, 'users' ); 
  1903.  
  1904. if ( ! empty( $uc->ID ) ) { 
  1905. clean_user_cache( $uc->ID ); 
  1906.  
  1907. /** 
  1908. * Fires at the end of the user activation process. 
  1909. * 
  1910. * @since 1.2.2 
  1911. * 
  1912. * @param int $user_id ID of the user being checked. 
  1913. * @param string $key Activation key. 
  1914. * @param array $user Array of user data. 
  1915. */ 
  1916. do_action( 'bp_core_activated_user', $user_id, $key, $user ); 
  1917.  
  1918. return $user_id; 
  1919.  
  1920. /** 
  1921. * Migrate signups from pre-2.0 configuration to wp_signups. 
  1922. * 
  1923. * @since 2.0.1 
  1924. */ 
  1925. function bp_members_migrate_signups() { 
  1926. global $wpdb; 
  1927.  
  1928. $status_2_ids = $wpdb->get_col( "SELECT ID FROM {$wpdb->users} WHERE user_status = '2'" ); 
  1929.  
  1930. if ( ! empty( $status_2_ids ) ) { 
  1931. $signups = get_users( array( 
  1932. 'fields' => array( 
  1933. 'ID',  
  1934. 'user_login',  
  1935. 'user_pass',  
  1936. 'user_registered',  
  1937. 'user_email',  
  1938. 'display_name',  
  1939. ),  
  1940. 'include' => $status_2_ids,  
  1941. ) ); 
  1942.  
  1943. // Fetch activation keys separately, to avoid the all_with_meta 
  1944. // overhead. 
  1945. $status_2_ids_sql = implode( ', ', $status_2_ids ); 
  1946. $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})" ); 
  1947.  
  1948. // Rekey. 
  1949. $activation_keys = array(); 
  1950. foreach ( $ak_data as $ak_datum ) { 
  1951. $activation_keys[ intval( $ak_datum->user_id ) ] = $ak_datum->meta_value; 
  1952.  
  1953. unset( $status_2_ids_sql, $status_2_ids, $ak_data ); 
  1954.  
  1955. // Merge. 
  1956. foreach ( $signups as &$signup ) { 
  1957. if ( isset( $activation_keys[ $signup->ID ] ) ) { 
  1958. $signup->activation_key = $activation_keys[ $signup->ID ]; 
  1959.  
  1960. // Reset the signup var as we're using it to process the migration. 
  1961. unset( $signup ); 
  1962.  
  1963. } else { 
  1964. return; 
  1965.  
  1966. foreach ( $signups as $signup ) { 
  1967. $meta = array(); 
  1968.  
  1969. // Rebuild the activation key, if missing. 
  1970. if ( empty( $signup->activation_key ) ) { 
  1971. $signup->activation_key = wp_generate_password( 32, false ); 
  1972.  
  1973. if ( bp_is_active( 'xprofile' ) ) { 
  1974. $meta['field_1'] = $signup->display_name; 
  1975.  
  1976. $meta['password'] = $signup->user_pass; 
  1977.  
  1978. $user_login = preg_replace( '/\s+/', '', sanitize_user( $signup->user_login, true ) ); 
  1979. $user_email = sanitize_email( $signup->user_email ); 
  1980.  
  1981. BP_Signup::add( array( 
  1982. 'user_login' => $user_login,  
  1983. 'user_email' => $user_email,  
  1984. 'registered' => $signup->user_registered,  
  1985. 'activation_key' => $signup->activation_key,  
  1986. 'meta' => $meta 
  1987. ) ); 
  1988.  
  1989. // Deleting these options will remove signups from users count. 
  1990. delete_user_option( $signup->ID, 'capabilities' ); 
  1991. delete_user_option( $signup->ID, 'user_level' ); 
  1992.  
  1993. /** 
  1994. * Map a user's WP display name to the XProfile fullname field, if necessary. 
  1995. * 
  1996. * This only happens when a user is registered in wp-admin by an administrator; 
  1997. * during normal registration, XProfile data is provided directly by the user. 
  1998. * 
  1999. * @since 1.2.0 
  2000. * 
  2001. * @param int $user_id ID of the user. 
  2002. * @return bool 
  2003. */ 
  2004. function bp_core_map_user_registration( $user_id ) { 
  2005.  
  2006. // Only map data when the site admin is adding users, not on registration. 
  2007. if ( ! is_admin() ) { 
  2008. return false; 
  2009.  
  2010. // Add the user's fullname to Xprofile. 
  2011. if ( bp_is_active( 'xprofile' ) ) { 
  2012. $firstname = bp_get_user_meta( $user_id, 'first_name', true ); 
  2013. $lastname = ' ' . bp_get_user_meta( $user_id, 'last_name', true ); 
  2014. $name = $firstname . $lastname; 
  2015.  
  2016. if ( empty( $name ) || ' ' == $name ) { 
  2017. $name = bp_get_user_meta( $user_id, 'nickname', true ); 
  2018.  
  2019. xprofile_set_field_data( 1, $user_id, $name ); 
  2020. add_action( 'user_register', 'bp_core_map_user_registration' ); 
  2021.  
  2022. /** 
  2023. * Get the avatar storage directory for use during registration. 
  2024. * 
  2025. * @since 1.1.0 
  2026. * 
  2027. * @return string|bool Directory path on success, false on failure. 
  2028. */ 
  2029. function bp_core_signup_avatar_upload_dir() { 
  2030. $bp = buddypress(); 
  2031.  
  2032. if ( empty( $bp->signup->avatar_dir ) ) { 
  2033. return false; 
  2034.  
  2035. $directory = 'avatars/signups'; 
  2036. $path = bp_core_avatar_upload_path() . '/' . $directory . '/' . $bp->signup->avatar_dir; 
  2037. $newbdir = $path; 
  2038. $newurl = bp_core_avatar_url() . '/' . $directory . '/' . $bp->signup->avatar_dir; 
  2039. $newburl = $newurl; 
  2040. $newsubdir = '/' . $directory . '/' . $bp->signup->avatar_dir; 
  2041.  
  2042. /** 
  2043. * Filters the avatar storage directory for use during registration. 
  2044. * 
  2045. * @since 1.1.1 
  2046. * 
  2047. * @param array $value Array of path and URL values for created storage directory. 
  2048. */ 
  2049. return apply_filters( 'bp_core_signup_avatar_upload_dir', array( 
  2050. 'path' => $path,  
  2051. 'url' => $newurl,  
  2052. 'subdir' => $newsubdir,  
  2053. 'basedir' => $newbdir,  
  2054. 'baseurl' => $newburl,  
  2055. 'error' => false 
  2056. ) ); 
  2057.  
  2058. /** 
  2059. * Send activation email to a newly registered user. 
  2060. * 
  2061. * @since 1.2.2 
  2062. * @since 2.5.0 Add the $user_login parameter. 
  2063. * 
  2064. * @param int|bool $user_id ID of the new user, false if BP_SIGNUPS_SKIP_USER_CREATION is true. 
  2065. * @param string $user_email Email address of the new user. 
  2066. * @param string $key Activation key. 
  2067. * @param string $user_login Optional. The user login name. 
  2068. */ 
  2069. function bp_core_signup_send_validation_email( $user_id, $user_email, $key, $user_login = '' ) { 
  2070. $args = array( 
  2071. 'tokens' => array( 
  2072. 'activate.url' => esc_url( trailingslashit( bp_get_activation_page() ) . "{$key}/" ),  
  2073. 'key' => $key,  
  2074. 'user.email' => $user_email,  
  2075. 'user.id' => $user_id,  
  2076. ),  
  2077. ); 
  2078.  
  2079. if ( $user_id ) { 
  2080. $to = $user_id; 
  2081. } else { 
  2082. $to = array( array( $user_email => $user_login ) ); 
  2083.  
  2084. bp_send_email( 'core-user-registration', $to, $args ); 
  2085.  
  2086. /** 
  2087. * Display a "resend email" link when an unregistered user attempts to log in. 
  2088. * 
  2089. * @since 1.2.2 
  2090. * 
  2091. * @param WP_User|WP_Error|null $user Either the WP_User or the WP_Error object. 
  2092. * @param string $username The inputted, attempted username. 
  2093. * @param string $password The inputted, attempted password. 
  2094. * @return WP_User|WP_Error 
  2095. */ 
  2096. function bp_core_signup_disable_inactive( $user = null, $username = '', $password ='' ) { 
  2097. // Login form not used. 
  2098. if ( empty( $username ) && empty( $password ) ) { 
  2099. return $user; 
  2100.  
  2101. // An existing WP_User with a user_status of 2 is either a legacy 
  2102. // signup, or is a user created for backward compatibility. See 
  2103. // {@link bp_core_signup_user()} for more details. 
  2104. if ( is_a( $user, 'WP_User' ) && 2 == $user->user_status ) { 
  2105. $user_login = $user->user_login; 
  2106.  
  2107. // If no WP_User is found corresponding to the username, this 
  2108. // is a potential signup. 
  2109. } elseif ( is_wp_error( $user ) && 'invalid_username' == $user->get_error_code() ) { 
  2110. $user_login = $username; 
  2111.  
  2112. // This is an activated user, so bail. 
  2113. } else { 
  2114. return $user; 
  2115.  
  2116. // Look for the unactivated signup corresponding to the login name. 
  2117. $signup = BP_Signup::get( array( 'user_login' => sanitize_user( $user_login ) ) ); 
  2118.  
  2119. // No signup or more than one, something is wrong. Let's bail. 
  2120. if ( empty( $signup['signups'][0] ) || $signup['total'] > 1 ) { 
  2121. return $user; 
  2122.  
  2123. // Unactivated user account found! 
  2124. // Set up the feedback message. 
  2125. $signup_id = $signup['signups'][0]->signup_id; 
  2126.  
  2127. $resend_url_params = array( 
  2128. 'action' => 'bp-resend-activation',  
  2129. 'id' => $signup_id,  
  2130. ); 
  2131.  
  2132. $resend_url = wp_nonce_url( 
  2133. add_query_arg( $resend_url_params, wp_login_url() ),  
  2134. 'bp-resend-activation' 
  2135. ); 
  2136.  
  2137. $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 ) ); 
  2138.  
  2139. 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 ); 
  2140. add_filter( 'authenticate', 'bp_core_signup_disable_inactive', 30, 3 ); 
  2141.  
  2142. /** 
  2143. * On the login screen, resends the activation email for a user. 
  2144. * 
  2145. * @since 2.0.0 
  2146. * 
  2147. * @see bp_core_signup_disable_inactive() 
  2148. */ 
  2149. function bp_members_login_resend_activation_email() { 
  2150. global $error; 
  2151.  
  2152. if ( empty( $_GET['id'] ) || empty( $_GET['_wpnonce'] ) ) { 
  2153. return; 
  2154.  
  2155. // Verify nonce. 
  2156. if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'bp-resend-activation' ) ) { 
  2157. die( 'Security check' ); 
  2158.  
  2159. $signup_id = (int) $_GET['id']; 
  2160.  
  2161. // Resend the activation email. 
  2162. // also updates the 'last sent' and '# of emails sent' values. 
  2163. $resend = BP_Signup::resend( array( $signup_id ) ); 
  2164.  
  2165. // Add feedback message. 
  2166. if ( ! empty( $resend['errors'] ) ) { 
  2167. $error = __( '<strong>ERROR</strong>: Your account has already been activated.', 'buddypress' ); 
  2168. } else { 
  2169. $error = __( 'Activation email resent! Please check your inbox or spam folder.', 'buddypress' ); 
  2170. add_action( 'login_form_bp-resend-activation', 'bp_members_login_resend_activation_email' ); 
  2171.  
  2172. /** 
  2173. * Redirect away from wp-signup.php if BP registration templates are present. 
  2174. * 
  2175. * @since 1.1.0 
  2176. */ 
  2177. function bp_core_wpsignup_redirect() { 
  2178.  
  2179. // Bail in admin or if custom signup page is broken. 
  2180. if ( is_admin() || ! bp_has_custom_signup_page() ) { 
  2181. return; 
  2182.  
  2183. $action = !empty( $_GET['action'] ) ? $_GET['action'] : ''; 
  2184.  
  2185. // Not at the WP core signup page and action is not register. 
  2186. if ( ! empty( $_SERVER['SCRIPT_NAME'] ) && false === strpos( 'wp-signup.php', $_SERVER['SCRIPT_NAME'] ) && ( 'register' != $action ) ) { 
  2187. return; 
  2188.  
  2189. bp_core_redirect( bp_get_signup_page() ); 
  2190. add_action( 'bp_init', 'bp_core_wpsignup_redirect' ); 
  2191.  
  2192. /** 
  2193. * Stop a logged-in user who is marked as a spammer. 
  2194. * 
  2195. * When an admin marks a live user as a spammer, that user can still surf 
  2196. * around and cause havoc on the site until that person is logged out. 
  2197. * 
  2198. * This code checks to see if a logged-in user is marked as a spammer. If so,  
  2199. * we redirect the user back to wp-login.php with the 'reauth' parameter. 
  2200. * 
  2201. * This clears the logged-in spammer's cookies and will ask the spammer to 
  2202. * reauthenticate. 
  2203. * 
  2204. * Note: A spammer cannot log back in - {@see bp_core_boot_spammer()}. 
  2205. * 
  2206. * Runs on 'bp_init' at priority 5 so the members component globals are setup 
  2207. * before we do our spammer checks. 
  2208. * 
  2209. * This is important as the $bp->loggedin_user object is setup at priority 4. 
  2210. * 
  2211. * @since 1.8.0 
  2212. */ 
  2213. function bp_stop_live_spammer() { 
  2214. // If we're on the login page, stop now to prevent redirect loop. 
  2215. $is_login = false; 
  2216. if ( isset( $GLOBALS['pagenow'] ) && ( false !== strpos( $GLOBALS['pagenow'], 'wp-login.php' ) ) ) { 
  2217. $is_login = true; 
  2218. } elseif ( isset( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], 'wp-login.php' ) ) { 
  2219. $is_login = true; 
  2220.  
  2221. if ( $is_login ) { 
  2222. return; 
  2223.  
  2224. // User isn't logged in, so stop! 
  2225. if ( ! is_user_logged_in() ) { 
  2226. return; 
  2227.  
  2228. // If spammer, redirect to wp-login.php and reauthorize. 
  2229. if ( bp_is_user_spammer( bp_loggedin_user_id() ) ) { 
  2230. // Setup login args. 
  2231. $args = array( 
  2232. // Custom action used to throw an error message. 
  2233. 'action' => 'bp-spam',  
  2234.  
  2235. // Reauthorize user to login. 
  2236. 'reauth' => 1 
  2237. ); 
  2238.  
  2239. /** 
  2240. * Filters the url used for redirection for a logged in user marked as spam. 
  2241. * 
  2242. * @since 1.8.0 
  2243. * 
  2244. * @param string $value URL to redirect user to. 
  2245. */ 
  2246. $login_url = apply_filters( 'bp_live_spammer_redirect', add_query_arg( $args, wp_login_url() ) ); 
  2247.  
  2248. // Redirect user to login page. 
  2249. wp_redirect( $login_url ); 
  2250. die(); 
  2251. add_action( 'bp_init', 'bp_stop_live_spammer', 5 ); 
  2252.  
  2253. /** 
  2254. * Show a custom error message when a logged-in user is marked as a spammer. 
  2255. * 
  2256. * @since 1.8.0 
  2257. */ 
  2258. function bp_live_spammer_login_error() { 
  2259. global $error; 
  2260.  
  2261. $error = __( '<strong>ERROR</strong>: Your account has been marked as a spammer.', 'buddypress' ); 
  2262.  
  2263. // Shake shake shake! 
  2264. add_action( 'login_head', 'wp_shake_js', 12 ); 
  2265. add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' ); 
  2266.  
  2267. /** 
  2268. * Get the displayed user Object 
  2269. * 
  2270. * @since 2.6.0 
  2271. * 
  2272. * @return object The displayed user object, null otherwise. 
  2273. */ 
  2274. function bp_get_displayed_user() { 
  2275. $bp = buddypress(); 
  2276.  
  2277. $displayed_user = null; 
  2278. if ( ! empty( $bp->displayed_user->id ) ) { 
  2279. $displayed_user = $bp->displayed_user; 
  2280.  
  2281. /** 
  2282. * Filters the displayed_user object corresponding to the displayed member. 
  2283. * 
  2284. * @since 2.6.0 
  2285. * 
  2286. * @param object $displayed_user The displayed_user object. 
  2287. */ 
  2288. return apply_filters( 'bp_get_displayed_user', $displayed_user ); 
  2289.  
  2290. /** Member Types *************************************************************/ 
  2291.  
  2292. /** 
  2293. * Output the slug of the member type taxonomy. 
  2294. * 
  2295. * @since 2.7.0 
  2296. */ 
  2297. function bp_member_type_tax_name() { 
  2298. echo bp_get_member_type_tax_name(); 
  2299.  
  2300. /** 
  2301. * Return the slug of the member type taxonomy. 
  2302. * 
  2303. * @since 2.7.0 
  2304. * 
  2305. * @return string The unique member taxonomy slug. 
  2306. */ 
  2307. function bp_get_member_type_tax_name() { 
  2308. /** 
  2309. * Filters the slug of the member type taxonomy. 
  2310. * 
  2311. * @since 2.7.0 
  2312. * 
  2313. * @param string $value Member type taxonomy slug. 
  2314. */ 
  2315. return apply_filters( 'bp_get_member_type_tax_name', 'bp_member_type' ); 
  2316.  
  2317. /** 
  2318. * Register a member type. 
  2319. * 
  2320. * @since 2.2.0 
  2321. * 
  2322. * @param string $member_type Unique string identifier for the member type. 
  2323. * @param array $args { 
  2324. * Array of arguments describing the member type. 
  2325. * 
  2326. * @type array $labels { 
  2327. * Array of labels to use in various parts of the interface. 
  2328. * 
  2329. * @type string $name Default name. Should typically be plural. 
  2330. * @type string $singular_name Singular name. 
  2331. * } 
  2332. * @type bool|string $has_directory Whether the member type should have its own type-specific directory. 
  2333. * Pass `true` to use the `$member_type` string as the type's slug. 
  2334. * Pass a string to customize the slug. Pass `false` to disable. 
  2335. * Default: true. 
  2336. * } 
  2337. * @return object|WP_Error Member type object on success, WP_Error object on failure. 
  2338. */ 
  2339. function bp_register_member_type( $member_type, $args = array() ) { 
  2340. $bp = buddypress(); 
  2341.  
  2342. if ( isset( $bp->members->types[ $member_type ] ) ) { 
  2343. return new WP_Error( 'bp_member_type_exists', __( 'Member type already exists.', 'buddypress' ), $member_type ); 
  2344.  
  2345. $r = bp_parse_args( $args, array( 
  2346. 'labels' => array(),  
  2347. 'has_directory' => true,  
  2348. ), 'register_member_type' ); 
  2349.  
  2350. $member_type = sanitize_key( $member_type ); 
  2351.  
  2352. /** 
  2353. * Filters the list of illegal member type names. 
  2354. * 
  2355. * - 'any' is a special pseudo-type, representing items unassociated with any member type. 
  2356. * - 'null' is a special pseudo-type, representing users without any type. 
  2357. * - '_none' is used internally to denote an item that should not apply to any member types. 
  2358. * 
  2359. * @since 2.4.0 
  2360. * 
  2361. * @param array $illegal_names Array of illegal names. 
  2362. */ 
  2363. $illegal_names = apply_filters( 'bp_member_type_illegal_names', array( 'any', 'null', '_none' ) ); 
  2364. if ( in_array( $member_type, $illegal_names, true ) ) { 
  2365. return new WP_Error( 'bp_member_type_illegal_name', __( 'You may not register a member type with this name.', 'buddypress' ), $member_type ); 
  2366.  
  2367. // Store the post type name as data in the object (not just as the array key). 
  2368. $r['name'] = $member_type; 
  2369.  
  2370. // Make sure the relevant labels have been filled in. 
  2371. $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] ); 
  2372. $r['labels'] = array_merge( array( 
  2373. 'name' => $default_name,  
  2374. 'singular_name' => $default_name,  
  2375. ), $r['labels'] ); 
  2376.  
  2377. // Directory slug. 
  2378. if ( $r['has_directory'] ) { 
  2379. // A string value is intepreted as the directory slug. Otherwise fall back on member type. 
  2380. if ( is_string( $r['has_directory'] ) ) { 
  2381. $directory_slug = $r['has_directory']; 
  2382. } else { 
  2383. $directory_slug = $member_type; 
  2384.  
  2385. // Sanitize for use in URLs. 
  2386. $r['directory_slug'] = sanitize_title( $directory_slug ); 
  2387. $r['has_directory'] = true; 
  2388. } else { 
  2389. $r['directory_slug'] = ''; 
  2390. $r['has_directory'] = false; 
  2391.  
  2392. $bp->members->types[ $member_type ] = $type = (object) $r; 
  2393.  
  2394. /** 
  2395. * Fires after a member type is registered. 
  2396. * 
  2397. * @since 2.2.0 
  2398. * 
  2399. * @param string $member_type Member type identifier. 
  2400. * @param object $type Member type object. 
  2401. */ 
  2402. do_action( 'bp_registered_member_type', $member_type, $type ); 
  2403.  
  2404. return $type; 
  2405.  
  2406. /** 
  2407. * Retrieve a member type object by name. 
  2408. * 
  2409. * @since 2.2.0 
  2410. * 
  2411. * @param string $member_type The name of the member type. 
  2412. * @return object A member type object. 
  2413. */ 
  2414. function bp_get_member_type_object( $member_type ) { 
  2415. $types = bp_get_member_types( array(), 'objects' ); 
  2416.  
  2417. if ( empty( $types[ $member_type ] ) ) { 
  2418. return null; 
  2419.  
  2420. return $types[ $member_type ]; 
  2421.  
  2422. /** 
  2423. * Get a list of all registered member type objects. 
  2424. * 
  2425. * @since 2.2.0 
  2426. * 
  2427. * @see bp_register_member_type() for accepted arguments. 
  2428. * 
  2429. * @param array|string $args Optional. An array of key => value arguments to match against 
  2430. * the member type objects. Default empty array. 
  2431. * @param string $output Optional. The type of output to return. Accepts 'names' 
  2432. * or 'objects'. Default 'names'. 
  2433. * @param string $operator Optional. The logical operation to perform. 'or' means only one 
  2434. * element from the array needs to match; 'and' means all elements 
  2435. * must match. Accepts 'or' or 'and'. Default 'and'. 
  2436. * @return array A list of member type names or objects. 
  2437. */ 
  2438. function bp_get_member_types( $args = array(), $output = 'names', $operator = 'and' ) { 
  2439. $types = buddypress()->members->types; 
  2440.  
  2441. $types = wp_filter_object_list( $types, $args, $operator ); 
  2442.  
  2443. /** 
  2444. * Filters the array of member type objects. 
  2445. * 
  2446. * This filter is run before the $output filter has been applied, so that 
  2447. * filtering functions have access to the entire member type objects. 
  2448. * 
  2449. * @since 2.2.0 
  2450. * 
  2451. * @param array $types Member type objects, keyed by name. 
  2452. * @param array $args Array of key=>value arguments for filtering. 
  2453. * @param string $operator 'or' to match any of $args, 'and' to require all. 
  2454. */ 
  2455. $types = apply_filters( 'bp_get_member_types', $types, $args, $operator ); 
  2456.  
  2457. if ( 'names' === $output ) { 
  2458. $types = wp_list_pluck( $types, 'name' ); 
  2459.  
  2460. return $types; 
  2461.  
  2462. /** 
  2463. * Set type for a member. 
  2464. * 
  2465. * @since 2.2.0 
  2466. * 
  2467. * @param int $user_id ID of the user. 
  2468. * @param string $member_type Member type. 
  2469. * @param bool $append Optional. True to append this to existing types for user,  
  2470. * false to replace. Default: false. 
  2471. * @return array $retval See {@see bp_set_object_terms()}. 
  2472. */ 
  2473. function bp_set_member_type( $user_id, $member_type, $append = false ) { 
  2474. // Pass an empty $member_type to remove a user's type. 
  2475. if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) { 
  2476. return false; 
  2477.  
  2478. $retval = bp_set_object_terms( $user_id, $member_type, bp_get_member_type_tax_name(), $append ); 
  2479.  
  2480. // Bust the cache if the type has been updated. 
  2481. if ( ! is_wp_error( $retval ) ) { 
  2482. wp_cache_delete( $user_id, 'bp_member_member_type' ); 
  2483.  
  2484. /** 
  2485. * Fires just after a user's member type has been changed. 
  2486. * 
  2487. * @since 2.2.0 
  2488. * 
  2489. * @param int $user_id ID of the user whose member type has been updated. 
  2490. * @param string $member_type Member type. 
  2491. * @param bool $append Whether the type is being appended to existing types. 
  2492. */ 
  2493. do_action( 'bp_set_member_type', $user_id, $member_type, $append ); 
  2494.  
  2495. return $retval; 
  2496.  
  2497. /** 
  2498. * Remove type for a member. 
  2499. * 
  2500. * @since 2.3.0 
  2501. * 
  2502. * @param int $user_id ID of the user. 
  2503. * @param string $member_type Member Type. 
  2504. * @return bool|WP_Error 
  2505. */ 
  2506. function bp_remove_member_type( $user_id, $member_type ) { 
  2507. // Bail if no valid member type was passed. 
  2508. if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) { 
  2509. return false; 
  2510.  
  2511. $deleted = bp_remove_object_terms( $user_id, $member_type, bp_get_member_type_tax_name() ); 
  2512.  
  2513. // Bust the cache if the type has been removed. 
  2514. if ( ! is_wp_error( $deleted ) ) { 
  2515. wp_cache_delete( $user_id, 'bp_member_member_type' ); 
  2516.  
  2517. /** 
  2518. * Fires just after a user's member type has been removed. 
  2519. * 
  2520. * @since 2.3.0 
  2521. * 
  2522. * @param int $user_id ID of the user whose member type has been updated. 
  2523. * @param string $member_type Member type. 
  2524. */ 
  2525. do_action( 'bp_remove_member_type', $user_id, $member_type ); 
  2526.  
  2527. return $deleted; 
  2528.  
  2529. /** 
  2530. * Get type for a member. 
  2531. * 
  2532. * @since 2.2.0 
  2533. * 
  2534. * @param int $user_id ID of the user. 
  2535. * @param bool $single Optional. Whether to return a single type string. If multiple types are found 
  2536. * for the user, the oldest one will be returned. Default: true. 
  2537. * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member 
  2538. * types (if $single is false). Returns false on failure. 
  2539. */ 
  2540. function bp_get_member_type( $user_id, $single = true ) { 
  2541. $types = wp_cache_get( $user_id, 'bp_member_member_type' ); 
  2542.  
  2543. if ( false === $types ) { 
  2544. $raw_types = bp_get_object_terms( $user_id, bp_get_member_type_tax_name() ); 
  2545.  
  2546. if ( ! is_wp_error( $raw_types ) ) { 
  2547. $types = array(); 
  2548.  
  2549. // Only include currently registered group types. 
  2550. foreach ( $raw_types as $mtype ) { 
  2551. if ( bp_get_member_type_object( $mtype->name ) ) { 
  2552. $types[] = $mtype->name; 
  2553.  
  2554. wp_cache_set( $user_id, $types, 'bp_member_member_type' ); 
  2555.  
  2556. $type = false; 
  2557. if ( ! empty( $types ) ) { 
  2558. if ( $single ) { 
  2559. $type = array_pop( $types ); 
  2560. } else { 
  2561. $type = $types; 
  2562.  
  2563. /** 
  2564. * Filters a user's member type(s). 
  2565. * 
  2566. * @since 2.2.0 
  2567. * 
  2568. * @param string $type Member type. 
  2569. * @param int $user_id ID of the user. 
  2570. * @param bool $single Whether to return a single type string, or an array. 
  2571. */ 
  2572. return apply_filters( 'bp_get_member_type', $type, $user_id, $single ); 
  2573.  
  2574. /** 
  2575. * Check whether the given user has a certain member type. 
  2576. * 
  2577. * @since 2.3.0 
  2578. * 
  2579. * @param int $user_id $user_id ID of the user. 
  2580. * @param string $member_type Member Type. 
  2581. * @return bool Whether the user has the given member type. 
  2582. */ 
  2583. function bp_has_member_type( $user_id, $member_type ) { 
  2584. // Bail if no valid member type was passed. 
  2585. if ( empty( $member_type ) || ! bp_get_member_type_object( $member_type ) ) { 
  2586. return false; 
  2587.  
  2588. // Get all user's member types. 
  2589. $types = bp_get_member_type( $user_id, false ); 
  2590.  
  2591. if ( ! is_array( $types ) ) { 
  2592. return false; 
  2593.  
  2594. return in_array( $member_type, $types ); 
  2595.  
  2596. /** 
  2597. * Delete a user's member type when the user when the user is deleted. 
  2598. * 
  2599. * @since 2.2.0 
  2600. * 
  2601. * @param int $user_id ID of the user. 
  2602. * @return array $value See {@see bp_set_member_type()}. 
  2603. */ 
  2604. function bp_remove_member_type_on_user_delete( $user_id ) { 
  2605. return bp_set_member_type( $user_id, '' ); 
  2606. add_action( 'wpmu_delete_user', 'bp_remove_member_type_on_user_delete' ); 
  2607. add_action( 'delete_user', 'bp_remove_member_type_on_user_delete' ); 
  2608.  
  2609. /** 
  2610. * Get the "current" member type, if one is provided, in member directories. 
  2611. * 
  2612. * @since 2.3.0 
  2613. * 
  2614. * @return string 
  2615. */ 
  2616. function bp_get_current_member_type() { 
  2617.  
  2618. /** 
  2619. * Filters the "current" member type, if one is provided, in member directories. 
  2620. * 
  2621. * @since 2.3.0 
  2622. * 
  2623. * @param string $value "Current" member type. 
  2624. */ 
  2625. return apply_filters( 'bp_get_current_member_type', buddypress()->current_member_type ); 
.