BP_Core_User

Fetch data about a BuddyPress user.

Defined (1)

The class is defined in the following location(s).

/bp-core/classes/class-bp-core-user.php  
  1. class BP_Core_User { 
  2.  
  3. /** 
  4. * ID of the user which the object relates to. 
  5. * @var integer 
  6. */ 
  7. public $id; 
  8.  
  9. /** 
  10. * The URL to the full size of the avatar for the user. 
  11. * @var string 
  12. */ 
  13. public $avatar; 
  14.  
  15. /** 
  16. * The URL to the thumb size of the avatar for the user. 
  17. * @var string 
  18. */ 
  19. public $avatar_thumb; 
  20.  
  21. /** 
  22. * The URL to the mini size of the avatar for the user. 
  23. * @var string 
  24. */ 
  25. public $avatar_mini; 
  26.  
  27. /** 
  28. * The full name of the user. 
  29. * @var string 
  30. */ 
  31. public $fullname; 
  32.  
  33. /** 
  34. * The email for the user. 
  35. * @var string 
  36. */ 
  37. public $email; 
  38.  
  39. /** 
  40. * The absolute url for the user's profile. 
  41. * @var string 
  42. */ 
  43. public $user_url; 
  44.  
  45. /** 
  46. * The HTML for the user link, with the link text being the user's full name. 
  47. * @var string 
  48. */ 
  49. public $user_link; 
  50.  
  51. /** 
  52. * Contains a formatted string when the last time the user was active. 
  53. * Example: "active 2 hours and 50 minutes ago" 
  54. * @var string 
  55. */ 
  56. public $last_active; 
  57.  
  58. /** Extras */ 
  59.  
  60. /** 
  61. * The total number of "Friends" the user has on site. 
  62. * @var integer 
  63. */ 
  64. public $total_friends; 
  65.  
  66. /** 
  67. * The total number of blog posts posted by the user. 
  68. * @var integer 
  69. * @deprecated No longer used 
  70. */ 
  71. public $total_blogs; 
  72.  
  73. /** 
  74. * The total number of groups the user is a part of. 
  75. * Example: "1 group", "2 groups" 
  76. * @var string 
  77. */ 
  78. public $total_groups; 
  79.  
  80. /** 
  81. * Profile information for the specific user. 
  82. * @since 1.2.0 
  83. * @var array 
  84. */ 
  85. public $profile_data; 
  86.  
  87. /** Public Methods *******************************************************/ 
  88.  
  89. /** 
  90. * Class constructor. 
  91. * @param integer $user_id The ID for the user being queried. 
  92. * @param bool $populate_extras Whether to fetch extra information such as 
  93. * group/friendship counts or not. Default: false. 
  94. */ 
  95. public function __construct( $user_id, $populate_extras = false ) { 
  96. if ( !empty( $user_id ) ) { 
  97. $this->id = $user_id; 
  98. $this->populate(); 
  99.  
  100. if ( !empty( $populate_extras ) ) { 
  101. $this->populate_extras(); 
  102.  
  103. /** 
  104. * Populate the instantiated class with data based on the User ID provided. 
  105. */ 
  106. public function populate() { 
  107.  
  108. if ( bp_is_active( 'xprofile' ) ) 
  109. $this->profile_data = $this->get_profile_data(); 
  110.  
  111. if ( !empty( $this->profile_data ) ) { 
  112. $full_name_field_name = bp_xprofile_fullname_field_name(); 
  113.  
  114. $this->user_url = bp_core_get_user_domain( $this->id, $this->profile_data['user_nicename'], $this->profile_data['user_login'] ); 
  115. $this->fullname = esc_attr( $this->profile_data[$full_name_field_name]['field_data'] ); 
  116. $this->user_link = "<a href='{$this->user_url}' title='{$this->fullname}'>{$this->fullname}</a>"; 
  117. $this->email = esc_attr( $this->profile_data['user_email'] ); 
  118. } else { 
  119. $this->user_url = bp_core_get_user_domain( $this->id ); 
  120. $this->user_link = bp_core_get_userlink( $this->id ); 
  121. $this->fullname = esc_attr( bp_core_get_user_displayname( $this->id ) ); 
  122. $this->email = esc_attr( bp_core_get_user_email( $this->id ) ); 
  123.  
  124. // Cache a few things that are fetched often. 
  125. wp_cache_set( 'bp_user_fullname_' . $this->id, $this->fullname, 'bp' ); 
  126. wp_cache_set( 'bp_user_email_' . $this->id, $this->email, 'bp' ); 
  127. wp_cache_set( 'bp_user_url_' . $this->id, $this->user_url, 'bp' ); 
  128.  
  129. $this->avatar = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'full', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->fullname ) ) ); 
  130. $this->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->fullname ) ) ); 
  131. $this->avatar_mini = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf( __( 'Profile photo of %s', 'buddypress' ), $this->fullname ), 'width' => 30, 'height' => 30 ) ); 
  132. $this->last_active = bp_core_get_last_activity( bp_get_user_last_activity( $this->id ), __( 'active %s', 'buddypress' ) ); 
  133.  
  134. /** 
  135. * Populates extra fields such as group and friendship counts. 
  136. */ 
  137. public function populate_extras() { 
  138.  
  139. if ( bp_is_active( 'friends' ) ) { 
  140. $this->total_friends = BP_Friends_Friendship::total_friend_count( $this->id ); 
  141.  
  142. if ( bp_is_active( 'groups' ) ) { 
  143. $this->total_groups = BP_Groups_Member::total_group_count( $this->id ); 
  144. $this->total_groups = sprintf( _n( '%d group', '%d groups', $this->total_groups, 'buddypress' ), $this->total_groups ); 
  145.  
  146. /** 
  147. * Fetch xprofile data for the current user. 
  148. * @see BP_XProfile_ProfileData::get_all_for_user() for description of 
  149. * return value. 
  150. * @return array See {@link BP_XProfile_Profile_Data::get_all_for_user()}. 
  151. */ 
  152. public function get_profile_data() { 
  153. return BP_XProfile_ProfileData::get_all_for_user( $this->id ); 
  154.  
  155. /** Static Methods ********************************************************/ 
  156.  
  157. /** 
  158. * Get a list of users that match the query parameters. 
  159. * Since BuddyPress 1.7, use {@link BP_User_Query} instead. 
  160. * @deprecated 1.7.0 Use {@link BP_User_Query}. 
  161. * @see BP_User_Query for a description of parameters, most of which 
  162. * are used there in the same way. 
  163. * @param string $type See {@link BP_User_Query}. 
  164. * @param int $limit See {@link BP_User_Query}. Default: 0. 
  165. * @param int $page See {@link BP_User_Query}. Default: 1. 
  166. * @param int $user_id See {@link BP_User_Query}. Default: 0. 
  167. * @param mixed $include See {@link BP_User_Query}. Default: false. 
  168. * @param string|bool $search_terms See {@link BP_User_Query}. 
  169. * Default: false. 
  170. * @param bool $populate_extras See {@link BP_User_Query}. 
  171. * Default: true. 
  172. * @param mixed $exclude See {@link BP_User_Query}. Default: false. 
  173. * @param string|bool $meta_key See {@link BP_User_Query}. 
  174. * Default: false. 
  175. * @param string|bool $meta_value See {@link BP_User_Query}. 
  176. * Default: false. 
  177. * @return array { 
  178. * @type int $total_users Total number of users matched by query 
  179. * params. 
  180. * @type array $paged_users The current page of users matched by 
  181. * query params. 
  182. * } 
  183. */ 
  184. public static function get_users( $type, $limit = 0, $page = 1, $user_id = 0, $include = false, $search_terms = false, $populate_extras = true, $exclude = false, $meta_key = false, $meta_value = false ) { 
  185. global $wpdb; 
  186.  
  187. _deprecated_function( __METHOD__, '1.7', 'BP_User_Query' ); 
  188.  
  189. $bp = buddypress(); 
  190.  
  191. $sql = array(); 
  192.  
  193. $sql['select_main'] = "SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.display_name, u.user_email"; 
  194.  
  195. if ( 'active' == $type || 'online' == $type || 'newest' == $type ) { 
  196. $sql['select_active'] = ", um.meta_value as last_activity"; 
  197.  
  198. if ( 'popular' == $type ) { 
  199. $sql['select_popular'] = ", um.meta_value as total_friend_count"; 
  200.  
  201. if ( 'alphabetical' == $type ) { 
  202. $sql['select_alpha'] = ", pd.value as fullname"; 
  203.  
  204. if ( $meta_key ) { 
  205. $sql['select_meta'] = ", umm.meta_key"; 
  206.  
  207. if ( $meta_value ) { 
  208. $sql['select_meta'] .= ", umm.meta_value"; 
  209.  
  210. $sql['from'] = "FROM {$wpdb->users} u LEFT JOIN {$wpdb->usermeta} um ON um.user_id = u.ID"; 
  211.  
  212. // We search against xprofile fields, so we must join the table. 
  213. if ( $search_terms && bp_is_active( 'xprofile' ) ) { 
  214. $sql['join_profiledata_search'] = "LEFT JOIN {$bp->profile->table_name_data} spd ON u.ID = spd.user_id"; 
  215.  
  216. // Alphabetical sorting is done by the xprofile Full Name field. 
  217. if ( 'alphabetical' == $type ) { 
  218. $sql['join_profiledata_alpha'] = "LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id"; 
  219.  
  220. if ( $meta_key ) { 
  221. $sql['join_meta'] = "LEFT JOIN {$wpdb->usermeta} umm ON umm.user_id = u.ID"; 
  222.  
  223. $sql['where'] = 'WHERE ' . bp_core_get_status_sql( 'u.' ); 
  224.  
  225. if ( 'active' == $type || 'online' == $type || 'newest' == $type ) { 
  226. $sql['where_active'] = $wpdb->prepare( "AND um.meta_key = %s", bp_get_user_meta_key( 'last_activity' ) ); 
  227.  
  228. if ( 'popular' == $type ) { 
  229. $sql['where_popular'] = $wpdb->prepare( "AND um.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) ); 
  230.  
  231. if ( 'online' == $type ) { 
  232. $sql['where_online'] = "AND DATE_ADD( um.meta_value, INTERVAL 5 MINUTE ) >= UTC_TIMESTAMP()"; 
  233.  
  234. if ( 'alphabetical' == $type ) { 
  235. $sql['where_alpha'] = "AND pd.field_id = 1"; 
  236.  
  237. if ( !empty( $exclude ) ) { 
  238. $exclude = implode( ', ', wp_parse_id_list( $exclude ) ); 
  239. $sql['where_exclude'] = "AND u.ID NOT IN ({$exclude})"; 
  240.  
  241. // Passing an $include value of 0 or '0' will necessarily result in an empty set 
  242. // returned. The default value of false will hit the 'else' clause. 
  243. if ( 0 === $include || '0' === $include ) { 
  244. $sql['where_users'] = "AND 0 = 1"; 
  245. } else { 
  246. if ( !empty( $include ) ) { 
  247. $include = implode( ', ', wp_parse_id_list( $include ) ); 
  248. $sql['where_users'] = "AND u.ID IN ({$include})"; 
  249. } elseif ( !empty( $user_id ) && bp_is_active( 'friends' ) ) { 
  250. $friend_ids = friends_get_friend_user_ids( $user_id ); 
  251.  
  252. if ( !empty( $friend_ids ) ) { 
  253. $friend_ids = implode( ', ', wp_parse_id_list( $friend_ids ) ); 
  254. $sql['where_friends'] = "AND u.ID IN ({$friend_ids})"; 
  255.  
  256. // User has no friends, return false since there will be no users to fetch. 
  257. } else { 
  258. return false; 
  259.  
  260. if ( !empty( $search_terms ) && bp_is_active( 'xprofile' ) ) { 
  261. $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 
  262. $sql['where_searchterms'] = $wpdb->prepare( "AND spd.value LIKE %s", $search_terms_like ); 
  263.  
  264. if ( !empty( $meta_key ) ) { 
  265. $sql['where_meta'] = $wpdb->prepare( " AND umm.meta_key = %s", $meta_key ); 
  266.  
  267. // If a meta value is provided, match it. 
  268. if ( $meta_value ) { 
  269. $sql['where_meta'] .= $wpdb->prepare( " AND umm.meta_value = %s", $meta_value ); 
  270.  
  271. switch ( $type ) { 
  272. case 'active': case 'online': default: 
  273. $sql[] = "ORDER BY um.meta_value DESC"; 
  274. break; 
  275. case 'newest': 
  276. $sql[] = "ORDER BY u.ID DESC"; 
  277. break; 
  278. case 'alphabetical': 
  279. $sql[] = "ORDER BY pd.value ASC"; 
  280. break; 
  281. case 'random': 
  282. $sql[] = "ORDER BY rand()"; 
  283. break; 
  284. case 'popular': 
  285. $sql[] = "ORDER BY CONVERT(um.meta_value, SIGNED) DESC"; 
  286. break; 
  287.  
  288. if ( !empty( $limit ) && !empty( $page ) ) { 
  289. $sql['pagination'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  290.  
  291. /** 
  292. * Filters the SQL used to query for paged users. 
  293. * @since 1.2.6 
  294. * @param string $value Concatenated SQL statement for the query. 
  295. * @param array $sql Array of SQL statement parts for the query. 
  296. */ 
  297. $paged_users_sql = apply_filters( 'bp_core_get_paged_users_sql', join( ' ', (array) $sql ), $sql ); 
  298. $paged_users = $wpdb->get_results( $paged_users_sql ); 
  299.  
  300. // Re-jig the SQL so we can get the total user count. 
  301. unset( $sql['select_main'] ); 
  302.  
  303. if ( !empty( $sql['select_active'] ) ) { 
  304. unset( $sql['select_active'] ); 
  305.  
  306. if ( !empty( $sql['select_popular'] ) ) { 
  307. unset( $sql['select_popular'] ); 
  308.  
  309. if ( !empty( $sql['select_alpha'] ) ) { 
  310. unset( $sql['select_alpha'] ); 
  311.  
  312. if ( !empty( $sql['pagination'] ) ) { 
  313. unset( $sql['pagination'] ); 
  314.  
  315. array_unshift( $sql, "SELECT COUNT(u.ID)" ); 
  316.  
  317. /** 
  318. * Filters the SQL used to query for total users. 
  319. * @since 1.2.6 
  320. * @param string $value Concatenated SQL statement for the query. 
  321. * @param array $sql Array of SQL statement parts for the query. 
  322. */ 
  323. $total_users_sql = apply_filters( 'bp_core_get_total_users_sql', join( ' ', (array) $sql ), $sql ); 
  324. $total_users = $wpdb->get_var( $total_users_sql ); 
  325.  
  326. /** 
  327. * Lets fetch some other useful data in a separate queries, this will be faster than querying the data for every user in a list. 
  328. * We can't add these to the main query above since only users who have this information will be returned (since the much of the data is in usermeta and won't support any type of directional join). 
  329. */ 
  330. if ( !empty( $populate_extras ) ) { 
  331. $user_ids = array(); 
  332.  
  333. foreach ( (array) $paged_users as $user ) { 
  334. $user_ids[] = $user->id; 
  335.  
  336. // Add additional data to the returned results. 
  337. $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids, $type ); 
  338.  
  339. return array( 'users' => $paged_users, 'total' => $total_users ); 
  340.  
  341.  
  342. /** 
  343. * Fetch the details for all users whose usernames start with the given letter. 
  344. * @global wpdb $wpdb WordPress database object. 
  345. * @param string $letter The letter the users names are to start with. 
  346. * @param int|null $limit The number of users we wish to retrive. 
  347. * @param int $page The page number we are currently on, used in conjunction 
  348. * with $limit to get the start position for the limit. 
  349. * @param bool $populate_extras If we should populate extra user fields. 
  350. * @param string $exclude Comma-separated IDs of users whose results 
  351. * aren't to be fetched. 
  352. * @return mixed False on error, otherwise associative array of results. 
  353. */ 
  354. public static function get_users_by_letter( $letter, $limit = null, $page = 1, $populate_extras = true, $exclude = '' ) { 
  355. global $wpdb; 
  356.  
  357. $pag_sql = ''; 
  358. if ( $limit && $page ) { 
  359. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  360.  
  361. // Multibyte compliance. 
  362. if ( function_exists( 'mb_strlen' ) ) { 
  363. if ( mb_strlen( $letter, 'UTF-8' ) > 1 || is_numeric( $letter ) || !$letter ) { 
  364. return false; 
  365. } else { 
  366. if ( strlen( $letter ) > 1 || is_numeric( $letter ) || !$letter ) { 
  367. return false; 
  368.  
  369. $bp = buddypress(); 
  370.  
  371. $letter_like = bp_esc_like( $letter ) . '%'; 
  372. $status_sql = bp_core_get_status_sql( 'u.' ); 
  373.  
  374. if ( !empty( $exclude ) ) { 
  375. $exclude = implode( ', ', wp_parse_id_list( $exclude ) ); 
  376. $exclude_sql = " AND u.id NOT IN ({$exclude})"; 
  377. } else { 
  378. $exclude_sql = ''; 
  379.  
  380. /** 
  381. * Filters the SQL used to query for total user count by first letter. 
  382. * @since 1.0.0 
  383. * @param string $value SQL prepared statement for the user count query. 
  384. */ 
  385. $total_users_sql = apply_filters( 'bp_core_users_by_letter_count_sql', $wpdb->prepare( "SELECT COUNT(DISTINCT u.ID) FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id LEFT JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id WHERE {$status_sql} AND pf.name = %s {$exclude_sql} AND pd.value LIKE %s ORDER BY pd.value ASC", bp_xprofile_fullname_field_name(), $letter_like ) ); 
  386.  
  387. /** 
  388. * Filters the SQL used to query for users by first letter. 
  389. * @since 1.0.0 
  390. * @param string $value SQL prepared statement for the user query. 
  391. */ 
  392. $paged_users_sql = apply_filters( 'bp_core_users_by_letter_sql', $wpdb->prepare( "SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.user_email FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id LEFT JOIN {$bp->profile->table_name_fields} pf ON pd.field_id = pf.id WHERE {$status_sql} AND pf.name = %s {$exclude_sql} AND pd.value LIKE %s ORDER BY pd.value ASC{$pag_sql}", bp_xprofile_fullname_field_name(), $letter_like ) ); 
  393.  
  394. $total_users = $wpdb->get_var( $total_users_sql ); 
  395. $paged_users = $wpdb->get_results( $paged_users_sql ); 
  396.  
  397. /** 
  398. * Lets fetch some other useful data in a separate queries, this will be 
  399. * faster than querying the data for every user in a list. We can't add 
  400. * these to the main query above since only users who have this 
  401. * information will be returned (since the much of the data is in 
  402. * usermeta and won't support any type of directional join) 
  403. */ 
  404. $user_ids = array(); 
  405. foreach ( (array) $paged_users as $user ) 
  406. $user_ids[] = (int) $user->id; 
  407.  
  408. // Add additional data to the returned results. 
  409. if ( $populate_extras ) { 
  410. $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids ); 
  411.  
  412. return array( 'users' => $paged_users, 'total' => $total_users ); 
  413.  
  414. /** 
  415. * Get details of specific users from the database. 
  416. * Use {@link BP_User_Query} with the 'user_ids' param instead. 
  417. * @global wpdb $wpdb WordPress database object. 
  418. * @param array $user_ids The user IDs of the users who we wish to 
  419. * fetch information on. 
  420. * @param int|null $limit The limit of results we want. 
  421. * @param int $page The page we are on for pagination. 
  422. * @param bool $populate_extras If we should populate extra user fields. 
  423. * @return array Associative array. 
  424. */ 
  425. public static function get_specific_users( $user_ids, $limit = null, $page = 1, $populate_extras = true ) { 
  426. global $wpdb; 
  427.  
  428. $pag_sql = ''; 
  429. if ( $limit && $page ) 
  430. $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 
  431.  
  432. $user_ids = implode( ', ', wp_parse_id_list( $user_ids ) ); 
  433. $status_sql = bp_core_get_status_sql(); 
  434.  
  435. /** 
  436. * Filter the SQL string used for querying specific user count. 
  437. * This same filter name is used for the paged user SQL, and so should be avoided. 
  438. * Use 'bp_core_user_get_specific_users_count_sql' instead. 
  439. * @deprecated 2.3.0 
  440. * @param string $sql SQL string. 
  441. */ 
  442. $total_users_sql = apply_filters( 'bp_core_get_specific_users_count_sql', "SELECT COUNT(ID) FROM {$wpdb->users} WHERE {$status_sql} AND ID IN ({$user_ids})" ); 
  443.  
  444. /** 
  445. * Filter the SQL string used for querying specific user count results. 
  446. * Use this instead of the deprecated 'bp_core_get_specific_users_count_sql'. 
  447. * @since 2.3.0 
  448. * @param string $sql SQL string. 
  449. * @param array $user_ids Array of IDs of specific users to fetch. 
  450. * @param int|null $limit Max number of records to return. Null for no limit. 
  451. * @param int $page The page we're on for pagination. 
  452. * @param bool $populate_extras Whether to populate extra user fields. 
  453. */ 
  454. $total_users_sql = apply_filters( 'bp_core_user_get_specific_users_count_sql', $total_users_sql, $user_ids, $limit, $page, $populate_extras ); 
  455.  
  456. /** 
  457. * Filter the SQL string used for querying specific user paged results. 
  458. * This same filter name is used for the user count SQL, and so should be avoided. 
  459. * Use 'bp_core_user_get_specific_users_paged_sql' instead. 
  460. * @deprecated 2.3.0 
  461. * @param string $sql SQL string. 
  462. */ 
  463. $paged_users_sql = apply_filters( 'bp_core_get_specific_users_count_sql', "SELECT ID as id, user_registered, user_nicename, user_login, user_email FROM {$wpdb->users} WHERE {$status_sql} AND ID IN ({$user_ids}) {$pag_sql}" ); 
  464.  
  465. /** 
  466. * Filter the SQL string used for querying specific user paged results. 
  467. * Use this instead of the deprecated 'bp_core_get_specific_users_count_sql'. 
  468. * @since 2.3.0 
  469. * @param string $sql SQL string. 
  470. * @param array $user_ids Array of IDs of specific users to fetch. 
  471. * @param int|null $limit Max number of records to return. Null for no limit. 
  472. * @param int $page The page we're on for pagination. 
  473. * @param bool $populate_extras Whether to populate extra user fields. 
  474. */ 
  475. $paged_users_sql = apply_filters( 'bp_core_user_get_specific_users_paged_sql', $paged_users_sql, $user_ids, $limit, $page, $populate_extras ); 
  476.  
  477. $total_users = $wpdb->get_var( $total_users_sql ); 
  478. $paged_users = $wpdb->get_results( $paged_users_sql ); 
  479.  
  480. /** 
  481. * Lets fetch some other useful data in a separate queries, this will be 
  482. * faster than querying the data for every user in a list. We can't add 
  483. * these to the main query above since only users who have this 
  484. * information will be returned (since the much of the data is in 
  485. * usermeta and won't support any type of directional join) 
  486. */ 
  487.  
  488. // Add additional data to the returned results. 
  489. if ( !empty( $populate_extras ) ) { 
  490. $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids ); 
  491.  
  492. return array( 'users' => $paged_users, 'total' => $total_users ); 
  493.  
  494. /** 
  495. * Find users who match on the value of an xprofile data. 
  496. * @global wpdb $wpdb WordPress database object. 
  497. * @param string $search_terms The terms to search the profile table 
  498. * value column for. 
  499. * @param int|null $limit The limit of results we want. 
  500. * @param int $page The page we are on for pagination. 
  501. * @param boolean $populate_extras If we should populate extra user fields. 
  502. * @return array Associative array. 
  503. */ 
  504. public static function search_users( $search_terms, $limit = null, $page = 1, $populate_extras = true ) { 
  505. global $wpdb; 
  506.  
  507. $bp = buddypress(); 
  508.  
  509. $user_ids = array(); 
  510. $pag_sql = $limit && $page ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * intval( $limit ) ), intval( $limit ) ) : ''; 
  511.  
  512. $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 
  513. $status_sql = bp_core_get_status_sql( 'u.' ); 
  514.  
  515. /** 
  516. * Filters the SQL used to query for searched users count. 
  517. * @since 1.0.0 
  518. * @param string $value SQL statement for the searched users count query. 
  519. */ 
  520. $total_users_sql = apply_filters( 'bp_core_search_users_count_sql', $wpdb->prepare( "SELECT COUNT(DISTINCT u.ID) as id FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE %s ORDER BY pd.value ASC", $search_terms_like ), $search_terms ); 
  521.  
  522. /** 
  523. * Filters the SQL used to query for searched users. 
  524. * @since 1.0.0 
  525. * @param string $value SQL statement for the searched users query. 
  526. */ 
  527. $paged_users_sql = apply_filters( 'bp_core_search_users_sql', $wpdb->prepare( "SELECT DISTINCT u.ID as id, u.user_registered, u.user_nicename, u.user_login, u.user_email FROM {$wpdb->users} u LEFT JOIN {$bp->profile->table_name_data} pd ON u.ID = pd.user_id WHERE {$status_sql} AND pd.value LIKE %s ORDER BY pd.value ASC{$pag_sql}", $search_terms_like ), $search_terms, $pag_sql ); 
  528.  
  529. $total_users = $wpdb->get_var( $total_users_sql ); 
  530. $paged_users = $wpdb->get_results( $paged_users_sql ); 
  531.  
  532. /** 
  533. * Lets fetch some other useful data in a separate queries, this will be faster than querying the data for every user in a list. 
  534. * We can't add these to the main query above since only users who have this information will be returned (since the much of the data is in usermeta and won't support any type of directional join) 
  535. */ 
  536. foreach ( (array) $paged_users as $user ) 
  537. $user_ids[] = $user->id; 
  538.  
  539. // Add additional data to the returned results. 
  540. if ( $populate_extras ) 
  541. $paged_users = BP_Core_User::get_user_extras( $paged_users, $user_ids ); 
  542.  
  543. return array( 'users' => $paged_users, 'total' => $total_users ); 
  544.  
  545. /** 
  546. * Fetch extra user information, such as friend count and last profile update message. 
  547. * Accepts multiple user IDs to fetch data for. 
  548. * @global wpdb $wpdb WordPress database object. 
  549. * @param array $paged_users An array of stdClass containing the users. 
  550. * @param string $user_ids The user ids to select information about. 
  551. * @param string|bool $type The type of fields we wish to get. 
  552. * @return mixed False on error, otherwise associative array of results. 
  553. */ 
  554. public static function get_user_extras( &$paged_users, &$user_ids, $type = false ) { 
  555. global $wpdb; 
  556.  
  557. $bp = buddypress(); 
  558.  
  559. if ( empty( $user_ids ) ) 
  560. return $paged_users; 
  561.  
  562. // Sanitize user IDs. 
  563. $user_ids = implode( ', ', wp_parse_id_list( $user_ids ) ); 
  564.  
  565. // Fetch the user's full name. 
  566. if ( bp_is_active( 'xprofile' ) && 'alphabetical' != $type ) { 
  567. $names = $wpdb->get_results( $wpdb->prepare( "SELECT pd.user_id as id, pd.value as fullname FROM {$bp->profile->table_name_fields} pf, {$bp->profile->table_name_data} pd WHERE pf.id = pd.field_id AND pf.name = %s AND pd.user_id IN ( {$user_ids} )", bp_xprofile_fullname_field_name() ) ); 
  568. for ( $i = 0, $count = count( $paged_users ); $i < $count; ++$i ) { 
  569. foreach ( (array) $names as $name ) { 
  570. if ( $name->id == $paged_users[$i]->id ) 
  571. $paged_users[$i]->fullname = $name->fullname; 
  572.  
  573. // Fetch the user's total friend count. 
  574. if ( 'popular' != $type ) { 
  575. $friend_count = $wpdb->get_results( $wpdb->prepare( "SELECT user_id as id, meta_value as total_friend_count FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key( 'total_friend_count' ) ) ); 
  576. for ( $i = 0, $count = count( $paged_users ); $i < $count; ++$i ) { 
  577. foreach ( (array) $friend_count as $fcount ) { 
  578. if ( $fcount->id == $paged_users[$i]->id ) 
  579. $paged_users[$i]->total_friend_count = (int) $fcount->total_friend_count; 
  580.  
  581. // Fetch whether or not the user is a friend. 
  582. if ( bp_is_active( 'friends' ) ) { 
  583. $friend_status = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id, is_confirmed FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d AND friend_user_id IN ( {$user_ids} ) ) OR (initiator_user_id IN ( {$user_ids} ) AND friend_user_id = %d )", bp_loggedin_user_id(), bp_loggedin_user_id() ) ); 
  584. for ( $i = 0, $count = count( $paged_users ); $i < $count; ++$i ) { 
  585. foreach ( (array) $friend_status as $status ) { 
  586. if ( $status->initiator_user_id == $paged_users[$i]->id || $status->friend_user_id == $paged_users[$i]->id ) 
  587. $paged_users[$i]->is_friend = $status->is_confirmed; 
  588.  
  589. // Fetch the user's last_activity. 
  590. if ( 'active' != $type ) { 
  591. $user_activity = $wpdb->get_results( $wpdb->prepare( "SELECT user_id as id, meta_value as last_activity FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key( 'last_activity' ) ) ); 
  592. for ( $i = 0, $count = count( $paged_users ); $i < $count; ++$i ) { 
  593. foreach ( (array) $user_activity as $activity ) { 
  594. if ( $activity->id == $paged_users[$i]->id ) 
  595. $paged_users[$i]->last_activity = $activity->last_activity; 
  596.  
  597. // Fetch the user's latest update. 
  598. $user_update = $wpdb->get_results( $wpdb->prepare( "SELECT user_id as id, meta_value as latest_update FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} )", bp_get_user_meta_key( 'bp_latest_update' ) ) ); 
  599. for ( $i = 0, $count = count( $paged_users ); $i < $count; ++$i ) { 
  600. foreach ( (array) $user_update as $update ) { 
  601. if ( $update->id == $paged_users[$i]->id ) 
  602. $paged_users[$i]->latest_update = $update->latest_update; 
  603.  
  604. return $paged_users; 
  605.  
  606. /** 
  607. * Get WordPress user details for a specified user. 
  608. * @global wpdb $wpdb WordPress database object. 
  609. * @param int $user_id User ID. 
  610. * @return array Associative array. 
  611. */ 
  612. public static function get_core_userdata( $user_id ) { 
  613. global $wpdb; 
  614.  
  615. if ( !$user = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->users} WHERE ID = %d LIMIT 1", $user_id ) ) ) 
  616. return false; 
  617.  
  618. return $user; 
  619.  
  620. /** 
  621. * Get last activity data for a user or set of users. 
  622. * @param int|array $user_id User IDs or multiple user IDs. 
  623. * @return array 
  624. */ 
  625. public static function get_last_activity( $user_id ) { 
  626. global $wpdb; 
  627.  
  628. // Sanitize and remove empty values. 
  629. $user_ids = array_filter( wp_parse_id_list( $user_id ) ); 
  630.  
  631. if ( empty( $user_ids ) ) { 
  632. return false; 
  633.  
  634. $uncached_user_ids = bp_get_non_cached_ids( $user_ids, 'bp_last_activity' ); 
  635. if ( ! empty( $uncached_user_ids ) ) { 
  636. $bp = buddypress(); 
  637.  
  638. $user_ids_sql = implode( ', ', $uncached_user_ids ); 
  639. $user_count = count( $uncached_user_ids ); 
  640.  
  641. $last_activities = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, date_recorded FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' AND user_id IN ({$user_ids_sql}) LIMIT {$user_count}", $bp->members->id ) ); 
  642.  
  643. foreach ( $last_activities as $last_activity ) { 
  644. wp_cache_set( $last_activity->user_id, array( 
  645. 'user_id' => $last_activity->user_id,  
  646. 'date_recorded' => $last_activity->date_recorded,  
  647. 'activity_id' => $last_activity->id,  
  648. ), 'bp_last_activity' ); 
  649.  
  650. // Fetch all user data from the cache. 
  651. $retval = array(); 
  652. foreach ( $user_ids as $user_id ) { 
  653. $retval[ $user_id ] = wp_cache_get( $user_id, 'bp_last_activity' ); 
  654.  
  655. if ( isset( $retval['user_id'] ) ) { 
  656. $retval[ $user_id ]['user_id'] = (int) $retval[ $user_id ]['user_id']; 
  657. if ( isset( $retval['activity_id'] ) ) { 
  658. $retval[ $user_id ]['activity_id'] = (int) $retval[ $user_id ]['activity_id']; 
  659.  
  660. return $retval; 
  661.  
  662. /** 
  663. * Set a user's last_activity value. 
  664. * Will create a new entry if it does not exist. Otherwise updates the 
  665. * existing entry. 
  666. * @since 2.0.0 
  667. * @param int $user_id ID of the user whose last_activity you are updating. 
  668. * @param string $time MySQL-formatted time string. 
  669. * @return bool True on success, false on failure. 
  670. */ 
  671. public static function update_last_activity( $user_id, $time ) { 
  672. global $wpdb; 
  673.  
  674. $table_name = buddypress()->members->table_name_last_activity; 
  675.  
  676. $activity = self::get_last_activity( $user_id ); 
  677.  
  678. if ( ! empty( $activity[ $user_id ] ) ) { 
  679. $updated = $wpdb->update( 
  680. $table_name,  
  681.  
  682. // Data to update. 
  683. array( 
  684. 'date_recorded' => $time,  
  685. ),  
  686.  
  687. // WHERE. 
  688. array( 
  689. 'id' => $activity[ $user_id ]['activity_id'],  
  690. ),  
  691.  
  692. // Data sanitization format. 
  693. array( 
  694. '%s',  
  695. ),  
  696.  
  697. // WHERE sanitization format. 
  698. array( 
  699. '%d',  
  700. ); 
  701.  
  702. // Add new date to existing activity entry for caching. 
  703. $activity[ $user_id ]['date_recorded'] = $time; 
  704.  
  705. } else { 
  706. $updated = $wpdb->insert( 
  707. $table_name,  
  708.  
  709. // Data. 
  710. array( 
  711. 'user_id' => $user_id,  
  712. 'component' => buddypress()->members->id,  
  713. 'type' => 'last_activity',  
  714. 'action' => '',  
  715. 'content' => '',  
  716. 'primary_link' => '',  
  717. 'item_id' => 0,  
  718. 'date_recorded' => $time,  
  719. ),  
  720.  
  721. // Data sanitization format. 
  722. array( 
  723. '%d',  
  724. '%s',  
  725. '%s',  
  726. '%s',  
  727. '%s',  
  728. '%s',  
  729. '%d',  
  730. '%s',  
  731. ); 
  732.  
  733. // Set up activity array for caching. 
  734. // View the foreach loop in the get_last_activity() method for format. 
  735. $activity = array(); 
  736. $activity[ $user_id ] = array( 
  737. 'user_id' => $user_id,  
  738. 'date_recorded' => $time,  
  739. 'activity_id' => $wpdb->insert_id,  
  740. ); 
  741.  
  742. // Set cache. 
  743. wp_cache_set( $user_id, $activity[ $user_id ], 'bp_last_activity' ); 
  744.  
  745. /** 
  746. * Fires when a user's last_activity value has been updated. 
  747. * @since 2.7.0 
  748. * @param int $user_id ID of the user. 
  749. * @param string $time Last activity timestamp, in 'Y-m-d H:i:s' format. 
  750. */ 
  751. do_action( 'bp_core_user_updated_last_activity', $user_id, $time ); 
  752.  
  753. return $updated; 
  754.  
  755. /** 
  756. * Delete a user's last_activity value. 
  757. * @since 2.0.0 
  758. * @param int $user_id ID of the user whose activity should be deleted. 
  759. * @return bool True on success, false on failure or if no last_activity 
  760. * is found for the user. 
  761. */ 
  762. public static function delete_last_activity( $user_id ) { 
  763. global $wpdb; 
  764.  
  765. $existing = self::get_last_activity( $user_id ); 
  766.  
  767. if ( empty( $existing ) || empty( $existing[ $user_id ]['activity_id'] ) ) { 
  768. return false; 
  769.  
  770. $deleted = $wpdb->delete( 
  771. buddypress()->members->table_name_last_activity,  
  772.  
  773. // WHERE. 
  774. array( 
  775. 'id' => $existing[ $user_id ]['activity_id'],  
  776. ),  
  777.  
  778. // WHERE sanitization format. 
  779. array( 
  780. '%s',  
  781. ); 
  782.  
  783. wp_cache_delete( $user_id, 'bp_last_activity' ); 
  784.  
  785. return $deleted;