Skip to:
Content

bbPress.org

Changeset 3860


Ignore:
Timestamp:
05/01/2012 10:08:50 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Add template tags and supporting functions to display user role in topics and replies. Fixes #1815. Props cnorris23 for original patch.

Location:
branches/plugin
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-core-caps.php

    r3758 r3860  
    556556 * sites that have global forums enabled want to create topics and replies
    557557 *
     558 * @since bbPress (r3860)
     559 *
     560 * @uses apply_filters() Allow override of hardcoded anonymous role
     561 * @return string
     562 */
     563function bbp_get_anonymous_role() {
     564    return apply_filters( 'bbp_get_anonymous_role', 'bbp_anonymous' );
     565}
     566
     567/**
     568 * The participant role for registered users without roles
     569 *
     570 * This is primarily for multisite compatibility when users without roles on
     571 * sites that have global forums enabled want to create topics and replies
     572 *
    558573 * @since bbPress (r3410)
    559574 *
     575 * @uses apply_filters() Allow override of hardcoded participant role
     576 * @return string
     577 */
     578function bbp_get_participant_role() {
     579    return apply_filters( 'bbp_get_participant_role', 'bbp_participant' );
     580}
     581
     582/**
     583 * The moderator role for bbPress users
     584 *
     585 * @since bbPress (r3410)
     586 *
    560587 * @param string $role
    561  * @uses apply_filters()
     588 * @uses apply_filters() Allow override of hardcoded moderator role
    562589 * @return string
    563590 */
    564 function bbp_get_participant_role() {
    565 
    566     // Hardcoded participant role
    567     $role = 'bbp_participant';
    568 
    569     // Allow override
    570     return apply_filters( 'bbp_get_participant_role', $role );
    571 }
    572 
    573 /**
    574  * The moderator role for bbPress users
    575  *
    576  * @since bbPress (r3410)
    577  *
    578  * @param string $role
    579  * @uses apply_filters()
    580  * @return string
    581  */
    582591function bbp_get_moderator_role() {
    583 
    584     // Hardcoded moderated user role
    585     $role = 'bbp_moderator';
    586 
    587     // Allow override
    588     return apply_filters( 'bbp_get_moderator_role', $role );
     592    return apply_filters( 'bbp_get_moderator_role', 'bbp_moderator' );
    589593}
    590594
  • branches/plugin/bbp-includes/bbp-reply-template.php

    r3845 r3860  
    950950     * @uses bbp_get_reply_author_url() To get the reply author url
    951951     * @uses bbp_get_reply_author_avatar() To get the reply author avatar
    952      * bbp_get_reply_author_display_name() To get the reply author display
     952     * @uses bbp_get_reply_author_display_name() To get the reply author display
    953953     *                                      name
     954     * @uses bbp_get_user_display_role() To get the reply author display role
     955     * @uses bbp_get_reply_author_id() To get the reply author id
    954956     * @uses apply_filters() Calls 'bbp_get_reply_author_link' with the
    955957     *                        author link and args
     
    962964            'type'       => 'both',
    963965            'size'       => 80,
    964             'sep'        => ' '
     966            'sep'        => ' ',
     967            'show_role'  => true
    965968        );
    966969        $r = bbp_parse_args( $args, $defaults, 'get_reply_author_link' );
     
    9981001                    $author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', $author_url, $link_title, $link_class, $link_text );
    9991002                }
     1003
     1004                if ( true === $show_role ) {
     1005                    $author_link[] = bbp_get_reply_author_role( array( 'reply_id' => $reply_id ) );
     1006                }
     1007
    10001008                $author_link = join( $sep, $author_link );
    10011009
     
    11071115
    11081116        return apply_filters( 'bbp_get_reply_author_email', $author_email, $reply_id );
     1117    }
     1118
     1119/**
     1120 * Output the reply author role
     1121 *
     1122 * @since bbPress (r3860)
     1123 *
     1124 * @param array $args Optional.
     1125 * @uses bbp_get_reply_author_role() To get the reply author role
     1126 */
     1127function bbp_reply_author_role( $args = array() ) {
     1128    echo bbp_get_reply_author_role( $args );
     1129}
     1130    /**
     1131     * Return the reply author role
     1132     *
     1133     * @since bbPress (r3860)
     1134     *
     1135     * @param array $args Optional.
     1136     * @uses bbp_get_reply_id() To get the reply id
     1137     * @uses bbp_get_user_display_role() To get the user display role
     1138     * @uses bbp_get_reply_author_id() To get the reply author id
     1139     * @uses apply_filters() Calls bbp_get_reply_author_role with the author
     1140     *                        role & args
     1141     * @return string Reply author role
     1142     */
     1143    function bbp_get_reply_author_role( $args = array() ) {
     1144        $defaults = array(
     1145            'reply_id' => 0,
     1146            'class'    => 'bbp-author-role',
     1147            'before'   => '',
     1148            'after'    => ''
     1149        );
     1150        $args = bbp_parse_args( $args, $defaults, 'get_reply_author_role' );
     1151        extract( $args, EXTR_SKIP );
     1152
     1153        $reply_id    = bbp_get_reply_id( $reply_id );
     1154        $role        = bbp_get_user_display_role( bbp_get_reply_author_id( $reply_id ) );
     1155        $author_role = sprintf( '%1$s<div class="%2$s">%3$s</div>%4$s', $before, $class, $role, $after );
     1156
     1157        return apply_filters( 'bbp_get_reply_author_role', $author_role, $args );
    11091158    }
    11101159
     
    17481797        $classes[] = 'bbp-parent-topic-' . bbp_get_reply_topic_id( $reply_id );
    17491798        $classes[] = 'user-id-' . bbp_get_reply_author_id( $reply_id );
    1750         $classes[] = ( bbp_get_reply_author_id( $reply_id ) == bbp_get_topic_author_id( bbp_get_reply_topic_id( $reply_id ) ) ? 'topic-author' : '' );     
     1799        $classes[] = ( bbp_get_reply_author_id( $reply_id ) == bbp_get_topic_author_id( bbp_get_reply_topic_id( $reply_id ) ) ? 'topic-author' : '' );
    17511800        $classes   = array_filter( $classes );
    17521801        $classes   = get_post_class( $classes, $reply_id );
  • branches/plugin/bbp-includes/bbp-topic-template.php

    r3855 r3860  
    12141214     * @uses bbp_is_topic_anonymous() To check if the topic is by an
    12151215     *                                 anonymous user
     1216     * @uses bbp_get_topic_author_url() To get the topic author url
    12161217     * @uses bbp_get_topic_author_avatar() To get the topic author avatar
    1217      * @uses bbp_get_topic_author_url() To get the topic author url
     1218     * @uses bbp_get_topic_author_display_name() To get the topic author display
     1219     *                                      name
     1220     * @uses bbp_get_user_display_role() To get the topic author display role
     1221     * @uses bbp_get_topic_author_id() To get the topic author id
    12181222     * @uses apply_filters() Calls 'bbp_get_topic_author_link' with the link
    12191223     *                        and args
     
    12261230            'type'       => 'both',
    12271231            'size'       => 80,
    1228             'sep'        => '&nbsp;'
     1232            'sep'        => '&nbsp;',
     1233            'show_role'  => true
    12291234        );
    12301235        $r = bbp_parse_args( $args, $defaults, 'get_topic_author_link' );
     
    12721277                    $author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', $author_url, $link_title, $link_class, $link_text );
    12731278                }
     1279
     1280                if ( true === $show_role ) {
     1281                    $author_link[] = bbp_get_topic_author_role( array( 'topic_id' => $topic_id ) );
     1282                }
     1283
    12741284                $author_link = join( $sep, $author_link );
    12751285
     
    13841394        return apply_filters( 'bbp_get_topic_author_email', $author_email, $topic_id );
    13851395    }
     1396
     1397/**
     1398 * Output the topic author role
     1399 *
     1400 * @since bbPress (r3860)
     1401 *
     1402 * @param array $args Optional.
     1403 * @uses bbp_get_topic_author_role() To get the topic author role
     1404 */
     1405function bbp_topic_author_role( $args = array() ) {
     1406    echo bbp_get_topic_author_role( $args );
     1407}
     1408    /**
     1409     * Return the topic author role
     1410     *
     1411     * @since bbPress (r3860)
     1412     *
     1413     * @param array $args Optional.
     1414     * @uses bbp_get_topic_id() To get the topic id
     1415     * @uses bbp_get_user_display_role() To get the user display role
     1416     * @uses bbp_get_topic_author_id() To get the topic author id
     1417     * @uses apply_filters() Calls bbp_get_topic_author_role with the author
     1418     *                        role & args
     1419     * @return string topic author role
     1420     */
     1421    function bbp_get_topic_author_role( $args = array() ) {
     1422        $defaults = array(
     1423            'topic_id' => 0,
     1424            'class'    => 'bbp-author-role',
     1425            'before'   => '',
     1426            'after'    => ''
     1427        );
     1428        $args = bbp_parse_args( $args, $defaults, 'get_topic_author_role' );
     1429        extract( $args, EXTR_SKIP );
     1430
     1431        $topic_id    = bbp_get_topic_id( $topic_id );
     1432        $role        = bbp_get_user_display_role( bbp_get_topic_author_id( $topic_id ) );
     1433        $author_role = sprintf( '%1$s<div class="%2$s">%3$s</div>%4$s', $before, $class, $role, $after );
     1434
     1435        return apply_filters( 'bbp_get_topic_author_role', $author_role, $args );
     1436    }
     1437
    13861438
    13871439/**
  • branches/plugin/bbp-includes/bbp-user-functions.php

    r3856 r3860  
    14001400}
    14011401
     1402/**
     1403 * Return a user's main role
     1404 *
     1405 * @since bbPress (r3860)
     1406 *
     1407 * @param int $user_id
     1408 * @uses bbp_get_user_id() To get the user id
     1409 * @uses get_userdata() To get the user data
     1410 * @uses apply_filters() Calls 'bbp_get_user_role' with the
     1411 *                        role and user id
     1412 * @return string
     1413 */
     1414function bbp_get_user_role( $user_id = 0 ) {
     1415
     1416    // Validate user id
     1417    $user_id = bbp_get_user_id( $user_id, false, false );
     1418    if ( empty( $user_id ) )
     1419        return false;
     1420
     1421    // Get userdata
     1422    $user = get_userdata( $user_id );
     1423
     1424    // Get the user's main role
     1425    $role = isset( $user->roles ) ? array_shift( $user->roles ) : bbp_get_anonymous_role();
     1426
     1427    return apply_filters( 'bbp_get_user_role', $role, $user_id, $user );
     1428}
     1429
    14021430/** Premissions ***************************************************************/
    14031431
  • branches/plugin/bbp-includes/bbp-user-template.php

    r3840 r3860  
    376376        $user_id = bbp_get_user_id( $user_id );
    377377        if ( empty( $user_id ) )
    378             return;
     378            return false;
    379379
    380380        // Pretty permalinks
     
    400400        return apply_filters( 'bbp_get_user_edit_profile_url', $url, $user_id, $user_nicename );
    401401
     402    }
     403
     404/**
     405 * Output a user's main role for display
     406 *
     407 * @since bbPress (r3860)
     408 *
     409 * @param int $user_id
     410 * @uses bbp_get_user_display_role To get the user display role
     411 */
     412function bbp_user_display_role( $user_id = 0 ) {
     413    echo bbp_get_user_display_role( $user_id );
     414}
     415    /**
     416     * Return a user's main role for display
     417     *
     418     * @since bbPress (r3860)
     419     *
     420     * @param int $user_id
     421     * @uses bbp_get_user_role() To get the main user role
     422     * @uses bbp_get_moderator_role() To get the moderator role
     423     * @uses bbp_get_participant_role() To get the participant role
     424     * @uses bbp_get_moderator_role() To get the moderator role
     425     * @uses apply_filters() Calls 'bbp_get_user_display_role' with the
     426     *                        display role, user id, and user role
     427     * @return string
     428     */
     429    function bbp_get_user_display_role( $user_id = 0 ) {
     430
     431        // Validate user id
     432        $user_id   = bbp_get_user_id( $user_id, false, false );
     433        $user_role = bbp_get_user_role( $user_id );
     434
     435        // Capes earn Vinz Clortho status
     436        if ( is_super_admin( $user_id ) ) {
     437            $role = __( 'Key Master', 'bbpress' );
     438
     439        // Not the keymaster of Gozer
     440        } else {
     441
     442            // Get the user's main role for display
     443            switch ( $user_role ) {
     444
     445                /** bbPress Roles *********************************************/
     446
     447                // Anonymous
     448                case bbp_get_anonymous_role() :
     449                    $role = __( 'Guest', 'bbpress' );
     450                    break;
     451
     452                // Multisite Participant Role
     453                case bbp_get_participant_role() :
     454                    $role = __( 'Member', 'bbpress' );
     455                    break;
     456
     457                // Moderator
     458                case bbp_get_moderator_role() :
     459                    $role = __( 'Moderator', 'bbpress' );
     460                    break;
     461
     462                /** WordPress Core Roles **************************************/
     463
     464                case 'administrator' :
     465                case 'editor'        :
     466                case 'author'        :
     467                case 'contributor'   :
     468                case 'subscriber'    :
     469                default              : // Any other role (plugins, etc...)
     470                    global $wp_roles;
     471
     472                    // Load roles if not set
     473                    if ( !isset( $wp_roles ) )
     474                        $wp_roles = new WP_Roles();                 
     475
     476                    // Get a translated role name
     477                    if ( !empty( $wp_roles->role_names[$user_role] ) )
     478                        $role = translate_user_role( $wp_roles->role_names[$user_role] );
     479
     480                    // Fallback for registered user
     481                    else
     482                        $role = __( 'Member', 'bbpress' );
     483
     484                    break;
     485            }
     486        }
     487
     488        return apply_filters( 'bbp_get_user_display_role', $role, $user_id, $user_role );
    402489    }
    403490
     
    14471534/**
    14481535 * Output a users topic count
    1449  * 
     1536 *
    14501537 * @since bbPress (r3632)
    14511538 *
    14521539 * @param int $user_id
    14531540 * @uses bbp_get_user_topic_count()
    1454  * @return string 
     1541 * @return string
    14551542 */
    14561543function bbp_user_topic_count( $user_id = 0 ) {
     
    14591546    /**
    14601547     * Return a users reply count
    1461      * 
     1548     *
    14621549     * @since bbPress (r3632)
    14631550     *
     
    14661553     * @uses get_user_meta()
    14671554     * @uses apply_filters()
    1468      * @return string 
     1555     * @return string
    14691556     */
    14701557    function bbp_get_user_topic_count( $user_id = 0 ) {
     
    14821569/**
    14831570 * Output a users reply count
    1484  * 
     1571 *
    14851572 * @since bbPress (r3632)
    14861573 *
    14871574 * @param int $user_id
    14881575 * @uses bbp_get_user_reply_count()
    1489  * @return string 
     1576 * @return string
    14901577 */
    14911578function bbp_user_reply_count( $user_id = 0 ) {
     
    14941581    /**
    14951582     * Return a users reply count
    1496      * 
     1583     *
    14971584     * @since bbPress (r3632)
    14981585     *
     
    15011588     * @uses get_user_meta()
    15021589     * @uses apply_filters()
    1503      * @return string 
     1590     * @return string
    15041591     */
    15051592    function bbp_get_user_reply_count( $user_id = 0 ) {
     
    15171604/**
    15181605 * Output a users total post count
    1519  * 
     1606 *
    15201607 * @since bbPress (r3632)
    15211608 *
    15221609 * @param int $user_id
    15231610 * @uses bbp_get_user_post_count()
    1524  * @return string 
     1611 * @return string
    15251612 */
    15261613function bbp_user_post_count( $user_id = 0 ) {
     
    15291616    /**
    15301617     * Return a users total post count
    1531      * 
     1618     *
    15321619     * @since bbPress (r3632)
    15331620     *
     
    15361623     * @uses get_user_meta()
    15371624     * @uses apply_filters()
    1538      * @return string 
     1625     * @return string
    15391626     */
    15401627    function bbp_get_user_post_count( $user_id = 0 ) {
    1541        
     1628
    15421629        // Validate user id
    15431630        $user_id = bbp_get_user_id( $user_id );
  • branches/plugin/bbp-theme-compat/css/bbpress.css

    r3848 r3860  
    200200    padding-right: 20px;
    201201    word-break: break-word;
     202}
     203
     204li.bbp-body div.bbp-topic-author .bbp-author-role,
     205li.bbp-body div.bbp-reply-author .bbp-author-role {
     206    font-size: 11px;
     207    font-style: italic;
    202208}
    203209
  • branches/plugin/bbp-themes/bbp-twentyten/css/bbpress.css

    r3835 r3860  
    139139    text-align: center;
    140140    vertical-align: top;
     141}
     142#content td.bbp-topic-author .bbp-author-role,
     143#content td.bbp-reply-author .bbp-author-role {
     144    font-size: 11px;
     145    font-style: italic;
    141146}
    142147.bbp-topic-title {
Note: See TracChangeset for help on using the changeset viewer.