Changeset 10355 for trunk/src/bp-core/classes/class-bp-user-query.php
- Timestamp:
- 11/15/2015 07:13:42 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-user-query.php
r10248 r10355 147 147 public function __construct( $query = null ) { 148 148 149 // Store the raw query vars for later access 149 // Store the raw query vars for later access. 150 150 $this->query_vars_raw = $query; 151 151 152 // Allow extending classes to register action/filter hooks 152 // Allow extending classes to register action/filter hooks. 153 153 $this->setup_hooks(); 154 154 … … 184 184 185 185 // Get user ids 186 // If the user_ids param is present, we skip the query 186 // If the user_ids param is present, we skip the query. 187 187 if ( false !== $this->query_vars['user_ids'] ) { 188 188 $this->user_ids = wp_parse_id_list( $this->query_vars['user_ids'] ); … … 193 193 } 194 194 195 // Bail if no user IDs were found 195 // Bail if no user IDs were found. 196 196 if ( empty( $this->user_ids ) ) { 197 197 return; 198 198 } 199 199 200 // Fetch additional data. First, using WP_User_Query 200 // Fetch additional data. First, using WP_User_Query. 201 201 $this->do_wp_user_query(); 202 202 203 // Get BuddyPress specific user data 203 // Get BuddyPress specific user data. 204 204 $this->populate_extras(); 205 205 } … … 231 231 $bp = buddypress(); 232 232 233 // Default query variables used here 233 // Default query variables used here. 234 234 $type = ''; 235 235 $per_page = 0; … … 244 244 extract( $this->query_vars ); 245 245 246 // Setup the main SQL query container 246 // Setup the main SQL query container. 247 247 $sql = array( 248 248 'select' => '', … … 253 253 ); 254 254 255 /* *TYPE **************************************************************/255 /* TYPE **************************************************************/ 256 256 257 257 // Determines the sort order, which means it also determines where the 258 // user IDs are drawn from (the SELECT and WHERE statements) 258 // user IDs are drawn from (the SELECT and WHERE statements). 259 259 switch ( $type ) { 260 260 261 261 // 'online' query happens against the last_activity usermeta key 262 262 // Filter 'bp_user_query_online_interval' to modify the 263 // number of minutes used as an interval 263 // number of minutes used as an interval. 264 264 case 'online' : 265 265 $this->uid_name = 'user_id'; … … 282 282 283 283 // 'active', 'newest', and 'random' queries 284 // all happen against the last_activity usermeta key 284 // all happen against the last_activity usermeta key. 285 285 case 'active' : 286 286 case 'newest' : … … 303 303 break; 304 304 305 // 'popular' sorts by the 'total_friend_count' usermeta 305 // 'popular' sorts by the 'total_friend_count' usermeta. 306 306 case 'popular' : 307 307 $this->uid_name = 'user_id'; … … 314 314 break; 315 315 316 // 'alphabetical' sorts depend on the xprofile setup 316 // 'alphabetical' sorts depend on the xprofile setup. 317 317 case 'alphabetical' : 318 318 … … 321 321 // can do so if xprofile sync is enabled, or if xprofile is inactive. 322 322 // 323 // @todo remove need for bp_is_active() check 323 // @todo remove need for bp_is_active() check. 324 324 if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) { 325 325 $this->uid_name = 'ID'; … … 330 330 331 331 // When profile sync is disabled, alphabetical sorts must happen against 332 // the xprofile table 332 // the xprofile table. 333 333 } else { 334 334 $this->uid_name = 'user_id'; … … 347 347 break; 348 348 349 // Any other 'type' falls through 349 // Any other 'type' falls through. 350 350 default : 351 351 $this->uid_name = 'ID'; … … 355 355 // In this case, we assume that a plugin is 356 356 // handling order, so we leave those clauses 357 // blank 358 357 // blank. 359 358 break; 360 359 } 361 360 362 /* *WHERE *************************************************************/363 364 // 'include' - User ids to include in the results 361 /* WHERE *************************************************************/ 362 363 // 'include' - User ids to include in the results. 365 364 $include = false !== $include ? wp_parse_id_list( $include ) : array(); 366 365 $include_ids = $this->get_include_ids( $include ); … … 370 369 } 371 370 372 // 'exclude' - User ids to exclude from the results 371 // 'exclude' - User ids to exclude from the results. 373 372 if ( false !== $exclude ) { 374 373 $exclude_ids = implode( ',', wp_parse_id_list( $exclude ) ); … … 377 376 378 377 // 'user_id' - When a user id is passed, limit to the friends of the user 379 // @todo remove need for bp_is_active() check 378 // @todo remove need for bp_is_active() check. 380 379 if ( ! empty( $user_id ) && bp_is_active( 'friends' ) ) { 381 380 $friend_ids = friends_get_friend_user_ids( $user_id ); … … 386 385 387 386 // If the user has no friends, the query should always 388 // return no users 387 // return no users. 389 388 } else { 390 389 $sql['where'][] = $this->no_results['where']; … … 392 391 } 393 392 394 /* *Search Terms ******************************************************/393 /* Search Terms ******************************************************/ 395 394 396 395 // 'search_terms' searches user_login and user_nicename 397 // xprofile field matches happen in bp_xprofile_bp_user_query_search() 396 // xprofile field matches happen in bp_xprofile_bp_user_query_search(). 398 397 if ( false !== $search_terms ) { 399 398 $search_terms = bp_esc_like( wp_kses_normalize_entities( $search_terms ) ); … … 438 437 439 438 // 'meta_key', 'meta_value' allow usermeta search 440 // To avoid global joins, do a separate query 439 // To avoid global joins, do a separate query. 441 440 if ( false !== $meta_key ) { 442 441 $meta_sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key ); … … 455 454 } 456 455 457 // 'per_page', 'page' - handles LIMIT 456 // 'per_page', 'page' - handles LIMIT. 458 457 if ( !empty( $per_page ) && !empty( $page ) ) { 459 458 $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) ); … … 472 471 $sql = apply_filters_ref_array( 'bp_user_query_uid_clauses', array( $sql, &$this ) ); 473 472 474 // Assemble the query chunks 473 // Assemble the query chunks. 475 474 $this->uid_clauses['select'] = $sql['select']; 476 475 $this->uid_clauses['where'] = ! empty( $sql['where'] ) ? 'WHERE ' . implode( ' AND ', $sql['where'] ) : ''; … … 502 501 global $wpdb; 503 502 504 // If counting using SQL_CALC_FOUND_ROWS, set it up here 503 // If counting using SQL_CALC_FOUND_ROWS, set it up here. 505 504 if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) { 506 505 $this->uid_clauses['select'] = str_replace( 'SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $this->uid_clauses['select'] ); 507 506 } 508 507 509 // Get the specific user ids 508 // Get the specific user ids. 510 509 $this->user_ids = $wpdb->get_col( "{$this->uid_clauses['select']} {$this->uid_clauses['where']} {$this->uid_clauses['orderby']} {$this->uid_clauses['order']} {$this->uid_clauses['limit']}" ); 511 510 512 // Get the total user count 511 // Get the total user count. 513 512 if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) { 514 513 … … 557 556 $wp_user_query = new WP_User_Query( apply_filters( 'bp_wp_user_query_args', array( 558 557 559 // Relevant 558 // Relevant. 560 559 'fields' => $fields, 561 560 'include' => $this->user_ids, 562 561 563 562 // Overrides 564 'blog_id' => 0, // BP does not require blog roles 565 'count_total' => false // We already have a count 563 'blog_id' => 0, // BP does not require blog roles. 564 'count_total' => false // We already have a count. 566 565 567 566 ), $this ) ); … … 583 582 } 584 583 585 // Reindex for easier matching 584 // Reindex for easier matching. 586 585 $r = array(); 587 586 foreach ( $wp_user_query->results as $u ) { … … 589 588 } 590 589 591 // Match up to the user ids from the main query 590 // Match up to the user ids from the main query. 592 591 foreach ( $this->user_ids as $key => $uid ) { 593 592 if ( isset( $r[ $uid ] ) ) { … … 595 594 596 595 // The BP template functions expect an 'id' 597 // (as opposed to 'ID') property 596 // (as opposed to 'ID') property. 598 597 $this->results[ $uid ]->id = $uid; 599 598 600 // remove user ID from original user_ids property599 // Remove user ID from original user_ids property. 601 600 } else { 602 601 unset( $this->user_ids[ $key ] ); … … 619 618 * @param array $include Sanitized array of user IDs, as passed to the 'include' 620 619 * parameter of the class constructor. 621 *622 620 * @return array The list of users to which the main query should be 623 621 * limited. … … 640 638 global $wpdb; 641 639 642 // Bail if no users 640 // Bail if no users. 643 641 if ( empty( $this->user_ids ) || empty( $this->results ) ) { 644 642 return; … … 647 645 // Bail if the populate_extras flag is set to false 648 646 // In the case of the 'popular' sort type, we force 649 // populate_extras to true, because we need the friend counts 647 // populate_extras to true, because we need the friend counts. 650 648 if ( 'popular' == $this->query_vars['type'] ) { 651 649 $this->query_vars['populate_extras'] = 1; … … 656 654 } 657 655 658 // Turn user ID's into a query-usable, comma separated value 656 // Turn user ID's into a query-usable, comma separated value. 659 657 $user_ids_sql = implode( ',', wp_parse_id_list( $this->user_ids ) ); 660 658 … … 679 677 do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) ); 680 678 681 // Fetch last_active data from the activity table 679 // Fetch last_active data from the activity table. 682 680 $last_activities = BP_Core_User::get_last_activity( $this->user_ids ); 683 681 684 // Set a last_activity value for each user, even if it's empty 682 // Set a last_activity value for each user, even if it's empty. 685 683 foreach ( $this->results as $user_id => $user ) { 686 684 $user_last_activity = isset( $last_activities[ $user_id ] ) ? $last_activities[ $user_id ]['date_recorded'] : ''; … … 691 689 // We want the three following pieces of info from usermeta: 692 690 // - friend count 693 // - latest update 691 // - latest update. 694 692 $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' ); 695 693 $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' ); 696 694 697 // total_friend_count must be set for each user, even if its698 // value is 0 695 // Total_friend_count must be set for each user, even if its 696 // value is 0. 699 697 foreach ( $this->results as $uindex => $user ) { 700 698 $this->results[$uindex]->total_friend_count = 0; 701 699 } 702 700 703 // Create, prepare, and run the separate usermeta query 701 // Create, prepare, and run the separate usermeta query. 704 702 $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $bp_latest_update_key ) ); 705 703 … … 723 721 724 722 // When meta_key or meta_value have been passed to the query, 725 // fetch the resulting values for use in the template functions 723 // fetch the resulting values for use in the template functions. 726 724 if ( ! empty( $this->query_vars['meta_key'] ) ) { 727 725 $meta_sql = array( … … 758 756 * @param string|array $member_types Array or comma-separated list of member types. 759 757 * @param string $operator 'IN' or 'NOT IN'. 760 *761 758 * @return string 762 759 */ … … 805 802 $clause = ''; 806 803 807 // no_results clauses are the same between IN and NOT IN.804 // The no_results clauses are the same between IN and NOT IN. 808 805 if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) { 809 806 $clause = $this->no_results['where'];
Note: See TracChangeset
for help on using the changeset viewer.