Skip to:
Content

bbPress.org


Ignore:
Timestamp:
07/15/2015 03:59:23 PM (7 years ago)
Author:
johnjamesjacoby
Message:

Moderators: First pass at per-forum moderators.

This commit introduces a powerful feature commonly found in other popular forum software that has been on our wishlist for nearly 9 years. It includes the following changes:

  • Custom forum-mod taxonomy for assigning user nicenames to forum IDs
  • Associated functions for defining capabilities, labels, etc...
  • New capability filters for ensuring forum moderators have the ability to moderate forums even without the moderator role assignment
  • New option for toggling the entire feature on/off (on by default)

Props jmdodd, netweb. See #459.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/forums/capabilities.php

    r5829 r5834  
    180180        /** Admin *************************************************************/
    181181
     182        // Forum admin area.
    182183        case 'bbp_forums_admin' :
    183184            $caps = array( 'keep_gate' );
    184185            break;
     186
     187        // Forum moderator admin area.
     188        case 'bbp_forum_mods_admin' :
     189            $caps = array( 'keep_gate' );
     190            break;
    185191    }
    186192
    187193    return apply_filters( 'bbp_map_forum_meta_caps', $caps, $cap, $user_id, $args );
    188194}
     195
     196/**
     197 * Return forum moderator capabilities
     198 *
     199 * @since bbPress (r5834)
     200 *
     201 * @uses apply_filters() Calls 'bbp_get_forum_mod_caps' with the capabilities
     202 *
     203 * @return array Forum mod capabilities.
     204 */
     205function bbp_get_forum_mod_caps() {
     206    return apply_filters( 'bbp_get_forum_mod_caps', array(
     207        'manage_terms' => 'keep_gate',
     208        'edit_terms'   => 'keep_gate',
     209        'delete_terms' => 'keep_gate',
     210        'assign_terms' => 'keep_gate',
     211    ) );
     212}
     213
     214/**
     215 * Maps forum moderator capabilities
     216 *
     217 * @since bbPress (rXXXX)
     218 *
     219 * @param array  $caps Capabilities for meta capability.
     220 * @param string $cap Capability name.
     221 * @param int    $user_id User id.
     222 * @param mixed  $args Arguments.
     223 * @uses apply_filters() Filter capabilities map results.
     224 *
     225 * @return array Actual capabilities for meta capability.
     226 */
     227function bbp_map_forum_mod_meta_caps( $caps, $cap, $user_id, $args ) {
     228
     229    // What capability is being checked?
     230    switch ( $cap ) {
     231        case 'manage_forum_mods'    :
     232        case 'edit_forum_mods'      :
     233        case 'delete_forum_mods'    :
     234        case 'assign_forum_mods'    :
     235        case 'bbp_forum_mods_admin' :
     236
     237            // Key Masters can always edit.
     238            if ( user_can( $user_id, 'keep_gate' ) ) {
     239                $caps = array( 'keep_gate' );
     240            }
     241    }
     242
     243    return apply_filters( 'bbp_map_forum_mod_meta_caps', $caps, $cap, $user_id, $args );
     244}
     245
     246/**
     247 * Get moderators of a forum
     248 *
     249 * @since bbPress (r5834)
     250 *
     251 * @param int $forum_id Forum id.
     252 * @uses bbp_get_forum_id() To get the forum id
     253 * @uses bbp_is_forum() To make sure it is a forum
     254 * @uses bbp_get_forum_mod_tax_id() To get the forum moderator taxonomy
     255 * @uses wp_get_object_terms() To get the forum's moderator terms
     256 * @uses is_wp_error() To check for errors
     257 * @uses bbp_get_term_taxonomy_user_id() To convert terms to user ids
     258 *
     259 * @return boolean|array Return false early, or if no moderator terms set, or
     260 *                 an array of User ids
     261 */
     262function bbp_get_forum_mod_ids( $forum_id = 0 ) {
     263
     264    // Bail if no forum ID.
     265    $forum_id = bbp_get_forum_id( $forum_id );
     266    if ( empty( $forum_id ) ) {
     267        return false;
     268    }
     269
     270    // Bail if forum does not exist.
     271    if ( ! bbp_is_forum( $forum_id ) ) {
     272        return false;
     273    }
     274
     275    // Get forum taxonomy terms.
     276    $taxonomy = bbp_get_forum_mod_tax_id();
     277    $terms    = wp_get_object_terms( $forum_id, $taxonomy, array(
     278        'fields' => 'ids',
     279    ) );
     280
     281    // Bail if no terms found.
     282    if ( empty( $terms ) || is_wp_error( $terms ) ) {
     283        return false;
     284    }
     285
     286    $moderators = array();
     287
     288    // Convert term ids to user ids.
     289    foreach ( $terms as $term ) {
     290        $user_id = bbp_get_term_taxonomy_user_id( $term, $taxonomy );
     291        if ( ! empty( $user_id ) ) {
     292            $moderators[] = $user_id;
     293        }
     294    }
     295
     296    // Moderators found.
     297    if ( ! empty( $moderators ) ) {
     298        return $moderators;
     299    }
     300
     301    return false;
     302}
     303
     304/**
     305 * Get forums of a moderator
     306 *
     307 * @since bbPress (r5834)
     308 *
     309 * @param int $user_id User id.
     310 * @uses get_userdata() To get the user object
     311 * @uses bbp_get_forum_mod_tax_id() To get the forum moderator taxonomy
     312 * @uses bbp_get_user_taxonomy_term_id() To get the user taxonomy term id
     313 * @uses get_term_by() To get the term id
     314 * @uses get_objects_in_term() Get the forums the user moderates
     315 * @uses is_wp_error() To check for errors
     316 * @uses bbp_is_forum() To make sure the objects are forums
     317 *
     318 * @return boolean|array Return false early, or if user has no forums, or
     319 *                 an array of Forum ids
     320 */
     321function bbp_get_moderator_forum_ids( $user_id = 0 ) {
     322
     323    // Bail if no user ID.
     324    if ( empty( $user_id ) ) {
     325        return false;
     326    }
     327
     328    // Bail if user does not eist.
     329    $user = get_userdata( $user_id );
     330    if ( empty( $user ) ) {
     331        return false;
     332    }
     333
     334    // Convert user id to term id.
     335    $taxonomy = bbp_get_forum_mod_tax_id();
     336    $term_id  = bbp_get_user_taxonomy_term_id( $user_id, $taxonomy );
     337
     338    // Get moderator forums.
     339    $forums   = get_objects_in_term( $term_id, $taxonomy );
     340
     341    // Forums found.
     342    if ( empty( $forums ) || is_wp_error( $forums ) ) {
     343        return false;
     344    }
     345
     346    // Make sure the ids returned are forums.
     347    $forum_ids = array();
     348    foreach ( $forums as $forum_id ) {
     349        if ( bbp_is_forum( $forum_id ) ) {
     350            $forum_ids[] = $forum_id;
     351        }
     352    }
     353
     354    return $forum_ids;
     355}
     356
     357/**
     358 * Can a user moderate a forum?
     359 *
     360 * @since bbPress (r5834)
     361 *
     362 * @param int $user_id User id.
     363 * @param int $forum_id Forum id.
     364 * @uses bbp_get_user_id()
     365 * @uses bbp_get_forum_id()
     366 * @uses bbp_get_moderator_forum_ids()
     367 * @uses apply_filters() Calls 'bbp_is_user_forum_mod' with the forums
     368 *
     369 * @return bool Return true if user is moderator of forum
     370 */
     371function bbp_is_user_forum_mod( $user_id = 0, $forum_id = 0 ) {
     372
     373    // Assume user cannot moderate the forum.
     374    $retval    = false;
     375
     376    // Validate user ID - fallback to current user if no ID passed.
     377    $user_id   = bbp_get_user_id( $user_id, false, ! empty( $user_id ) );
     378    $forum_id  = bbp_get_forum_id( $forum_id );
     379
     380    // Get forums the user can moderate.
     381    $forum_ids = bbp_get_moderator_forum_ids( $user_id );
     382
     383    // Is this forum ID in the users array of forum IDs?
     384    if ( ! empty( $forum_ids ) ) {
     385        $retval = in_array( $forum_id, $forum_ids );
     386    }
     387
     388    return (bool) apply_filters( 'bbp_is_user_forum_mod', $retval, $user_id, $forum_id, $forum_ids );
     389}
Note: See TracChangeset for help on using the changeset viewer.