Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/05/2016 06:27:54 PM (6 years ago)
Author:
johnjamesjacoby
Message:

Moderators: Refactor per-forum moderators to use meta-data instead of mocked taxonomy terms.

If the future of Forums is a taxonomy (vs. a custom post-type) then a per-forum Moderator taxonomy for a Forum taxonomy won't work very well, for a few reasons:

  • Scalability
  • Taxonomies for taxonomies is a bit more inception than should be required for this simple feature
  • Forum moderators do not require much of what taxonomy objects provide (permalinks, visibility, metadata, etc...)
  • User taxonomy terms matching nicenames works okay for something like Automattic's P2 theme, but bbPress requires a user ID based solution to avoid data synchronization issues between nicenames & term slugs

So... the future of per-forum per-user capability mappings is in meta-data using map_meta_cap.

This commit:

  • Removes the forum_mod taxonomy and surrounding code additions introduced in the first pass in r5834
  • Renames forum_mod functions to forum_moderator to be more explicit
  • Adds CRUD wrapper functions for per-forum moderator meta data
  • Adds administrative interfaces for assigning moderators to forums for wp-admin and forum edit pages
  • Adds helper functions for getting user nicenames & IDs

Note that this feature has now been refactored to no longer be forum specific (I.E. object agnostic) -- it's possible for any user access to be mapped based on the object type using any meta-data key. While this is currently useful for per-forum moderators, it may be user for per-topic blocking, per-topic-tag moderation, etc...

See #459.

File:
1 edited

Legend:

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

    r6026 r6056  
    465465
    466466/**
    467  * Output value of forum mods field
    468  *
    469  * @since 2.6.0 bbPress (r5837)
    470  *
    471  * @uses bbp_get_form_forum_mods() To get the value of forum mods field
    472  */
    473 function bbp_form_forum_mods() {
    474     echo bbp_get_form_forum_mods();
    475 }
    476     /**
    477      * Return value of forum mods field
    478      *
    479      * @since 2.6.0 bbPress (r5837)
    480      *
    481      * @uses bbp_is_forum_edit() To check if it's the forum edit page
    482      * @uses apply_filters() Calls 'bbp_get_form_forum_mods' with the mods
    483      *
    484      * @return string Value of forum mods field
    485      */
    486     function bbp_get_form_forum_mods() {
    487 
    488         // Get _POST data
    489         if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_mods'] ) ) {
    490             $forum_mods = wp_unslash( $_POST['bbp_forum_mods'] );
    491 
    492         // Get edit data
    493         } elseif ( bbp_is_single_forum() || bbp_is_forum_edit() ) {
    494 
    495             // Get the forum ID
    496             $forum_id = bbp_get_forum_id( get_the_ID() );
    497 
    498             // Forum exists
    499             if ( ! empty( $forum_id ) ) {
    500                 $forum_mods = bbp_get_forum_mod_names( $forum_id );
    501             }
    502 
    503         // No data
    504         } else {
    505             $forum_mods = '';
    506         }
    507 
    508         return apply_filters( 'bbp_get_form_forum_mods', $forum_mods );
    509     }
    510 
    511 /**
    512467 * Output the forums last active ID
    513468 *
     
    22252180    }
    22262181
    2227 /** Moderators ****************************************************************/
    2228 
    2229 /**
    2230  * Output the unique id of the forum moderators taxonomy
    2231  *
    2232  * @since 2.6.0 bbPress (r5834)
    2233  *
    2234  * @uses bbp_get_forum_mod_tax_id() To get the forum modorator taxonomy ID
    2235  */
    2236 function bbp_forum_mod_tax_id() {
    2237     echo bbp_get_forum_mod_tax_id();
    2238 }
    2239     /**
    2240      * Return the unique id of the forum moderators taxonomy
    2241      *
    2242      * @since 2.6.0 bbPress (r5834)
    2243      *
    2244      * @uses apply_filters() Calls 'bbp_get_forum_mod_tax_id' with the forum
    2245      *                        moderator taxonomy id
    2246      * @return string The unique forum moderators taxonomy
    2247      */
    2248     function bbp_get_forum_mod_tax_id() {
    2249         return apply_filters( 'bbp_get_forum_mod_tax_id', bbpress()->forum_mod_tax_id );
    2250     }
    2251 
    2252 /**
    2253  * Return array of labels used by the forum-mod taxonomy
    2254  *
    2255  * @since 2.6.0 bbPress (r5834)
    2256  *
    2257  * @uses apply_filters() Calls 'bbp_get_forum_mod_tax_id' with the forum
    2258  *                        moderator taxonomy labels
    2259  * @return array
    2260  */
    2261 function bbp_get_forum_mod_tax_labels() {
    2262     return apply_filters( 'bbp_get_forum_mod_tax_labels', array(
    2263         'name'                       => __( 'Forum Moderators',     'bbpress' ),
    2264         'singular_name'              => __( 'Forum Moderator',      'bbpress' ),
    2265         'search_items'               => __( 'Search Moderators',    'bbpress' ),
    2266         'popular_items'              => __( 'Popular Moderators',   'bbpress' ),
    2267         'all_items'                  => __( 'All Moderators',       'bbpress' ),
    2268         'edit_item'                  => __( 'Edit Moderator',       'bbpress' ),
    2269         'update_item'                => __( 'Update Moderator',     'bbpress' ),
    2270         'add_new_item'               => __( 'Add New Moderator',    'bbpress' ),
    2271         'new_item_name'              => __( 'New Moderator Name',   'bbpress' ),
    2272         'view_item'                  => __( 'View Forum Moderator', 'bbpress' ),
    2273         'separate_items_with_commas' => __( 'Separate moderator names with commas', 'bbpress' ),
    2274     ) );
    2275 }
    2276 
    2277 /**
    2278  * Output a the moderators of a forum
    2279  *
    2280  * @since 2.6.0 bbPress (r5834)
    2281  *
    2282  * @param int   $forum_id Optional. Topic id
    2283  * @param array $args     See {@link bbp_get_forum_mod_list()}
    2284  * @uses bbp_get_topic_tag_list() To get the forum mod list
    2285  */
    2286 function bbp_forum_mod_list( $forum_id = 0, $args = array() ) {
    2287     echo bbp_get_forum_mod_list( $forum_id, $args );
    2288 }
    2289     /**
    2290      * Return the moderators of a forum
    2291      *
    2292      * @since 2.6.0 bbPress (r5834)
    2293      *
    2294      * @param int   $forum_id Optional. Forum id
    2295      * @param array $args     This function supports these arguments:
    2296      *  - before: Before the tag list
    2297      *  - sep: Tag separator
    2298      *  - after: After the tag list
    2299      * @uses bbp_get_forum_id()  To get the forum id
    2300      * @uses get_the_term_list() To get the moderator list
    2301      *
    2302      * @return string Moderator list of the forum
    2303      */
    2304     function bbp_get_forum_mod_list( $forum_id = 0, $args = array() ) {
    2305 
    2306         // Bail if forum-mods are off
    2307         if ( ! bbp_allow_forum_mods() ) {
    2308             return '';
    2309         }
    2310 
    2311         // Parse arguments against default values
    2312         $r = bbp_parse_args( $args, array(
    2313             'before' => '<div class="bbp-forum-mods"><p>' . esc_html__( 'Moderators:', 'bbpress' ) . '&nbsp;',
    2314             'sep'    => ', ',
    2315             'after'  => '</p></div>',
    2316             'none'   => ''
    2317         ), 'get_forum_mod_list' );
    2318 
    2319         // Bail if forum ID is invalid
    2320         $forum_id = bbp_get_forum_id( $forum_id );
    2321         if ( empty( $forum_id ) ) {
    2322             return '';
    2323         }
    2324 
    2325         // Get forum moderators
    2326         $moderators = bbp_get_forum_mods( $forum_id );
    2327         if ( ! empty( $moderators ) ) {
    2328 
    2329             // In admin, use nicenames
    2330             if ( is_admin() ) {
    2331 
    2332                 // @todo link to filtering forums by moderator
    2333                 $users = wp_list_pluck( $moderators, 'name' );
    2334 
    2335             // In theme, use display names & profile links
    2336             } else {
    2337                 $users    = array();
    2338                 $term_ids = wp_list_pluck( $moderators, 'term_id' );
    2339                 foreach ( $term_ids as $term_id ) {
    2340                     $user_id = bbp_get_term_taxonomy_user_id( $term_id );
    2341                     $users[] = bbp_get_user_profile_link( $user_id );
    2342                 }
    2343             }
    2344 
    2345             $retval = $r['before'] . implode( $r['sep'], $users ) . $r['after'];
    2346 
    2347         // No forum moderators
    2348         } else {
    2349             $retval = $r['none'];
    2350         }
    2351 
    2352         return $retval;
    2353     }
    2354 
    23552182/** Forms *********************************************************************/
    23562183
     
    24272254
    24282255        return apply_filters( 'bbp_get_form_forum_content', $forum_content );
     2256    }
     2257
     2258/**
     2259 * Output value of forum moderators field
     2260 *
     2261 * @since 2.6.0 bbPress (r5837)
     2262 *
     2263 * @uses bbp_get_form_forum_moderators() To get the value of forum moderators field
     2264 */
     2265function bbp_form_forum_moderators() {
     2266    echo bbp_get_form_forum_moderators();
     2267}
     2268    /**
     2269     * Return value of forum moderators field
     2270     *
     2271     * @since 2.6.0 bbPress (r5837)
     2272     *
     2273     * @uses bbp_is_forum_edit() To check if it's the forum edit page
     2274     * @uses apply_filters() Calls 'bbp_get_form_forum_mods' with the mods
     2275     *
     2276     * @return string Value of forum mods field
     2277     */
     2278    function bbp_get_form_forum_moderators() {
     2279
     2280        // Default return value
     2281        $forum_mods = '';
     2282
     2283        // Get _POST data
     2284        if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_moderators'] ) ) {
     2285            $forum_mods = wp_unslash( $_POST['bbp_moderators'] );
     2286
     2287        // Get edit data
     2288        } elseif ( bbp_is_single_forum() || bbp_is_forum_edit() ) {
     2289
     2290            // Get the forum ID
     2291            $forum_id = bbp_get_forum_id( get_the_ID() );
     2292
     2293            // Forum exists
     2294            if ( ! empty( $forum_id ) ) {
     2295
     2296                // Get moderator IDs
     2297                $user_ids = bbp_get_moderator_ids( $forum_id );
     2298                if ( ! empty( $user_ids ) ) {
     2299                    $user_nicenames = bbp_get_user_nicenames_from_ids( $user_ids );
     2300
     2301                    // Comma separate user nicenames
     2302                    if ( ! empty( $user_nicenames ) ) {
     2303                        $forum_mods = implode( ', ', wp_list_pluck( $user_nicenames, 'user_nicename' ) );
     2304                    }
     2305                }
     2306            }
     2307        }
     2308
     2309        return apply_filters( 'bbp_get_form_forum_moderators', $forum_mods );
    24292310    }
    24302311
     
    26482529        $r = bbp_parse_args( $args, array(
    26492530            'select_id'    => 'bbp_forum_type',
     2531            'select_class' => 'bbp_dropdown',
    26502532            'tab'          => false,
    26512533            'forum_id'     => $forum_id,
     
    26812563        ob_start(); ?>
    26822564
    2683         <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ) ?>_select"<?php echo $tab; ?>>
     2565        <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ) ?>_select" class="<?php echo esc_attr( $r['select_class'] ); ?>"<?php echo $tab; ?>>
    26842566
    26852567            <?php foreach ( bbp_get_forum_types( $r['forum_id'] ) as $key => $label ) : ?>
     
    27402622        $r = bbp_parse_args( $args, array(
    27412623            'select_id'    => 'bbp_forum_status',
     2624            'select_class' => 'bbp_dropdown',
    27422625            'tab'          => false,
    27432626            'forum_id'     => $forum_id,
     
    27732656        ob_start(); ?>
    27742657
    2775         <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ) ?>_select"<?php echo $tab; ?>>
     2658        <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ) ?>_select" class="<?php echo esc_attr( $r['select_class'] ); ?>"<?php echo $tab; ?>>
    27762659
    27772660            <?php foreach ( bbp_get_forum_statuses( $r['forum_id'] ) as $key => $label ) : ?>
     
    28322715        $r = bbp_parse_args( $args, array(
    28332716            'select_id'    => 'bbp_forum_visibility',
     2717            'select_class' => 'bbp_dropdown',
    28342718            'tab'          => false,
    28352719            'forum_id'     => $forum_id,
     
    28652749        ob_start(); ?>
    28662750
    2867         <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ) ?>_select"<?php echo $tab; ?>>
     2751        <select name="<?php echo esc_attr( $r['select_id'] ) ?>" id="<?php echo esc_attr( $r['select_id'] ) ?>_select" class="<?php echo esc_attr( $r['select_class'] ); ?>"<?php echo $tab; ?>>
    28682752
    28692753            <?php foreach ( bbp_get_forum_visibilities( $r['forum_id'] ) as $key => $label ) : ?>
Note: See TracChangeset for help on using the changeset viewer.