Skip to:
Content

bbPress.org

Changeset 7083


Ignore:
Timestamp:
05/21/2020 01:59:29 AM (8 days ago)
Author:
johnjamesjacoby
Message:

Formatting: tweak regular expression for @ mentions.

This commit fixes a bug causing usernames to be made clickable even after they were already made clickable previously by the email address filter.

In trunk, for 2.7.0.

See #3371.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/common/formatting.php

    r7006 r7083  
    463463 */
    464464function bbp_make_mentions_clickable( $text = '' ) {
    465     return preg_replace_callback( '#@([0-9a-zA-Z-_]+)#i', 'bbp_make_mentions_clickable_callback', $text );
     465    return preg_replace_callback( '#([\s>])@([0-9a-zA-Z-_]+)#i', 'bbp_make_mentions_clickable_callback', $text );
    466466}
    467467
     
    477477function bbp_make_mentions_clickable_callback( $matches = array() ) {
    478478
     479    // Bail if the match is empty malformed
     480    if ( empty( $matches[2] ) || ! is_string( $matches[2] ) ) {
     481        return $matches[0];
     482    }
     483
    479484    // Get user; bail if not found
    480     $user = get_user_by( 'slug', $matches[1] );
     485    $user = get_user_by( 'slug', $matches[2] );
    481486    if ( empty( $user ) || bbp_is_user_inactive( $user->ID ) ) {
    482487        return $matches[0];
    483488    }
    484489
     490    // Default anchor classes
     491    $classes = array(
     492        'bbp-user-mention',
     493        'bbp-user-id-' . absint( $user->ID )
     494    );
     495
    485496    // Filter classes
    486     $classes = (array) apply_filters( 'bbp_make_mentions_clickable_classes', array(
    487         'bbp-user-id-' . $user->ID,
    488         'bbp-user-mention'
    489     ) );
     497    $classes = (array) apply_filters( 'bbp_make_mentions_clickable_classes', $classes, $user );
    490498
    491499    // Escape & implode if not empty, otherwise an empty string
     
    494502        : '';
    495503
     504    // Setup as a variable to avoid a potentially empty class attribute
     505    $class = ! empty( $class_str )
     506        ? ' class="' . esc_attr( $class_str ) . '"'
     507        : '';
     508
    496509    // Create the link to the user's profile
     510    $html   = '<a href="%1$s"' . $class . '">%2$s</a>';
    497511    $url    = bbp_get_user_profile_url( $user->ID );
    498     $clicky = '<a href="%1$s" class="' . esc_attr( $class_str ) . '">%2$s</a>';
    499     $anchor = sprintf( $clicky, esc_url( $url ), esc_html( $matches[0] ) );
     512    $anchor = sprintf( $html, esc_url( $url ), esc_html( $matches[0] ) );
     513
     514    // Prevent this link from being followed by bots
    500515    $link   = bbp_rel_nofollow( $anchor );
    501516
    502     return $link;
     517    // Concatenate the matches into the return value
     518    $retval = $matches[1] . $link;
     519
     520    // Return the link
     521    return $retval;
    503522}
    504523
Note: See TracChangeset for help on using the changeset viewer.