Skip to:
Content

bbPress.org

Changeset 4323


Ignore:
Timestamp:
11/03/2012 10:14:32 AM (12 years ago)
Author:
johnjamesjacoby
Message:

Mentions:

  • Add username @-mention filters.
  • Automatically links to profiles when @username is used in a topic or reply content area.
Location:
trunk/includes/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/core/filters.php

    r4304 r4323  
    128128add_filter( 'bbp_get_reply_content', 'convert_smilies',    20   );
    129129add_filter( 'bbp_get_reply_content', 'wpautop',            30   );
     130add_filter( 'bbp_get_reply_content', 'bbp_mention_filter', 40   );
    130131
    131132// Run filters on topic content
     
    137138add_filter( 'bbp_get_topic_content', 'convert_smilies',    20   );
    138139add_filter( 'bbp_get_topic_content', 'wpautop',            30   );
     140add_filter( 'bbp_get_topic_content', 'bbp_mention_filter', 40   );
    139141
    140142// Add number format filter to functions requiring numeric output
  • trunk/includes/core/functions.php

    r4321 r4323  
    282282}
    283283
     284/**
     285 * Searches through the content to locate usernames, designated by an @ sign.
     286 *
     287 * @since bbPress (r4323)
     288 *
     289 * @param string $content The content
     290 * @return bool|array $usernames Existing usernames. False if no matches.
     291 */
     292function bbp_find_mentions( $content = '' ) {
     293    $pattern   = '/[@]+([A-Za-z0-9-_\.@]+)\b/';
     294    preg_match_all( $pattern, $content, $usernames );
     295    $usernames = array_unique( array_filter( $usernames[1] ) );
     296
     297    // Bail if no usernames
     298    if ( empty( $usernames ) )
     299        return false;
     300
     301    return $usernames;
     302}
     303
     304/**
     305 * Finds and links @-mentioned users in the content
     306 *
     307 * @since bbPress (r4323)
     308 *
     309 * @uses bbp_find_mentions() To get usernames in content areas
     310 * @return string $content Content filtered for mentions
     311 */
     312function bbp_mention_filter( $content = '' ) {
     313
     314    // Get Usernames and bail if none exist
     315    $usernames = bbp_find_mentions( $content );
     316    if ( empty( $usernames ) )
     317        return $content;
     318
     319    // Loop through usernames and link to profiles
     320    foreach( (array) $usernames as $username ) {
     321
     322        // Skip if username does not exist or user is not active
     323        $user_id = username_exists( $username );
     324        if ( empty( $user_id ) || bbp_is_user_inactive( $user_id ) )
     325            continue;
     326
     327        // Replace name in content
     328        $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bbp_get_user_profile_url( $user_id ) . "' rel='nofollow' class='bbp-mention-link $username'>@$username</a>", $content );
     329    }
     330
     331    // Return modified content
     332    return $content;
     333}
     334
    284335/** Post Statuses *************************************************************/
    285336
Note: See TracChangeset for help on using the changeset viewer.