Skip to:
Content

BuddyPress.org

Changeset 7890


Ignore:
Timestamp:
02/16/2014 03:58:37 PM (6 years ago)
Author:
boonebgorges
Message:

Don't auto-link @mentions that appear inside the context of links

This prevents illegitimate double-linking when text like
href="mailto:foo@…" or <a href="http://twitter.com/bar">@bar</a> happen
to match a BP username like @bar.

Fixes #4680

Props ninnypants

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r7397 r7890  
    196196
    197197/**
    198  * Finds and links @-mentioned users in the contents of a given item.
    199  *
    200  * @since BuddyPress (1.2)
    201  *
    202  * @uses bp_activity_find_mentions()
    203  * @uses bp_core_get_user_domain()
     198 * Find and link @-mentioned users in the contents of a given item.
     199 *
     200 * @since BuddyPress (1.2.0)
    204201 *
    205202 * @param string $content The contents of a given item.
     
    221218        return $content;
    222219
     220    // We don't want to link @mentions that are inside of links, so we
     221    // temporarily remove them
     222    $replace_count = 0;
     223    $replacements = array();
     224    foreach ( $usernames as $username ) {
     225        // prevent @ name linking inside <a> tags
     226        preg_match_all( '/(<a.*?(?!<\/a>)@' . $username . '.*?<\/a>)/', $content, $content_matches );
     227        if ( ! empty( $content_matches[1] ) ) {
     228            foreach ( $content_matches[1] as $replacement ) {
     229                $replacements[ '#BPAN' . $replace_count ] = $replacement;
     230                $content = str_replace( $replacement, '#BPAN' . $replace_count, $content );
     231                $replace_count++;
     232            }
     233        }
     234    }
     235
    223236    // Linkify the mentions with the username
    224     foreach( (array) $usernames as $user_id => $username ) {
     237    foreach ( (array) $usernames as $user_id => $username ) {
    225238        $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bp_core_get_user_domain( $user_id ) . "' rel='nofollow'>@$username</a>", $content );
     239    }
     240
     241    // put everything back
     242    if ( ! empty( $replacements ) ) {
     243        foreach ( $replacements as $placeholder => $original ) {
     244            $content = str_replace( $placeholder, $original, $content );
     245        }
    226246    }
    227247
Note: See TracChangeset for help on using the changeset viewer.