Skip to:
Content

BuddyPress.org

Changeset 8088


Ignore:
Timestamp:
03/09/2014 02:23:24 PM (11 years ago)
Author:
boonebgorges
Message:

Match user_nicename and user_login in addition to xprofile fields in messages autocomplete

The AJAX handler bp_legacy_theme_ajax_messages_autocomplete_results() now uses
BP_User_Query and its search_terms parameter for matching users.

Fixes #5155

Props imath for an initial patch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-templates/bp-legacy/buddypress-functions.php

    r8063 r8088  
    13511351
    13521352/**
    1353  * AJAX handler for autocomplete. Displays friends only, unless BP_MESSAGES_AUTOCOMPLETE_ALL is defined.
    1354  *
    1355  * @return string HTML
    1356  * @since BuddyPress (1.2)
     1353 * AJAX handler for autocomplete.
     1354 *
     1355 * Displays friends only, unless BP_MESSAGES_AUTOCOMPLETE_ALL is defined.
     1356 *
     1357 * @since BuddyPress (1.2.0)
     1358 *
     1359 * @return string HTML.
    13571360 */
    13581361function bp_legacy_theme_ajax_messages_autocomplete_results() {
     
    13621365        $autocomplete_all = buddypress()->messages->autocomplete_all;
    13631366
    1364     $pag_page = 1;
    1365     $limit    = (int) $_GET['limit'] ? $_GET['limit'] : apply_filters( 'bp_autocomplete_max_results', 10 );
    1366 
    1367     // Get the user ids based on the search terms
    1368     if ( ! empty( $autocomplete_all ) ) {
    1369         $users = BP_Core_User::search_users( $_GET['q'], $limit, $pag_page );
    1370 
    1371         if ( ! empty( $users['users'] ) ) {
    1372             // Build an array with the correct format
    1373             $user_ids = array();
    1374             foreach( $users['users'] as $user ) {
    1375                 if ( $user->id != bp_loggedin_user_id() ) {
    1376                     $user_ids[] = $user->id;
    1377                 }
    1378             }
    1379 
    1380             $user_ids = apply_filters( 'bp_core_autocomplete_ids', $user_ids, $_GET['q'], $limit );
    1381         }
    1382 
    1383     } else {
    1384         if ( bp_is_active( 'friends' ) ) {
    1385             $users = friends_search_friends( $_GET['q'], bp_loggedin_user_id(), $limit, 1 );
    1386 
    1387             // Keeping the bp_friends_autocomplete_list filter for backward compatibility
    1388             $users = apply_filters( 'bp_friends_autocomplete_list', $users, $_GET['q'], $limit );
    1389 
    1390             if ( ! empty( $users['friends'] ) ) {
    1391                 $user_ids = apply_filters( 'bp_friends_autocomplete_ids', $users['friends'], $_GET['q'], $limit );
    1392             }
    1393         }
    1394     }
    1395 
    1396     if ( ! empty( $user_ids ) ) {
    1397         foreach ( $user_ids as $user_id ) {
    1398             $ud = get_userdata( $user_id );
    1399             if ( ! $ud ) {
    1400                 continue;
    1401             }
    1402 
     1367    $pag_page     = 1;
     1368    $limit        = (int) $_GET['limit'] ? $_GET['limit'] : apply_filters( 'bp_autocomplete_max_results', 10 );
     1369    $search_terms = isset( $_GET['q'] ) ? $_GET['q'] : '';
     1370
     1371    $user_query_args = array(
     1372        'search_terms' => $search_terms,
     1373        'page'         => intval( $pag_page ),
     1374        'per_page'     => intval( $limit ),
     1375    );
     1376
     1377    // If only matching against friends, get an $include param for
     1378    // BP_User_Query
     1379    if ( ! $autocomplete_all && bp_is_active( 'friends' ) ) {
     1380        $include = BP_Friends_Friendship::get_friend_user_ids( bp_loggedin_user_id() );
     1381
     1382        // Ensure zero matches if no friends are found
     1383        if ( empty( $include ) ) {
     1384            $include = array( 0 );
     1385        }
     1386
     1387        $user_query_args['include'] = $include;
     1388    }
     1389
     1390    $user_query = new BP_User_Query( $user_query_args );
     1391
     1392    // Backward compatibility - if a plugin is expecting a legacy
     1393    // filter, pass the IDs through the filter and requery (groan)
     1394    if ( has_filter( 'bp_core_autocomplete_ids' ) || has_filter( 'bp_friends_autocomplete_ids' ) ) {
     1395        $found_user_ids = wp_list_pluck( $user_query->results, 'ID' );
     1396
     1397        if ( $autocomplete_all ) {
     1398            $found_user_ids = apply_filters( 'bp_core_autocomplete_ids', $found_user_ids );
     1399        } else {
     1400            $found_user_ids = apply_filters( 'bp_friends_autocomplete_ids', $found_user_ids );
     1401        }
     1402
     1403        if ( empty( $found_user_ids ) ) {
     1404            $found_user_ids = array( 0 );
     1405        }
     1406
     1407        // Repopulate the $user_query variable
     1408        $user_query = new BP_User_Query( array(
     1409            'include' => $found_user_ids,
     1410        ) );
     1411    }
     1412
     1413    if ( ! empty( $user_query->results ) ) {
     1414        foreach ( $user_query->results as $user ) {
    14031415            if ( bp_is_username_compatibility_mode() ) {
    14041416                // Sanitize for spaces. Use urlencode() rather
    14051417                // than rawurlencode() because %20 breaks JS
    1406                 $username = urlencode( $ud->user_login );
     1418                $username = urlencode( $user->user_login );
    14071419            } else {
    1408                 $username = $ud->user_nicename;
     1420                $username = $user->user_nicename;
    14091421            }
    14101422
    14111423            // Note that the final line break acts as a delimiter for the
    14121424            // autocomplete javascript and thus should not be removed
    1413             echo '<span id="link-' . esc_attr( $username ) . '" href="' . bp_core_get_user_domain( $user_id ) . '"></span>' . bp_core_fetch_avatar( array( 'item_id' => $user_id, 'type' => 'thumb', 'width' => 15, 'height' => 15, 'alt' => $ud->display_name ) ) . ' &nbsp;' . bp_core_get_user_displayname( $user_id ) . ' (' . esc_html( $username ) . ')' . "\n";
    1414         }
    1415     }
    1416 
    1417     exit;
    1418 }
     1425            echo '<span id="link-' . esc_attr( $username ) . '" href="' . bp_core_get_user_domain( $user->ID ) . '"></span>' . bp_core_fetch_avatar( array( 'item_id' => $user->ID, 'type' => 'thumb', 'width' => 15, 'height' => 15, 'alt' => $user->display_name ) ) . ' &nbsp;' . bp_core_get_user_displayname( $user->ID ) . ' (' . esc_html( $username ) . ')' . "\n";
     1426        }
     1427    }
     1428
     1429    exit;
     1430}
Note: See TracChangeset for help on using the changeset viewer.