/**
 * Locate usernames in an activity content string, as designated by an @ sign.
 *
 * @since BuddyPress (1.5)
 *
 * @param string $content The content of the activity, usually found in $activity->content.
 * @return array|bool Associative array with user ID as key and username as value. Boolean false if no mentions found.
 */
function bp_activity_find_mentions($content)
{
    $pattern = '/[@]+([A-Za-z0-9-_\\.@]+)\\b/';
    preg_match_all($pattern, $content, $usernames);
    // Make sure there's only one instance of each username
    if (!($usernames = array_unique($usernames[1]))) {
        return false;
    }
    $mentioned_users = array();
    // We've found some mentions! Check to see if users exist
    foreach ((array) $usernames as $key => $username) {
        $user_id = bp_activity_get_userid_from_mentionname($username);
        // user ID exists, so let's add it to our array
        if (!empty($user_id)) {
            $mentioned_users[$user_id] = $username;
        }
    }
    if (empty($mentioned_users)) {
        return false;
    }
    return $mentioned_users;
}
Ejemplo n.º 2
0
/**
 * Locate usernames in an activity content string, as designated by an @ sign.
 *
 * @since 1.5.0
 *
 * @param string $content The content of the activity, usually found in
 *                        $activity->content.
 * @return array|bool Associative array with user ID as key and username as
 *                    value. Boolean false if no mentions found.
 */
function bp_activity_find_mentions($content)
{
    $pattern = '/[@]+([A-Za-z0-9-_\\.@]+)\\b/';
    preg_match_all($pattern, $content, $usernames);
    // Make sure there's only one instance of each username.
    $usernames = array_unique($usernames[1]);
    // Bail if no usernames.
    if (empty($usernames)) {
        return false;
    }
    $mentioned_users = array();
    // We've found some mentions! Check to see if users exist.
    foreach ((array) array_values($usernames) as $username) {
        $user_id = bp_activity_get_userid_from_mentionname($username);
        // The user ID exists, so let's add it to our array.
        if (!empty($user_id)) {
            $mentioned_users[$user_id] = $username;
        }
    }
    if (empty($mentioned_users)) {
        return false;
    }
    /**
     * Filters the mentioned users.
     *
     * @since 2.5.0
     *
     * @param array $mentioned_users Associative array with user IDs as keys and usernames as values.
     */
    return apply_filters('bp_activity_mentioned_users', $mentioned_users);
}
Ejemplo n.º 3
0
 /**
  * @group bp_activity_get_userid_from_mentionname
  */
 public function test_bp_activity_get_userid_from_mentionname_compatibilitymode_on()
 {
     add_filter('bp_is_username_compatibility_mode', '__return_true');
     // all spaces are hyphens
     $u1 = $this->factory->user->create(array('user_login' => 'foo bar baz', 'user_nicename' => 'foobarbaz'));
     // no spaces are hyphens
     $u2 = $this->factory->user->create(array('user_login' => 'foo-bar-baz-1', 'user_nicename' => 'foobarbaz-1'));
     // some spaces are hyphens
     $u3 = $this->factory->user->create(array('user_login' => 'foo bar-baz 2', 'user_nicename' => 'foobarbaz-2'));
     $u4 = $this->factory->user->create(array('user_login' => 'foo.bar.baz', 'user_nicename' => 'foo-bar-baz'));
     $this->assertEquals($u1, bp_activity_get_userid_from_mentionname('foo-bar-baz'));
     $this->assertEquals($u2, bp_activity_get_userid_from_mentionname('foo-bar-baz-1'));
     $this->assertEquals($u3, bp_activity_get_userid_from_mentionname('foo-bar-baz-2'));
     $this->assertEquals($u4, bp_activity_get_userid_from_mentionname('foo.bar.baz'));
     remove_filter('bp_is_username_compatibility_mode', '__return_true');
 }