Changeset 13887 for trunk/src/bp-core/classes/class-bp-user-query.php
- Timestamp:
- 06/01/2024 10:19:51 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-user-query.php
r13395 r13887 18 18 * 19 19 * @since 1.7.0 20 * @since 10.0.0 Added $date_queryparameter.20 * @since 10.0.0 Added `$date_query` parameter. 21 21 * 22 22 * @param array $query { … … 44 44 * @type array|string $member_type__in Array or comma-separated list of member types to limit results to. 45 45 * @type array|string $member_type__not_in Array or comma-separated list of member types that will be 46 * excluded from results.46 * excluded from results. 47 47 * @type string|bool $meta_key Limit results to users that have usermeta associated with this meta_key. 48 48 * Usually used with $meta_value. Default: false. … … 72 72 * 73 73 * @since 1.8.0 74 * 74 75 * @var array 75 76 */ … … 80 81 * 81 82 * @since 1.7.0 83 * 82 84 * @var array 83 85 */ … … 88 90 * 89 91 * @since 1.7.0 92 * 90 93 * @var array 91 94 */ … … 96 99 * 97 100 * @since 1.7.0 101 * 98 102 * @var int 99 103 */ … … 104 108 * 105 109 * @since 1.7.0 110 * 106 111 * @var array 107 112 */ … … 112 117 * 113 118 * @since 1.7.0 119 * 114 120 * @var array 115 121 */ … … 120 126 * 121 127 * @since 2.2.0 128 * 122 129 * @var string 123 130 */ … … 128 135 * 129 136 * @since 1.7.0 137 * 130 138 * @var string 131 139 */ … … 136 144 * 137 145 * @since 1.7.0 138 * @var string 139 */ 140 protected $no_results = array( 'join' => '', 'where' => '0 = 1' ); 141 146 * 147 * @var array 148 */ 149 protected $no_results = array( 150 'join' => '', 151 'where' => '0 = 1', 152 ); 142 153 143 154 /** Methods ***************************************************************/ … … 233 244 * Prepare the query for user_ids. 234 245 * 246 * @global wpdb $wpdb WordPress database object. 247 * 235 248 * @since 1.7.0 236 249 */ … … 256 269 $sql = array( 257 270 'select' => '', 258 'where' => array( '1=1'),271 'where' => array( '1=1' ), 259 272 'orderby' => '', 260 273 'order' => '', 261 'limit' => '' 274 'limit' => '', 262 275 ); 263 276 … … 271 284 // Filter 'bp_user_query_online_interval' to modify the 272 285 // number of minutes used as an interval. 273 case 'online' :274 $this->uid_name = 'user_id';286 case 'online': 287 $this->uid_name = 'user_id'; 275 288 $this->uid_table = $bp->members->table_name_last_activity; 276 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";277 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );289 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 290 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); 278 291 279 292 /** … … 284 297 * @param int $value Amount of minutes for threshold. Default 15. 285 298 */ 286 $sql['where'][] = $wpdb->prepare( "u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) );287 $sql['orderby'] = "ORDER BY u.date_recorded";288 $sql['order'] = "DESC";299 $sql['where'][] = $wpdb->prepare( 'u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )', apply_filters( 'bp_user_query_online_interval', 15 ) ); 300 $sql['orderby'] = 'ORDER BY u.date_recorded'; 301 $sql['order'] = 'DESC'; 289 302 290 303 // Date query. … … 298 311 // 'active', 'newest', and 'random' queries 299 312 // all happen against the last_activity usermeta key. 300 case 'active' :301 case 'newest' :302 case 'random' :303 $this->uid_name = 'user_id';313 case 'active': 314 case 'newest': 315 case 'random': 316 $this->uid_name = 'user_id'; 304 317 $this->uid_table = $bp->members->table_name_last_activity; 305 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";306 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id );307 308 if ( 'newest' == $type ) {309 $sql['orderby'] = "ORDER BY u.user_id";310 $sql['order'] = "DESC";311 } elseif ( 'random' == $type ) {312 $sql['orderby'] = "ORDER BY rand()";318 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 319 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); 320 321 if ( 'newest' === $type ) { 322 $sql['orderby'] = 'ORDER BY u.user_id'; 323 $sql['order'] = 'DESC'; 324 } elseif ( 'random' === $type ) { 325 $sql['orderby'] = 'ORDER BY rand()'; 313 326 } else { 314 $sql['orderby'] = "ORDER BY u.date_recorded";315 $sql['order'] = "DESC";327 $sql['orderby'] = 'ORDER BY u.date_recorded'; 328 $sql['order'] = 'DESC'; 316 329 } 317 330 … … 325 338 326 339 // 'popular' sorts by the 'total_friend_count' usermeta. 327 case 'popular' :328 $this->uid_name = 'user_id';340 case 'popular': 341 $this->uid_name = 'user_id'; 329 342 $this->uid_table = $wpdb->usermeta; 330 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";331 $sql['where'][] = $wpdb->prepare( "u.meta_key = %s", bp_get_user_meta_key( 'total_friend_count' ) );332 $sql['orderby'] = "ORDER BY CONVERT(u.meta_value, SIGNED)";333 $sql['order'] = "DESC";343 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 344 $sql['where'][] = $wpdb->prepare( 'u.meta_key = %s', bp_get_user_meta_key( 'total_friend_count' ) ); 345 $sql['orderby'] = 'ORDER BY CONVERT(u.meta_value, SIGNED)'; 346 $sql['order'] = 'DESC'; 334 347 335 348 break; 336 349 337 350 // 'alphabetical' sorts depend on the xprofile setup. 338 case 'alphabetical' : 339 351 case 'alphabetical': 340 352 // We prefer to do alphabetical sorts against the display_name field 341 353 // of wp_users, because the table is smaller and better indexed. We … … 344 356 // @todo remove need for bp_is_active() check. 345 357 if ( ! bp_disable_profile_sync() || ! bp_is_active( 'xprofile' ) ) { 346 $this->uid_name = 'ID';358 $this->uid_name = 'ID'; 347 359 $this->uid_table = $wpdb->users; 348 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";349 $sql['orderby'] = "ORDER BY u.display_name";350 $sql['order'] = "ASC";351 352 // When profile sync is disabled, alphabetical sorts must happen against353 // the xprofile table.360 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 361 $sql['orderby'] = 'ORDER BY u.display_name'; 362 $sql['order'] = 'ASC'; 363 364 // When profile sync is disabled, alphabetical sorts must happen against 365 // the xprofile table. 354 366 } else { 355 $this->uid_name = 'user_id';367 $this->uid_name = 'user_id'; 356 368 $this->uid_table = $bp->profile->table_name_data; 357 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";358 $sql['where'][] = $wpdb->prepare( "u.field_id = %d", bp_xprofile_fullname_field_id() );359 $sql['orderby'] = "ORDER BY u.value";360 $sql['order'] = "ASC";369 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 370 $sql['where'][] = $wpdb->prepare( 'u.field_id = %d', bp_xprofile_fullname_field_id() ); 371 $sql['orderby'] = 'ORDER BY u.value'; 372 $sql['order'] = 'ASC'; 361 373 } 362 374 … … 364 376 // to infer spam/deleted/non-activated users. To ensure that these users 365 377 // are filtered out, we add an appropriate sub-query. 366 $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql( '' ) . " )";378 $sql['where'][] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE " . bp_core_get_status_sql( '' ) . ' )'; 367 379 368 380 break; 369 381 370 382 // Any other 'type' falls through. 371 default :372 $this->uid_name = 'ID';383 default: 384 $this->uid_name = 'ID'; 373 385 $this->uid_table = $wpdb->users; 374 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u";386 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$this->uid_table} u"; 375 387 376 388 // In this case, we assume that a plugin is … … 387 399 388 400 // An array containing nothing but 0 should always fail. 389 if ( 1 === count( $include_ids ) && 0 == reset( $include_ids ) ) {401 if ( 1 === count( $include_ids ) && 0 === reset( $include_ids ) ) { 390 402 $sql['where'][] = $this->no_results['where']; 391 403 } elseif ( ! empty( $include_ids ) ) { … … 409 421 $sql['where'][] = "u.{$this->uid_name} IN ({$friend_ids})"; 410 422 411 // If the user has no friends, the query should always412 // return no users.423 // If the user has no friends, the query should always 424 // return no users. 413 425 } else { 414 426 $sql['where'][] = $this->no_results['where']; … … 427 439 $search_terms_space = '%' . $search_terms . ' %'; 428 440 } elseif ( $search_wildcard === 'right' ) { 429 $search_terms_nospace = $search_terms . '%';441 $search_terms_nospace = $search_terms . '%'; 430 442 $search_terms_space = '% ' . $search_terms . '%'; 431 443 } else { … … 434 446 } 435 447 436 $matched_user_ids = $wpdb->get_col( $wpdb->prepare( 437 "SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s )", 438 $search_terms_nospace, 439 $search_terms_space, 440 $search_terms_nospace, 441 $search_terms_space 442 ) ); 443 444 $match_in_clause = empty( $matched_user_ids) ? 'NULL' : implode( ',', $matched_user_ids ); 448 $matched_user_ids = $wpdb->get_col( 449 $wpdb->prepare( 450 "SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE %s OR user_login LIKE %s OR user_nicename LIKE %s OR user_nicename LIKE %s )", 451 $search_terms_nospace, 452 $search_terms_space, 453 $search_terms_nospace, 454 $search_terms_space 455 ) 456 ); 457 458 $match_in_clause = empty( $matched_user_ids ) ? 'NULL' : implode( ',', $matched_user_ids ); 445 459 $sql['where']['search'] = "u.{$this->uid_name} IN ({$match_in_clause})"; 446 460 } … … 455 469 $member_type_clause = $this->get_sql_clause_for_member_types( $member_type__not_in, 'NOT IN' ); 456 470 457 // Member types to include.471 // Member types to include. 458 472 } elseif ( ! empty( $member_type ) ) { 459 473 $member_type_clause = $this->get_sql_clause_for_member_types( $member_type, 'IN' ); … … 470 484 471 485 if ( false !== $meta_value ) { 472 $meta_sql .= $wpdb->prepare( " AND meta_value = %s", $meta_value );486 $meta_sql .= $wpdb->prepare( ' AND meta_value = %s', $meta_value ); 473 487 } 474 488 … … 476 490 477 491 if ( ! empty( $found_user_ids ) ) { 478 $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")";492 $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ')'; 479 493 } else { 480 494 $sql['where'][] = '1 = 0'; … … 483 497 484 498 // 'per_page', 'page' - handles LIMIT. 485 if ( ! empty( $per_page ) && !empty( $page ) ) {486 $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $page - 1 ) * $per_page ), intval( $per_page ) );499 if ( ! empty( $per_page ) && ! empty( $page ) ) { 500 $sql['limit'] = $wpdb->prepare( 'LIMIT %d, %d', intval( ( $page - 1 ) * $per_page ), intval( $per_page ) ); 487 501 } else { 488 502 $sql['limit'] = ''; … … 524 538 * Also used to quickly perform user total counts. 525 539 * 540 * @global wpdb $wpdb WordPress database object. 541 * 526 542 * @since 1.7.0 527 543 */ … … 530 546 531 547 // If counting using SQL_CALC_FOUND_ROWS, set it up here. 532 if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) {548 if ( 'sql_calc_found_rows' === $this->query_vars['count_total'] ) { 533 549 $this->uid_clauses['select'] = str_replace( 'SELECT', 'SELECT SQL_CALC_FOUND_ROWS', $this->uid_clauses['select'] ); 534 550 } … … 538 554 539 555 // Get the total user count. 540 if ( 'sql_calc_found_rows' == $this->query_vars['count_total'] ) {556 if ( 'sql_calc_found_rows' === $this->query_vars['count_total'] ) { 541 557 542 558 /** … … 552 568 * @param BP_User_Query $user_query Current BP_User_Query instance. 553 569 */ 554 $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', "SELECT FOUND_ROWS()", $this ) );555 } elseif ( 'count_query' == $this->query_vars['count_total'] ) {556 $count_select = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] );570 $this->total_users = $wpdb->get_var( apply_filters( 'bp_found_user_query', 'SELECT FOUND_ROWS()', $this ) ); 571 } elseif ( 'count_query' === $this->query_vars['count_total'] ) { 572 $count_select = preg_replace( '/^SELECT.*?FROM (\S+) u/', "SELECT COUNT(u.{$this->uid_name}) FROM $1 u", $this->uid_clauses['select'] ); 557 573 558 574 /** This filter is documented in bp-core/classes/class-bp-user-query.php */ … … 582 598 * @param BP_User_Query $user_query Current BP_User_Query instance. 583 599 */ 584 $wp_user_query = new WP_User_Query( apply_filters( 'bp_wp_user_query_args', array( 585 586 // Relevant. 587 'fields' => $fields, 588 'include' => $this->user_ids, 589 590 // Overrides 591 'blog_id' => 0, // BP does not require blog roles. 592 'count_total' => false // We already have a count. 593 594 ), $this ) ); 600 $wp_user_query = new WP_User_Query( 601 apply_filters( 602 'bp_wp_user_query_args', 603 array( 604 // Relevant. 605 'fields' => $fields, 606 'include' => $this->user_ids, 607 608 // Overrides. 609 'blog_id' => 0, // BP does not require blog roles. 610 'count_total' => false, // We already have a count. 611 ), 612 $this 613 ) 614 ); 595 615 596 616 /* … … 613 633 foreach ( $this->user_ids as $key => $uid ) { 614 634 if ( isset( $r[ $uid ] ) ) { 615 $r[ $uid ]->ID = (int) $uid;635 $r[ $uid ]->ID = (int) $uid; 616 636 $r[ $uid ]->user_status = (int) $r[ $uid ]->user_status; 617 637 … … 622 642 $this->results[ $uid ]->id = (int) $uid; 623 643 624 // Remove user ID from original user_ids property.644 // Remove user ID from original user_ids property. 625 645 } else { 626 646 unset( $this->user_ids[ $key ] ); … … 641 661 * @since 1.8.0 642 662 * 643 * @param array $include Sanitized array of user IDs, as passed to the 'include'644 * parameter of the class constructor.663 * @param array $include_ids Sanitized array of user IDs, as passed to the 'include' 664 * parameter of the class constructor. 645 665 * @return array The list of users to which the main query should be 646 666 * limited. 647 667 */ 648 public function get_include_ids( $include = array() ) {649 return $include ;668 public function get_include_ids( $include_ids = array() ) { 669 return $include_ids; 650 670 } 651 671 … … 671 691 // In the case of the 'popular' sort type, we force 672 692 // populate_extras to true, because we need the friend counts. 673 if ( 'popular' == $this->query_vars['type'] ) {693 if ( 'popular' === $this->query_vars['type'] ) { 674 694 $this->query_vars['populate_extras'] = 1; 675 695 } … … 707 727 // Set a last_activity value for each user, even if it's empty. 708 728 foreach ( $this->results as $user_id => $user ) { 709 $user_last_activity = isset( $last_activities[ $user_id ]['date_recorded'] ) ? $last_activities[ $user_id ]['date_recorded'] : '';729 $user_last_activity = isset( $last_activities[ $user_id ]['date_recorded'] ) ? $last_activities[ $user_id ]['date_recorded'] : ''; 710 730 $this->results[ $user_id ]->last_activity = $user_last_activity; 711 731 } … … 716 736 // - latest update. 717 737 $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' ); 718 $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' );738 $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' ); 719 739 720 740 // Total_friend_count must be set for each user, even if its 721 741 // value is 0. 722 742 foreach ( $this->results as $uindex => $user ) { 723 $this->results[ $uindex]->total_friend_count = 0;743 $this->results[ $uindex ]->total_friend_count = 0; 724 744 } 725 745 … … 731 751 foreach ( $user_metas as $user_meta ) { 732 752 switch ( $user_meta->meta_key ) { 733 case $total_friend_count_key :753 case $total_friend_count_key: 734 754 $key = 'total_friend_count'; 735 755 break; 736 756 737 case $bp_latest_update_key :757 case $bp_latest_update_key: 738 758 $key = 'latest_update'; 739 759 break; … … 749 769 if ( ! empty( $this->query_vars['meta_key'] ) ) { 750 770 $meta_sql = array( 751 'select' => "SELECT user_id, meta_key, meta_value",771 'select' => 'SELECT user_id, meta_key, meta_value', 752 772 'from' => "FROM $wpdb->usermeta", 753 'where' => $wpdb->prepare( "WHERE meta_key = %s", $this->query_vars['meta_key'] )773 'where' => $wpdb->prepare( 'WHERE meta_key = %s', $this->query_vars['meta_key'] ), 754 774 ); 755 775 756 776 if ( false !== $this->query_vars['meta_value'] ) { 757 $meta_sql['where'] .= $wpdb->prepare( " AND meta_value = %s", $this->query_vars['meta_value'] );777 $meta_sql['where'] .= $wpdb->prepare( ' AND meta_value = %s', $this->query_vars['meta_value'] ); 758 778 } 759 779 … … 779 799 * @since 2.4.0 780 800 * 801 * @global wpdb $wpdb WordPress database object. 802 * 781 803 * @param string|array $member_types Array or comma-separated list of member types. 782 804 * @param string $operator 'IN' or 'NOT IN'. … … 803 825 } 804 826 805 $tax_query = new WP_Tax_Query( array(827 $tax_query = new WP_Tax_Query( 806 828 array( 807 'taxonomy' => bp_get_member_type_tax_name(), 808 'field' => 'name', 809 'operator' => $operator, 810 'terms' => $types, 811 ), 812 ) ); 829 array( 830 'taxonomy' => bp_get_member_type_tax_name(), 831 'field' => 'name', 832 'operator' => $operator, 833 'terms' => $types, 834 ), 835 ) 836 ); 813 837 814 838 // Switch to the root blog, where member type taxonomies live. … … 828 852 $clause = $this->no_results['where']; 829 853 830 // The tax_query clause generated for NOT IN can be used almost as-is. We just trim the leading 'AND'.854 // The tax_query clause generated for NOT IN can be used almost as-is. We just trim the leading 'AND'. 831 855 } elseif ( 'NOT IN' === $operator ) { 832 856 $clause = preg_replace( '/^\s*AND\s*/', '', $sql_clauses['where'] ); 833 857 834 // IN clauses must be converted to a subquery.858 // IN clauses must be converted to a subquery. 835 859 } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) { 836 860 $clause = "u.{$this->uid_name} IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)