bb_user_search

The BuddyPress bb user search function.

Description

bb_user_search( (string) $args = '' ); 

Parameters (1)

0. $args — Optional. (string) => ''
The args.

Usage

  1. if ( !function_exists( 'bb_user_search' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-core.php'; 
  3.  
  4. // The args. 
  5. $args = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bb_user_search($args); 
  9.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-core.php  
  1. functionbb_user_search $args = '' ) { 
  2. global $bbdb, $bb_last_countable_query; 
  3.  
  4. if ( $args && is_string( $args ) && false === strpos( $args, '=' ) ) { 
  5. $args = array( 'query' => $args ); 
  6.  
  7. $defaults = array( 
  8. 'query' => '',  
  9. 'append_meta' => true,  
  10. 'user_login' => true,  
  11. 'display_name' => true,  
  12. 'user_nicename' => false,  
  13. 'user_url' => true,  
  14. 'user_email' => false,  
  15. 'user_meta' => false,  
  16. 'users_per_page' => false,  
  17. 'page' => false,  
  18. 'roles' => false 
  19. ); 
  20.  
  21. $args = wp_parse_args( $args, $defaults ); 
  22. extract( $args, EXTR_SKIP ); 
  23.  
  24. $query = trim( $query ); 
  25. if ( $query && strlen( preg_replace( '/[^a-z0-9]/i', '', $query ) ) < 3 ) { 
  26. return new WP_Error( 'invalid-query', __('Your search term was too short') ); 
  27. $query = $bbdb->escape( $query ); 
  28.  
  29. if ( !$page ) { 
  30. $page = $GLOBALS['page']; 
  31. $page = (int) $page; 
  32.  
  33. $limit = 0 < (int) $users_per_page ? (int) $users_per_page : bb_get_option( 'page_topics' ); 
  34. if ( 1 < $page ) { 
  35. $limit = ($limit * ($page - 1)) . ", $limit"; 
  36.  
  37. $likeit = preg_replace( '/\s+/', '%', like_escape( $query ) ); 
  38.  
  39. $fields = array(); 
  40. foreach ( array( 'user_login', 'display_name', 'user_nicename', 'user_url', 'user_email' ) as $field ) { 
  41. if ( $$field ) { 
  42. $fields[] = $field; 
  43.  
  44. if ( $roles ) { 
  45. $roles = (array) $roles; 
  46.  
  47. if ( $roles && !empty( $roles ) && false === $role_user_ids = apply_filters( 'bb_user_search_role_user_ids', false, $roles, $args ) ) { 
  48. $role_meta_key = $bbdb->escape( $bbdb->prefix . 'capabilities' ); 
  49. $role_sql_terms = array(); 
  50. foreach ( $roles as $role ) { 
  51. $role_sql_terms[] = "`meta_value` LIKE '%" . $bbdb->escape( like_escape( $role ) ) . "%'"; 
  52. $role_sql_terms = join( ' OR ', $role_sql_terms ); 
  53. $role_sql = "SELECT `user_id` FROM `$bbdb->usermeta` WHERE `meta_key` = '$role_meta_key' AND ($role_sql_terms);"; 
  54. $role_user_ids = $bbdb->get_col( $role_sql, 0 ); 
  55. if ( is_wp_error( $role_user_ids ) ) { 
  56. return false; 
  57.  
  58. if ( is_array( $role_user_ids ) && empty( $role_user_ids ) ) { 
  59. return false; 
  60.  
  61. if ( $query && $user_meta && false === $meta_user_ids = apply_filters( 'bb_user_search_meta_user_ids', false, $args ) ) { 
  62. $meta_sql = "SELECT `user_id` FROM `$bbdb->usermeta` WHERE `meta_value` LIKE ('%$likeit%')"; 
  63. if ( empty( $fields ) ) { 
  64. $meta_sql .= " LIMIT $limit"; 
  65. $meta_user_ids = $bbdb->get_col( $meta_sql, 0 ); 
  66. if ( is_wp_error( $meta_user_ids ) ) { 
  67. $meta_user_ids = false; 
  68.  
  69. $user_ids = array(); 
  70. if ( $role_user_ids && $meta_user_ids ) { 
  71. $user_ids = array_intersect( (array) $role_user_ids, (array) $meta_user_ids ); 
  72. } elseif ( $role_user_ids ) { 
  73. $user_ids = (array) $role_user_ids; 
  74. } elseif ( $meta_user_ids ) { 
  75. $user_ids = (array) $meta_user_ids; 
  76.  
  77. $sql = "SELECT * FROM $bbdb->users"; 
  78.  
  79. $sql_terms = array(); 
  80. if ( $query && count( $fields ) ) { 
  81. foreach ( $fields as $field ) { 
  82. $sql_terms[] = "$field LIKE ('%$likeit%')"; 
  83.  
  84. $user_ids_sql = ''; 
  85. if ( $user_ids ) { 
  86. $user_ids_sql = "AND ID IN (". join(', ', $user_ids) . ")"; 
  87.  
  88. if ( $query && empty( $sql_terms ) ) { 
  89. return new WP_Error( 'invalid-query', __( 'Your query parameters are invalid' ) ); 
  90.  
  91. if ( count( $sql_terms ) || count( $user_ids ) ) { 
  92. $sql .= ' WHERE '; 
  93.  
  94. if ( count( $sql_terms ) ) { 
  95. $sql .= '(' . implode( ' OR ', $sql_terms ) . ')'; 
  96.  
  97. if ( count( $sql_terms ) && count( $user_ids ) ) { 
  98. $sql .= ' AND '; 
  99.  
  100. if ( count( $user_ids ) ) { 
  101. $sql .= '`ID` IN (' . join( ', ', $user_ids ) . ')'; 
  102.  
  103. $sql .= " ORDER BY user_login LIMIT $limit"; 
  104.  
  105. $bb_last_countable_query = $sql; 
  106.  
  107. do_action( bb_user_search, $sql, $args ); 
  108.  
  109. if ( ( $users = $bbdb->get_results( $sql ) ) && $append_meta ) { 
  110. return bb_append_meta( $users, 'user' ); 
  111.  
  112. return $users ? $users : false;