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