| | 7 | * BuddyPress User Query class |
| | 8 | * |
| | 9 | * Used for querying users in a BuddyPress context, in situations where |
| | 10 | * WP_User_Query won't do the trick: Member directories, the Friends component, |
| | 11 | * etc. |
| | 12 | * |
| | 13 | * @since BuddyPress (1.7) |
| | 14 | */ |
| | 15 | class BP_User_Query { |
| | 16 | |
| | 17 | /** Variables *************************************************************/ |
| | 18 | |
| | 19 | /** |
| | 20 | * Array of variables to query with |
| | 21 | * |
| | 22 | * @since BuddyPress (1.7) |
| | 23 | * @var array |
| | 24 | */ |
| | 25 | public $query_vars = array(); |
| | 26 | |
| | 27 | /** |
| | 28 | * List of found users and their respective data |
| | 29 | * |
| | 30 | * @since BuddyPress (1.7) |
| | 31 | * @access public To allow components to manipulate them |
| | 32 | * @var array |
| | 33 | */ |
| | 34 | public $results = array(); |
| | 35 | |
| | 36 | /** |
| | 37 | * Total number of found users for the current query |
| | 38 | * |
| | 39 | * @since BuddyPress (1.7) |
| | 40 | * @access public To allow components to manipulate it |
| | 41 | * @var int |
| | 42 | */ |
| | 43 | public $total_users = 0; |
| | 44 | |
| | 45 | /** |
| | 46 | * List of found user ID's |
| | 47 | * |
| | 48 | * @since BuddyPress (1.7) |
| | 49 | * @access private To disallow components from manipulating them |
| | 50 | * @var array |
| | 51 | */ |
| | 52 | private $user_ids = array(); |
| | 53 | |
| | 54 | /** |
| | 55 | * SQL clauses for the user ID query |
| | 56 | * |
| | 57 | * @since BuddyPress (1.7) |
| | 58 | * @access protected To disallow |
| | 59 | * @var array() |
| | 60 | */ |
| | 61 | private $uid_clauses = array(); |
| | 62 | |
| | 63 | /** |
| | 64 | * SQL database column name to order by |
| | 65 | * |
| | 66 | * @since BuddyPress (1.7) |
| | 67 | * @var string |
| | 68 | */ |
| | 69 | private $uid_name = ''; |
| | 70 | |
| | 71 | /** Methods ***************************************************************/ |
| | 72 | |
| | 73 | /** |
| | 74 | * Constructor |
| | 75 | * |
| | 76 | * @since 1.7 |
| | 77 | * |
| | 78 | * @param string|array $query The query variables |
| | 79 | */ |
| | 80 | public function __construct( $query = null ) { |
| | 81 | if ( ! empty( $query ) ) { |
| | 82 | $this->query_vars = wp_parse_args( $query, array( |
| | 83 | 'type' => 'newest', |
| | 84 | 'per_page' => 0, |
| | 85 | 'page' => 1, |
| | 86 | 'user_id' => 0, |
| | 87 | 'include' => false, |
| | 88 | 'search_terms' => false, |
| | 89 | 'exclude' => false, |
| | 90 | 'meta_key' => false, |
| | 91 | 'meta_value' => false, |
| | 92 | 'populate_extras' => true, |
| | 93 | 'count_total' => 'count_query' |
| | 94 | ) ); |
| | 95 | |
| | 96 | // Get user ids |
| | 97 | $this->prepare_user_ids_query(); |
| | 98 | $this->do_user_ids_query(); |
| | 99 | } |
| | 100 | |
| | 101 | // Bail if no user IDs were found |
| | 102 | if ( empty( $this->user_ids ) ) { |
| | 103 | return; |
| | 104 | } |
| | 105 | |
| | 106 | // Fetch additional data. First, using WP_User_Query |
| | 107 | $this->do_wp_user_query(); |
| | 108 | |
| | 109 | // Get BuddyPress specific user data |
| | 110 | $this->populate_extras(); |
| | 111 | } |
| | 112 | |
| | 113 | /** |
| | 114 | * Prepare the query for user_ids |
| | 115 | * |
| | 116 | * @since BuddyPress (1.7) |
| | 117 | */ |
| | 118 | protected function prepare_user_ids_query() { |
| | 119 | global $wpdb, $bp; |
| | 120 | |
| | 121 | // Default query variables used here |
| | 122 | $type = ''; |
| | 123 | $per_page = 0; |
| | 124 | $page = 1; |
| | 125 | $user_id = 0; |
| | 126 | $include = false; |
| | 127 | $search_terms = false; |
| | 128 | $exclude = false; |
| | 129 | $meta_key = false; |
| | 130 | $meta_value = false; |
| | 131 | |
| | 132 | extract( $this->query_vars ); |
| | 133 | |
| | 134 | // Setup the main SQL query container |
| | 135 | $sql = array( |
| | 136 | 'select' => '', |
| | 137 | 'where' => array(), |
| | 138 | 'orderby' => '', |
| | 139 | 'order' => '', |
| | 140 | 'limit' => '' |
| | 141 | ); |
| | 142 | |
| | 143 | /** TYPE **************************************************************/ |
| | 144 | |
| | 145 | // Determines the sort order, which means it also determines where the |
| | 146 | // user IDs are drawn from (the SELECT and WHERE statements) |
| | 147 | switch ( $type ) { |
| | 148 | |
| | 149 | // 'active', 'online', 'newest', and 'random' queries |
| | 150 | // all happen against the last_activity usermeta key |
| | 151 | case 'active' : |
| | 152 | case 'online' : |
| | 153 | case 'newest' : |
| | 154 | case 'random' : |
| | 155 | $this->uid_name = 'user_id'; |
| | 156 | $sql['select'] = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$wpdb->usermeta} u"; |
| | 157 | $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'last_activity' ) ); |
| | 158 | |
| | 159 | if ( 'newest' == $type ) { |
| | 160 | $sql['orderby'] = "ORDER BY u.user_id"; |
| | 161 | $sql['order'] = "DESC"; |
| | 162 | } else if ( 'random' == $type ) { |
| | 163 | $sql['orderby'] = "ORDER BY rand()"; |
| | 164 | } else { |
| | 165 | $sql['orderby'] = "ORDER BY u.meta_value"; |
| | 166 | $sql['order'] = "DESC"; |
| | 167 | } |
| | 168 | |
| | 169 | break; |
| | 170 | |
| | 171 | // 'popular' sorts by the 'total_friend_count' usermeta |
| | 172 | case 'popular' : |
| | 173 | $this->uid_name = 'user_id'; |
| | 174 | $sql['select'] = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$wpdb->usermeta} u"; |
| | 175 | $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) ); |
| | 176 | $sql['orderby'] = "ORDER BY u.meta_value"; |
| | 177 | $sql['order'] = "DESC"; |
| | 178 | |
| | 179 | break; |
| | 180 | |
| | 181 | // 'alphabetical' sorts depend on the xprofile setup |
| | 182 | case 'alphabetical' : |
| | 183 | |
| | 184 | // We prefer to do alphabetical sorts against the display_name field |
| | 185 | // of wp_users, because the table is smaller and better indexed. We |
| | 186 | // can do so if xprofile sync is enabled, or if xprofile is inactive. |
| | 187 | // |
| | 188 | // @todo remove need for bp_is_active() check |
| | 189 | if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) { |
| | 190 | $this->uid_name = 'ID'; |
| | 191 | $sql['select'] = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$wpdb->users} u"; |
| | 192 | $sql['orderby'] = "ORDER BY u.display_name"; |
| | 193 | $sql['order'] = "ASC"; |
| | 194 | |
| | 195 | // When profile sync is disabled, alphabetical sorts must happen against |
| | 196 | // the xprofile table |
| | 197 | } else { |
| | 198 | $fullname_field_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s", bp_xprofile_fullname_field_name() ) ); |
| | 199 | |
| | 200 | $this->uid_name = 'user_id'; |
| | 201 | $sql['select'] = "SELECT DISTINCT u.{$this->uid_name} as id FROM {$bp->profile->table_name_data} u"; |
| | 202 | $sql['where'][] = "u.field_id = {$fullname_field_id}"; |
| | 203 | $sql['orderby'] = "ORDER BY u.value"; |
| | 204 | $sql['order'] = "ASC"; |
| | 205 | } |
| | 206 | |
| | 207 | break; |
| | 208 | } |
| | 209 | |
| | 210 | /** WHERE *************************************************************/ |
| | 211 | |
| | 212 | // 'include' - User ids to include in the results |
| | 213 | if ( false !== $include ) { |
| | 214 | $include = wp_parse_id_list( $include ); |
| | 215 | $include_ids = $wpdb->escape( implode( ',', (array) $include ) ); |
| | 216 | $sql['where'][] = "u.{$this->uid_name} IN ({$include_ids})"; |
| | 217 | } |
| | 218 | |
| | 219 | // 'exclude' - User ids to exclude from the results |
| | 220 | if ( false !== $exclude ) { |
| | 221 | $exclude = wp_parse_id_list( $exclude ); |
| | 222 | $exclude_ids = $wpdb->escape( implode( ',', (array) $exclude ) ); |
| | 223 | $sql['where'][] = "u.{$this->uid_name} NOT IN ({$exclude_ids})"; |
| | 224 | } |
| | 225 | |
| | 226 | // 'user_id' - When a user id is passed, limit to the friends of the user |
| | 227 | // @todo remove need for bp_is_active() check |
| | 228 | if ( !empty( $user_id ) && bp_is_active( 'friends' ) ) { |
| | 229 | $friend_ids = friends_get_friend_user_ids( $user_id ); |
| | 230 | $friend_ids = $wpdb->escape( implode( ',', (array) $friend_ids ) ); |
| | 231 | |
| | 232 | if ( !empty( $friend_ids ) ) { |
| | 233 | $sql['where'][] = "u.{$this->uid_name} NOT IN ({$friend_ids})"; |
| | 234 | } else { |
| | 235 | // If the user has no friends, make sure the query returns null |
| | 236 | $sql['where'][] = "0 = 1"; |
| | 237 | } |
| | 238 | } |
| | 239 | |
| | 240 | /** Search Terms ******************************************************/ |
| | 241 | |
| | 242 | // 'search_terms' searches the xprofile fields |
| | 243 | // To avoid global joins, do a separate query |
| | 244 | // @todo remove need for bp_is_active() check |
| | 245 | if ( false !== $search_terms && bp_is_active( 'xprofile' ) ) { |
| | 246 | $found_user_ids = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE %s", '%%' . like_escape( $search_terms ) . '%%' ), ARRAY_N ); |
| | 247 | |
| | 248 | if ( ! empty( $found_user_ids ) ) { |
| | 249 | $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")"; |
| | 250 | } |
| | 251 | } |
| | 252 | |
| | 253 | // 'meta_key', 'meta_value' allow usermeta search |
| | 254 | // To avoid global joins, do a separate query |
| | 255 | if ( false !== $meta_key ) { |
| | 256 | $meta_sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key ); |
| | 257 | |
| | 258 | if ( false !== $meta_value ) { |
| | 259 | $meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value ); |
| | 260 | } |
| | 261 | |
| | 262 | $found_user_ids = $wpdb->get_col( $meta_sql ); |
| | 263 | |
| | 264 | if ( ! empty( $found_user_ids ) ) { |
| | 265 | $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")"; |
| | 266 | } |
| | 267 | } |
| | 268 | |
| | 269 | // 'per_page', 'page' - handles LIMIT |
| | 270 | if ( !empty( $per_page ) && !empty( $page ) ) { |
| | 271 | $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) ); |
| | 272 | } else { |
| | 273 | $sql['limit'] = ''; |
| | 274 | } |
| | 275 | |
| | 276 | // Assemble the query chunks |
| | 277 | $this->uid_clauses['select'] = $sql['select']; |
| | 278 | $this->uid_clauses['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : ''; |
| | 279 | $this->uid_clauses['orderby'] = $sql['orderby']; |
| | 280 | $this->uid_clauses['order'] = $sql['order']; |
| | 281 | $this->uid_clauses['limit'] = $sql['limit']; |
| | 282 | |
| | 283 | do_action_ref_array( 'bp_pre_user_query', array( &$this ) ); |
| | 284 | } |
| | 285 | |
| | 286 | /** |
| | 287 | * Perform a database query to specifically get only user IDs, using |
| | 288 | * existing query variables set previously in the constructor. |
| | 289 | * |
| | 290 | * Also used to quickly perform user total counts. |
| | 291 | * |
| | 292 | * @since BuddyPress (1.7) |
| | 293 | */ |
| | 294 | protected function do_user_ids_query() { |
| | 295 | global $wpdb; |
| | 296 | |
| | 297 | // If counting using SQL_CALC_FOUND_ROWS, set it up here |
| | 298 | if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) { |
| | 299 | $this->uid_clauses['select'] = str_replace( 'SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $this->uid_clauses['select'] ); |
| | 300 | } |
| | 301 | |
| | 302 | // Get the specific user ids |
| | 303 | $this->user_ids = $wpdb->get_col( $wpdb->prepare( "{$this->uid_clauses['select']} {$this->uid_clauses['where']} {$this->uid_clauses['orderby']} {$this->uid_clauses['order']} {$this->uid_clauses['limit']}" ) ); |
| | 304 | |
| | 305 | // Get the total user count |
| | 306 | if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) { |
| | 307 | $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "SELECT FOUND_ROWS()", $this ) ); |
| | 308 | } elseif ( 'count_query' == $this->query_vars['count_total'] ) { |
| | 309 | $count_select = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(DISTINCT u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] ); |
| | 310 | $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "{$count_select} {$this->uid_clauses['where']}", $this ) ); |
| | 311 | } |
| | 312 | } |
| | 313 | |
| | 314 | /** |
| | 315 | * Perform a database query using the WP_User_Query() object, using existing |
| | 316 | * fields, variables, and user ID's set previously in this class. |
| | 317 | * |
| | 318 | * @since BuddyPress (1.7) |
| | 319 | */ |
| | 320 | protected function do_wp_user_query() { |
| | 321 | $wp_user_query = new WP_User_Query( apply_filters( 'bp_wp_user_query_args', array( |
| | 322 | |
| | 323 | // Relevant |
| | 324 | 'fields' => array( 'ID', 'user_registered', 'user_login', 'user_nicename', 'display_name', 'user_email' ), |
| | 325 | 'include' => $this->user_ids, |
| | 326 | |
| | 327 | // Overrides |
| | 328 | 'blog_id' => 0, // BP does not require blog roles |
| | 329 | 'count_total' => false // We already have a count |
| | 330 | |
| | 331 | ), $this ) ); |
| | 332 | |
| | 333 | // Reindex for easier matching |
| | 334 | $r = array(); |
| | 335 | foreach ( $wp_user_query->results as $u ) { |
| | 336 | $r[ $u->ID ] = $u; |
| | 337 | } |
| | 338 | |
| | 339 | // Match up to the user ids from the main query |
| | 340 | foreach ( $this->user_ids as $uid ) { |
| | 341 | if ( isset( $r[ $uid ] ) ) { |
| | 342 | $this->results[ $uid ] = $r[ $uid ]; |
| | 343 | |
| | 344 | // The BP template functions expect an 'id' |
| | 345 | // (as opposed to 'ID') property |
| | 346 | $this->results[ $uid ]->id = $uid; |
| | 347 | } |
| | 348 | } |
| | 349 | } |
| | 350 | |
| | 351 | /** |
| | 352 | * Perform a database query to populate any extra metadata we might need. |
| | 353 | * Different components will hook into the 'bp_user_query_populate_extras' |
| | 354 | * action to loop in the things they want. |
| | 355 | * |
| | 356 | * @since BuddyPress (1.7) |
| | 357 | * |
| | 358 | * @global BuddyPress $bp |
| | 359 | * @global WPDB $wpdb |
| | 360 | * @return |
| | 361 | */ |
| | 362 | protected function populate_extras() { |
| | 363 | global $wpdb; |
| | 364 | |
| | 365 | // Bail if no users |
| | 366 | if ( empty( $this->user_ids ) || empty( $this->results ) ) { |
| | 367 | return; |
| | 368 | } |
| | 369 | |
| | 370 | // Bail if the populate_extras flag is set to false |
| | 371 | // In the case of the 'popular' sort type, we force |
| | 372 | // populate_extras to true, because we need the friend counts |
| | 373 | if ( 'popular' == $this->query_vars['type'] ) { |
| | 374 | $this->query_vars['populate_extras'] = 1; |
| | 375 | } |
| | 376 | |
| | 377 | if ( ! (bool) $this->query_vars['populate_extras'] ) { |
| | 378 | return; |
| | 379 | } |
| | 380 | |
| | 381 | // Turn user ID's into a query-usable, comma separated value |
| | 382 | $user_ids_sql = implode( ',', wp_parse_id_list( $this->user_ids ) ); |
| | 383 | |
| | 384 | /** |
| | 385 | * Use this action to independently populate your own custom extras. |
| | 386 | * |
| | 387 | * Note that anything you add here should query using $user_ids_sql, to |
| | 388 | * avoid running multiple queries per user in the loop. |
| | 389 | * |
| | 390 | * Two BuddyPress components currently do this: |
| | 391 | * - XProfile: To override display names |
| | 392 | * - Friends: To set whether or not a user is the current users friend |
| | 393 | * |
| | 394 | * @see bp_xprofile_filter_user_query_populate_extras() |
| | 395 | * @see bp_friends_filter_user_query_populate_extras() |
| | 396 | */ |
| | 397 | do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) ); |
| | 398 | |
| | 399 | // Fetch usermeta data |
| | 400 | // We want the three following pieces of info from usermeta: |
| | 401 | // - friend count |
| | 402 | // - last activity |
| | 403 | // - latest update |
| | 404 | $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' ); |
| | 405 | $last_activity_key = bp_get_user_meta_key( 'last_activity' ); |
| | 406 | $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' ); |
| | 407 | |
| | 408 | // total_friend_count must be set for each user, even if its |
| | 409 | // value is 0 |
| | 410 | foreach ( $this->results as $uindex => $user ) { |
| | 411 | $this->results[$uindex]->total_friend_count = 0; |
| | 412 | } |
| | 413 | |
| | 414 | // Create, prepare, and run the seperate usermeta query |
| | 415 | $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $last_activity_key, $bp_latest_update_key ) ); |
| | 416 | |
| | 417 | // The $members_template global expects the index key to be different |
| | 418 | // from the meta_key in some cases, so we rejig things here. |
| | 419 | foreach ( $user_metas as $user_meta ) { |
| | 420 | switch ( $user_meta->meta_key ) { |
| | 421 | case $total_friend_count_key : |
| | 422 | $key = 'total_friend_count'; |
| | 423 | break; |
| | 424 | |
| | 425 | case $last_activity_key : |
| | 426 | $key = 'last_activity'; |
| | 427 | break; |
| | 428 | |
| | 429 | case $bp_latest_update_key : |
| | 430 | $key = 'latest_update'; |
| | 431 | break; |
| | 432 | } |
| | 433 | |
| | 434 | if ( isset( $this->results[ $user_meta->user_id ] ) ) { |
| | 435 | $this->results[ $user_meta->user_id ]->{$key} = $user_meta->meta_value; |
| | 436 | } |
| | 437 | } |
| | 438 | |
| | 439 | // When meta_key or meta_value have been passed to the query, |
| | 440 | // fetch the resulting values for use in the template functions |
| | 441 | if ( ! empty( $this->query_vars['meta_key'] ) ) { |
| | 442 | $meta_sql = array( |
| | 443 | 'select' => "SELECT user_id, meta_key, meta_value", |
| | 444 | 'from' => "FROM $wpdb->usermeta", |
| | 445 | 'where' => $wpdb->prepare( "WHERE meta_key = %s", $this->query_vars['meta_key'] ) |
| | 446 | ); |
| | 447 | |
| | 448 | if ( false !== $this->query_vars['meta_value'] ) { |
| | 449 | $meta_sql['where'] .= $wpdb->prepare( " AND meta_value = %s", $this->query_vars['meta_value'] ); |
| | 450 | } |
| | 451 | |
| | 452 | $metas = $wpdb->get_results( $wpdb->prepare( "{$meta_sql['select']} {$meta_sql['from']} {$meta_sql['where']}" ) ); |
| | 453 | |
| | 454 | if ( ! empty( $metas ) ) { |
| | 455 | foreach ( $metas as $meta ) { |
| | 456 | if ( isset( $this->results[ $meta->user_id ] ) ) { |
| | 457 | $this->results[ $meta->user_id ]->meta_key = $meta->meta_key; |
| | 458 | |
| | 459 | if ( ! empty( $meta->meta_value ) ) { |
| | 460 | $this->results[ $meta->user_id ]->meta_value = $meta->meta_value; |
| | 461 | } |
| | 462 | } |
| | 463 | } |
| | 464 | } |
| | 465 | } |
| | 466 | } |
| | 467 | } |
| | 468 | |
| | 469 | /** |