BP_Friends_Friendship::search_friends( string $filter, int $user_id, int|null $limit = null, int|null $page = null )
Search the friends of a user by a search string.
Description Description
Parameters Parameters
- $filter
-
(Required) The search string, matched against xprofile fields (if available), or usermeta 'nickname' field.
- $user_id
-
(Required) ID of the user whose friends are being searched.
- $limit
-
(Optional) Max number of friends to return.
Default value: null
- $page
-
(Optional) The page of results to return. Default: null (no pagination - return all results).
Default value: null
Return Return
(array|bool) On success, an array: { @type array $friends IDs of friends returned by the query. @type int $count Total number of friends (disregarding pagination) who match the search. }. Returns false on failure.
Source Source
File: bp-friends/classes/class-bp-friends-friendship.php
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 | public static function search_friends( $filter , $user_id , $limit = null, $page = null ) { global $wpdb ; /* * TODO: Optimize this function. */ if ( empty ( $user_id ) ) $user_id = bp_loggedin_user_id(); // Only search for matching strings at the beginning of the // name (@todo - figure out why this restriction). $search_terms_like = bp_esc_like( $filter ) . '%' ; $pag_sql = '' ; if ( ! empty ( $limit ) && ! empty ( $page ) ) $pag_sql = $wpdb ->prepare( " LIMIT %d, %d" , intval ( ( $page - 1 ) * $limit ), intval ( $limit ) ); if ( ! $friend_ids = BP_Friends_Friendship::get_friend_user_ids( $user_id ) ) return false; // Get all the user ids for the current user's friends. $fids = implode( ',' , wp_parse_id_list( $friend_ids ) ); if ( empty ( $fids ) ) return false; $bp = buddypress(); // Filter the user_ids based on the search criteria. if ( bp_is_active( 'xprofile' ) ) { $sql = $wpdb ->prepare( "SELECT DISTINCT user_id FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE %s {$pag_sql}" , $search_terms_like ); $total_sql = $wpdb ->prepare( "SELECT COUNT(DISTINCT user_id) FROM {$bp->profile->table_name_data} WHERE user_id IN ({$fids}) AND value LIKE %s" , $search_terms_like ); } else { $sql = $wpdb ->prepare( "SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE %s {$pag_sql}" , $search_terms_like ); $total_sql = $wpdb ->prepare( "SELECT COUNT(DISTINCT user_id) FROM {$wpdb->usermeta} WHERE user_id IN ({$fids}) AND meta_key = 'nickname' AND meta_value LIKE %s" , $search_terms_like ); } $filtered_friend_ids = $wpdb ->get_col( $sql ); $total_friend_ids = $wpdb ->get_var( $total_sql ); if ( empty ( $filtered_friend_ids ) ) return false; return array ( 'friends' => array_map ( 'intval' , $filtered_friend_ids ), 'total' => (int) $total_friend_ids ); } |
Changelog Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |