Skip to:
Content

bbPress.org

Changeset 4672


Ignore:
Timestamp:
12/31/2012 01:21:33 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Nicename Usage:

  • Introduce template tags for getting and displaying a user nicenames. Helpful for themes that want easier access to @mentions data.
  • Improve mention filters to use 'slug' instead of 'login.' Fixes possible mismatches when login and nicename are different.
Location:
trunk/includes
Files:
2 edited

Legend:

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

    r4579 r4672  
    282282}
    283283
     284/** Mentions ******************************************************************/
     285
    284286/**
    285287 * Searches through the content to locate usernames, designated by an @ sign.
     
    321323
    322324        // 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        $user = get_user_by( 'slug', $username );
     326        if ( empty( $user->ID ) || bbp_is_user_inactive( $user->ID ) )
    325327            continue;
    326328
    327329        // 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 );
     330        $content = preg_replace( '/(@' . $username . '\b)/', "<a href='" . bbp_get_user_profile_url( $user->ID ) . "' rel='nofollow' class='bbp-mention-link {$username}'>@{$username}</a>", $content );
    329331    }
    330332
  • trunk/includes/users/template-tags.php

    r4617 r4672  
    241241
    242242        return apply_filters( 'bbp_get_user_profile_link', $user_link, $user_id );
     243    }
     244
     245/**
     246 * Output a users nicename to the screen
     247 *
     248 * @since bbPress (r4671)
     249 *
     250 * @param int $user_id User ID whose nicename to get
     251 * @param array $args before|after|user_id|force
     252 */
     253function bbp_user_nicename( $user_id = 0, $args = array() ) {
     254    echo bbp_get_user_nicename( $user_id, $args );
     255}
     256    /**
     257     * Return a users nicename to the screen
     258     *
     259     * @since bbPress (r4671)
     260     *
     261     * @param int $user_id User ID whose nicename to get
     262     * @param array $args before|after|user_id|force
     263     * @return string User nicename, maybe wrapped in before/after strings
     264     */
     265    function bbp_get_user_nicename( $user_id = 0, $args = array() ) {
     266
     267        // Bail if no user ID passed
     268        $user_id = bbp_get_user_id( $user_id, false, false );
     269        if ( empty( $user_id ) )
     270            return false;
     271
     272        // Parse default arguments
     273        $r = bbp_parse_args( $args, array(
     274            'user_id' => $user_id,
     275            'before'  => '',
     276            'after'   => '',
     277            'force'   => ''
     278        ), 'get_user_nicename' );
     279
     280        // Get the user data and nicename
     281        if ( empty( $r['force'] ) ) {
     282            $user     = get_userdata( $user_id );
     283            $nicename = $user->user_nicename;
     284
     285        // Force the nicename to something else
     286        } else {
     287            $nicename = (string) $r['force'];
     288        }
     289
     290        // Maybe wrap the nicename
     291        $retval = !empty( $nicename ) ? ( $r['before'] . $nicename . $r['after'] ) : '';
     292
     293        // Filter and return
     294        return (string) apply_filters( 'bbp_get_user_nicename', $retval, $user_id, $args );
    243295    }
    244296
     
    290342            // Get username if not passed
    291343            if ( empty( $user_nicename ) ) {
    292                 $user = get_userdata( $user_id );
    293                 if ( !empty( $user->user_nicename ) ) {
    294                     $user_nicename = $user->user_nicename;
    295                 }
     344                $user_nicename = bbp_get_user_nicename( $user_id );
    296345            }
    297346
Note: See TracChangeset for help on using the changeset viewer.