Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/14/2013 03:43:43 PM (6 years ago)
Author:
boonebgorges
Message:

Remove BuddyPress's restriction spaces in user_login

BuddyPress has historically enforced a no-spaces rule on user_login during
user registration. Originally this was rooted in WPMU's own peculiar character
restrictions, and when the MU requirement was dropped, the same restrictions
were carried over to WordPress Single.

However, these restrictions have caused various problems. BP enforced the "no
spaces" rule during registration by simply swapping out spaces with hyphens and
not telling users. This caused immense confusion. Moreover, the restriction
caused problems when bypassing BP's native user registration, as when
integrating with an external authentication service; these external usernames
*can* sometimes have spaces, and certain areas of BuddyPress were not equipped
to deal with them.

This changeset removes the no-spaces restriction from BuddyPress, and hands
off user_login validation to WordPress Multisite when possible (meaning that on
MS, spaces will still not be allowed during native registration). It also
makes the necessary adjustments throughout BuddyPress to ensure that spaces
in user_login will not break related functionality. On a normal setup, BP (and
WP) only use user_login for authentication, but several changes were necessary
to account for "username compatibility mode", where the user_login is displayed
publicly:

  • Refactor the way that activity @-mentions work in username compatibility mode. We now have functions for converting user IDs to "mentionname" (and vice versa) which will produce @-mention-safe versions of user_nicename or user_login, as appropriate.
  • Use proper URL encoding when building and parsing URLs that contain usernames when compatibility mode is enabled.
  • Fix private messaging autocomplete to work with spaces.

See #4622

Fixes #5185

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-functions.php

    r7469 r7570  
    7373    // We've found some mentions! Check to see if users exist
    7474    foreach( (array) $usernames as $key => $username ) {
    75         if ( bp_is_username_compatibility_mode() ) {
    76             $user_id = username_exists( $username );
    77         } else {
    78             $user_id = bp_core_get_userid_from_nicename( $username );
    79         }
     75        $user_id = bp_activity_get_userid_from_mentionname( $username );
    8076
    8177        // user ID exists, so let's add it to our array
     
    246242
    247243    return $return;
     244}
     245
     246/**
     247 * Determine a user's "mentionname", the name used for that user in @-mentions.
     248 *
     249 * @since BuddyPress (1.9.0)
     250 *
     251 * @return string User name appropriate for @-mentions.
     252 */
     253function bp_activity_get_user_mentionname( $user_id ) {
     254    $mentionname = '';
     255
     256    $userdata = bp_core_get_core_userdata( $user_id );
     257
     258    if ( $userdata ) {
     259        if ( bp_is_username_compatibility_mode() ) {
     260            $mentionname = str_replace( ' ', '-', $userdata->user_login );
     261        } else {
     262            $mentionname = $userdata->user_nicename;
     263        }
     264    }
     265
     266    return $mentionname;
     267}
     268
     269/**
     270 * Get a user ID from a "mentionname", the name used for a user in @-mentions.
     271 *
     272 * @since BuddyPress (1.9.0)
     273 *
     274 * @return int|bool ID of the user, if one is found. Otherwise false.
     275 */
     276function bp_activity_get_userid_from_mentionname( $mentionname ) {
     277    $user_id = false;
     278
     279    // In username compatibility mode, hyphens are ambiguous between
     280    // actual hyphens and converted spaces.
     281    //
     282    // @todo There is the potential for username clashes between 'foo bar'
     283    // and 'foo-bar' in compatibility mode. Come up with a system for
     284    // unique mentionnames.
     285    if ( bp_is_username_compatibility_mode() ) {
     286        // First, try the raw username
     287        $userdata = get_user_by( 'login', $mentionname );
     288
     289        // Doing a direct query to use proper regex. Necessary to
     290        // account for hyphens + spaces in the same user_login.
     291        if ( empty( $userdata ) || ! is_a( $userdata, 'WP_User' ) ) {
     292            global $wpdb;
     293            $regex   = esc_sql( str_replace( '-', '[ \-]', $mentionname ) );
     294            $user_id = $wpdb->get_var( "SELECT ID FROM {$wpdb->users} WHERE user_login REGEXP '{$regex}'" );
     295        } else {
     296            $user_id = $userdata->ID;
     297        }
     298
     299    // When username compatibility mode is disabled, the mentionname is
     300    // the same as the nicename
     301    } else {
     302        $user_id = bp_core_get_userid_from_nicename( $mentionname );
     303    }
     304
     305
     306    return $user_id;
    248307}
    249308
Note: See TracChangeset for help on using the changeset viewer.