Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/03/2014 08:02:18 PM (12 years ago)
Author:
djpaul
Message:

at-mentions: overhaul the mentions implementation for Private Messages and Groups (admin) components.

Previously, these components had seperate implementations of username auto-suggestions, which weren't
reusable outside of where they'd been built, nor easily extended by developers, or other plugins.
This change creates a central API for auto-suggestions, currently only for usernames, but easily
extensible for other kinds of auto-suggestions in the future (i.e. group names, or #hashtags).

See #3278

File:
1 edited

Legend:

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

    r8341 r8557  
    13841384 */
    13851385function bp_legacy_theme_ajax_messages_autocomplete_results() {
    1386 
     1386        $limit = isset( $_GET['limit'] ) ? absint( $_GET['limit'] )          : (int) apply_filters( 'bp_autocomplete_max_results', 10 );
     1387        $term  = isset( $_GET['q'] )     ? sanitize_text_field( $_GET['q'] ) : '';
     1388       
    13871389        // Include everyone in the autocomplete, or just friends?
    1388         if ( bp_is_current_component( bp_get_messages_slug() ) )
    1389                 $autocomplete_all = buddypress()->messages->autocomplete_all;
    1390 
    1391         $pag_page     = 1;
    1392         $limit        = (int) $_GET['limit'] ? $_GET['limit'] : apply_filters( 'bp_autocomplete_max_results', 10 );
    1393         $search_terms = isset( $_GET['q'] ) ? $_GET['q'] : '';
    1394 
    1395         $user_query_args = array(
    1396                 'search_terms' => $search_terms,
    1397                 'page'         => intval( $pag_page ),
    1398                 'per_page'     => intval( $limit ),
    1399         );
    1400 
    1401         // If only matching against friends, get an $include param for
    1402         // BP_User_Query
    1403         if ( ! $autocomplete_all && bp_is_active( 'friends' ) ) {
    1404                 $include = BP_Friends_Friendship::get_friend_user_ids( bp_loggedin_user_id() );
    1405 
    1406                 // Ensure zero matches if no friends are found
    1407                 if ( empty( $include ) ) {
    1408                         $include = array( 0 );
    1409                 }
    1410 
    1411                 $user_query_args['include'] = $include;
    1412         }
    1413 
    1414         $user_query = new BP_User_Query( $user_query_args );
    1415 
    1416         // Backward compatibility - if a plugin is expecting a legacy
    1417         // filter, pass the IDs through the filter and requery (groan)
    1418         if ( has_filter( 'bp_core_autocomplete_ids' ) || has_filter( 'bp_friends_autocomplete_ids' ) ) {
    1419                 $found_user_ids = wp_list_pluck( $user_query->results, 'ID' );
    1420 
    1421                 if ( $autocomplete_all ) {
    1422                         $found_user_ids = apply_filters( 'bp_core_autocomplete_ids', $found_user_ids );
    1423                 } else {
    1424                         $found_user_ids = apply_filters( 'bp_friends_autocomplete_ids', $found_user_ids );
    1425                 }
    1426 
    1427                 if ( empty( $found_user_ids ) ) {
    1428                         $found_user_ids = array( 0 );
    1429                 }
    1430 
    1431                 // Repopulate the $user_query variable
    1432                 $user_query = new BP_User_Query( array(
    1433                         'include' => $found_user_ids,
    1434                 ) );
    1435         }
    1436 
    1437         if ( ! empty( $user_query->results ) ) {
    1438                 foreach ( $user_query->results as $user ) {
    1439                         if ( bp_is_username_compatibility_mode() ) {
    1440                                 // Sanitize for spaces. Use urlencode() rather
    1441                                 // than rawurlencode() because %20 breaks JS
    1442                                 $username = urlencode( $user->user_login );
    1443                         } else {
    1444                                 $username = $user->user_nicename;
    1445                         }
     1390        if ( bp_is_current_component( bp_get_messages_slug() ) ) {
     1391                $only_friends = ( buddypress()->messages->autocomplete_all === false );
     1392        } else {
     1393                $only_friends = true;
     1394        }
     1395
     1396        $suggestions = bp_core_get_suggestions( array(
     1397                'limit'        => $limit,
     1398                'only_friends' => $only_friends,
     1399                'term'         => $term,
     1400                'type'         => 'members',
     1401        ) );
     1402
     1403        if ( $suggestions && ! is_wp_error( $suggestions ) ) {
     1404                foreach ( $suggestions as $user ) {
    14461405
    14471406                        // Note that the final line break acts as a delimiter for the
    14481407                        // autocomplete javascript and thus should not be removed
    1449                         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";
    1450                 }
    1451         }
    1452 
    1453         exit;
    1454 }
     1408                        printf( '<span id="%s" href="#"></span><img src="%s" style="width: 15px"> &nbsp; %s (%s)' . "\n",
     1409                                esc_attr( 'link-' . $user->ID ),
     1410                                esc_url( $user->image ),
     1411                                esc_html( $user->name ),
     1412                                esc_html( $user->ID )
     1413                        );
     1414                }
     1415        }
     1416
     1417        exit;
     1418}
Note: See TracChangeset for help on using the changeset viewer.