Skip to:
Content

bbPress.org

Changeset 5834


Ignore:
Timestamp:
07/15/2015 03:59:23 PM (10 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.

Location:
trunk/src
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r5829 r5834  
    224224        // Post type identifiers
    225225        $this->forum_post_type   = apply_filters( 'bbp_forum_post_type',  'forum'     );
     226        $this->forum_mod_tax_id  = apply_filters( 'bbp_forum_mod_tax_id', 'forum-mod' );
    226227        $this->topic_post_type   = apply_filters( 'bbp_topic_post_type',  'topic'     );
     228        $this->topic_tag_tax_id  = apply_filters( 'bbp_topic_tag_tax_id', 'topic-tag' );
    227229        $this->reply_post_type   = apply_filters( 'bbp_reply_post_type',  'reply'     );
    228         $this->topic_tag_tax_id  = apply_filters( 'bbp_topic_tag_tax_id', 'topic-tag' );
    229230
    230231        // Status identifiers
     
    652653
    653654    /**
    654      * Register the topic tag taxonomy
    655      *
    656      * @since bbPress (r2464)
     655     * Register the topic tag and forum moderator taxonomies
     656     *
     657     * @since bbPress (r2464) Added bbp_get_topic_tag_tax_id() taxonomy
     658     * @since bbPress (r5834) Added bbp_get_forum_mod_tax_id() taxonomy
     659     *
    657660     * @uses register_taxonomy() To register the taxonomy
     661     * @uses bbp_get_topic_post_type() To get the topic post type
     662     * @uses bbp_get_topic_tag_tax_labels() To get the topic tag taxonomy labels
     663     * @uses bbp_get_topic_tag_tax_rewrite() To get the topic tag taxonomy slug
     664     * @uses bbp_get_topic_tag_caps() To get topic tag capabilities
     665     * @uses bbp_allow_topic_tags() To check if topic tags are allowed
     666     * @uses current_user_can() To check if the current user can edit/delete tags
     667     * @uses bbp_get_forum_post_type() To get the forum post type
     668     * @uses bbp_get_forum_mod_tax_labels() To get the forum moderator taxonomy label
     669     * @uses bbp_get_forum_mod_caps() To check the forum moderator capabilities
     670     * @uses bbp_allow_forum_mods() To check if forum moderators are allowed
     671     * @uses current_user_can() To check if the current user can edit/delete forums
    658672     */
    659673    public static function register_taxonomies() {
    660674
    661         // Register the topic-tag taxonomy
     675        // Register the topic-tag taxonomy.
    662676        register_taxonomy(
    663677            bbp_get_topic_tag_tax_id(),
     
    673687                'show_in_nav_menus'     => false,
    674688                'public'                => true,
    675                 'show_ui'               => bbp_allow_topic_tags() && current_user_can( 'bbp_topic_tags_admin' )
     689                'show_ui'               => bbp_allow_topic_tags() && current_user_can( 'bbp_topic_tags_admin' ),
     690            )
     691        ) );
     692
     693        // Register the forum-mod taxonomy.
     694        register_taxonomy(
     695            bbp_get_forum_mod_tax_id(),
     696            bbp_get_forum_post_type(),
     697            apply_filters( 'bbp_register_forum_moderator_taxonomy', array(
     698                'labels'                => bbp_get_forum_mod_tax_labels(),
     699                'capabilities'          => bbp_get_forum_mod_caps(),
     700                'update_count_callback' => '_update_post_term_count',
     701                'query_var'             => false,
     702                'show_tagcloud'         => true,
     703                'hierarchical'          => false,
     704                'show_in_menu'          => true,
     705                'show_in_nav_menus'     => false,
     706                'public'                => false,
     707                'show_ui'               => bbp_allow_forum_mods() && current_user_can( 'bbp_forum_mods_admin' ),
    676708            )
    677709        ) );
  • trunk/src/includes/admin/forums.php

    r5829 r5834  
    6464        add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
    6565
    66         // Metabox actions
    67         add_action( 'add_meta_boxes',        array( $this, 'attributes_metabox'      ) );
    68         add_action( 'save_post',             array( $this, 'attributes_metabox_save' ) );
    69 
    7066        // Forum Column headers.
    7167        add_filter( 'manage_' . $this->post_type . '_posts_columns',        array( $this, 'column_headers' )        );
     
    7571        add_filter( 'page_row_actions',                                     array( $this, 'row_actions'    ), 10, 2 );
    7672
     73        // Metabox actions
     74        add_action( 'add_meta_boxes', array( $this, 'attributes_metabox'      ) );
     75        add_action( 'save_post',      array( $this, 'attributes_metabox_save' ) );
     76
    7777        // Check if there are any bbp_toggle_forum_* requests on admin_init, also have a message displayed
    7878        add_action( 'load-edit.php',  array( $this, 'toggle_forum'        ) );
    7979        add_action( 'admin_notices',  array( $this, 'toggle_forum_notice' ) );
     80
     81        // Forum moderators AJAX; run at -1 to preempt built-in tag search
     82        add_action( 'wp_ajax_ajax-tag-search', array( $this, 'ajax_tag_search'         ), -1 );
    8083
    8184        // Contextual Help
     
    260263
    261264        do_action( 'bbp_forum_attributes_metabox' );
     265    }
     266
     267    /**
     268     * Return user nicename suggestions instead of tag suggestions
     269     *
     270     * @since bbPress (r5834)
     271     *
     272     * @uses bbp_get_forum_mod_tax_id() To get the forum moderator taxonomy id
     273     * @uses sanitize_key() To sanitize the taxonomy id
     274     * @uses get_taxonomy() To get the forum moderator taxonomy
     275     * @uses current_user_can() To check if the current user add/edit forum moderators
     276     * @uses get_users() To get an array of users
     277     * @uses user_nicename() To get the users nice name
     278     *
     279     * @return Return early if not a request for forum moderators tax
     280     */
     281    public function ajax_tag_search() {
     282
     283        // Only do AJAX if this is a forum moderators tax search.
     284        if ( ! isset( $_GET['tax'] ) || ( bbp_get_forum_mod_tax_id() !== $_GET['tax'] ) ) {
     285            return;
     286        }
     287
     288        $taxonomy = sanitize_key( $_GET['tax'] );
     289        $tax      = get_taxonomy( $taxonomy );
     290        if ( empty( $tax ) ) {
     291            wp_die( 0 );
     292        }
     293
     294        // Check permissions.
     295        if ( ! current_user_can( $tax->cap->assign_terms ) ) {
     296            wp_die( -1 );
     297        }
     298
     299        $s = stripslashes( $_GET['q'] );
     300
     301        // Replace tag delimiter with a comma if needed.
     302        $comma = _x( ',', 'tag delimiter', 'bbpress' );
     303        if ( ',' !== $comma ) {
     304            $s = str_replace( $comma, ',', $s );
     305        }
     306
     307        if ( false !== strpos( $s, ',' ) ) {
     308            $s = explode( ',', $s );
     309            $s = $s[ count( $s ) - 1 ];
     310        }
     311
     312        // Search on at least 2 characters.
     313        $s = trim( $s );
     314        if ( strlen( $s ) < 2 ) {
     315            wp_die(); // Require 2 chars for matching.
     316        }
     317
     318        // Get users.
     319        $results = array();
     320        $users   = get_users( array(
     321            'blog_id'        => 0, // All users.
     322            'fields'         => array( 'user_nicename' ),
     323            'search'         => '*' . $s . '*',
     324            'search_columns' => array( 'user_nicename' ),
     325            'orderby'        => 'user_nicename',
     326        ) );
     327
     328        // Format the users into a nice array.
     329        if ( ! empty( $users ) ) {
     330            foreach ( array_values( $users ) as $details ) {
     331                $results[] = $details->user_nicename;
     332            }
     333        }
     334
     335        // Echo results for AJAX.
     336        echo join( $results, "\n" );
     337        wp_die();
    262338    }
    263339
     
    349425        /*<![CDATA[*/
    350426
    351             #misc-publishing-actions,
     427            #minor-publishing,
    352428            #save-post {
    353429                display: none;
     
    373449
    374450            .column-author,
     451            .column-bbp_forum_mods,
    375452            .column-bbp_reply_author,
    376453            .column-bbp_topic_author {
     
    566643        }
    567644
    568         $columns = array (
     645        // Set list table column headers
     646        $columns = array(
    569647            'cb'                    => '<input type="checkbox" />',
    570             'title'                 => __( 'Forum',     'bbpress' ),
    571             'bbp_forum_topic_count' => __( 'Topics',    'bbpress' ),
    572             'bbp_forum_reply_count' => __( 'Replies',   'bbpress' ),
    573             'author'                => __( 'Creator',   'bbpress' ),
    574             'bbp_forum_created'     => __( 'Created' ,  'bbpress' ),
    575             'bbp_forum_freshness'   => __( 'Freshness', 'bbpress' )
     648            'title'                 => __( 'Forum',      'bbpress' ),
     649            'bbp_forum_topic_count' => __( 'Topics',     'bbpress' ),
     650            'bbp_forum_reply_count' => __( 'Replies',    'bbpress' ),
     651            'author'                => __( 'Creator',    'bbpress' ),
     652            'bbp_forum_mods'        => __( 'Moderators', 'bbpress' ),
     653            'bbp_forum_created'     => __( 'Created' ,   'bbpress' ),
     654            'bbp_forum_freshness'   => __( 'Freshness',  'bbpress' )
    576655        );
     656
     657        // Remove forum mods column if not enabled
     658        if ( ! bbp_allow_forum_mods() ) {
     659            unset( $columns['bbp_forum_mods'] );
     660        }
    577661
    578662        return apply_filters( 'bbp_admin_forums_column_headers', $columns );
     
    609693            case 'bbp_forum_reply_count' :
    610694                bbp_forum_reply_count( $forum_id );
     695                break;
     696
     697            case 'bbp_forum_mods' :
     698                $moderators = wp_get_object_terms( $forum_id, bbp_get_forum_mod_tax_id() );
     699                if ( empty( $moderators ) ) {
     700                    esc_html__( 'None', 'bbpress' );
     701                } else {
     702                    echo implode( ', ', wp_list_pluck( $moderators, 'name' ) );
     703                }
    611704                break;
    612705
  • trunk/src/includes/admin/settings.php

    r5829 r5834  
    162162            ),
    163163
     164            // Allow per-forum moderators
     165            '_bbp_allow_forum_mods' => array(
     166                'title'             => __( 'Forum Moderators', 'bbpress' ),
     167                'callback'          => 'bbp_admin_setting_callback_forum_mods',
     168                'sanitize_callback' => 'intval',
     169                'args'              => array()
     170            ),
     171
    164172            // Allow topic tags
    165173            '_bbp_allow_search' => array(
     
    597605    <input name="_bbp_allow_topic_tags" id="_bbp_allow_topic_tags" type="checkbox" value="1" <?php checked( bbp_allow_topic_tags( true ) ); bbp_maybe_admin_setting_disabled( '_bbp_allow_topic_tags' ); ?> />
    598606    <label for="_bbp_allow_topic_tags"><?php esc_html_e( 'Allow topics to have tags', 'bbpress' ); ?></label>
     607
     608<?php
     609}
     610
     611/**
     612 * Allow forum-mods setting field
     613 *
     614 * @since bbPress (r5834)
     615 *
     616 * @uses checked() To display the checked attribute
     617 */
     618function bbp_admin_setting_callback_forum_mods() {
     619?>
     620
     621    <input name="_bbp_allow_forum_mods" id="_bbp_allow_forum_mods" type="checkbox" value="1" <?php checked( bbp_allow_forum_mods( true ) ); bbp_maybe_admin_setting_disabled( '_bbp_allow_forum_mods' ); ?> />
     622    <label for="_bbp_allow_forum_mods"><?php esc_html_e( 'Allow forums to have dedicated moderators', 'bbpress' ); ?></label>
    599623
    600624<?php
  • trunk/src/includes/core/filters.php

    r5820 r5834  
    255255add_filter( 'bbp_map_meta_caps', 'bbp_map_primary_meta_caps',   10, 4 ); // Primary caps
    256256add_filter( 'bbp_map_meta_caps', 'bbp_map_forum_meta_caps',     10, 4 ); // Forums
     257add_filter( 'bbp_map_meta_caps', 'bbp_map_forum_mod_meta_caps', 10, 4 ); // Forum mods
    257258add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_meta_caps',     10, 4 ); // Topics
     259add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_tag_meta_caps', 10, 4 ); // Topic tags
    258260add_filter( 'bbp_map_meta_caps', 'bbp_map_reply_meta_caps',     10, 4 ); // Replies
    259 add_filter( 'bbp_map_meta_caps', 'bbp_map_topic_tag_meta_caps', 10, 4 ); // Topic tags
    260261
    261262/** Deprecated ****************************************************************/
  • trunk/src/includes/core/options.php

    r5770 r5834  
    1313/**
    1414 * Get the default site options and their values.
    15  * 
     15 *
    1616 * These option
    1717 *
     
    3838        '_bbp_allow_revisions'        => 1,                          // Allow revisions
    3939        '_bbp_allow_topic_tags'       => 1,                          // Allow topic tagging
     40        '_bbp_allow_forum_mods'       => 1,                          // Allow per-forum moderation
    4041        '_bbp_allow_threaded_replies' => 0,                          // Allow threaded replies
    4142        '_bbp_allow_search'           => 1,                          // Allow forum-wide search
     
    233234function bbp_allow_topic_tags( $default = 1 ) {
    234235    return (bool) apply_filters( 'bbp_allow_topic_tags', (bool) get_option( '_bbp_allow_topic_tags', $default ) );
     236}
     237
     238/**
     239 * Are per-forum moderators allowed
     240 *
     241 * @since bbPress (r5834)
     242 *
     243 * @param bool $default Optional. Default value true.
     244 * @uses get_option() To get the allow per-forum moderators
     245 *
     246 * @return bool Are per-forum moderators allowed?
     247 */
     248function bbp_allow_forum_mods( $default = 1 ) {
     249    return (bool) apply_filters( 'bbp_allow_forum_mods', (bool) get_option( '_bbp_allow_forum_mods', $default ) );
    235250}
    236251
  • 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}
  • trunk/src/includes/forums/template.php

    r5770 r5834  
    21782178    }
    21792179
     2180/** Moderators ****************************************************************/
     2181
     2182/**
     2183 * Output the unique id of the forum moderators taxonomy
     2184 *
     2185 * @since bbPress (r5834)
     2186 *
     2187 * @uses bbp_get_forum_mod_tax_id() To get the forum modorator taxonomy ID
     2188 */
     2189function bbp_forum_mod_tax_id() {
     2190    echo bbp_get_forum_mod_tax_id();
     2191}
     2192    /**
     2193     * Return the unique id of the forum moderators taxonomy
     2194     *
     2195     * @since bbPress (r5834)
     2196     *
     2197     * @uses apply_filters() Calls 'bbp_get_forum_mod_tax_id' with the forum
     2198     *                        moderator taxonomy id
     2199     * @return string The unique forum moderators taxonomy
     2200     */
     2201    function bbp_get_forum_mod_tax_id() {
     2202        return apply_filters( 'bbp_get_forum_mod_tax_id', bbpress()->forum_mod_tax_id );
     2203    }
     2204
     2205/**
     2206 * Return array of labels used by the forum-mod taxonomy
     2207 *
     2208 * @since bbPress (r5834)
     2209 *
     2210 * @uses apply_filters() Calls 'bbp_get_forum_mod_tax_id' with the forum
     2211 *                        moderator taxonomy labels
     2212 * @return array
     2213 */
     2214function bbp_get_forum_mod_tax_labels() {
     2215    return apply_filters( 'bbp_get_forum_mod_tax_labels', array(
     2216        'name'                       => __( 'Forum Moderators',     'bbpress' ),
     2217        'singular_name'              => __( 'Forum Moderator',      'bbpress' ),
     2218        'search_items'               => __( 'Search Moderators',    'bbpress' ),
     2219        'popular_items'              => __( 'Popular Moderators',   'bbpress' ),
     2220        'all_items'                  => __( 'All Moderators',       'bbpress' ),
     2221        'edit_item'                  => __( 'Edit Moderator',       'bbpress' ),
     2222        'update_item'                => __( 'Update Moderator',     'bbpress' ),
     2223        'add_new_item'               => __( 'Add New Moderator',    'bbpress' ),
     2224        'new_item_name'              => __( 'New Moderator Name',   'bbpress' ),
     2225        'view_item'                  => __( 'View Forum Moderator', 'bbpress' ),
     2226        'separate_items_with_commas' => __( 'Separate moderator names with commas', 'bbpress' ),
     2227    ) );
     2228}
     2229
     2230/**
     2231 * Output a the moderators of a forum
     2232 *
     2233 * @param int   $forum_id Optional. Topic id
     2234 * @param array $args     See {@link bbp_get_forum_mod_list()}
     2235 * @uses bbp_get_topic_tag_list() To get the forum mod list
     2236 */
     2237function bbp_forum_mod_list( $forum_id = 0, $args = array() ) {
     2238    echo bbp_get_forum_mod_list( $forum_id, $args );
     2239}
     2240    /**
     2241     * Return the moderators of a forum
     2242     *
     2243     * @param int   $forum_id Optional. Forum id
     2244     * @param array $args     This function supports these arguments:
     2245     *  - before: Before the tag list
     2246     *  - sep: Tag separator
     2247     *  - after: After the tag list
     2248     * @uses bbp_get_forum_id()  To get the forum id
     2249     * @uses get_the_term_list() To get the moderator list
     2250     *
     2251     * @return string Moderator list of the forum
     2252     */
     2253    function bbp_get_forum_mod_list( $forum_id = 0, $args = array() ) {
     2254
     2255        // Bail if forum-mods are off
     2256        if ( ! bbp_allow_forum_mods() ) {
     2257            return;
     2258        }
     2259
     2260        // Parse arguments against default values
     2261        $r = bbp_parse_args( $args, array(
     2262            'before' => '<div class="bbp-forum-mods"><p>' . esc_html__( 'Moderators:', 'bbpress' ) . '&nbsp;',
     2263            'sep'    => ', ',
     2264            'after'  => '</p></div>'
     2265        ), 'get_forum_mod_list' );
     2266
     2267        $forum_id = bbp_get_forum_id( $forum_id );
     2268
     2269        $retval   = get_the_term_list( $forum_id, bbp_get_forum_mod_id(), $r['before'], $r['sep'], $r['after'] );
     2270
     2271        return $retval;
     2272    }
     2273
    21802274/** Forms *********************************************************************/
    21812275
  • trunk/src/includes/replies/capabilities.php

    r5829 r5834  
    4040 * @uses get_post() To get the post
    4141 * @uses get_post_type_object() To get the post type object
     42 * @uses bbp_get_public_status_id() To get the public status id
     43 * @uses bbp_is_user_forum_mod() To check if the user is a forum moderator
     44 * @uses bbp_get_reply_forum_id() To get the repliy forum id
    4245 * @uses apply_filters() Filter mapped results
     46 *
    4347 * @return array Actual capabilities for meta capability
    4448 */
     
    6367                if ( ! empty( $_post ) ) {
    6468
    65                     // Get caps for post type object
     69                    // Get post type object
    6670                    $post_type = get_post_type_object( $_post->post_type );
    6771
     
    118122            if ( ! empty( $_post ) ) {
    119123
    120                 // Get caps for post type object
     124                // Get post type object
    121125                $post_type = get_post_type_object( $_post->post_type );
    122126                $caps      = array();
     
    127131
    128132                // User is author so allow edit if not in admin
    129                 } elseif ( !is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
     133                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
    130134                    $caps[] = $post_type->cap->edit_posts;
    131135
    132                 // Unknown, so map to edit_others_posts
     136                // User is a per-forum moderator, make sure they can spectate.
     137                } elseif ( bbp_allow_forum_mods() && bbp_is_user_forum_mod( $user_id, bbp_get_reply_forum_id( $_post->ID ) ) ) {
     138                    $caps = array( 'spectate' );
     139
     140                // Fallback to edit_others_posts.
    133141                } else {
    134142                    $caps[] = $post_type->cap->edit_others_posts;
     
    146154            if ( ! empty( $_post ) ) {
    147155
    148                 // Get caps for post type object
     156                // Get post type object
    149157                $post_type = get_post_type_object( $_post->post_type );
    150158                $caps      = array();
  • trunk/src/includes/topics/capabilities.php

    r5770 r5834  
    5858 * @uses get_post() To get the post
    5959 * @uses get_post_type_object() To get the post type object
     60 * @uses bbp_get_public_status_id() To get the  public status id
     61 * @uses bbp_is_user_forum_mod() To check if the user is a forum moderator
     62 * @uses bbp_get_topic_forum_id() To get the opic forum id
    6063 * @uses apply_filters() Filter capability map results
     64 *
    6165 * @return array Actual capabilities for meta capability
    6266 */
     
    150154                // Unknown, so map to edit_others_posts
    151155                } else {
    152                     $caps[] = $post_type->cap->edit_others_posts;
     156
     157                    // If user is a per-forum moderator, make sure they can spectate.
     158                    if ( bbp_is_user_forum_mod( $user_id, bbp_get_topic_forum_id( $_post->ID ) ) ) {
     159                        $caps = array( 'spectate' );
     160
     161                    // Fallback to edit_others_posts.
     162                    } else {
     163                        $caps[] = $post_type->cap->edit_others_posts;
     164                    }
    153165                }
    154166            }
  • trunk/src/includes/users/capabilities.php

    r5827 r5834  
    1515 * @since bbPress (r4242)
    1616 *
    17  * @param array $caps Capabilities for meta capability
    18  * @param string $cap Capability name
    19  * @param int $user_id User id
    20  * @param array $args Arguments
     17 * @param array  $caps Capabilities for meta capability.
     18 * @param string $cap Capability name.
     19 * @param int    $user_id User id.
     20 * @param array  $args Arguments.
     21 * @uses bbp_is_user_inactive() To check if user is spammer or deleted
     22 * @uses get_post() To get the post
     23 * @uses bbp_get_forum_post_type() To get the forum post type
     24 * @uses bbp_get_topic_post_type() To get the topic post type
     25 * @uses bbp_get_topic_forum_id() To get the topic forum id
     26 * @uses bbp_get_reply_post_type() To get the reply post type
     27 * @uses bbp_get_reply_forum_id() To get the reply forum id
     28 * @uses bbp_is_user_forum_mod() To check if the user is a forum moderator
    2129 * @uses apply_filters() Filter mapped results
     30 *
    2231 * @return array Actual capabilities for meta capability
    2332 */
     
    2635    // What capability is being checked?
    2736    switch ( $cap ) {
    28         case 'spectate'    :
    29         case 'participate' :
    30         case 'moderate'    :
    31 
    32             // Do not allow inactive users
     37        case 'spectate' :
     38
     39            // Do not allow inactive users.
    3340            if ( bbp_is_user_inactive( $user_id ) ) {
    3441                $caps = array( 'do_not_allow' );
    3542
    36             // Moderators are always participants
     43            // Default to the current cap.
    3744            } else {
    3845                $caps = array( $cap );
     46            }
     47            break;
     48
     49        case 'participate' :
     50
     51            // Do not allow inactive users.
     52            if ( bbp_is_user_inactive( $user_id ) ) {
     53                $caps = array( 'do_not_allow' );
     54
     55            // Default to the current cap.
     56            } else {
     57                $caps = array( $cap );
     58            }
     59            break;
     60
     61        case 'moderate' :
     62
     63            // Do not allow inactive users.
     64            if ( bbp_is_user_inactive( $user_id ) ) {
     65                $caps = array( 'do_not_allow' );
     66
     67            // Default to the current cap.
     68            } else {
     69                $caps = array( $cap );
     70
     71                // Bail if no post to check.
     72                if ( empty( $args[0] ) ) {
     73                    break;
     74                }
     75
     76                // Get the post.
     77                $_post = get_post( $args[0] );
     78                if ( empty( $_post ) ) {
     79                    break;
     80                }
     81
     82                // Get forum ID for specific type of post.
     83                switch ( $_post->post_type ) {
     84
     85                    // Forum.
     86                    case bbp_get_forum_post_type() :
     87                        $forum_id = $_post->ID;
     88                        break;
     89
     90                    // Topic.
     91                    case bbp_get_topic_post_type() :
     92                        $forum_id = bbp_get_topic_forum_id( $_post->ID );
     93                        break;
     94
     95                    // Reply.
     96                    case bbp_get_reply_post_type() :
     97                        $forum_id = bbp_get_reply_forum_id( $_post->ID );
     98                        break;
     99
     100                    // Any other post type defaults to 0.
     101                    default :
     102                        $forum_id = 0;
     103                        break;
     104                }
     105
     106                // Bail if no forum ID.
     107                if ( empty( $forum_id ) ) {
     108                    break;
     109                }
     110
     111                // If user is a per-forum moderator, make sure they can spectate.
     112                if ( bbp_is_user_forum_mod( $user_id, $forum_id ) ) {
     113                    $caps = array( 'spectate' );
     114                }
    39115            }
    40116
     
    212288        return;
    213289    }
    214    
     290
    215291    // Bail if current user cannot promote the passing user
    216292    if ( ! current_user_can( 'promote_user', $user_id ) ) {
     
    308384
    309385    // Don't add the user, but still give them the correct caps dynamically
    310     } else {       
     386    } else {
    311387        $bbp->current_user->caps[$new_role] = true;
    312388        $bbp->current_user->get_role_caps();
     
    671747 * @since bbPress (r4783)
    672748 *
    673  * @param int $user_id 
     749 * @param int $user_id
    674750 * @return bool True if keymaster, false if not
    675751 */
  • trunk/src/includes/users/functions.php

    r5827 r5834  
    19061906}
    19071907
     1908/** User Nicename Taxonomies **************************************************/
     1909
     1910/**
     1911 * Return the term id for a given user id and taxonomy
     1912 *
     1913 * @since bbPress (r5834)
     1914 *
     1915 * @param int    $user_id User id.
     1916 * @param string $taxonomy Taxonomy.
     1917 * @uses get_userdata() To get the user data
     1918 * @uses taxonomy_exists() To make sure the taxonomy exists
     1919 * @uses get_term_by() To get the term by name
     1920 *
     1921 * @return boolean|int Return false early, or if not found, or int term id
     1922 */
     1923function bbp_get_user_taxonomy_term_id( $user_id = 0, $taxonomy = '' ) {
     1924
     1925    // Bail if no user ID.
     1926    if ( empty( $user_id ) ) {
     1927        return false;
     1928    }
     1929
     1930    // Bail if user does not exist.
     1931    $user = get_userdata( $user_id );
     1932    if ( empty( $user ) ) {
     1933        return false;
     1934    }
     1935
     1936    // Bail if no taxonomy.
     1937    if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
     1938        return false;
     1939    }
     1940
     1941    // Get the term id.
     1942    $term = get_term_by( 'name', $user->user_nicename, $taxonomy );
     1943    if ( ! empty( $term ) ) {
     1944        return $term->term_id;
     1945    }
     1946
     1947    return false;
     1948}
     1949
     1950/**
     1951 * Return the user id for a given term id and taxonomy
     1952 *
     1953 * @since bbPress (r5834)
     1954 *
     1955 * @param int    $term_id Term id.
     1956 * @param string $taxonomy Taxonomy.
     1957 * @uses taxonomy_exists() To make sure the taxonomy exists
     1958 * @uses get_term() To get the term by term id
     1959 * @uses get_user_by() To get the user by nicename
     1960 *
     1961 * @return boolean|int Return false early, or if not found, or int user id
     1962 */
     1963function bbp_get_term_taxonomy_user_id( $term_id = 0, $taxonomy = '' ) {
     1964
     1965    // Bail if no user ID.
     1966    if ( empty( $term_id ) ) {
     1967        return false;
     1968    }
     1969
     1970    // Bail if no taxonomy.
     1971    if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
     1972        return false;
     1973    }
     1974
     1975    // Bail if no term exists.
     1976    $term = get_term( $term_id, $taxonomy );
     1977    if ( empty( $term ) ) {
     1978        return false;
     1979    }
     1980
     1981    // Get the user by nicename.
     1982    $nicename = $term->name;
     1983    $user     = get_user_by( 'slug', $nicename );
     1984    if ( ! empty( $user ) ) {
     1985        return $user->ID;
     1986    }
     1987
     1988    return false;
     1989}
     1990
     1991function bbp_filter_forum_mod_term_link( $termlink = '', $term = '', $taxonomy = '' ) {
     1992
     1993    // Bail if taxonomy is not forum mod
     1994    if ( bbp_get_forum_mod_tax_id() !== $taxonomy ) {
     1995        return $termlink;
     1996    }
     1997
     1998    // Bail if forum mods is not allowed
     1999    if ( ! bbp_allow_forum_mods() ) {
     2000        return $termlink;
     2001    }
     2002
     2003    // Get user ID from taxonomy term
     2004    $user_id = bbp_get_term_taxonomy_user_id( $term->term_id, bbp_get_forum_mod_tax_id() );
     2005
     2006    if ( is_admin() ) {
     2007
     2008        // Get the moderator's display name
     2009        $display_name = get_userdata( $user_id )->display_name;
     2010        $user_link    = get_edit_user_link( $user_id );
     2011
     2012        // Link or name only
     2013        if ( ! empty( $user_link ) ) {
     2014            $retval = '<a href="' . esc_url( $user_link ) . '">' . esc_html( $display_name ) . '</a>';
     2015
     2016        // Can't edit
     2017        } else {
     2018            $retval = $display_name;
     2019        }
     2020
     2021    // Theme side term link
     2022    } else {
     2023        $retval = bbp_get_user_profile_link( $user_id );
     2024    }
     2025
     2026    return $retval;
     2027}
     2028
    19082029/** Permissions ***************************************************************/
    19092030
Note: See TracChangeset for help on using the changeset viewer.