<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][8088] trunk/bp-templates/bp-legacy/buddypress-functions.php: Match user_nicename and user_login in addition to xprofile fields in messages autocomplete</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/8088">8088</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-03-09 14:23:24 +0000 (Sun, 09 Mar 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/5155">#5155</a>
Props imath for an initial patch</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbptemplatesbplegacybuddypressfunctionsphp">trunk/bp-templates/bp-legacy/buddypress-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbptemplatesbplegacybuddypressfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-templates/bp-legacy/buddypress-functions.php (8087 => 8088)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-templates/bp-legacy/buddypress-functions.php 2014-03-09 13:58:11 UTC (rev 8087)
+++ trunk/bp-templates/bp-legacy/buddypress-functions.php 2014-03-09 14:23:24 UTC (rev 8088)
</span><span class="lines">@@ -1350,10 +1350,13 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * AJAX handler for autocomplete. Displays friends only, unless BP_MESSAGES_AUTOCOMPLETE_ALL is defined.
</del><ins>+ * AJAX handler for autocomplete.
</ins><span class="cx"> *
</span><del>- * @return string HTML
- * @since BuddyPress (1.2)
</del><ins>+ * Displays friends only, unless BP_MESSAGES_AUTOCOMPLETE_ALL is defined.
+ *
+ * @since BuddyPress (1.2.0)
+ *
+ * @return string HTML.
</ins><span class="cx"> */
</span><span class="cx"> function bp_legacy_theme_ajax_messages_autocomplete_results() {
</span><span class="cx">
</span><span class="lines">@@ -1361,56 +1364,65 @@
</span><span class="cx"> if ( bp_is_current_component( bp_get_messages_slug() ) )
</span><span class="cx"> $autocomplete_all = buddypress()->messages->autocomplete_all;
</span><span class="cx">
</span><del>- $pag_page = 1;
- $limit = (int) $_GET['limit'] ? $_GET['limit'] : apply_filters( 'bp_autocomplete_max_results', 10 );
</del><ins>+ $pag_page = 1;
+ $limit = (int) $_GET['limit'] ? $_GET['limit'] : apply_filters( 'bp_autocomplete_max_results', 10 );
+ $search_terms = isset( $_GET['q'] ) ? $_GET['q'] : '';
</ins><span class="cx">
</span><del>- // Get the user ids based on the search terms
- if ( ! empty( $autocomplete_all ) ) {
- $users = BP_Core_User::search_users( $_GET['q'], $limit, $pag_page );
</del><ins>+ $user_query_args = array(
+ 'search_terms' => $search_terms,
+ 'page' => intval( $pag_page ),
+ 'per_page' => intval( $limit ),
+ );
</ins><span class="cx">
</span><del>- if ( ! empty( $users['users'] ) ) {
- // Build an array with the correct format
- $user_ids = array();
- foreach( $users['users'] as $user ) {
- if ( $user->id != bp_loggedin_user_id() ) {
- $user_ids[] = $user->id;
- }
- }
</del><ins>+ // If only matching against friends, get an $include param for
+ // BP_User_Query
+ if ( ! $autocomplete_all && bp_is_active( 'friends' ) ) {
+ $include = BP_Friends_Friendship::get_friend_user_ids( bp_loggedin_user_id() );
</ins><span class="cx">
</span><del>- $user_ids = apply_filters( 'bp_core_autocomplete_ids', $user_ids, $_GET['q'], $limit );
</del><ins>+ // Ensure zero matches if no friends are found
+ if ( empty( $include ) ) {
+ $include = array( 0 );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- } else {
- if ( bp_is_active( 'friends' ) ) {
- $users = friends_search_friends( $_GET['q'], bp_loggedin_user_id(), $limit, 1 );
</del><ins>+ $user_query_args['include'] = $include;
+ }
</ins><span class="cx">
</span><del>- // Keeping the bp_friends_autocomplete_list filter for backward compatibility
- $users = apply_filters( 'bp_friends_autocomplete_list', $users, $_GET['q'], $limit );
</del><ins>+ $user_query = new BP_User_Query( $user_query_args );
</ins><span class="cx">
</span><del>- if ( ! empty( $users['friends'] ) ) {
- $user_ids = apply_filters( 'bp_friends_autocomplete_ids', $users['friends'], $_GET['q'], $limit );
- }
</del><ins>+ // Backward compatibility - if a plugin is expecting a legacy
+ // filter, pass the IDs through the filter and requery (groan)
+ if ( has_filter( 'bp_core_autocomplete_ids' ) || has_filter( 'bp_friends_autocomplete_ids' ) ) {
+ $found_user_ids = wp_list_pluck( $user_query->results, 'ID' );
+
+ if ( $autocomplete_all ) {
+ $found_user_ids = apply_filters( 'bp_core_autocomplete_ids', $found_user_ids );
+ } else {
+ $found_user_ids = apply_filters( 'bp_friends_autocomplete_ids', $found_user_ids );
</ins><span class="cx"> }
</span><ins>+
+ if ( empty( $found_user_ids ) ) {
+ $found_user_ids = array( 0 );
+ }
+
+ // Repopulate the $user_query variable
+ $user_query = new BP_User_Query( array(
+ 'include' => $found_user_ids,
+ ) );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( ! empty( $user_ids ) ) {
- foreach ( $user_ids as $user_id ) {
- $ud = get_userdata( $user_id );
- if ( ! $ud ) {
- continue;
- }
-
</del><ins>+ if ( ! empty( $user_query->results ) ) {
+ foreach ( $user_query->results as $user ) {
</ins><span class="cx"> if ( bp_is_username_compatibility_mode() ) {
</span><span class="cx"> // Sanitize for spaces. Use urlencode() rather
</span><span class="cx"> // than rawurlencode() because %20 breaks JS
</span><del>- $username = urlencode( $ud->user_login );
</del><ins>+ $username = urlencode( $user->user_login );
</ins><span class="cx"> } else {
</span><del>- $username = $ud->user_nicename;
</del><ins>+ $username = $user->user_nicename;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // Note that the final line break acts as a delimiter for the
</span><span class="cx"> // autocomplete javascript and thus should not be removed
</span><del>- 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 ) ) . ' ' . bp_core_get_user_displayname( $user_id ) . ' (' . esc_html( $username ) . ')' . "\n";
</del><ins>+ 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 ) ) . ' ' . bp_core_get_user_displayname( $user->ID ) . ' (' . esc_html( $username ) . ')' . "\n";
</ins><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>