Skip to:
Content

bbPress.org

Ticket #3371: 3371.patch

File 3371.patch, 2.2 KB (added by johnjamesjacoby, 6 years ago)
  • src/includes/common/formatting.php

     
    462462 * @return string
    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
    468468/**
     
    476476 */
    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
    492500        $class_str = ! empty( $classes )
    493501                ? implode( ' ', array_map( 'sanitize_html_class', $classes ) )
    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
    505524/** Numbers *******************************************************************/