Changeset 8087
- Timestamp:
- 03/09/2014 01:58:11 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-core/bp-core-classes.php
r8054 r8087 362 362 /** Search Terms ******************************************************/ 363 363 364 // 'search_terms' searches the xprofile fields 365 // To avoid global joins, do a separate query 366 // @todo remove need for bp_is_active() check 367 if ( false !== $search_terms && bp_is_active( 'xprofile' ) ) { 364 // 'search_terms' searches user_login and user_nicename 365 // xprofile field matches happen in bp_xprofile_bp_user_query_search() 366 if ( false !== $search_terms ) { 368 367 $search_terms_clean = esc_sql( esc_sql( $search_terms ) ); 369 $search_terms_clean = like_escape( $search_terms_clean ); 370 $found_user_ids_query = "SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%" . $search_terms_clean . "%'"; 371 $found_user_ids = $wpdb->get_col( $found_user_ids_query ); 372 373 if ( ! empty( $found_user_ids ) ) { 374 $sql['where'][] = "u.{$this->uid_name} IN (" . implode( ',', wp_parse_id_list( $found_user_ids ) ) . ")"; 375 } else { 376 $sql['where'][] = $this->no_results['where']; 377 } 368 $sql['where']['search'] = "u.{$this->uid_name} IN ( SELECT ID FROM {$wpdb->users} WHERE ( user_login LIKE '%{$search_terms_clean}%' OR user_nicename LIKE '%{$search_terms_clean}%' ) )"; 378 369 } 379 370 … … 400 391 $sql['limit'] = ''; 401 392 } 393 394 // Allow custom filters 395 $sql = apply_filters_ref_array( 'bp_user_query_uid_clauses', array( $sql, &$this ) ); 402 396 403 397 // Assemble the query chunks -
trunk/bp-xprofile/bp-xprofile-functions.php
r7885 r8087 455 455 456 456 /** 457 * When search_terms are passed to BP_User_Query, search against xprofile fields. 458 * 459 * @since BuddyPress (2.0.0) 460 * 461 * @param array $sql Clauses in the user_id SQL query. 462 * @param BP_User_Query User query object. 463 */ 464 function bp_xprofile_bp_user_query_search( $sql, BP_User_Query $query ) { 465 global $wpdb; 466 467 if ( empty( $query->query_vars['search_terms'] ) || empty( $sql['where']['search'] ) ) { 468 return $sql; 469 } 470 471 $bp = buddypress(); 472 473 $search_terms_clean = esc_sql( esc_sql( $query->query_vars['search_terms'] ) ); 474 475 // Combine the core search (against wp_users) into a single OR clause 476 // with the xprofile_data search 477 $search_core = $sql['where']['search']; 478 $search_xprofile = "u.{$query->uid_name} IN ( SELECT user_id FROM {$bp->profile->table_name_data} WHERE value LIKE '%{$search_terms_clean}%' )"; 479 $search_combined = "( {$search_xprofile} OR {$search_core} )"; 480 481 $sql['where']['search'] = $search_combined; 482 483 return $sql; 484 } 485 add_action( 'bp_user_query_uid_clauses', 'bp_xprofile_bp_user_query_search', 10, 2 ); 486 487 /** 457 488 * Syncs Xprofile data to the standard built in WordPress profile data. 458 489 * -
trunk/tests/testcases/core/class-bp-user-query.php
r7638 r8087 203 203 204 204 /** 205 * @group search_terms 206 */ 207 public function test_bp_user_query_search_core_fields() { 208 $user_id = $this->create_user( array( 209 'user_login' => 'foo', 210 ) ); 211 xprofile_set_field_data( 1, $user_id, "Bar" ); 212 $q = new BP_User_Query( array( 'search_terms' => 'foo', ) ); 213 214 $found_user_id = null; 215 if ( ! empty( $q->results ) ) { 216 $found_user = array_pop( $q->results ); 217 $found_user_id = $found_user->ID; 218 } 219 220 $this->assertEquals( $user_id, $found_user_id ); 221 } 222 /** 205 223 * @group exclude 206 224 */
Note: See TracChangeset
for help on using the changeset viewer.