Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/05/2016 06:27:54 PM (8 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/users/capabilities.php

    r5951 r6056  
    2626 * @uses bbp_get_reply_post_type() To get the reply post type
    2727 * @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
     28 * @uses bbp_is_user_forum_moderator() To check if the user is a forum moderator
    2929 * @uses apply_filters() Filter mapped results
    3030 *
     
    110110
    111111                // If user is a per-forum moderator, make sure they can spectate.
    112                 if ( bbp_is_user_forum_mod( $user_id, $forum_id ) ) {
     112                if ( bbp_is_user_forum_moderator( $user_id, $forum_id ) ) {
    113113                    $caps = array( 'spectate' );
    114114                }
     
    798798    return (bool) apply_filters( 'bbp_show_user_profile', $retval, $user_id );
    799799}
     800
     801/** Moderators ****************************************************************/
     802
     803/**
     804 * Add a moderator to an object
     805 *
     806 * @since 2.6.0 bbPRess
     807 *
     808 * @param int $object_id Traditionally a forum ID, but could be useful
     809 * @param int $user_id
     810 *
     811 * @return @mixed
     812 */
     813function bbp_add_moderator( $object_id = 0, $user_id = 0 ) {
     814    return add_post_meta( $object_id, '_bbp_moderator_id', $user_id );
     815}
     816
     817/**
     818 * Remove a moderator user ID from an object
     819 *
     820 * @since 2.6.0 bbPress
     821 *
     822 * @param int $object_id
     823 * @param int $user_id
     824 *
     825 * @return mixed
     826 */
     827function bbp_remove_moderator( $object_id = 0, $user_id = 0 ) {
     828    return delete_post_meta( $object_id, '_bbp_moderator_id', $user_id );
     829}
     830
     831/**
     832 * Get user IDs of moderators for an object
     833 *
     834 * @since 2.6.0 bbPress
     835 *
     836 * @param int $object_id
     837 *
     838 * @return mixed
     839 */
     840function bbp_get_moderator_ids( $object_id = 0 ) {
     841    return get_post_meta( $object_id, '_bbp_moderator_id', false );
     842}
     843
     844/**
     845 * Get moderators for a specific object ID. Will return global moderators when
     846 * object ID is empty.
     847 *
     848 * @since 2.6.0 bbPress
     849 *
     850 * @param int $object_id
     851 *
     852 * @return array
     853 */
     854function bbp_get_moderators( $object_id = 0 ) {
     855
     856    // Get global moderators
     857    if ( empty( $object_id ) ) {
     858        $users = get_users( array(
     859            'role__in' => bbp_get_moderator_role(),
     860        ) );
     861
     862    // Get object moderators
     863    } else {
     864        $users = get_users( array(
     865            'include' => bbp_get_moderator_ids( $object_id ),
     866        ) );
     867    }
     868
     869    return apply_filters( 'bbp_get_moderators', $users, $object_id );
     870}
Note: See TracChangeset for help on using the changeset viewer.