Skip to:
Content

BuddyPress.org

Changeset 8087


Ignore:
Timestamp:
03/09/2014 01:58:11 PM (11 years ago)
Author:
boonebgorges
Message:

BP_User_Query search_terms should match against user_login and user_nicename in addition to xprofile fields

This changeset changes the search_terms logic in BP_User_Query so that it
matches user_login and user_nicename in the core users table. Then, via filter,
the xprofile component amends the WHERE clause so that search_terms are matched
in the users table OR the xprofile data table.

Clauses have been refactored to subqueries rather than storing the matched
user IDs in PHP, for improved performance.

See #5155

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-classes.php

    r8054 r8087  
    362362        /** Search Terms ******************************************************/
    363363
    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 ) {
    368367            $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}%' ) )";
    378369        }
    379370
     
    400391            $sql['limit'] = '';
    401392        }
     393
     394        // Allow custom filters
     395        $sql = apply_filters_ref_array( 'bp_user_query_uid_clauses', array( $sql, &$this ) );
    402396
    403397        // Assemble the query chunks
  • trunk/bp-xprofile/bp-xprofile-functions.php

    r7885 r8087  
    455455
    456456/**
     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 */
     464function 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}
     485add_action( 'bp_user_query_uid_clauses', 'bp_xprofile_bp_user_query_search', 10, 2 );
     486
     487/**
    457488 * Syncs Xprofile data to the standard built in WordPress profile data.
    458489 *
  • trunk/tests/testcases/core/class-bp-user-query.php

    r7638 r8087  
    203203
    204204    /**
     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    /**
    205223     * @group exclude
    206224     */
Note: See TracChangeset for help on using the changeset viewer.